Voor de pagina met de powerpoint presentatie klik hier

MQTT staat voor “Message Queuing Telemetry Transport”.
MQTT is een eenvoudig lichtgewicht protocol om over internet te communiceren. Het is bedoeld om kleine berichten te versturen, eventueel met batterij gevoede apparatuur. Er zijn heel veel fabrikanten die het ondersteunen. Het is relatief eenvouding en goedkoop om er zelf mee aan de slag te gaan.
MQTT Client communicatie software kan je installeren op de PC of je smartphone maar ook op een arduino of ESP32. Er zijn veel programma’s en voorbeeld programma’s beschikbaar.
MQTT werkt net als veel internet applicaties met een Client-server model. Een MQTT server heet een “Broker”. Deze kan je op verschilende manieren draaien:
1. Op je PC als testserver
2. Op een NAS
3. Op een embedded platform zoals een Arduino, ESP32 of LILYGO
A34 test MQTT broker
Ik heb voor de A34 een test MQTT broker aangezet op a34.pi1nov.nl. Het poortnummer is 1885. Deze kan dus gebruikt worden om te testen.

MQTTFX: MQTT PC software voor monitoring

MQTT Explorer
Dit is ook een hele mooie client om MQTT verkeer te monitoren en zelf een bericht te kunnen versturen: https://mqtt-explorer.com/

MQTT Alert for Android
https://play.google.com/store/apps/details?id=gigiosoft.MQTTAlert&hl=en
Een leuke android app om mee te stoeien….

Arduino Mega + ethernet shield

