Publish sensor data

After an existing internet connection, the data should now be sent to the cloud. For this the protocol MQTT is used.

working task 1

Apply the above additions to the source code in your program. After successful transfer to the microcontroller, the sensor data should be sent to the broker.

working task 2

Review your understanding of the illustrated program by answering the following questions:

  • How often are the sensor data sent to the broker?
  • Which topics were chosen for the sensor data?

working task 3

Install an MQTT app (such as MQTT Dash) to display the published data.

MQTT

With the help of the MQTT protocol z. B. data to a so-called “broker” (= a server on the Internet, which controls the MQTT protocol) are sent.For initial tests, brokers that are freely accessible on the Internet can be used. It should be noted, however, that the data sent there can be viewed and manipulated by anyone. Therefore, no security-related or personal data should be sent.

The data is sent to the server under unique names, the so-called “Topics”. The topics are noted as strings with slashed sections to allow mapping of hierarchies. If, for example, the temperatures of each room of a house are recorded, the following description for the topics would be conceivable:

  • /MyHouse/Kitchen/Temperature
  • /MyHouse/LivingRoom/Temperature
  • ….

Once the data has been sent to the server, they can be viewed using an app that retrieves the data from the broker via the MQTT protocol.

MQTT sample code

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>

// MQTT Connection
String HOST_NAME="Klasse.Schüler";
const char* mqttServer = "iot.eclipse.org";
PubSubClient mqttClient(espClient);
long lastMessageTime = 0;

[...]

void setup() { 
 [...]
 setupMQTT();
}

[...]

void setupMQTT() {
 mqttClient.setServer(mqttServer, 1883);
}

void connectMQTT() {
 // Loop until connection is established
 while (!mqttClient.connected())
 {
   Serial.print("Try to connect MQTT ...");
   // random or unique mqttClient ID
   String mqttClientId = String("CRBK_")+String(HOST_NAME);
 
   // for authentification use username and password like: mqttClient.connect(mqttClientId,userName,passWord))
   if (mqttClient.connect(mqttClientId.c_str()))
   {
      Serial.println("connected");
   } else {
     Serial.print("failed, errorcode=");
     Serial.print(mqttClient.state());
     Serial.println(" try again in 5 seconds");
     delay(5000);
   }
 }
} //end connectMQTT()

void processMQTT() {
 if (!mqttClient.connected()) {
   connectMQTT();
 }
 mqttClient.loop();
 long now = millis();
 if (now - lastMessageTime > 6000 && lastSensorTime!=lastMessageTime) {
   lastMessageTime = lastSensorTime;

   //publish sensor data to MQTT broker
   Serial.println("publish on server");
   mqttClient.publish(("CRBK/"+HOST_NAME+"/Temperature").c_str(), String(lastTemperature).c_str());
   mqttClient.publish(("CRBK/"+HOST_NAME+"/Humidity").c_str(), String(lastHumidity).c_str());
 } 
}

void loop() { 
 processInputs();
 processMQTT();
 processOutputs();
}

Leave a Reply