ESP8266 & Public MQTT Broker mosquitto.org & Node-RED

In the last tests we have taken the MQTT protocol since it has taken great strength the last few years and we think it is thanks to its simplicity, security, practicality and very lightweight projects IoT and M2M applications.

Thanks to the contribution of developers and developers of MQTT applications, there are public MQTT Internet broker for tests of control and monitoring from the internet, are available to connect from any MQTT client in this case we will use the public mosquitto Broker, which has a dashboard for View MQTT and web sockets connections.

In this way we make a simple and uncomplicated IoT application.

Since it is public and gratuitous you must have some considerations of which we will speak below as not abusing the service and not trusting.

Website: mosquitto.org

Dashboard MQTT: mosquitto.org

mosquitto

 

Connections Broker MQTT

Broker: test.mosquitto.org
TCP Port: 

  • 1883 : MQTT, unencrypted
  • 8883 : MQTT, encrypted
  • 8884 : MQTT, encrypted, client certificate required


Websocket Port: 

  • 8080 : MQTT over WebSockets, unencrypted
  • 8081 : MQTT over WebSockets, encrypted





Other publics Broker’s

Test

Then we will do 2 tests:

  1. Connection Broker MQTT mosquitto.org with Node-RED.
  2. Connection Broker MQTT mosquitto.org with ESP8266 & Node-RED.

Connection Broker MQTT Mosquitto.org with Node-RED

Node

Using Node-RED previously installed on a local server in my network, we will make the MQTT connection with mosquitto.org to validate the connection from any MQTT client.

This way we could send and receive data between our devices and applications over the internet without the need for things as complex or expensive as fixed vpn ip’s and other inconveniences for such IoT applications.





Connection Broker MQTT Mosquitto.org with ESP8266 & Node-RED

In this case the module ESP8266 12E NodeMCU configured as client MQTT read a temperature sensor DS18B20 Protocol (Onewire) sends the temperature via MQTT to the Broker Mosquitto and Node-RED installed in the local network requests the value of temperature and graph On Node-RED Dashboard.

MQTT Topic

“temperature/PDAControl/sensor”

Message

Temperature value examples “28.9”

Full Video Explained ESP8266 Mosquitto.org & Node-RED

 Similar Tests

  1. Tutorial Amplifier Audio PAM8302A ESP8266 Node-RED MQTT
  2. Tutorial ESP8266 Node-RED Granafa InfluxDB MQTT
  3. Tutorial ESP8266 DS18B20 Temperature Node-RED MQTT (Mosquitto) IoT
  4. Tutorial ESP8266 Node-RED Granafa InfluxDB MQTT
  5.  Tutorial ESP8266 and Node-RED MQTT GPIO (Mosquitto) # 1
  6. Tutorial ESP8266 Control Servo Node-RED MQTT (Mosquitto) IoT #2
  7. Node-RED Platform

Arduino IDE Code

The ESP8266 module is configured as an MQTT client and performs the periodic temperature reading of the DS18B20 sensor, connected to the D4 pin (Gpio 02), supply to 5v, with its respective recommended resistance 4.7K in the maxim datasheet.

Requires libraries:

/* MQTT Client Temperature Onewire
* More information about projects PDAControl 
* PDAControl English http://pdacontrolen.com 
* Mas informacion sobre proyectos PDAControl
* PDAControl Espanol http://pdacontrolen.com 
* Channel Youtube https://www.youtube.com/c/JhonValenciaPDAcontrol/videos 
*/

#include < ESP8266WiFi.h >
#include < PubSubClient.h >
#include < OneWire.h >
#include < DallasTemperature.h >

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2 // pin 2

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

// Update these with values suitable for your network.

const char* ssid = "************";
const char* password = "************";
const char* mqtt_server = "test.mosquitto.org"; /// MQTT Broker
int mqtt_port = 1883;

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

void setup() { 
 Serial.begin(115200);
 // Start up the library
 sensors.begin();
 
 setup_wifi();
 client.setServer(mqtt_server, mqtt_port);
 client.setCallback(callback);

 Serial.println("Connected ");
 Serial.print("MQTT Server ");
 Serial.print(mqtt_server);
 Serial.print(":");
 Serial.println(String(mqtt_port)); 
 Serial.print("ESP8266 IP ");
 Serial.println(WiFi.localIP()); 
 Serial.println("Modbus RTU Master Online");

 
}

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* payload, unsigned int length) {
 Serial.print("Message arrived [");
 Serial.print(topic);
 Serial.print("] ");
 for (int i = 0; i < length; i++) {
 Serial.print((char)payload[i]);
 }
 Serial.println();
 
}

void reconnect() {
 // Loop until we're reconnected
 while (!client.connected()) {
 Serial.print("Attempting MQTT connection...");
 // Attempt to connect
 if (client.connect("ESP8266Client")) {

 Serial.println("connected");
 // client.subscribe("event");
 } 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() {
 
 sensors.requestTemperatures();
 float celsius = sensors.getTempCByIndex(0);
 Serial.println(sensors.getTempCByIndex(0));
 
 char temperaturenow [15];
 dtostrf(celsius,7, 3, temperaturenow); //// convert float to char 
 client.publish("temperature/PDAControl/sensor", temperaturenow); /// send char 
 
 if (!client.connected()) {
 reconnect();

 }
 client.loop();

 delay(10000);
 
}

Materials and where to buy

Conclusions and recommendations

We consider that although technically the broker is public we do not realize applications that are constantly connected to this broker, the following recommendations given by mosquitto.org must be taken into account:

You are free to use it for any application, but please do not abuse or rely on it for anything of importance. You must also build your client to cope with the reboot Broker, more referenced documentation on test.mosquitto.org

We only perform fast tests, although it should be noted that they have mounted their IoT applications with this broker without any apparent inconvenience the service is active and public.

With a broker in the cloud (intenet) from anywhere we could connect our devices and applications without limits of IP addresses and other restrictions, I currently investigate plans and services of MQTT servers.

In next tutorials we will try other public MQTT servers and interactions with FRED (Node-RED).

Other publics Broker’s

References

 

 





Leave a Reply