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.

377 lines
10 KiB

8 years ago
7 years ago
8 years ago
8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 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. unsigned long _loopDelay = 0;
  22. void hardwareSetup() {
  23. EEPROM.begin(EEPROM_SIZE);
  24. #if DEBUG_SERIAL_SUPPORT
  25. DEBUG_PORT.begin(SERIAL_BAUDRATE);
  26. #if DEBUG_ESP_WIFI
  27. DEBUG_PORT.setDebugOutput(true);
  28. #endif
  29. #elif defined(SERIAL_BAUDRATE)
  30. Serial.begin(SERIAL_BAUDRATE);
  31. #endif
  32. #if SPIFFS_SUPPORT
  33. SPIFFS.begin();
  34. #endif
  35. #if defined(ESPLIVE)
  36. //The ESPLive has an ADC MUX which needs to be configured.
  37. pinMode(16, OUTPUT);
  38. digitalWrite(16, HIGH); //Defualt CT input (pin B, solder jumper B)
  39. #endif
  40. }
  41. void hardwareLoop() {
  42. // Heartbeat
  43. static unsigned long last_uptime = 0;
  44. if ((millis() - last_uptime > HEARTBEAT_INTERVAL) || (last_uptime == 0)) {
  45. last_uptime = millis();
  46. heartbeat();
  47. }
  48. }
  49. // -----------------------------------------------------------------------------
  50. // BOOTING
  51. // -----------------------------------------------------------------------------
  52. unsigned int sectors(size_t size) {
  53. return (int) (size + SPI_FLASH_SEC_SIZE - 1) / SPI_FLASH_SEC_SIZE;
  54. }
  55. void welcome() {
  56. DEBUG_MSG_P(PSTR("\n\n"));
  57. DEBUG_MSG_P(PSTR("[INIT] %s %s\n"), (char *) APP_NAME, (char *) APP_VERSION);
  58. DEBUG_MSG_P(PSTR("[INIT] %s\n"), (char *) APP_AUTHOR);
  59. DEBUG_MSG_P(PSTR("[INIT] %s\n\n"), (char *) APP_WEBSITE);
  60. DEBUG_MSG_P(PSTR("[INIT] CPU chip ID: 0x%06X\n"), ESP.getChipId());
  61. DEBUG_MSG_P(PSTR("[INIT] CPU frequency: %d MHz\n"), ESP.getCpuFreqMHz());
  62. DEBUG_MSG_P(PSTR("[INIT] SDK version: %s\n"), ESP.getSdkVersion());
  63. DEBUG_MSG_P(PSTR("[INIT] Core version: %s\n"), ESP.getCoreVersion().c_str());
  64. DEBUG_MSG_P(PSTR("\n"));
  65. // -------------------------------------------------------------------------
  66. FlashMode_t mode = ESP.getFlashChipMode();
  67. DEBUG_MSG_P(PSTR("[INIT] Flash chip ID: 0x%06X\n"), ESP.getFlashChipId());
  68. DEBUG_MSG_P(PSTR("[INIT] Flash speed: %u Hz\n"), ESP.getFlashChipSpeed());
  69. 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");
  70. DEBUG_MSG_P(PSTR("\n"));
  71. DEBUG_MSG_P(PSTR("[INIT] Flash sector size: %8u bytes\n"), SPI_FLASH_SEC_SIZE);
  72. DEBUG_MSG_P(PSTR("[INIT] Flash size (CHIP): %8u bytes\n"), ESP.getFlashChipRealSize());
  73. DEBUG_MSG_P(PSTR("[INIT] Flash size (SDK): %8u bytes / %4d sectors\n"), ESP.getFlashChipSize(), sectors(ESP.getFlashChipSize()));
  74. DEBUG_MSG_P(PSTR("[INIT] Firmware size: %8u bytes / %4d sectors\n"), ESP.getSketchSize(), sectors(ESP.getSketchSize()));
  75. DEBUG_MSG_P(PSTR("[INIT] OTA size: %8u bytes / %4d sectors\n"), ESP.getFreeSketchSpace(), sectors(ESP.getFreeSketchSpace()));
  76. #if SPIFFS_SUPPORT
  77. FSInfo fs_info;
  78. bool fs = SPIFFS.info(fs_info);
  79. if (fs) {
  80. DEBUG_MSG_P(PSTR("[INIT] SPIFFS size: %8u bytes / %4d sectors\n"), fs_info.totalBytes, sectors(fs_info.totalBytes));
  81. }
  82. #else
  83. DEBUG_MSG_P(PSTR("[INIT] SPIFFS size: %8u bytes / %4d sectors\n"), 0, 0);
  84. #endif
  85. DEBUG_MSG_P(PSTR("[INIT] EEPROM size: %8u bytes / %4d sectors\n"), settingsMaxSize(), sectors(settingsMaxSize()));
  86. DEBUG_MSG_P(PSTR("[INIT] Empty space: %8u bytes / 4 sectors\n"), 4 * SPI_FLASH_SEC_SIZE);
  87. DEBUG_MSG_P(PSTR("\n"));
  88. // -------------------------------------------------------------------------
  89. #if SPIFFS_SUPPORT
  90. if (fs) {
  91. DEBUG_MSG_P(PSTR("[INIT] SPIFFS total size: %8u bytes\n"), fs_info.totalBytes);
  92. DEBUG_MSG_P(PSTR("[INIT] used size: %8u bytes\n"), fs_info.usedBytes);
  93. DEBUG_MSG_P(PSTR("[INIT] block size: %8u bytes\n"), fs_info.blockSize);
  94. DEBUG_MSG_P(PSTR("[INIT] page size: %8u bytes\n"), fs_info.pageSize);
  95. DEBUG_MSG_P(PSTR("[INIT] max files: %8u\n"), fs_info.maxOpenFiles);
  96. DEBUG_MSG_P(PSTR("[INIT] max length: %8u\n"), fs_info.maxPathLength);
  97. } else {
  98. DEBUG_MSG_P(PSTR("[INIT] No SPIFFS partition\n"));
  99. }
  100. DEBUG_MSG_P(PSTR("\n"));
  101. #endif
  102. // -------------------------------------------------------------------------
  103. DEBUG_MSG_P(PSTR("[INIT] MANUFACTURER: %s\n"), MANUFACTURER);
  104. DEBUG_MSG_P(PSTR("[INIT] DEVICE: %s\n"), DEVICE);
  105. DEBUG_MSG_P(PSTR("[INIT] SUPPORT:"));
  106. #if ALEXA_SUPPORT
  107. DEBUG_MSG_P(PSTR(" ALEXA"));
  108. #endif
  109. #if ANALOG_SUPPORT
  110. DEBUG_MSG_P(PSTR(" ANALOG"));
  111. #endif
  112. #if COUNTER_SUPPORT
  113. DEBUG_MSG_P(PSTR(" COUNTER"));
  114. #endif
  115. #if DEBUG_SERIAL_SUPPORT
  116. DEBUG_MSG_P(PSTR(" DEBUG_SERIAL"));
  117. #endif
  118. #if DEBUG_TELNET_SUPPORT
  119. DEBUG_MSG_P(PSTR(" DEBUG_TELNET"));
  120. #endif
  121. #if DEBUG_UDP_SUPPORT
  122. DEBUG_MSG_P(PSTR(" DEBUG_UDP"));
  123. #endif
  124. #if DHT_SUPPORT
  125. DEBUG_MSG_P(PSTR(" DHT"));
  126. #endif
  127. #if DOMOTICZ_SUPPORT
  128. DEBUG_MSG_P(PSTR(" DOMOTICZ"));
  129. #endif
  130. #if DS18B20_SUPPORT
  131. DEBUG_MSG_P(PSTR(" DS18B20"));
  132. #endif
  133. #if HOMEASSISTANT_SUPPORT
  134. DEBUG_MSG_P(PSTR(" HOMEASSISTANT"));
  135. #endif
  136. #if I2C_SUPPORT
  137. DEBUG_MSG_P(PSTR(" I2C"));
  138. #endif
  139. #if INFLUXDB_SUPPORT
  140. DEBUG_MSG_P(PSTR(" INFLUXDB"));
  141. #endif
  142. #if MDNS_SUPPORT
  143. DEBUG_MSG_P(PSTR(" MDNS"));
  144. #endif
  145. #if NOFUSS_SUPPORT
  146. DEBUG_MSG_P(PSTR(" NOFUSS"));
  147. #endif
  148. #if NTP_SUPPORT
  149. DEBUG_MSG_P(PSTR(" NTP"));
  150. #endif
  151. #if RF_SUPPORT
  152. DEBUG_MSG_P(PSTR(" RF"));
  153. #endif
  154. #if SPIFFS_SUPPORT
  155. DEBUG_MSG_P(PSTR(" SPIFFS"));
  156. #endif
  157. #if TELNET_SUPPORT
  158. DEBUG_MSG_P(PSTR(" TELNET"));
  159. #endif
  160. #if TERMINAL_SUPPORT
  161. DEBUG_MSG_P(PSTR(" TERMINAL"));
  162. #endif
  163. #if WEB_SUPPORT
  164. DEBUG_MSG_P(PSTR(" WEB"));
  165. #endif
  166. DEBUG_MSG_P(PSTR("\n\n"));
  167. // -------------------------------------------------------------------------
  168. unsigned char custom_reset = customReset();
  169. if (custom_reset > 0) {
  170. char buffer[32];
  171. strcpy_P(buffer, custom_reset_string[custom_reset-1]);
  172. DEBUG_MSG_P(PSTR("[INIT] Last reset reason: %s\n"), buffer);
  173. } else {
  174. DEBUG_MSG_P(PSTR("[INIT] Last reset reason: %s\n"), (char *) ESP.getResetReason().c_str());
  175. }
  176. DEBUG_MSG_P(PSTR("[INIT] Free heap: %u bytes\n"), ESP.getFreeHeap());
  177. #if ADC_VCC_ENABLED
  178. DEBUG_MSG_P(PSTR("[INIT] Power: %d mV\n"), ESP.getVcc());
  179. #endif
  180. DEBUG_MSG_P(PSTR("[INIT] Power saving delay value: %lu ms\n"), _loopDelay);
  181. DEBUG_MSG_P(PSTR("\n"));
  182. }
  183. void setup() {
  184. // Init EEPROM, Serial and SPIFFS
  185. hardwareSetup();
  186. // Question system stability
  187. #if SYSTEM_CHECK_ENABLED
  188. systemCheck(false);
  189. #endif
  190. // Init persistance and terminal features
  191. settingsSetup();
  192. if (getSetting("hostname").length() == 0) {
  193. setSetting("hostname", getIdentifier());
  194. }
  195. // Cache loop delay value to speed things (recommended max 250ms)
  196. _loopDelay = atol(getSetting("loopDelay", LOOP_DELAY_TIME).c_str());
  197. // Show welcome message and system configuration
  198. welcome();
  199. // Basic modules, will always run
  200. wifiSetup();
  201. otaSetup();
  202. #if TELNET_SUPPORT
  203. telnetSetup();
  204. #endif
  205. // Do not run the next services if system is flagged stable
  206. #if SYSTEM_CHECK_ENABLED
  207. if (!systemCheck()) return;
  208. #endif
  209. // Init webserver required before any module that uses API
  210. #if WEB_SUPPORT
  211. webSetup();
  212. #endif
  213. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  214. lightSetup();
  215. #endif
  216. relaySetup();
  217. buttonSetup();
  218. ledSetup();
  219. mqttSetup();
  220. #ifdef ITEAD_SONOFF_RFBRIDGE
  221. rfbSetup();
  222. #endif
  223. #if POWER_PROVIDER != POWER_PROVIDER_NONE
  224. powerSetup();
  225. #endif
  226. #if NTP_SUPPORT
  227. ntpSetup();
  228. #endif
  229. #if I2C_SUPPORT
  230. i2cSetup();
  231. #endif
  232. #if ALEXA_SUPPORT
  233. alexaSetup();
  234. #endif
  235. #if NOFUSS_SUPPORT
  236. nofussSetup();
  237. #endif
  238. #if INFLUXDB_SUPPORT
  239. influxDBSetup();
  240. #endif
  241. #if DS18B20_SUPPORT
  242. dsSetup();
  243. #endif
  244. #if ANALOG_SUPPORT
  245. analogSetup();
  246. #endif
  247. #if COUNTER_SUPPORT
  248. counterSetup();
  249. #endif
  250. #if DHT_SUPPORT
  251. dhtSetup();
  252. #endif
  253. #if RF_SUPPORT
  254. rfSetup();
  255. #endif
  256. #if IR_SUPPORT
  257. irSetup();
  258. #endif
  259. #if DOMOTICZ_SUPPORT
  260. domoticzSetup();
  261. #endif
  262. #if HOMEASSISTANT_SUPPORT
  263. haSetup();
  264. #endif
  265. // Prepare configuration for version 2.0
  266. hwUpwardsCompatibility();
  267. saveSettings();
  268. }
  269. void loop() {
  270. hardwareLoop();
  271. settingsLoop();
  272. wifiLoop();
  273. otaLoop();
  274. #if SYSTEM_CHECK_ENABLED
  275. systemCheckLoop();
  276. // Do not run the next services if system is flagged stable
  277. if (!systemCheck()) return;
  278. #endif
  279. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  280. lightLoop();
  281. #endif
  282. relayLoop();
  283. buttonLoop();
  284. ledLoop();
  285. mqttLoop();
  286. #ifdef ITEAD_SONOFF_RFBRIDGE
  287. rfbLoop();
  288. #endif
  289. #if POWER_PROVIDER != POWER_PROVIDER_NONE
  290. powerLoop();
  291. #endif
  292. #if NTP_SUPPORT
  293. ntpLoop();
  294. #endif
  295. #if ALEXA_SUPPORT
  296. alexaLoop();
  297. #endif
  298. #if NOFUSS_SUPPORT
  299. nofussLoop();
  300. #endif
  301. #if DS18B20_SUPPORT
  302. dsLoop();
  303. #endif
  304. #if ANALOG_SUPPORT
  305. analogLoop();
  306. #endif
  307. #if COUNTER_SUPPORT
  308. counterLoop();
  309. #endif
  310. #if DHT_SUPPORT
  311. dhtLoop();
  312. #endif
  313. #if RF_SUPPORT
  314. rfLoop();
  315. #endif
  316. #if IR_SUPPORT
  317. irLoop();
  318. #endif
  319. // Power saving delay
  320. delay(_loopDelay);
  321. }