/*
Basic MQTT example
Een basis sketch voor demonstratie van een MQTT Client.
De hardware is een Arduino Mega 2560 met daarop een netwerkshield. Ook is
een I2C dispay module aangesloten waar ontvangen tekts op weergegeven wordt.
De software haakt aan op de A34 MQTT server op a34.pi1nov.nl. De gebruikte poort is 1885.
*/
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 20 chars and 4 line display
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
String rx_string = "test";
// Update these with values suitable for your network.
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(192, 168, 20, 222);
//IPAddress server(192, 168, 20, 219);
char serverName[] = "a34.pi1nov.nl";
int poortnummer = 1885;
//char rx_array[] = "";
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
rx_string = "";
for (int i=0;i<length;i++) {
rx_string.concat((char)payload[i]);
}
lcd.setCursor(0,2);
lcd.print(" ");
lcd.setCursor(0,2);
lcd.print(rx_string);
for (int i=0;i<length;i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
EthernetClient ethClient;
PubSubClient client(ethClient);
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
//delay(1000);
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("arduinoClient")) { //"arduinoClient" is de unieke client_ID
Serial.println("connected");
// Once connected, publish an announcement...
//delay(1000);
client.publish("A34/mqtt","MQTT PA3CTG Ard.Mega");
// ... and resubscribe
client.subscribe("A34");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup()
{
Serial.begin(9600);
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("MQTT PA3CTG");
delay(3000);
client.setServer("a34.pi1nov.nl", 1885);
client.setCallback(callback);
//Ethernet.begin(mac, ip);
if (Ethernet.begin(mac) == 0) {
while (1) {
Serial.println("Failed to configure Ethernet using DHCP");
delay(1000);
}
}
Serial.println(Ethernet.localIP()); //displays: dhcp IP
// Allow the hardware to sort itself out
}
void loop()
{
if (!client.connected()) {
reconnect();
}
client.loop();
}

ESP32 module

ESP32 Wroom Node-MCU op een experimenteerprint met een aangesloten Dallas DS18B20 temperatuur sensor(one-wire).
// Software voor de Liebherr 6 laden vrieskast
// ESP32 module en MQTT voor diepvries sturing
// Maakt verbinding met Router 2 voor de wifi
// MQTT:
// Stuurt temperatuur naar topic "Liebherr"
// MQTT broker IP: 192.168.20.219, poort 1885
//
// V1.3: 27 mei 2022.
// V1.4: 17 juli 2022
// V1.5: 25 okt 2026 ombouw van ESP8266 naar ESP32 voor demo A34
#include <WiFi.h>
#include <PubSubClient.h>
// DS18B20 gegevens
//#include <DS18B20.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// GPIO where the DS18B20 is connected to
const int oneWireBus = 33;
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
// definitie van de LED op de ESP8266
#define LED 13 //Define blinking LED pin
#define Relais 26 //define relais output pin D2
//#define Sensor 33 //define DS18B20 input pin D1
//DS18B20 ds(Sensor); //D1 op de ESP32
//uint8_t address[] = {40, 187, 207, 73, 246, 56, 60, 197}; //uniek adres van de DS18B20 sensor
//uint8_t selected;
String Temperatuur;
float Tempflt;
boolean WifiStatus = false;
boolean MQTTStatus = false;
// Change the credentials below, so your ESP8266 connects to your router
const char* ssid = "Router 2";
const char* password = "ABCDE12345";
// Change the variable to your Raspberry Pi IP address, so it connects to your MQTT broker
const char* mqtt_server = "a34.pi1nov.nl";
const int port = 1885;
// Initializes the espClient. You should change the espClient name if you have multiple ESPs running in your home automation system
WiFiClient espClient;
PubSubClient client(espClient);
// Timers auxiliar variables
long now = millis();
long lastMeasure = 0;
//******************************************************************************************************************************
// Don't change the function below. This functions connects your ESP8266 to your router
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);
if (WiFi.status() != WL_CONNECTED) {
delay(5000);
Serial.print(".");
}
if (WiFi.status() != WL_CONNECTED) {
delay(5000);
Serial.print(".");
}
if (WiFi.status() != WL_CONNECTED) {
delay(5000);
Serial.print(".");
}
if(WiFi.status() == WL_CONNECTED){
Serial.println("");
Serial.print("WiFi connected - ESP IP address: ");
Serial.println(WiFi.localIP());
}
}
//********************************************************************************************************************************
// This functions is executed when some device publishes a message to a topic that your ESP32 is subscribed to
// Change the function below to add logic to your program, so when a device publishes a message to a topic that
// your ESP32 is subscribed you can actually do something
void callback(String 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];
}
// Feel free to add more if statements to control more GPIOs with MQTT
// If a message is received on the topic room/lamp, you check if the message is either on or off.
// Turns the lamp GPIO according to the message
Serial.println();
}
//*************************************************************************************************************************************************
// This functions reconnects your ESP8266 to your MQTT broker
// Change the function below if you want to subscribe to more topics with your ESP8266
void reconnect() {
// Loop until we're reconnected
if (!client.connected()) {
Serial.print("Attempting MQTT connection...");
delay(3000);
// Attempt to connect
/*
YOU MIGHT NEED TO CHANGE THIS LINE, IF YOU'RE HAVING PROBLEMS WITH MQTT MULTIPLE CONNECTIONS
To change the ESP device ID, you will have to give a new name to the ESP8266.
Here's how it looks:
if (client.connect("ESP8266Client")) {
You can do it like this:
if (client.connect("ESP1_Office")) {
Then, for the other ESP:
if (client.connect("ESP2_Garage")) {
That should solve your MQTT multiple connections problem
*/
if (client.connect("ESP32lient", "naam", "wachtwoord")) {
Serial.println("connected");
// Subscribe or resubscribe to a topic
// You can subscribe to more topics (to control more LEDs in this example)
client.subscribe("Liebherr");
MQTTStatus = true;
}
else {
Serial.print("failed, rc=");
Serial.println(client.state());
//Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
//delay(5000);
MQTTStatus = false;
}
}
}
//*************************************************************************************************************************
// The setup function sets your ESP GPIOs to Outputs, starts the serial communication at a baud rate of 9600 or115200
// Sets your mqtt broker and sets the callback function
// The callback function is what receives messages and actually controls the LEDs
void setup() {
// Init serial
Serial.begin(9600);
delay(1000); //Time for init serial port
Serial.println("Starting software....... wait");
pinMode(LED, OUTPUT); // Initialize the LED pin as an output
digitalWrite(LED, HIGH);
// setup D2 (GPIO-4) als relais sturing
pinMode(Relais, OUTPUT); //relais sturing
digitalWrite(Relais, HIGH); // Zet de compressor uit
//Serial.println("Waiting 60 seconds to cool down the PTC");
//delay(60000); // Wacht bij het opstarten 1 minuten om de PTC in het startrelais af te laten koelen.
Serial.println("Continu.....");
setup_wifi();
client.setServer(mqtt_server, port);
client.setCallback(callback);
// Init DS18B20 sensor
sensors.begin();
//selected = ds.select(address);
}
// *******************************************************************************************************
// For this project, you don't need to change anything in the loop function.
// Basically it ensures that you ESP is connected to your broker
void loop() {
delay(1000);
now = millis();
// Publishes new temperature and humidity every 30 seconds
if (now - lastMeasure > 5000) {
lastMeasure = now;
sensors.requestTemperatures();
Tempflt = sensors.getTempCByIndex(0);//Temperatuur = (ds.getTempC());
//Tempflt = (ds.getTempC());
Serial.print("De DS18B20 sensor temperatuur is ");
Serial.println(Tempflt);
if(Tempflt > -16){
digitalWrite(Relais, LOW); //Compressor aan
}
if(Tempflt < -18){
digitalWrite(Relais, HIGH); //Compressor uit
}
if(WiFi.status() == WL_CONNECTED){
if (!client.connected()) {
reconnect();
}
if(MQTTStatus = true){
if(!client.loop()){
client.connect("ESP32Client");
}
if(!client.loop()){
client.connect("ESP32Client");
}
float hic = Tempflt;
static char temperatureTemp[7];
dtostrf(hic, 6, 2, temperatureTemp);
// dtostrf(float_value, min_width, num_digits_after_decimal, where_to_store_string)
client.publish("Liebherr", temperatureTemp);
client.publish("Test_MQTT", "Test 12345");
}
}
}
}

