|
|
/*
|
|
|
SimpleMQTTClient.ino
|
|
|
The purpose of this exemple is to illustrate a simple handling of MQTT and Wifi connection.
|
|
|
Once it connects successfully to a Wifi network and a MQTT broker, it subscribe to a topic and send a message to it.
|
|
|
It will also send a message delayed 5 seconds later.
|
|
|
*/
|
|
|
|
|
|
#include "EspMQTTClient.h"
|
|
|
|
|
|
EspMQTTClient client(
|
|
|
"rico2",
|
|
|
"xxxxxx",
|
|
|
"192.168.1.3", // MQTT Broker server ip
|
|
|
"MQTTUsername", // Can be omitted if not needed
|
|
|
"MQTTPassword", // Can be omitted if not needed
|
|
|
"pilote_cuisine", // Client name that uniquely identify your device
|
|
|
1883 // The MQTT port, default to 1883. this line can be omitted
|
|
|
);
|
|
|
|
|
|
const int heating_pin = 14;
|
|
|
|
|
|
void message_callback(const String &message)
|
|
|
{
|
|
|
Serial.println(message);
|
|
|
if (message == "off") {
|
|
|
digitalWrite(heating_pin, LOW);
|
|
|
}
|
|
|
if (message == "on") {
|
|
|
digitalWrite(heating_pin, HIGH);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void setup()
|
|
|
{
|
|
|
pinMode(heating_pin, OUTPUT); // Initialize the LED_BUILTIN pin as an output
|
|
|
digitalWrite(heating_pin, LOW); // Turn the LED on (Note that LOW is the voltage level
|
|
|
|
|
|
Serial.begin(115200);
|
|
|
|
|
|
// 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("chauffage/cuisine", &message_callback);
|
|
|
}
|
|
|
|
|
|
void loop()
|
|
|
{
|
|
|
client.loop();
|
|
|
}
|