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.
#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); }