Fork of the espurna firmware for `mhsw` switches
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.

317 lines
7.1 KiB

  1. /*
  2. ESPurna
  3. Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "espurna.h"
  16. #include "alexa.h"
  17. #include "api.h"
  18. #include "broker.h"
  19. #include "button.h"
  20. #include "crash.h"
  21. #include "debug.h"
  22. #include "domoticz.h"
  23. #include "encoder.h"
  24. #include "homeassistant.h"
  25. #include "i2c.h"
  26. #include "influxdb.h"
  27. #include "ir.h"
  28. #include "led.h"
  29. #include "light.h"
  30. #include "lightfox.h"
  31. #include "llmnr.h"
  32. #include "mdns.h"
  33. #include "mqtt.h"
  34. #include "netbios.h"
  35. #include "nofuss.h"
  36. #include "ntp.h"
  37. #include "ota.h"
  38. #include "relay.h"
  39. #include "rfbridge.h"
  40. #include "rfm69.h"
  41. #include "rpc.h"
  42. #include "rpnrules.h"
  43. #include "rtcmem.h"
  44. #include "scheduler.h"
  45. #include "sensor.h"
  46. #include "ssdp.h"
  47. #include "telnet.h"
  48. #include "thermostat.h"
  49. #include "thingspeak.h"
  50. #include "tuya.h"
  51. #include "uartmqtt.h"
  52. #include "web.h"
  53. #include "ws.h"
  54. std::vector<void_callback_f> _loop_callbacks;
  55. std::vector<void_callback_f> _reload_callbacks;
  56. bool _reload_config = false;
  57. unsigned long _loop_delay = 0;
  58. // -----------------------------------------------------------------------------
  59. // GENERAL CALLBACKS
  60. // -----------------------------------------------------------------------------
  61. void espurnaRegisterLoop(void_callback_f callback) {
  62. _loop_callbacks.push_back(callback);
  63. }
  64. void espurnaRegisterReload(void_callback_f callback) {
  65. _reload_callbacks.push_back(callback);
  66. }
  67. void espurnaReload() {
  68. _reload_config = true;
  69. }
  70. void _espurnaReload() {
  71. for (const auto& callback : _reload_callbacks) {
  72. callback();
  73. }
  74. }
  75. unsigned long espurnaLoopDelay() {
  76. return _loop_delay;
  77. }
  78. // -----------------------------------------------------------------------------
  79. // BOOTING
  80. // -----------------------------------------------------------------------------
  81. void setup() {
  82. // -------------------------------------------------------------------------
  83. // Basic modules, will always run
  84. // -------------------------------------------------------------------------
  85. // Cache initial free heap value
  86. setInitialFreeHeap();
  87. // Init logging module
  88. #if DEBUG_SUPPORT
  89. debugSetup();
  90. #endif
  91. // Init GPIO functions
  92. gpioSetup();
  93. // Init RTCMEM
  94. rtcmemSetup();
  95. // Init EEPROM
  96. eepromSetup();
  97. // Init persistance
  98. settingsSetup();
  99. // Configure logger and crash recorder
  100. #if DEBUG_SUPPORT
  101. debugConfigureBoot();
  102. crashSetup();
  103. #endif
  104. // Return bogus free heap value for broken devices
  105. // XXX: device is likely to trigger other bugs! tread carefuly
  106. wtfHeap(getSetting<int>("wtfHeap", 0));
  107. // Init Serial, SPIFFS and system check
  108. systemSetup();
  109. // Init terminal features
  110. #if TERMINAL_SUPPORT
  111. terminalSetup();
  112. #endif
  113. // Hostname & board name initialization
  114. if (getSetting("hostname").length() == 0) {
  115. setDefaultHostname();
  116. }
  117. setBoardName();
  118. // Show welcome message and system configuration
  119. info(true);
  120. wifiSetup();
  121. #if OTA_ARDUINOOTA_SUPPORT
  122. arduinoOtaSetup();
  123. #endif
  124. #if TELNET_SUPPORT
  125. telnetSetup();
  126. #endif
  127. #if OTA_CLIENT != OTA_CLIENT_NONE
  128. otaClientSetup();
  129. #endif
  130. // -------------------------------------------------------------------------
  131. // Check if system is stable
  132. // -------------------------------------------------------------------------
  133. #if SYSTEM_CHECK_ENABLED
  134. if (!systemCheck()) return;
  135. #endif
  136. // -------------------------------------------------------------------------
  137. // Next modules will be only loaded if system is flagged as stable
  138. // -------------------------------------------------------------------------
  139. // Init webserver required before any module that uses API
  140. #if WEB_SUPPORT
  141. webSetup();
  142. wsSetup();
  143. #if DEBUG_WEB_SUPPORT
  144. debugWebSetup();
  145. #endif
  146. #if OTA_WEB_SUPPORT
  147. otaWebSetup();
  148. #endif
  149. #endif
  150. #if API_SUPPORT
  151. apiSetup();
  152. #endif
  153. // lightSetup must be called before relaySetup
  154. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  155. lightSetup();
  156. #endif
  157. #if RELAY_SUPPORT
  158. relaySetup();
  159. #endif
  160. #if BUTTON_SUPPORT
  161. buttonSetup();
  162. #endif
  163. #if ENCODER_SUPPORT && (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE)
  164. encoderSetup();
  165. #endif
  166. #if LED_SUPPORT
  167. ledSetup();
  168. #endif
  169. #if MQTT_SUPPORT
  170. mqttSetup();
  171. #endif
  172. #if MDNS_SERVER_SUPPORT
  173. mdnsServerSetup();
  174. #endif
  175. #if MDNS_CLIENT_SUPPORT
  176. mdnsClientSetup();
  177. #endif
  178. #if LLMNR_SUPPORT
  179. llmnrSetup();
  180. #endif
  181. #if NETBIOS_SUPPORT
  182. netbiosSetup();
  183. #endif
  184. #if SSDP_SUPPORT
  185. ssdpSetup();
  186. #endif
  187. #if NTP_SUPPORT
  188. ntpSetup();
  189. #endif
  190. #if I2C_SUPPORT
  191. i2cSetup();
  192. #endif
  193. #if RF_SUPPORT
  194. rfbSetup();
  195. #endif
  196. #if ALEXA_SUPPORT
  197. alexaSetup();
  198. #endif
  199. #if NOFUSS_SUPPORT
  200. nofussSetup();
  201. #endif
  202. #if SENSOR_SUPPORT
  203. sensorSetup();
  204. #endif
  205. #if INFLUXDB_SUPPORT
  206. idbSetup();
  207. #endif
  208. #if THINGSPEAK_SUPPORT
  209. tspkSetup();
  210. #endif
  211. #if RFM69_SUPPORT
  212. rfm69Setup();
  213. #endif
  214. #if IR_SUPPORT
  215. irSetup();
  216. #endif
  217. #if DOMOTICZ_SUPPORT
  218. domoticzSetup();
  219. #endif
  220. #if HOMEASSISTANT_SUPPORT
  221. haSetup();
  222. #endif
  223. #if SCHEDULER_SUPPORT
  224. schSetup();
  225. #endif
  226. #if RPN_RULES_SUPPORT
  227. rpnSetup();
  228. #endif
  229. #if UART_MQTT_SUPPORT
  230. uartmqttSetup();
  231. #endif
  232. #ifdef FOXEL_LIGHTFOX_DUAL
  233. lightfoxSetup();
  234. #endif
  235. #if THERMOSTAT_SUPPORT
  236. thermostatSetup();
  237. #endif
  238. #if THERMOSTAT_DISPLAY_SUPPORT
  239. displaySetup();
  240. #endif
  241. #if TUYA_SUPPORT
  242. Tuya::tuyaSetup();
  243. #endif
  244. // 3rd party code hook
  245. #if USE_EXTRA
  246. extraSetup();
  247. #endif
  248. // Prepare configuration for version 2.0
  249. migrate();
  250. // Set up delay() after loop callbacks are finished
  251. // Note: should be after settingsSetup()
  252. _loop_delay = constrain(
  253. getSetting("loopDelay", LOOP_DELAY_TIME), 0, 300
  254. );
  255. saveSettings();
  256. }
  257. void loop() {
  258. // Reload config before running any callbacks
  259. if (_reload_config) {
  260. _espurnaReload();
  261. _reload_config = false;
  262. }
  263. // Call registered loop callbacks
  264. for (unsigned char i = 0; i < _loop_callbacks.size(); i++) {
  265. (_loop_callbacks[i])();
  266. }
  267. // Power saving delay
  268. if (_loop_delay) delay(_loop_delay);
  269. }