Akshayan SinhaCETECH11
Published © CC BY-NC-SA

Mosquitto MQTT | IoT Platform Series - 7

Know and Understand MQTT protocol. Then implement and build a project to subscribe and publish on the public server of test. mosquitto. org.

IntermediateProtip1 hour423
Mosquitto MQTT | IoT Platform Series - 7

Things used in this project

Hardware components

Espressif ESP32 Development Board - Developer Edition
Espressif ESP32 Development Board - Developer Edition
×1

Software apps and online services

MQTT
MQTT

Story

Read more

Code

Mosquitto MQTT using PubSub Client Library

Arduino
#include <WiFi.h>
#include <PubSubClient.h>

// Replace the next variables with your SSID/Password combination
const char* ssid     = "XXXXXXXXXX";
const char* password = "XXXXXXXXXX";

// Add your MQTT Broker IP address, example:
const char* mqtt_server = "test.mosquitto.org";

WiFiClient espClient;
PubSubClient client(espClient);

long lastMsg = 0;
char msg[50];
int value = 0;
float temperature = 0;
float humidity = 0;

void setup() {
  Serial.begin(115200);
  // default settings

  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* message, unsigned int length) {
  Serial.print("Message arrived on topic: ");
  Serial.print(topic);
  Serial.print(". Message: ");
  String messageTemp;
  
  for (int i = 0; i < length; i++) {
    Serial.print((char)message[i]);
    messageTemp += (char)message[i];
  }
  Serial.println();
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    String clientId = "client-" + random(100, 999);
    if (client.connect(clientId.c_str())) {
      Serial.println("connected");
      
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void loop() {  
  
  if (!client.connected()) {
      reconnect();
    }
  client.loop();

  long now = millis();
  
  if (now - lastMsg > 2000) {
    lastMsg = now;
    Serial.println(client.connected());
    sendData();
  }
}

void sendData(){
    Serial.println();
    client.publish("schoolofiot/gap", "--------------");
    
    //Read Temperature
    temperature = random(30,40); 
    char tempString[8];
    dtostrf(temperature, 1, 2, tempString);
    Serial.print("Temperature: ");
    Serial.println(tempString);
    String tempdata = "Temperature: " + String(tempString);
    client.publish("schoolofiot/temperature", tempdata.c_str());

    //Read Humidity
    humidity = random(60,70); 
    char humString[8];
    dtostrf(humidity, 1, 2, humString);
    Serial.print("Humidity: ");
    Serial.println(humString);
    String humdata = "Humidity: " + String(humString);
    client.publish("schoolofiot/humidity", humdata.c_str());
  
}

Credits

Akshayan Sinha

Akshayan Sinha

28 projects • 27 followers
Robotics Software Engineer with a makermind.
CETECH11

CETECH11

57 projects • 14 followers

Comments