Node-Red
Geen zin in regels programmeren? Dan is misschien Node-Red wat voor jou. Een programma maken met “blokken”. https://nodered.org/
Node-Red maakt gebruik van Node.js en Javascript wat weer veel gebruikt wordt bij het maken van websites.


MIT App ontwerp tool
Zelf een App voor Android maken? Dat kan met de MIT op hun website

Mosquitto Broker
Bekende Broker (server) software is de mosquitto software. Deze kan gewoon op je windows PC of laptop geinstalleerd worden. Kan ook op andere platformen(bijv. Linux) gebruikt worden!
https://mosquitto.org/download/
ESP32 broker software
// ********************************************************
// simplebroker aangepast voor A34 MQTT testavond
// V1.0 5 nov. 2026
// De orginele versie heeft DHCP. Aangepast voor fixed IP
// ********************************************************
#include"sMQTTBroker.h"
sMQTTBroker broker;
void setup()
{
Serial.begin(9600);
Serial.println("");Serial.println("");
Serial.println("Vertraging 5 seconden");
delay(5000); //even wachten
Serial.println("");
Serial.println("Serieel bericht... Ik ben er....!");
const char* ssid = "Router 2"; // The SSID (name) of the Wi-Fi network you want to connect to
const char* password = "ABCDE12345"; // The password of the Wi-Fi network
// Set your Static IP address
IPAddress local_IP(192, 168, 20, 219);
// Set your Gateway IP address
IPAddress gateway(192, 168, 20, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(8, 8, 8, 8); //optional
IPAddress secondaryDNS(8, 8, 4, 4); //optionalWiFi.begin(ssid, password);
// Configures static IP address
if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
Serial.println("STA Failed to configure");
}
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
delay(1000);
}
Serial.println("Connection established!");
Serial.print("IP address:\t");
Serial.println(WiFi.localIP());
Serial.println("Simpelbroker MQTT is online");
const unsigned short mqttPort=1885;
broker.init(mqttPort);
// all done
}
void loop()
{
broker.update();
}
