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.

398 lines
11 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
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. // -----------------------------------------------------------------------------
  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_uptime = 0;
  43. if ((millis() - last_uptime > HEARTBEAT_INTERVAL) || (last_uptime == 0)) {
  44. last_uptime = 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: %d 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] 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 LLMNR_SUPPORT
  143. DEBUG_MSG_P(PSTR(" LLMNR"));
  144. #endif
  145. #if MDNS_SUPPORT
  146. DEBUG_MSG_P(PSTR(" MDNS"));
  147. #endif
  148. #if NETBIOS_SUPPORT
  149. DEBUG_MSG_P(PSTR(" NETBIOS"));
  150. #endif
  151. #if NOFUSS_SUPPORT
  152. DEBUG_MSG_P(PSTR(" NOFUSS"));
  153. #endif
  154. #if NTP_SUPPORT
  155. DEBUG_MSG_P(PSTR(" NTP"));
  156. #endif
  157. #if RF_SUPPORT
  158. DEBUG_MSG_P(PSTR(" RF"));
  159. #endif
  160. #if SPIFFS_SUPPORT
  161. DEBUG_MSG_P(PSTR(" SPIFFS"));
  162. #endif
  163. #if TELNET_SUPPORT
  164. DEBUG_MSG_P(PSTR(" TELNET"));
  165. #endif
  166. #if TERMINAL_SUPPORT
  167. DEBUG_MSG_P(PSTR(" TERMINAL"));
  168. #endif
  169. #if WEB_SUPPORT
  170. DEBUG_MSG_P(PSTR(" WEB"));
  171. #endif
  172. DEBUG_MSG_P(PSTR("\n\n"));
  173. // -------------------------------------------------------------------------
  174. unsigned char reason = resetReason();
  175. if (reason > 0) {
  176. char buffer[32];
  177. strcpy_P(buffer, custom_reset_string[reason-1]);
  178. DEBUG_MSG_P(PSTR("[INIT] Last reset reason: %s\n"), buffer);
  179. } else {
  180. DEBUG_MSG_P(PSTR("[INIT] Last reset reason: %s\n"), (char *) ESP.getResetReason().c_str());
  181. }
  182. DEBUG_MSG_P(PSTR("[INIT] Free heap: %u bytes\n"), ESP.getFreeHeap());
  183. #if ADC_VCC_ENABLED
  184. DEBUG_MSG_P(PSTR("[INIT] Power: %d mV\n"), ESP.getVcc());
  185. #endif
  186. DEBUG_MSG_P(PSTR("[INIT] Power saving delay value: %lu ms\n"), _loopDelay);
  187. DEBUG_MSG_P(PSTR("\n"));
  188. }
  189. void setup() {
  190. // Init EEPROM, Serial and SPIFFS
  191. hardwareSetup();
  192. // Question system stability
  193. #if SYSTEM_CHECK_ENABLED
  194. systemCheck(false);
  195. #endif
  196. // Init persistance and terminal features
  197. settingsSetup();
  198. if (getSetting("hostname").length() == 0) {
  199. setSetting("hostname", getIdentifier());
  200. }
  201. // Cache loop delay value to speed things (recommended max 250ms)
  202. _loopDelay = atol(getSetting("loopDelay", LOOP_DELAY_TIME).c_str());
  203. // Show welcome message and system configuration
  204. welcome();
  205. // Basic modules, will always run
  206. wifiSetup();
  207. otaSetup();
  208. #if TELNET_SUPPORT
  209. telnetSetup();
  210. #endif
  211. // Do not run the next services if system is flagged stable
  212. #if SYSTEM_CHECK_ENABLED
  213. if (!systemCheck()) return;
  214. #endif
  215. // Init webserver required before any module that uses API
  216. #if WEB_SUPPORT
  217. webSetup();
  218. wsSetup();
  219. apiSetup();
  220. #endif
  221. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  222. lightSetup();
  223. #endif
  224. relaySetup();
  225. buttonSetup();
  226. ledSetup();
  227. #if MQTT_SUPPORT
  228. mqttSetup();
  229. #endif
  230. #if MDNS_SUPPORT
  231. mdnsSetup();
  232. #endif
  233. #if LLMNR_SUPPORT
  234. llmnrSetup();
  235. #endif
  236. #if NETBIOS_SUPPORT
  237. netbiosSetup();
  238. #endif
  239. #if NTP_SUPPORT
  240. ntpSetup();
  241. #endif
  242. #if I2C_SUPPORT
  243. i2cSetup();
  244. #endif
  245. #ifdef ITEAD_SONOFF_RFBRIDGE
  246. rfbSetup();
  247. #endif
  248. #if POWER_PROVIDER != POWER_PROVIDER_NONE
  249. powerSetup();
  250. #endif
  251. #if ALEXA_SUPPORT
  252. alexaSetup();
  253. #endif
  254. #if NOFUSS_SUPPORT
  255. nofussSetup();
  256. #endif
  257. #if INFLUXDB_SUPPORT
  258. idbSetup();
  259. #endif
  260. #if DS18B20_SUPPORT
  261. dsSetup();
  262. #endif
  263. #if ANALOG_SUPPORT
  264. analogSetup();
  265. #endif
  266. #if COUNTER_SUPPORT
  267. counterSetup();
  268. #endif
  269. #if DHT_SUPPORT
  270. dhtSetup();
  271. #endif
  272. #if RF_SUPPORT
  273. rfSetup();
  274. #endif
  275. #if IR_SUPPORT
  276. irSetup();
  277. #endif
  278. #if DOMOTICZ_SUPPORT
  279. domoticzSetup();
  280. #endif
  281. #if HOMEASSISTANT_SUPPORT
  282. haSetup();
  283. #endif
  284. // Prepare configuration for version 2.0
  285. hwUpwardsCompatibility();
  286. saveSettings();
  287. }
  288. void loop() {
  289. hardwareLoop();
  290. settingsLoop();
  291. wifiLoop();
  292. otaLoop();
  293. #if SYSTEM_CHECK_ENABLED
  294. systemCheckLoop();
  295. // Do not run the next services if system is flagged stable
  296. if (!systemCheck()) return;
  297. #endif
  298. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  299. lightLoop();
  300. #endif
  301. relayLoop();
  302. buttonLoop();
  303. ledLoop();
  304. #if MQTT_SUPPORT
  305. mqttLoop();
  306. #endif
  307. #ifdef ITEAD_SONOFF_RFBRIDGE
  308. rfbLoop();
  309. #endif
  310. #if POWER_PROVIDER != POWER_PROVIDER_NONE
  311. powerLoop();
  312. #endif
  313. #if NTP_SUPPORT
  314. ntpLoop();
  315. #endif
  316. #if ALEXA_SUPPORT
  317. alexaLoop();
  318. #endif
  319. #if NOFUSS_SUPPORT
  320. nofussLoop();
  321. #endif
  322. #if DS18B20_SUPPORT
  323. dsLoop();
  324. #endif
  325. #if ANALOG_SUPPORT
  326. analogLoop();
  327. #endif
  328. #if COUNTER_SUPPORT
  329. counterLoop();
  330. #endif
  331. #if DHT_SUPPORT
  332. dhtLoop();
  333. #endif
  334. #if RF_SUPPORT
  335. rfLoop();
  336. #endif
  337. #if IR_SUPPORT
  338. irLoop();
  339. #endif
  340. // Power saving delay
  341. delay(_loopDelay);
  342. }