MQTT, or MQ Telemetry Transport, has become a de facto standard in this world, as it’s easy to set up, and works well without a lot of computing overhead. “MQ” at one time stood for Message Queuing, but it has now apparently transcended its acronym status.
How Does MQTT Work?
MQTT uses a publish-subscribe methodology, where clients send and receive messages to each other through a centralized broker, also sometimes called a server. Clients both publish and subscribe to information channels called topics, and any data that passes on via the broker is tagged with a topic label. Once clients are pointed to the broker’s IP address, there’s no more system configuration involved. Clients simply send messages to a topic (that may or may not exist elsewhere) by publishing topic-tagged data to it. Clients listen to topics by subscribing to them.
Note that each client knows nothing about the other clients on the network; the broker merely takes care of data distribution. This can be to one client, many clients, or none, if nothing else is actually subscribed. All a client needs to know is where to find the broker/server. If a client’s IP address changes, or there are other modifications in the underlying system, as long as each client knows where to find the server, things will still function properly.
There are many ready to go and opensource mqtt client is available eg. mqtt fx, mqtt lens etc.
We can use then for teeting and visualisation purposes.
#include<Arduino.h>#include"common.h"#include<WiFi.h>#include"mqtt_credential.h"#include<Wire.h>#include<WiFi.h>#include<PubSubClient.h>#define use_dummy_data FALSE// Replace the next variables with your SSID/Password combinationconstchar*ssid=WIFI_SSID;constchar*password=WIFI_PASSWORD;// Add your MQTT Broker IP address, example://const char* mqtt_server = "192.168.1.144";constchar*mqtt_server=broker_address;WiFiClientespClient;PubSubClientclient(espClient);longlastMsg=0;charmsg[50];intvalue=0;voidsetup_wifi(){delay(10);// We start by connecting to a WiFi networkSerial.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());}voidsetup(){Serial.begin(115200);setup_wifi();client.setServer(mqtt_server,broker_port);client.setCallback(callback);// #if !(use_dummy_data)// bme_setup();// #endifrandomSeed(analogRead(0));}voidcallback(char*topic,byte*message,unsignedintlength){Serial.print("Message arrived on topic: ");Serial.print(topic);Serial.print(". Message: ");StringmessageTemp;for(inti=0;i<length;i++){Serial.print((char)message[i]);messageTemp+=(char)message[i];}Serial.println();}voidreconnect(){// Loop until we're reconnectedwhile(!client.connected()){Serial.print("Attempting MQTT connection...");// Attempt to connectif(client.connect("demo",mqtt_client_username,mqtt_client_password)){Serial.println("connected");// Subscribeclient.subscribe("Tank_node_sub");}else{Serial.print("failed, rc=");Serial.print(client.state());Serial.println(" try again in 5 seconds");// Wait 5 seconds before retryingdelay(5000);}}}voidloop(){if(!client.connected()){reconnect();}client.loop();longnow=millis();if(now-lastMsg>5000){lastMsg=now;// #if !(use_dummy_data)// client.publish(mqtt_publish_topic, get_BME_payload().c_str());// #elseclient.publish(mqtt_publish_topic,get_dummy_BME_payload().c_str());// #endif// Serial.println(get_BME_payload());}}