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.

458 lines
12 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
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
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. unsigned int sectors(size_t size) {
  52. return (int) (size + SPI_FLASH_SEC_SIZE - 1) / SPI_FLASH_SEC_SIZE;
  53. }
  54. void welcome() {
  55. DEBUG_MSG_P(PSTR("\n\n"));
  56. DEBUG_MSG_P(PSTR("[INIT] %s %s\n"), (char *) APP_NAME, (char *) APP_VERSION);
  57. DEBUG_MSG_P(PSTR("[INIT] %s\n"), (char *) APP_AUTHOR);
  58. DEBUG_MSG_P(PSTR("[INIT] %s\n\n"), (char *) APP_WEBSITE);
  59. DEBUG_MSG_P(PSTR("[INIT] CPU chip ID: 0x%06X\n"), ESP.getChipId());
  60. DEBUG_MSG_P(PSTR("[INIT] CPU frequency: %u MHz\n"), ESP.getCpuFreqMHz());
  61. DEBUG_MSG_P(PSTR("[INIT] SDK version: %s\n"), ESP.getSdkVersion());
  62. DEBUG_MSG_P(PSTR("[INIT] Core version: %s\n"), getCoreVersion().c_str());
  63. DEBUG_MSG_P(PSTR("[INIT] Core revision: %s\n"), getCoreRevision().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] BOARD: %s\n"), getBoardName().c_str());
  104. DEBUG_MSG_P(PSTR("[INIT] SUPPORT:"));
  105. #if ALEXA_SUPPORT
  106. DEBUG_MSG_P(PSTR(" ALEXA"));
  107. #endif
  108. #if DEBUG_SERIAL_SUPPORT
  109. DEBUG_MSG_P(PSTR(" DEBUG_SERIAL"));
  110. #endif
  111. #if DEBUG_TELNET_SUPPORT
  112. DEBUG_MSG_P(PSTR(" DEBUG_TELNET"));
  113. #endif
  114. #if DEBUG_UDP_SUPPORT
  115. DEBUG_MSG_P(PSTR(" DEBUG_UDP"));
  116. #endif
  117. #if DOMOTICZ_SUPPORT
  118. DEBUG_MSG_P(PSTR(" DOMOTICZ"));
  119. #endif
  120. #if HOMEASSISTANT_SUPPORT
  121. DEBUG_MSG_P(PSTR(" HOMEASSISTANT"));
  122. #endif
  123. #if I2C_SUPPORT
  124. DEBUG_MSG_P(PSTR(" I2C"));
  125. #endif
  126. #if INFLUXDB_SUPPORT
  127. DEBUG_MSG_P(PSTR(" INFLUXDB"));
  128. #endif
  129. #if LLMNR_SUPPORT
  130. DEBUG_MSG_P(PSTR(" LLMNR"));
  131. #endif
  132. #if MDNS_SERVER_SUPPORT
  133. DEBUG_MSG_P(PSTR(" MDNS"));
  134. #endif
  135. #if NETBIOS_SUPPORT
  136. DEBUG_MSG_P(PSTR(" NETBIOS"));
  137. #endif
  138. #if NOFUSS_SUPPORT
  139. DEBUG_MSG_P(PSTR(" NOFUSS"));
  140. #endif
  141. #if NTP_SUPPORT
  142. DEBUG_MSG_P(PSTR(" NTP"));
  143. #endif
  144. #if RF_SUPPORT
  145. DEBUG_MSG_P(PSTR(" RF"));
  146. #endif
  147. #if SCHEDULER_SUPPORT
  148. DEBUG_MSG_P(PSTR(" SCHEDULER"));
  149. #endif
  150. #if SENSOR_SUPPORT
  151. DEBUG_MSG_P(PSTR(" SENSOR"));
  152. #endif
  153. #if SPIFFS_SUPPORT
  154. DEBUG_MSG_P(PSTR(" SPIFFS"));
  155. #endif
  156. #if SSDP_SUPPORT
  157. DEBUG_MSG_P(PSTR(" SSDP"));
  158. #endif
  159. #if TELNET_SUPPORT
  160. DEBUG_MSG_P(PSTR(" TELNET"));
  161. #endif
  162. #if TERMINAL_SUPPORT
  163. DEBUG_MSG_P(PSTR(" TERMINAL"));
  164. #endif
  165. #if THINGSPEAK_SUPPORT
  166. DEBUG_MSG_P(PSTR(" THINGSPEAK"));
  167. #endif
  168. #if WEB_SUPPORT
  169. DEBUG_MSG_P(PSTR(" WEB"));
  170. #endif
  171. #if SENSOR_SUPPORT
  172. DEBUG_MSG_P(PSTR("\n[INIT] SENSORS:"));
  173. #if ANALOG_SUPPORT
  174. DEBUG_MSG_P(PSTR(" ANALOG"));
  175. #endif
  176. #if BMX280_SUPPORT
  177. DEBUG_MSG_P(PSTR(" BMX280"));
  178. #endif
  179. #if DALLAS_SUPPORT
  180. DEBUG_MSG_P(PSTR(" DALLAS"));
  181. #endif
  182. #if DHT_SUPPORT
  183. DEBUG_MSG_P(PSTR(" DHTXX"));
  184. #endif
  185. #if DIGITAL_SUPPORT
  186. DEBUG_MSG_P(PSTR(" DIGITAL"));
  187. #endif
  188. #if ECH1560_SUPPORT
  189. DEBUG_MSG_P(PSTR(" ECH1560"));
  190. #endif
  191. #if EMON_ADC121_SUPPORT
  192. DEBUG_MSG_P(PSTR(" EMON_ADC121"));
  193. #endif
  194. #if EMON_ADS1X15_SUPPORT
  195. DEBUG_MSG_P(PSTR(" EMON_ADX1X15"));
  196. #endif
  197. #if EMON_ANALOG_SUPPORT
  198. DEBUG_MSG_P(PSTR(" EMON_ANALOG"));
  199. #endif
  200. #if EVENTS_SUPPORT
  201. DEBUG_MSG_P(PSTR(" EVENTS"));
  202. #endif
  203. #if HLW8012_SUPPORT
  204. DEBUG_MSG_P(PSTR(" HLW8012"));
  205. #endif
  206. #if MHZ19_SUPPORT
  207. DEBUG_MSG_P(PSTR(" MHZ19"));
  208. #endif
  209. #if PMSX003_SUPPORT
  210. DEBUG_MSG_P(PSTR(" PMSX003"));
  211. #endif
  212. #if SHT3X_I2C_SUPPORT
  213. DEBUG_MSG_P(PSTR(" SHT3X_I2C"));
  214. #endif
  215. #if SI7021_SUPPORT
  216. DEBUG_MSG_P(PSTR(" SI7021"));
  217. #endif
  218. #if V9261F_SUPPORT
  219. DEBUG_MSG_P(PSTR(" V9261F"));
  220. #endif
  221. #endif // SENSOR_SUPPORT
  222. DEBUG_MSG_P(PSTR("\n\n"));
  223. // -------------------------------------------------------------------------
  224. unsigned char reason = resetReason();
  225. if (reason > 0) {
  226. char buffer[32];
  227. strcpy_P(buffer, custom_reset_string[reason-1]);
  228. DEBUG_MSG_P(PSTR("[INIT] Last reset reason: %s\n"), buffer);
  229. } else {
  230. DEBUG_MSG_P(PSTR("[INIT] Last reset reason: %s\n"), (char *) ESP.getResetReason().c_str());
  231. }
  232. DEBUG_MSG_P(PSTR("[INIT] Free heap: %u bytes\n"), getFreeHeap());
  233. #if ADC_VCC_ENABLED
  234. DEBUG_MSG_P(PSTR("[INIT] Power: %u mV\n"), ESP.getVcc());
  235. #endif
  236. DEBUG_MSG_P(PSTR("[INIT] Power saving delay value: %lu ms\n"), _loopDelay);
  237. if (!systemCheck()) DEBUG_MSG_P(PSTR("\n[INIT] Device is in SAFE MODE\n"));
  238. DEBUG_MSG_P(PSTR("\n"));
  239. }
  240. void setup() {
  241. // Init EEPROM, Serial and SPIFFS
  242. hardwareSetup();
  243. // Question system stability
  244. #if SYSTEM_CHECK_ENABLED
  245. systemCheck(false);
  246. #endif
  247. // Init persistance and terminal features
  248. settingsSetup();
  249. if (getSetting("hostname").length() == 0) {
  250. setSetting("hostname", getIdentifier());
  251. }
  252. setBoardName();
  253. // Cache loop delay value to speed things (recommended max 250ms)
  254. _loopDelay = atol(getSetting("loopDelay", LOOP_DELAY_TIME).c_str());
  255. // Show welcome message and system configuration
  256. welcome();
  257. // Basic modules, will always run
  258. wifiSetup();
  259. otaSetup();
  260. #if TELNET_SUPPORT
  261. telnetSetup();
  262. #endif
  263. // Do not run the next services if system is flagged stable
  264. #if SYSTEM_CHECK_ENABLED
  265. if (!systemCheck()) return;
  266. #endif
  267. // Init webserver required before any module that uses API
  268. #if WEB_SUPPORT
  269. webSetup();
  270. wsSetup();
  271. apiSetup();
  272. #endif
  273. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  274. lightSetup();
  275. #endif
  276. relaySetup();
  277. buttonSetup();
  278. ledSetup();
  279. #if MQTT_SUPPORT
  280. mqttSetup();
  281. #endif
  282. #if MDNS_SERVER_SUPPORT
  283. mdnsServerSetup();
  284. #endif
  285. #if LLMNR_SUPPORT
  286. llmnrSetup();
  287. #endif
  288. #if NETBIOS_SUPPORT
  289. netbiosSetup();
  290. #endif
  291. #if SSDP_SUPPORT
  292. ssdpSetup();
  293. #endif
  294. #if NTP_SUPPORT
  295. ntpSetup();
  296. #endif
  297. #if I2C_SUPPORT
  298. i2cSetup();
  299. #if I2C_CLEAR_BUS
  300. i2cClearBus();
  301. #endif
  302. i2cScan();
  303. #endif
  304. #ifdef ITEAD_SONOFF_RFBRIDGE
  305. rfbSetup();
  306. #endif
  307. #if ALEXA_SUPPORT
  308. alexaSetup();
  309. #endif
  310. #if NOFUSS_SUPPORT
  311. nofussSetup();
  312. #endif
  313. #if INFLUXDB_SUPPORT
  314. idbSetup();
  315. #endif
  316. #if THINGSPEAK_SUPPORT
  317. tspkSetup();
  318. #endif
  319. #if RF_SUPPORT
  320. rfSetup();
  321. #endif
  322. #if IR_SUPPORT
  323. irSetup();
  324. #endif
  325. #if DOMOTICZ_SUPPORT
  326. domoticzSetup();
  327. #endif
  328. #if HOMEASSISTANT_SUPPORT
  329. haSetup();
  330. #endif
  331. #if SENSOR_SUPPORT
  332. sensorSetup();
  333. #endif
  334. #if SCHEDULER_SUPPORT
  335. schSetup();
  336. #endif
  337. // Prepare configuration for version 2.0
  338. migrate();
  339. saveSettings();
  340. }
  341. void loop() {
  342. hardwareLoop();
  343. settingsLoop();
  344. wifiLoop();
  345. otaLoop();
  346. #if SYSTEM_CHECK_ENABLED
  347. systemCheckLoop();
  348. // Do not run the next services if system is flagged stable
  349. if (!systemCheck()) return;
  350. #endif
  351. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  352. lightLoop();
  353. #endif
  354. relayLoop();
  355. buttonLoop();
  356. ledLoop();
  357. #if MQTT_SUPPORT
  358. mqttLoop();
  359. #endif
  360. #ifdef ITEAD_SONOFF_RFBRIDGE
  361. rfbLoop();
  362. #endif
  363. #if SSDP_SUPPORT
  364. ssdpLoop();
  365. #endif
  366. #if NTP_SUPPORT
  367. ntpLoop();
  368. #endif
  369. #if ALEXA_SUPPORT
  370. alexaLoop();
  371. #endif
  372. #if NOFUSS_SUPPORT
  373. nofussLoop();
  374. #endif
  375. #if RF_SUPPORT
  376. rfLoop();
  377. #endif
  378. #if IR_SUPPORT
  379. irLoop();
  380. #endif
  381. #if SENSOR_SUPPORT
  382. sensorLoop();
  383. #endif
  384. #if THINGSPEAK_SUPPORT
  385. tspkLoop();
  386. #endif
  387. #if SCHEDULER_SUPPORT
  388. schLoop();
  389. #endif
  390. #if MDNS_CLIENT_SUPPORT
  391. mdnsClientLoop();
  392. #endif
  393. // Power saving delay
  394. delay(_loopDelay);
  395. }