From 5e7f606f675a27be66c6a86dec9fa1d644834db7 Mon Sep 17 00:00:00 2001 From: Richard Genoud Date: Sat, 18 Jan 2020 09:32:03 +0100 Subject: [PATCH] kitchen_pilot_wire_control.ino: use the same code as the main --- .../kitchen_pilot_wire_control.ino | 84 ++++++++++++++++------ 1 file changed, 61 insertions(+), 23 deletions(-) diff --git a/soft/kitchen/kitchen_pilot_wire_control/kitchen_pilot_wire_control.ino b/soft/kitchen/kitchen_pilot_wire_control/kitchen_pilot_wire_control.ino index d0e88f0..6067e90 100644 --- a/soft/kitchen/kitchen_pilot_wire_control/kitchen_pilot_wire_control.ino +++ b/soft/kitchen/kitchen_pilot_wire_control/kitchen_pilot_wire_control.ino @@ -1,6 +1,6 @@ /* kitchen_pilot_wire_control.ino - Switches on or off a heater. + Switches on or off several heaters. */ #include @@ -19,7 +19,21 @@ EspMQTTClient client( 1883 // The MQTT port, default to 1883. this line can be omitted ); -const int heating_pin = 14; +enum rooms { + KITCHEN, + NB_HEATERS +}; + +struct heater { + int pin; + enum rooms room; + const char *topic; + MessageReceivedCallback callback; + os_timer_t keep_alive_timer; +}; + +static struct heater heaters[NB_HEATERS]; + const int led_pin = 2; #define led_on(state) do {\ @@ -39,12 +53,12 @@ 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) +void reset_timer(enum rooms room) { + os_timer_t *timer = &(heaters[room].keep_alive_timer); + os_timer_disarm(timer); - os_timer_setfn(timer, shutdown_heater, NULL); + os_timer_setfn(timer, shutdown_heater, &(heaters[room].room)); os_timer_arm(timer, 3600000, false); } @@ -61,48 +75,70 @@ void lwdtFeed(void) { lwdTimeout = lwdTime + LWD_TIMEOUT_MS; } -void message_callback(const String &message) +void handle_message(enum rooms room, const String &message) { Serial.println(message); if (message.equals("0") || message.equalsIgnoreCase("off") || message.equalsIgnoreCase("false")) { - heater_off(heating_pin); + heater_off(heaters[room].pin); } if (message.equals("1") || message.equalsIgnoreCase("on") || message.equalsIgnoreCase("true")) { - heater_on(heating_pin); + heater_on(heaters[room].pin); } - reset_timer(&keep_alive_timer); + reset_timer(room); } -void shutdown_heater(void *dummy) +void kitchen_callback(const String &message) { + handle_message(KITCHEN, message); +} + +void shutdown_heater(void *arg) +{ + enum rooms room = *(enum rooms *)arg; + // 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); + Serial.print("No MQTT message for one hour on topic "); + Serial.print(heaters[room].topic); + Serial.println(", switching off."); + heater_off(heaters[room].pin); } void setup() { - pinMode(heating_pin, OUTPUT); - pinMode(led_pin, OUTPUT); - heater_off(heating_pin); + unsigned i; + + heaters[KITCHEN].room = KITCHEN; + + heaters[KITCHEN].pin = 14; + + heaters[KITCHEN].topic = "chauffage/cuisine"; + + heaters[KITCHEN].callback = kitchen_callback; + + for (i = 0; i < NB_HEATERS; i++) { + pinMode(heaters[i].pin, OUTPUT); + heater_off(heaters[i].pin); + } Serial.begin(115200); - // Optionnal functionnalities of EspMQTTClient : + // 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); + for (i = 0; i < NB_HEATERS; i++) { + os_timer_setfn(&(heaters[i].keep_alive_timer), shutdown_heater, &(heaters[i].room)); + os_timer_arm(&(heaters[i].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); + for (unsigned i = 0; i < NB_HEATERS; i++) { + client.subscribe(heaters[i].topic, heaters[i].callback); + } } void loop() @@ -113,7 +149,9 @@ void loop() client.loop(); if (!client.isConnected()) { // if something wrong happens, switch off the heater - heater_off(heating_pin); + for (unsigned i = 0; i < NB_HEATERS; i++) { + heater_off(heaters[i].pin); + } } // blink ESP blue led to show that we are alive