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.

349 lines
9.6 KiB

8 years ago
8 years ago
8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
  1. /*
  2. ESPurna
  3. Copyright (C) 2016-2017 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. #include <FastLED.h> // W.T.H. is this include ALSO needed here ?!?!!
  18. // -----------------------------------------------------------------------------
  19. // METHODS
  20. // -----------------------------------------------------------------------------
  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. }
  35. void hardwareLoop() {
  36. // System check
  37. static bool checked = false;
  38. if (!checked && (millis() > CRASH_SAFE_TIME)) {
  39. // Check system as stable
  40. systemCheck(true);
  41. checked = true;
  42. }
  43. // Heartbeat
  44. static unsigned long last_uptime = 0;
  45. if ((millis() - last_uptime > HEARTBEAT_INTERVAL) || (last_uptime == 0)) {
  46. last_uptime = millis();
  47. heartbeat();
  48. }
  49. }
  50. // -----------------------------------------------------------------------------
  51. // BOOTING
  52. // -----------------------------------------------------------------------------
  53. unsigned int sectors(size_t size) {
  54. return (int) (size + SPI_FLASH_SEC_SIZE - 1) / SPI_FLASH_SEC_SIZE;
  55. }
  56. void welcome() {
  57. DEBUG_MSG_P(PSTR("\n\n"));
  58. DEBUG_MSG_P(PSTR("[INIT] %s %s\n"), (char *) APP_NAME, (char *) APP_VERSION);
  59. DEBUG_MSG_P(PSTR("[INIT] %s\n"), (char *) APP_AUTHOR);
  60. DEBUG_MSG_P(PSTR("[INIT] %s\n\n"), (char *) APP_WEBSITE);
  61. DEBUG_MSG_P(PSTR("[INIT] CPU chip ID: 0x%06X\n"), ESP.getChipId());
  62. DEBUG_MSG_P(PSTR("[INIT] CPU frequency: %d MHz\n"), ESP.getCpuFreqMHz());
  63. DEBUG_MSG_P(PSTR("[INIT] SDK version: %s\n"), ESP.getSdkVersion());
  64. DEBUG_MSG_P(PSTR("[INIT] Core version: %s\n"), ESP.getCoreVersion().c_str());
  65. DEBUG_MSG_P(PSTR("\n"));
  66. // -------------------------------------------------------------------------
  67. FlashMode_t mode = ESP.getFlashChipMode();
  68. DEBUG_MSG_P(PSTR("[INIT] Flash chip ID: 0x%06X\n"), ESP.getFlashChipId());
  69. DEBUG_MSG_P(PSTR("[INIT] Flash speed: %u Hz\n"), ESP.getFlashChipSpeed());
  70. DEBUG_MSG_P(PSTR("[INIT] Flash mode: %s\n"), mode == FM_QIO ? "QIO" : mode == FM_QOUT ? "QOUT" : mode == FM_DIO ? "DIO" : mode == FM_DOUT ? "DOUT" : "UNKNOWN");
  71. DEBUG_MSG_P(PSTR("\n"));
  72. DEBUG_MSG_P(PSTR("[INIT] Flash sector size: %8u bytes\n"), SPI_FLASH_SEC_SIZE);
  73. DEBUG_MSG_P(PSTR("[INIT] Flash size (CHIP): %8u bytes\n"), ESP.getFlashChipRealSize());
  74. DEBUG_MSG_P(PSTR("[INIT] Flash size (SDK): %8u bytes / %4d sectors\n"), ESP.getFlashChipSize(), sectors(ESP.getFlashChipSize()));
  75. DEBUG_MSG_P(PSTR("[INIT] Firmware size: %8u bytes / %4d sectors\n"), ESP.getSketchSize(), sectors(ESP.getSketchSize()));
  76. DEBUG_MSG_P(PSTR("[INIT] OTA size: %8u bytes / %4d sectors\n"), ESP.getFreeSketchSpace(), sectors(ESP.getFreeSketchSpace()));
  77. #if SPIFFS_SUPPORT
  78. FSInfo fs_info;
  79. bool fs = SPIFFS.info(fs_info);
  80. if (fs) {
  81. DEBUG_MSG_P(PSTR("[INIT] SPIFFS size: %8u bytes / %4d sectors\n"), fs_info.totalBytes, sectors(fs_info.totalBytes));
  82. }
  83. #else
  84. DEBUG_MSG_P(PSTR("[INIT] SPIFFS size: %8u bytes / %4d sectors\n"), 0, 0);
  85. #endif
  86. DEBUG_MSG_P(PSTR("[INIT] EEPROM size: %8u bytes / %4d sectors\n"), settingsMaxSize(), sectors(settingsMaxSize()));
  87. DEBUG_MSG_P(PSTR("[INIT] Empty space: %8u bytes / 4 sectors\n"), 4 * SPI_FLASH_SEC_SIZE);
  88. DEBUG_MSG_P(PSTR("\n"));
  89. // -------------------------------------------------------------------------
  90. #if SPIFFS_SUPPORT
  91. if (fs) {
  92. DEBUG_MSG_P(PSTR("[INIT] SPIFFS total size: %8u bytes\n"), fs_info.totalBytes);
  93. DEBUG_MSG_P(PSTR("[INIT] used size: %8u bytes\n"), fs_info.usedBytes);
  94. DEBUG_MSG_P(PSTR("[INIT] block size: %8u bytes\n"), fs_info.blockSize);
  95. DEBUG_MSG_P(PSTR("[INIT] page size: %8u bytes\n"), fs_info.pageSize);
  96. DEBUG_MSG_P(PSTR("[INIT] max files: %8u\n"), fs_info.maxOpenFiles);
  97. DEBUG_MSG_P(PSTR("[INIT] max length: %8u\n"), fs_info.maxPathLength);
  98. } else {
  99. DEBUG_MSG_P(PSTR("[INIT] No SPIFFS partition\n"));
  100. }
  101. DEBUG_MSG_P(PSTR("\n"));
  102. #endif
  103. // -------------------------------------------------------------------------
  104. DEBUG_MSG_P(PSTR("[INIT] MANUFACTURER: %s\n"), MANUFACTURER);
  105. DEBUG_MSG_P(PSTR("[INIT] DEVICE: %s\n"), DEVICE);
  106. DEBUG_MSG_P(PSTR("[INIT] SUPPORT:"));
  107. #if ALEXA_SUPPORT
  108. DEBUG_MSG_P(PSTR(" ALEXA"));
  109. #endif
  110. #if ANALOG_SUPPORT
  111. DEBUG_MSG_P(PSTR(" ANALOG"));
  112. #endif
  113. #if COUNTER_SUPPORT
  114. DEBUG_MSG_P(PSTR(" COUNTER"));
  115. #endif
  116. #if DEBUG_SERIAL_SUPPORT
  117. DEBUG_MSG_P(PSTR(" DEBUG_SERIAL"));
  118. #endif
  119. #if DEBUG_UDP_SUPPORT
  120. DEBUG_MSG_P(PSTR(" DEBUG_UDP"));
  121. #endif
  122. #if DHT_SUPPORT
  123. DEBUG_MSG_P(PSTR(" DHT"));
  124. #endif
  125. #if DOMOTICZ_SUPPORT
  126. DEBUG_MSG_P(PSTR(" DOMOTICZ"));
  127. #endif
  128. #if DS18B20_SUPPORT
  129. DEBUG_MSG_P(PSTR(" DS18B20"));
  130. #endif
  131. #if HOMEASSISTANT_SUPPORT
  132. DEBUG_MSG_P(PSTR(" HOMEASSISTANT"));
  133. #endif
  134. #if I2C_SUPPORT
  135. DEBUG_MSG_P(PSTR(" I2C"));
  136. #endif
  137. #if INFLUXDB_SUPPORT
  138. DEBUG_MSG_P(PSTR(" INFLUXDB"));
  139. #endif
  140. #if MDNS_SUPPORT
  141. DEBUG_MSG_P(PSTR(" MDNS"));
  142. #endif
  143. #if NOFUSS_SUPPORT
  144. DEBUG_MSG_P(PSTR(" NOFUSS"));
  145. #endif
  146. #if NTP_SUPPORT
  147. DEBUG_MSG_P(PSTR(" NTP"));
  148. #endif
  149. #if RF_SUPPORT
  150. DEBUG_MSG_P(PSTR(" RF"));
  151. #endif
  152. #if SPIFFS_SUPPORT
  153. DEBUG_MSG_P(PSTR(" SPIFFS"));
  154. #endif
  155. #if TERMINAL_SUPPORT
  156. DEBUG_MSG_P(PSTR(" TERMINAL"));
  157. #endif
  158. #if WEB_SUPPORT
  159. DEBUG_MSG_P(PSTR(" WEB"));
  160. #endif
  161. DEBUG_MSG_P(PSTR("\n\n"));
  162. // -------------------------------------------------------------------------
  163. unsigned char custom_reset = customReset();
  164. if (custom_reset > 0) {
  165. char buffer[32];
  166. strcpy_P(buffer, custom_reset_string[custom_reset-1]);
  167. DEBUG_MSG_P(PSTR("[INIT] Last reset reason: %s\n"), buffer);
  168. } else {
  169. DEBUG_MSG_P(PSTR("[INIT] Last reset reason: %s\n"), (char *) ESP.getResetReason().c_str());
  170. }
  171. DEBUG_MSG_P(PSTR("[INIT] Free heap: %u bytes\n"), ESP.getFreeHeap());
  172. #if ADC_VCC_ENABLED
  173. DEBUG_MSG_P(PSTR("[INIT] Power: %d mV\n"), ESP.getVcc());
  174. #endif
  175. DEBUG_MSG_P(PSTR("\n"));
  176. }
  177. void setup() {
  178. // Init EEPROM, Serial and SPIFFS
  179. hardwareSetup();
  180. // Question system stability
  181. systemCheck(false);
  182. // Show welcome message and system configuration
  183. welcome();
  184. // Init persistance and terminal features
  185. settingsSetup();
  186. if (getSetting("hostname").length() == 0) {
  187. setSetting("hostname", getIdentifier());
  188. }
  189. wifiSetup();
  190. otaSetup();
  191. #if TELNET_SUPPORT
  192. telnetSetup();
  193. #endif
  194. // Do not run the next services if system is flagged stable
  195. if (!systemCheck()) return;
  196. #if WEB_SUPPORT
  197. webSetup();
  198. #endif
  199. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  200. lightSetup();
  201. #endif
  202. relaySetup();
  203. buttonSetup();
  204. ledSetup();
  205. mqttSetup();
  206. #ifdef ITEAD_SONOFF_RFBRIDGE
  207. rfbSetup();
  208. #endif
  209. #if POWER_PROVIDER != POWER_PROVIDER_NONE
  210. powerSetup();
  211. #endif
  212. #if NTP_SUPPORT
  213. ntpSetup();
  214. #endif
  215. #if I2C_SUPPORT
  216. i2cSetup();
  217. #endif
  218. #if ALEXA_SUPPORT
  219. alexaSetup();
  220. #endif
  221. #if NOFUSS_SUPPORT
  222. nofussSetup();
  223. #endif
  224. #if INFLUXDB_SUPPORT
  225. influxDBSetup();
  226. #endif
  227. #if DS18B20_SUPPORT
  228. dsSetup();
  229. #endif
  230. #if ANALOG_SUPPORT
  231. analogSetup();
  232. #endif
  233. #if COUNTER_SUPPORT
  234. counterSetup();
  235. #endif
  236. #if DHT_SUPPORT
  237. dhtSetup();
  238. #endif
  239. #if RF_SUPPORT
  240. rfSetup();
  241. #endif
  242. #if DOMOTICZ_SUPPORT
  243. domoticzSetup();
  244. #endif
  245. // Prepare configuration for version 2.0
  246. hwUpwardsCompatibility();
  247. saveSettings();
  248. }
  249. void loop() {
  250. hardwareLoop();
  251. settingsLoop();
  252. wifiLoop();
  253. otaLoop();
  254. // Do not run the next services if system is flagged stable
  255. if (!systemCheck()) return;
  256. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  257. lightLoop();
  258. #endif
  259. buttonLoop();
  260. relayLoop();
  261. ledLoop();
  262. mqttLoop();
  263. #ifdef ITEAD_SONOFF_RFBRIDGE
  264. rfbLoop();
  265. #endif
  266. #if POWER_PROVIDER != POWER_PROVIDER_NONE
  267. powerLoop();
  268. #endif
  269. #if NTP_SUPPORT
  270. ntpLoop();
  271. #endif
  272. #if ALEXA_SUPPORT
  273. alexaLoop();
  274. #endif
  275. #if NOFUSS_SUPPORT
  276. nofussLoop();
  277. #endif
  278. #if DS18B20_SUPPORT
  279. dsLoop();
  280. #endif
  281. #if ANALOG_SUPPORT
  282. analogLoop();
  283. #endif
  284. #if COUNTER_SUPPORT
  285. counterLoop();
  286. #endif
  287. #if DHT_SUPPORT
  288. dhtLoop();
  289. #endif
  290. #if RF_SUPPORT
  291. rfLoop();
  292. #endif
  293. }