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.

259 lines
5.4 KiB

8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
8 years ago
8 years ago
  1. /*
  2. ESPurna
  3. Copyright (C) 2016-2018 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 "config/all.h"
  16. #include <EEPROM.h>
  17. // -----------------------------------------------------------------------------
  18. // METHODS
  19. // -----------------------------------------------------------------------------
  20. unsigned long _loopDelay = 0;
  21. void hardwareSetup() {
  22. EEPROM.begin(EEPROM_SIZE);
  23. #if DEBUG_SERIAL_SUPPORT
  24. DEBUG_PORT.begin(SERIAL_BAUDRATE);
  25. #if DEBUG_ESP_WIFI
  26. DEBUG_PORT.setDebugOutput(true);
  27. #endif
  28. #elif defined(SERIAL_BAUDRATE)
  29. Serial.begin(SERIAL_BAUDRATE);
  30. #endif
  31. #if SPIFFS_SUPPORT
  32. SPIFFS.begin();
  33. #endif
  34. #if defined(ESPLIVE)
  35. //The ESPLive has an ADC MUX which needs to be configured.
  36. pinMode(16, OUTPUT);
  37. digitalWrite(16, HIGH); //Defualt CT input (pin B, solder jumper B)
  38. #endif
  39. }
  40. void hardwareLoop() {
  41. // Heartbeat
  42. #if HEARTBEAT_ENABLED
  43. static unsigned long last = 0;
  44. if ((last == 0) || (millis() - last > HEARTBEAT_INTERVAL)) {
  45. last = millis();
  46. heartbeat();
  47. }
  48. #endif // HEARTBEAT_ENABLED
  49. }
  50. // -----------------------------------------------------------------------------
  51. // BOOTING
  52. // -----------------------------------------------------------------------------
  53. void setup() {
  54. // Init EEPROM, Serial and SPIFFS
  55. hardwareSetup();
  56. // Question system stability
  57. #if SYSTEM_CHECK_ENABLED
  58. systemCheck(false);
  59. #endif
  60. // Init persistance and terminal features
  61. settingsSetup();
  62. if (getSetting("hostname").length() == 0) {
  63. setSetting("hostname", getIdentifier());
  64. }
  65. setBoardName();
  66. // Cache loop delay value to speed things (recommended max 250ms)
  67. _loopDelay = atol(getSetting("loopDelay", LOOP_DELAY_TIME).c_str());
  68. // Show welcome message and system configuration
  69. info();
  70. // Basic modules, will always run
  71. wifiSetup();
  72. otaSetup();
  73. #if TELNET_SUPPORT
  74. telnetSetup();
  75. #endif
  76. // Do not run the next services if system is flagged stable
  77. #if SYSTEM_CHECK_ENABLED
  78. if (!systemCheck()) return;
  79. #endif
  80. // Init webserver required before any module that uses API
  81. #if WEB_SUPPORT
  82. webSetup();
  83. wsSetup();
  84. apiSetup();
  85. #endif
  86. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  87. lightSetup();
  88. #endif
  89. relaySetup();
  90. buttonSetup();
  91. ledSetup();
  92. #if MQTT_SUPPORT
  93. mqttSetup();
  94. #endif
  95. #if MDNS_SERVER_SUPPORT
  96. mdnsServerSetup();
  97. #endif
  98. #if LLMNR_SUPPORT
  99. llmnrSetup();
  100. #endif
  101. #if NETBIOS_SUPPORT
  102. netbiosSetup();
  103. #endif
  104. #if SSDP_SUPPORT
  105. ssdpSetup();
  106. #endif
  107. #if NTP_SUPPORT
  108. ntpSetup();
  109. #endif
  110. #if I2C_SUPPORT
  111. i2cSetup();
  112. #if I2C_CLEAR_BUS
  113. i2cClearBus();
  114. #endif
  115. i2cScan();
  116. #endif
  117. #ifdef ITEAD_SONOFF_RFBRIDGE
  118. rfbSetup();
  119. #endif
  120. #if ALEXA_SUPPORT
  121. alexaSetup();
  122. #endif
  123. #if NOFUSS_SUPPORT
  124. nofussSetup();
  125. #endif
  126. #if INFLUXDB_SUPPORT
  127. idbSetup();
  128. #endif
  129. #if THINGSPEAK_SUPPORT
  130. tspkSetup();
  131. #endif
  132. #if RF_SUPPORT
  133. rfSetup();
  134. #endif
  135. #if IR_SUPPORT
  136. irSetup();
  137. #endif
  138. #if DOMOTICZ_SUPPORT
  139. domoticzSetup();
  140. #endif
  141. #if HOMEASSISTANT_SUPPORT
  142. haSetup();
  143. #endif
  144. #if SENSOR_SUPPORT
  145. sensorSetup();
  146. #endif
  147. #if SCHEDULER_SUPPORT
  148. schSetup();
  149. #endif
  150. // 3rd party code hook
  151. #if USE_EXTRA
  152. extraSetup();
  153. #endif
  154. // Prepare configuration for version 2.0
  155. migrate();
  156. saveSettings();
  157. }
  158. void loop() {
  159. hardwareLoop();
  160. settingsLoop();
  161. wifiLoop();
  162. otaLoop();
  163. #if SYSTEM_CHECK_ENABLED
  164. systemCheckLoop();
  165. // Do not run the next services if system is flagged stable
  166. if (!systemCheck()) return;
  167. #endif
  168. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  169. lightLoop();
  170. #endif
  171. relayLoop();
  172. buttonLoop();
  173. ledLoop();
  174. #if MQTT_SUPPORT
  175. mqttLoop();
  176. #endif
  177. #ifdef ITEAD_SONOFF_RFBRIDGE
  178. rfbLoop();
  179. #endif
  180. #if SSDP_SUPPORT
  181. ssdpLoop();
  182. #endif
  183. #if NTP_SUPPORT
  184. ntpLoop();
  185. #endif
  186. #if ALEXA_SUPPORT
  187. alexaLoop();
  188. #endif
  189. #if NOFUSS_SUPPORT
  190. nofussLoop();
  191. #endif
  192. #if RF_SUPPORT
  193. rfLoop();
  194. #endif
  195. #if IR_SUPPORT
  196. irLoop();
  197. #endif
  198. #if SENSOR_SUPPORT
  199. sensorLoop();
  200. #endif
  201. #if THINGSPEAK_SUPPORT
  202. tspkLoop();
  203. #endif
  204. #if SCHEDULER_SUPPORT
  205. schLoop();
  206. #endif
  207. #if MDNS_CLIENT_SUPPORT
  208. mdnsClientLoop();
  209. #endif
  210. // 3rd party code hook
  211. #if USE_EXTRA
  212. extraLoop();
  213. #endif
  214. // Power saving delay
  215. delay(_loopDelay);
  216. }