0% found this document useful (0 votes)
53 views1 page

Ardui

This document defines an EspMQTTClient that connects an ESP32 device to an MQTT broker at 192.168.0.254 port 1883 with the client ID "esp32". It enables debugging messages, HTTP updating, and a last will message. The onConnectionEstablished function subscribes to receive messages on "acasa/bucatarie" to control an LED, publishes to "mytopic/test", and schedules a delayed publish.

Uploaded by

Andras Szilagyi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views1 page

Ardui

This document defines an EspMQTTClient that connects an ESP32 device to an MQTT broker at 192.168.0.254 port 1883 with the client ID "esp32". It enables debugging messages, HTTP updating, and a last will message. The onConnectionEstablished function subscribes to receive messages on "acasa/bucatarie" to control an LED, publishes to "mytopic/test", and schedules a delayed publish.

Uploaded by

Andras Szilagyi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include "EspMQTTClient.

h"

EspMQTTClient client(
"TehniciAvansate",
"12345678",
"192.168.0.254", // MQTT Broker server ip
"", // Can be omitted if not needed
"", // Can be omitted if not needed
"esp32", // Client name that uniquely identify your device
1883 // The MQTT port, default to 1883. this line can be omitted
);

void setup()
{
Serial.begin(115200);
pinMode(25, OUTPUT);
// Optionnal functionnalities of EspMQTTClient :
client.enableDebuggingMessages(); // Enable debugging messages sent to serial
output
client.enableHTTPWebUpdater(); // Enable the web updater. User and password
default to values of MQTTUsername and MQTTPassword. These can be overrited with
enableHTTPWebUpdater("user", "password").
client.enableLastWillMessage("TestClient/lastwill", "I am going offline"); //
You can activate the retain flag by setting the third parameter to true
}

// This function is called once everything is connected (Wifi and MQTT)


// WARNING : YOU MUST IMPLEMENT IT IF YOU USE EspMQTTClient
void onConnectionEstablished()
{
// Subscribe to "mytopic/test" and display received message to Serial
client.subscribe("acasa/bucatarie", [](const String & payload) {
Serial.println(payload);
if(payload=="0")
{
digitalWrite(25, LOW);
}else if (payload=="1"){
digitalWrite(25, HIGH);
}
});

// Publish a message to "mytopic/test"


client.publish("mytopic/test", "This is a message"); // You can activate the
retain flag by setting the third parameter to true

// Execute delayed instructions


client.executeDelayed(5 * 1000, []() {
client.publish("mytopic/test", "This is a message sent 5 seconds later");
});
}

void loop()
{
client.loop();
}

You might also like