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.

247 lines
5.2 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. static unsigned long last = 0;
  43. if ((last == 0) || (millis() - last > HEARTBEAT_INTERVAL)) {
  44. last = millis();
  45. heartbeat();
  46. }
  47. }
  48. // -----------------------------------------------------------------------------
  49. // BOOTING
  50. // -----------------------------------------------------------------------------
  51. void setup() {
  52. // Init EEPROM, Serial and SPIFFS
  53. hardwareSetup();
  54. // Question system stability
  55. #if SYSTEM_CHECK_ENABLED
  56. systemCheck(false);
  57. #endif
  58. // Init persistance and terminal features
  59. settingsSetup();
  60. if (getSetting("hostname").length() == 0) {
  61. setSetting("hostname", getIdentifier());
  62. }
  63. setBoardName();
  64. // Cache loop delay value to speed things (recommended max 250ms)
  65. _loopDelay = atol(getSetting("loopDelay", LOOP_DELAY_TIME).c_str());
  66. // Show welcome message and system configuration
  67. info();
  68. // Basic modules, will always run
  69. wifiSetup();
  70. otaSetup();
  71. #if TELNET_SUPPORT
  72. telnetSetup();
  73. #endif
  74. // Do not run the next services if system is flagged stable
  75. #if SYSTEM_CHECK_ENABLED
  76. if (!systemCheck()) return;
  77. #endif
  78. // Init webserver required before any module that uses API
  79. #if WEB_SUPPORT
  80. webSetup();
  81. wsSetup();
  82. apiSetup();
  83. #endif
  84. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  85. lightSetup();
  86. #endif
  87. relaySetup();
  88. buttonSetup();
  89. ledSetup();
  90. #if MQTT_SUPPORT
  91. mqttSetup();
  92. #endif
  93. #if MDNS_SERVER_SUPPORT
  94. mdnsServerSetup();
  95. #endif
  96. #if LLMNR_SUPPORT
  97. llmnrSetup();
  98. #endif
  99. #if NETBIOS_SUPPORT
  100. netbiosSetup();
  101. #endif
  102. #if SSDP_SUPPORT
  103. ssdpSetup();
  104. #endif
  105. #if NTP_SUPPORT
  106. ntpSetup();
  107. #endif
  108. #if I2C_SUPPORT
  109. i2cSetup();
  110. #if I2C_CLEAR_BUS
  111. i2cClearBus();
  112. #endif
  113. i2cScan();
  114. #endif
  115. #ifdef ITEAD_SONOFF_RFBRIDGE
  116. rfbSetup();
  117. #endif
  118. #if ALEXA_SUPPORT
  119. alexaSetup();
  120. #endif
  121. #if NOFUSS_SUPPORT
  122. nofussSetup();
  123. #endif
  124. #if INFLUXDB_SUPPORT
  125. idbSetup();
  126. #endif
  127. #if THINGSPEAK_SUPPORT
  128. tspkSetup();
  129. #endif
  130. #if RF_SUPPORT
  131. rfSetup();
  132. #endif
  133. #if IR_SUPPORT
  134. irSetup();
  135. #endif
  136. #if DOMOTICZ_SUPPORT
  137. domoticzSetup();
  138. #endif
  139. #if HOMEASSISTANT_SUPPORT
  140. haSetup();
  141. #endif
  142. #if SENSOR_SUPPORT
  143. sensorSetup();
  144. #endif
  145. #if SCHEDULER_SUPPORT
  146. schSetup();
  147. #endif
  148. // Prepare configuration for version 2.0
  149. migrate();
  150. saveSettings();
  151. }
  152. void loop() {
  153. hardwareLoop();
  154. settingsLoop();
  155. wifiLoop();
  156. otaLoop();
  157. #if SYSTEM_CHECK_ENABLED
  158. systemCheckLoop();
  159. // Do not run the next services if system is flagged stable
  160. if (!systemCheck()) return;
  161. #endif
  162. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  163. lightLoop();
  164. #endif
  165. relayLoop();
  166. buttonLoop();
  167. ledLoop();
  168. #if MQTT_SUPPORT
  169. mqttLoop();
  170. #endif
  171. #ifdef ITEAD_SONOFF_RFBRIDGE
  172. rfbLoop();
  173. #endif
  174. #if SSDP_SUPPORT
  175. ssdpLoop();
  176. #endif
  177. #if NTP_SUPPORT
  178. ntpLoop();
  179. #endif
  180. #if ALEXA_SUPPORT
  181. alexaLoop();
  182. #endif
  183. #if NOFUSS_SUPPORT
  184. nofussLoop();
  185. #endif
  186. #if RF_SUPPORT
  187. rfLoop();
  188. #endif
  189. #if IR_SUPPORT
  190. irLoop();
  191. #endif
  192. #if SENSOR_SUPPORT
  193. sensorLoop();
  194. #endif
  195. #if THINGSPEAK_SUPPORT
  196. tspkLoop();
  197. #endif
  198. #if SCHEDULER_SUPPORT
  199. schLoop();
  200. #endif
  201. #if MDNS_CLIENT_SUPPORT
  202. mdnsClientLoop();
  203. #endif
  204. // Power saving delay
  205. delay(_loopDelay);
  206. }