element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Blog Publish data with MQTT - The smart entrance - Internet of Holiday Lights RoadTest
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: matfur92
  • Date Created: 11 Jan 2015 11:34 PM Date Created
  • Views 630 views
  • Likes 0 likes
  • Comments 0 comments
  • RoadTest
  • holiday
  • iot_holidaylights
  • smart_entrance
  • christmas
  • iot
  • arduino
Related
Recommended

Publish data with MQTT - The smart entrance - Internet of Holiday Lights RoadTest

matfur92
matfur92
11 Jan 2015

Hello everyone!


Here I am with the second step of my project. In previous point (http://www.element14.com/community/groups/arduino/blog/2014/12/23/the-smart-entrance--my-project-for-internet-of-holiday-lights-roadtest) I explained how to install mosquitto.

 

I go then to explain through a couple of pictures and code what I did to make it possible to write in the topic I chose updates of the data collected by the sensors.
Every 10 seconds my Arduino Yun reads data detected (humidity, temperature, air quality, fuel gases, flames and distance) from the sensors connected (see photo) and, through the MQTT protocol communicates them to the server.
To verify that everything happens correctly I installed in my smartphone "MyMQTT", a simple but powerful application to enroll in the topic, and also wanting to write messages on them. The configuration has only required the inclusion of IP of my server and the topic to which register.
Once started Arduino, as you can see from the screenshot, every 10 seconds, the topic is updated with the sensor data.


Below I leave you also also the code I used to be able to carry out the above and in the attached library for MQTT. Unfortunately this is not Paho as it is very greedy for space and occupied memory (about 92% against a 46% of this).


In the next post will address the issue in webcam and the one after that, hoping that the power pack, the pilot of the strip led according to the temperature received from the server using MQTT.

imageimage

 

#include <SPI.h>
#include <Bridge.h>
#include <YunClient.h>
#include <IPStack.h>
#include <Countdown.h>
#include <MQTTClient.h>
#include <Wire.h>
#include <dht11.h>
char printbuf[100];
// DHT11 sensor pins
dht11 DHT11;
#define DHT11_PIN 8 //pin number 8 of digital port
int pinSensoreFiamma = 7;
//HC RS04 ultrasound sensor
int triggerPort = 4;
int echoPort = 2;
YunClient yunClient;
IPStack ipstack(yunClient);
MQTT::Client<IPStack, Countdown> client = MQTT::Client<IPStack, Countdown>(ipstack);
byte mac[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // replace with your device's MAC
const char* topic = "/smart_entrance/toYun"; // replace with your topic

void connect() {
    char hostname[] = "0.0.0.0"; // replace with your server IP or domain
    int port = 1883;
    sprintf(printbuf, "Connecting to %s:%d\n", hostname, port);
    Serial.print(printbuf);
    int rc = ipstack.connect(hostname, port);
    if (rc != 1) {
        sprintf(printbuf, "rc from TCP connect is %d\n", rc);
        Serial.print(printbuf);
    }
    Serial.println("MQTT connecting");
    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
    data.MQTTVersion = 3;
    data.clientID.cstring = (char*) "arduino-yun"; //name of arduino on mosquitto
    rc = client.connect(data);
    if (rc != 0) {
        sprintf(printbuf, "rc from MQTT connect is %d\n", rc);
        Serial.print(printbuf);
    }
}

void setup() {
    Wire.begin();
    Bridge.begin();
    Serial.begin(115200);
    // sets the digital pin 7 as input
    pinMode(pinSensoreFiamma, INPUT);
    //ultrasound sensor
    pinMode( triggerPort, OUTPUT );
    pinMode( echoPort, INPUT );
    connect();
}

void loop() {
    if (!client.isConnected()){
        connect();
    }
    MQTT::Message message;
    while (true){
        int chk;
        Serial.print("DHT11, ");
        chk = DHT11.read(DHT11_PIN);    // READ DATA //pin number 8 of digital
        switch (chk){
          case DHTLIB_ERROR_CHECKSUM:
                      Serial.print("Checksum error, ");
                      break;
          case DHTLIB_ERROR_TIMEOUT:
                      Serial.print("Time out error, ");
                      break;
          default:
                      Serial.print("Unknown error, ");
                      break;
        }

        //MQ135 Air Quality Control
        float RoRs;
        int airqValue   = analogRead(0);    // read air quality sensor
        if(DHT11.temperature >=20.0&&DHT11.temperature<=50.0){//adjust for dependency on temperature
          RoRs = -0.0034 * DHT11.temperature + 1.067;
        }
        else if(DHT11.temperature>=-10.0&&DHT11.temperature<=5.0){//adjust for dependency on temperature
          RoRs = -0.0300 * DHT11.temperature + 1.4;
        }
        else if (DHT11.temperature >= 5.0 && DHT11.temperature <= 20.0){//adjust for dependency on temperature
          RoRs = -0.0167 * DHT11.temperature + 1.333;
        }
        else {
          RoRs = 1;
        }
        RoRs= RoRs * (-0.001923 * DHT11.humidity + 1.0634); //adjust for dependency on humidity
        int adjAirQValue = airqValue * RoRs;

        //MQ-2 General combustible gas
        int mq2   = analogRead(2);    // read MQ-2 General combustible gas

         char fuocoAcceso[8]="";
        if(digitalRead(pinSensoreFiamma)==1){
          sprintf(fuocoAcceso, "%s","spento");
        }else{
          sprintf(fuocoAcceso, "%s","acceso");
        }

         // trigger
        digitalWrite( triggerPort, LOW );
  
        //impulse of 10microsec on trigger
        digitalWrite( triggerPort, HIGH );
        delayMicroseconds( 10 );
        digitalWrite( triggerPort, LOW );
  
        long duration = pulseIn( echoPort, HIGH );
        long centimetri = 0.034 * duration / 2;

        if( duration > 58000 ){
          duration=-1;
          centimetri=-1;
        }

        MQTT::Message message;
        message.qos = MQTT::QOS0;
        message.retained = false;
        char str[50] = "";
        sprintf(str, "%d,%d,%d,%d,%s,%d", DHT11.humidity, DHT11.temperature,adjAirQValue,mq2,fuocoAcceso,centimetri);
        message.payload = str;
        message.payloadlen = strlen(str);
        int rc = client.publish("/smart_entrance", message);
        if (rc != 0) {
          Serial.print("Message publish failed with return code : ");
          Serial.println(rc);
        }
        client.yield(10000);
    }
    delay(100);
}

Attachments:
MQTTClient.zip
  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube