/*
|
|
kitchen_pilot_wire_control.ino
|
|
Switches on or off a heater.
|
|
*/
|
|
|
|
#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;
|
|
|
|
#define heater_on(pin) do {\
|
|
digitalWrite(pin, LOW); \
|
|
} while(0)
|
|
|
|
#define heater_off(pin) do {\
|
|
digitalWrite(pin, HIGH); \
|
|
} while(0)
|
|
|
|
|
|
os_timer_t keep_alive_timer;
|
|
|
|
void reset_timer(os_timer_t *timer)
|
|
{
|
|
os_timer_disarm(timer);
|
|
os_timer_setfn(timer, shutdown_heater, NULL);
|
|
os_timer_arm(timer, 3600000, false);
|
|
}
|
|
|
|
void message_callback(const String &message)
|
|
{
|
|
Serial.println(message);
|
|
if (message == "off") {
|
|
heater_off(heating_pin);
|
|
}
|
|
if (message == "on") {
|
|
heater_on(heating_pin);
|
|
}
|
|
reset_timer(&keep_alive_timer);
|
|
}
|
|
|
|
void shutdown_heater(void *dummy)
|
|
{
|
|
// if we didn't receive a message for an hour,
|
|
// shutdown the heater
|
|
Serial.println("No MQTT message for one hour, switching off.");
|
|
heater_off(heating_pin);
|
|
}
|
|
|
|
void setup()
|
|
{
|
|
pinMode(heating_pin, OUTPUT);
|
|
heater_off(heating_pin);
|
|
|
|
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
|
|
os_timer_setfn(&keep_alive_timer, shutdown_heater, NULL);
|
|
os_timer_arm(&keep_alive_timer, 3600000, false);
|
|
}
|
|
|
|
// 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();
|
|
if (!client.isConnected()) {
|
|
// if something wrong happens, switch off the heater
|
|
heater_off(heating_pin);
|
|
}
|
|
}
|