Thermostat pour piloter jusqu'à 4 radiateurs avec fil pilote
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

126 lines
3.2 KiB

/*
kitchen_pilot_wire_control.ino
Switches on or off a heater.
*/
#include <Ticker.h>
#include "EspMQTTClient.h"
#define LWD_TIMEOUT_MS 15000
EspMQTTClient client(
"rico",
"xxxxxx",
"192.168.1.2", // 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;
const int led_pin = 2;
#define led_on(state) do {\
digitalWrite(led_pin, (state) ? LOW : HIGH); \
} while (0)
#define heater_on(pin) do {\
digitalWrite(pin, LOW); \
} while(0)
#define heater_off(pin) do {\
digitalWrite(pin, HIGH); \
} while(0)
// loop watchdog variables
unsigned long lwdTime = 0;
unsigned long lwdTimeout = LWD_TIMEOUT_MS;
Ticker lwdTicker;
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 ICACHE_RAM_ATTR lwdtcb(void)
{
if (((millis() - lwdTime) > LWD_TIMEOUT_MS) || ((lwdTimeout - lwdTime) != LWD_TIMEOUT_MS))
{
ESP.restart();
}
}
void lwdtFeed(void) {
lwdTime = millis();
lwdTimeout = lwdTime + LWD_TIMEOUT_MS;
}
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);
pinMode(led_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()
{
static unsigned int counter;
static bool led_state = false;
client.loop();
if (!client.isConnected()) {
// if something wrong happens, switch off the heater
heater_off(heating_pin);
}
// blink ESP blue led to show that we are alive
if (((led_state) && (counter % 1000 == 0)) || (!led_state && (counter % 30000 == 0))) {
led_state = !led_state;
led_on(led_state);
}
counter++;
lwdtFeed();
}