LightHub 4.6.0
Open-source smart home controller firmware
Loading...
Searching...
No Matches
main.h
Go to the documentation of this file.
1
14#pragma once
15
16#include "options.h"
17
18#if defined(M5STACK)
19#include <M5Stack.h>
20#endif
21
22#if defined(__SAM3X8E__)
23#include <watchdog.h>
24 #if not defined (NOIP)
25 #include <ArduinoHttpClient.h>
26 #endif
27//#include "TimerInterrupt_Generic.h"
28#endif
29
30#if defined(ARDUINO_ARCH_AVR)
31 #if not defined (NOIP)
32 #include "HTTPClient.h"
33 #endif
34
35#include <avr/pgmspace.h>
36#include <avr/wdt.h>
37#endif
38
39#if defined(ARDUINO_ARCH_ESP8266)
40#include <FS.h> //this needs to be first, or it all crashes and burns...
41#ifndef FS_STORAGE
42#include <ESP_EEPROM.h>
43#endif
44//#include <ESP8266HTTPClient.h>
45//#include <ArduinoHttpClient.h>
46//#include "HttpClient.h"
47//#include <WiFiManager.h>
48//#include <DNSServer.h>
49//#include <ESP8266WebServer.h>
50//#include <ESP8266WiFi.h>
51#include <user_interface.h>
52//#define Ethernet WiFi
53#endif
54
55#if defined (ARDUINO_ARCH_ESP32)
56#include <FS.h> //this needs to be first, or it all crashes and burns...
57//#include "SPIFFS.h"
58//#include <EEPROM.h>
59//#include <NRFFlashStorage.h>
60//#include "HttpClient.h"
61//#include <ArduinoHttpClient.h>
62//#include <HTTPClient.h>
63//#include <WiFi.h>
64//#include <WiFiClient.h>
65//#include <WiFiClientSecure.h>
66//#include <WebServer.h>
67//#include <WiFiManager.h>
68//include <DNSServer.h>
69
70//#define Ethernet WiFi
71#endif
72
73#ifdef NRF5
74//#include <NRFFlashStorage.h>
75#include <ArduinoHttpClient.h>
76#endif
77
78#if defined (ARDUINO_ARCH_STM32) and not defined (NOIP)
79#include "HttpClient.h"
80//#include "UIPEthernet.h"
81//#include <NRFFlashStorage.h>
82//#include <EEPROM.h>
83#endif
84
85#include "streamlog.h"
89
90#if defined(__SAM3X8E__)
91#define wdt_res() watchdogReset()
92#define wdt_en()
93#define wdt_dis()
94#endif
95
96//#ifndef DHCP_RETRY_INTERVAL
97//#define DHCP_RETRY_INTERVAL 60000
98//#endif
99
100#if defined(ESP8266)
101#define wdt_en() wdt_enable(WDTO_8S)
102#define wdt_dis() wdt_disable()
103#define wdt_res() wdt_reset()
104#endif
105
106#ifdef ARDUINO_ARCH_ESP32
107#define wdt_res()
108#define wdt_en()
109#define wdt_dis()
110#endif
111
112#if defined(NRF5)
113#define wdt_res()
114#define wdt_en()
115#define wdt_dis()
116#endif
117
118#if defined(ARDUINO_ARCH_STM32)
119#define wdt_res()
120#define wdt_en()
121#define wdt_dis()
122#endif
123
124//#if defined(ESP8266)
125//#define wdt_res()
126//#define wdt_en()
127//#define wdt_dis()
128//#endif
129#if defined(ARDUINO_ARCH_AVR)
130#if defined(WATCH_DOG_TICKER_DISABLE)
131#define wdt_en() wdt_disable()
132#define wdt_dis() wdt_disable()
133#define wdt_res() wdt_disable()
134#else
135#define wdt_en() wdt_enable(WDTO_8S)
136#define wdt_dis() wdt_disable()
137#define wdt_res() wdt_reset()
138#endif
139#endif
140
141#ifndef OWIRE_DISABLE
142#include "DallasTemperature.h"
143#endif
144
145#ifdef _modbus
146#include <ModbusMaster.h>
147#endif
148
149#ifdef _owire
150#include "owTerm.h"
151#endif
152
153#if defined(_dmxin) || defined(_dmxout) || defined (_artnet)
154#include "dmx.h"
155#endif
156
157#ifdef WIFI_ENABLE
158
159 #if defined(ARDUINO_ARCH_ESP32)
160 #include <WiFi.h>
161 #include <HTTPClient.h>
162 #include <WiFiClient.h>
163 #include <WiFiClientSecure.h>
164 #include <WiFiManager.h>
165 #include <WebServer.h>
166 #include <ESPmDNS.h>
167 #else
168 #include <ESP8266WiFi.h>
169 #include <ESP8266HTTPClient.h>
170 #include <WiFiManager.h>
171 #include <DNSServer.h>
172 #include <ESP8266WebServer.h>
173 #include <ESP8266mDNS.h>
174 #endif
175#define Ethernet WiFi
176#else
177#if not defined (NOIP) //Wired connection
178 #ifdef Wiz5500
179 #include <Ethernet2.h>
180 #else
181 #ifdef UIPETHERNET
182 #include "UIPEthernet.h"
183 #else
184 #include <Ethernet.h>
185 #endif
186 #endif
187#endif
188#endif
189
190
191#ifdef _artnet
192#include <Artnet.h>
193#endif
194
195#ifdef SD_CARD_INSERTED
196#include "sd_card_w5100.h"
197#endif
198
199#ifdef MDNS_ENABLE
200 #ifndef WIFI_ENABLE
201 #include <ArduinoMDNS.h>
202 #endif
203#endif
204
205#ifdef IPMODBUS
206#include "ipmodbus.h"
207#endif
208
209#include "Arduino.h"
210#include "utils.h"
211#include "textconst.h"
212 #if not defined (NOIP)
213 #include <PubSubClient.h>
214 #endif
215#include <SPI.h>
216#include <string.h>
217#include "aJSON.h"
218#include <Cmd.h>
219#include "stdarg.h"
220#include "item.h"
221#include "inputs.h"
222#ifdef CANDRV
223#include <candriver.h>
224#endif
225
226#ifdef _artnet
227extern Artnet *artnet;
228#endif
229
258
260extern lan_status lanStatus;
261
263typedef union {
264 uint32_t UID_Long[5];
265 uint8_t UID_Byte[20];
266} UID;
267
273
274#if not defined (NOIP)
282void mqttCallback(char *topic, byte *payload, unsigned int length);
304void onMQTTConnect();
310
311#endif
312
317void setupMacAddress();
321void printMACAddress();
322
323#ifndef OWIRE_DISABLE
330void Changed(int i, DeviceAddress addr, float currentTemp);
331#endif
332
337void modbusIdle(void);
338
345int cmdFunctionHelp(int arg_cnt, char **args);
346
353int cmdFunctionKill(int arg_cnt, char **args);
354
360bool applyConfig();
361
368int cmdFunctionLoad(int arg_cnt, char **args);
369
375
382int cmdFunctionSave(int arg_cnt, char **args);
383
390int cmdFunctionSetMac(int arg_cnt, char **args);
391
398int cmdFunctionGet(int arg_cnt, char **args);
399
406int cmdFunctionLoglevel(int arg_cnt, char **args);
407
412void printBool(bool arg);
413
414int cmdFunctionSave(int arg_cnt, char **args);
415/*
416void saveFlash(short n, char *str);
417
418int loadFlash(short n, char *str, short l=MAXFLASHSTR);
419
420void saveFlash(short n, IPAddress& ip);
421
422int ipLoadFromFlash(short n, IPAddress &ip);
423*/
424
425
426
431void preTransmission();
432
437void postTransmission();
438
443void setup_main();
444
449void loop_main();
450
454void owIdle(void);
455
456void modbusIdle(void);
457
462void inputLoop(short);
463
467void inputSensorsLoop();
468
472void inputSetup(void);
476void inputStop(void);
477
481void pollingLoop(void);
482
487void thermoLoop(void);
488
495short thermoSetCurTemp(char *name, float t);
496
497void modbusIdle(void);
498
502void printConfigSummary();
503
508void setupCmdArduino();
509
514
520bool IsThermostat(const aJsonObject *item);
521
529bool disabledDisconnected(const aJsonObject *thermoExtensionArray, int thermoLatestCommand);
530
534void resetHard();
535
541bool cleanConf(short locksAlowed=0);
542
547
552int16_t attachMaturaTimer();
553
557void setFirstBroker();
561void setNextBroker();
562
563//void printFreeRam();
lan_status lanStatus
Definition main.cpp:104
Definition streamlog.h:35
DMX input/output (and Art-Net) support.
LightHub input system (digital/analog pins, sensors, encoders).
IP<->UART Modbus gateway (Modbus TCP/UDP over RS485).
Item (virtual channel) system of LightHub.
short thermoSetCurTemp(char *name, float t)
int cmdFunctionLoglevel(int arg_cnt, char **args)
CLI command "log": changes serial/UDP log levels.
Definition main.cpp:1686
void setNextBroker()
Moves to the next configured MQTT broker (failover).
Definition main.cpp:1069
bool applyConfig()
Applies the loaded JSON config tree to the running system (items, inputs, topics, drivers).
Definition main.cpp:1418
bool cleanConf(short locksAlowed)
Frees the current config tree and its driver resources.
Definition main.cpp:180
void preTransmission()
Marks that an Ethernet transmission is about to happen; disables time-sensitive drivers as needed.
Definition main.cpp:2136
void printFirmwareVersionAndBuildOptions()
Prints firmware version, revision and build options at boot.
Definition main.cpp:2532
void setupMacAddress()
Resolves the controller MAC address (from flash or chip UID) and stores it in the system config.
Definition main.cpp:2817
void thermoLoop(void)
Thermostat control loop: checks temperature readings and drives heater relays (on/off/error bypass) w...
Definition main.cpp:3259
void printBool(bool arg)
Prints a bool as "+" or "-" to the info log.
Definition main.cpp:1861
void inputSetup(void)
Sets up input channels from the loaded config.
Definition main.cpp:3135
void postTransmission()
Marks that an Ethernet transmission has finished; re-enables drivers and handles lost-time compensati...
Definition main.cpp:2145
int cmdFunctionLoad(int arg_cnt, char **args)
CLI command "load": loads the config from the portal or flash.
Definition main.cpp:1600
void owIdle(void)
Idle hook invoked by the 1-Wire/DS2482 code between operations.
Definition main.cpp:2954
void inputLoop(short cause)
Polls digital/analog inputs and processes their events.
Definition main.cpp:3053
void mqttCallback(char *topic, byte *payload, unsigned int length)
MQTT receive callback: dispatches incoming messages to item command processing or config handling.
Definition main.cpp:393
int cmdFunctionHelp(int arg_cnt, char **args)
CLI command "help": lists available CLI commands.
Definition main.cpp:1367
int cmdFunctionKill(int arg_cnt, char **args)
CLI command "kill": aborts the current long operation.
Definition main.cpp:1395
int16_t attachMaturaTimer()
Attaches the Matura timer (interrupt-based timing helper).
Definition main.cpp:2254
int cmdFunctionGet(int arg_cnt, char **args)
CLI command "get": re-downloads the config from the portal.
Definition main.cpp:1772
int loadConfigFromHttp()
Downloads the device JSON config from the config server (HTTP GET with If-None-Match ETag),...
Definition main.cpp:1877
void loop_main()
Application main loop: runs the LAN/MQTT state machine, CLI poller, item polling, thermostat,...
Definition main.cpp:2879
void resetHard()
Hard-resets the board (watchdog or MCU reset).
Definition main.cpp:1353
int loadConfigFromEEPROM()
Loads the last saved config from flash into the config tree.
Definition main.cpp:1613
bool isNotRetainingStatus()
Checks whether the controller is not in the retain/collecting phase.
Definition main.cpp:246
void setup_main()
Application setup: init serial, flash config, MAC, LAN, HTTP/CLI listeners and all drivers (runs once...
Definition main.cpp:2355
void setFirstBroker()
Selects the first configured MQTT broker for connection.
Definition main.cpp:1062
void modbusIdle(void)
Idle hook invoked by the Modbus code between serial operations to let the main system breathe.
Definition main.cpp:3008
void inputSensorsLoop()
Polls periodic sensors (DHT, ultrasonic, ...) connected to inputs.
Definition main.cpp:3109
void printCurentLanConfig()
Prints the current LAN configuration to the serial port.
Definition main.cpp:1340
void setupCmdArduino()
Registers the serial CLI commands (help, save, load, get, mac, ip, kill, reboot, log,...
Definition main.cpp:2850
lan_status lanLoop()
One iteration of the LAN/MQTT state machine; advances the ethernet/WiFi/MQTT boot sequence and return...
Definition main.cpp:613
void printMACAddress()
Prints the current MAC address to the info log.
Definition main.cpp:543
int cmdFunctionSave(int arg_cnt, char **args)
CLI command "save": persists the current config tree to flash.
Definition main.cpp:1660
int cmdFunctionSetMac(int arg_cnt, char **args)
CLI command "mac": changes the controller MAC address.
Definition main.cpp:1818
void printConfigSummary()
Prints a summary of the current network/system config.
Definition main.cpp:1568
void onInitialStateInitLAN()
Initial LAN setup: initializes ethernet/WiFi and starts the address acquisition sequence.
Definition main.cpp:1198
void pollingLoop(void)
Periodic item polling: refreshes states and publishes changes.
Definition main.cpp:3186
void ip_ready_config_loaded_connecting_to_broker()
State handler for IP_READY_CONFIG_LOADED_CONNECTING_TO_BROKER: connects to the configured MQTT broker...
Definition main.cpp:1077
void inputStop(void)
Stops and releases all input channels.
Definition main.cpp:3161
void onMQTTConnect()
Called on (re)connect to the MQTT broker: sets up topics, subscribes and publishes retained states.
Definition main.cpp:927
bool IsThermostat(const aJsonObject *item)
Checks whether a config array entry is a thermostat item.
Streamlog errorSerial
bool disabledDisconnected(const aJsonObject *thermoExtensionArray, int thermoLatestCommand)
Checks whether a thermostat must be stopped (disabled or its temperature sensor disconnected for too ...
Streamlog infoSerial
lan_status
LAN/MQTT boot-up and runtime state machine states.
Definition main.h:237
@ INITIAL_STATE
Definition main.h:238
@ GET
Definition main.h:254
@ DO_GET
Definition main.h:253
@ IP_READY_CONFIG_LOADED_CONNECTING_TO_BROKER
Definition main.h:242
@ LIBS_INITIALIZED
Definition main.h:241
@ AWAITING_CONFIG
Definition main.h:256
@ DO_REINIT
Definition main.h:246
@ GET_IN_PROGRESS
Definition main.h:255
@ REINIT
Definition main.h:247
@ DO_RECONNECT
Definition main.h:248
@ DO_NOTHING
Definition main.h:252
@ OPERATION_NO_MQTT
Definition main.h:245
@ HAVE_IP_ADDRESS
Definition main.h:240
@ RETAINING_COLLECTING
Definition main.h:243
@ DO_READ_RE_CONFIG
Definition main.h:251
@ OPERATION
Definition main.h:244
@ RECONNECT
Definition main.h:249
@ READ_RE_CONFIG
Definition main.h:250
@ AWAITING_ADDRESS
Definition main.h:239
Streamlog debugSerial
void Changed(int i, DeviceAddress addr, float currentTemp)
1-Wire temperature change callback.
1-Wire temperature sensing for thermostats.
Topic/suffix string constants.
Chip unique ID (20 bytes), viewable as 5 longs or 20 bytes.
Definition main.h:263
uint8_t UID_Byte[20]
Definition main.h:265
uint32_t UID_Long[5]
Definition main.h:264
Utility helpers of LightHub.