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.

442 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-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. static bool on_connect = true;
  44. bool send = (last_uptime == 0);
  45. send = send || (millis() - last_uptime > HEARTBEAT_INTERVAL);
  46. if (mqttConnected()) {
  47. send = send || on_connect;
  48. } else {
  49. on_connect = true;
  50. }
  51. if (send) {
  52. last_uptime = millis();
  53. if (mqttConnected()) on_connect = false;
  54. heartbeat();
  55. }
  56. }
  57. // -----------------------------------------------------------------------------
  58. // BOOTING
  59. // -----------------------------------------------------------------------------
  60. unsigned int sectors(size_t size) {
  61. return (int) (size + SPI_FLASH_SEC_SIZE - 1) / SPI_FLASH_SEC_SIZE;
  62. }
  63. void welcome() {
  64. DEBUG_MSG_P(PSTR("\n\n"));
  65. DEBUG_MSG_P(PSTR("[INIT] %s %s\n"), (char *) APP_NAME, (char *) APP_VERSION);
  66. DEBUG_MSG_P(PSTR("[INIT] %s\n"), (char *) APP_AUTHOR);
  67. DEBUG_MSG_P(PSTR("[INIT] %s\n\n"), (char *) APP_WEBSITE);
  68. DEBUG_MSG_P(PSTR("[INIT] CPU chip ID: 0x%06X\n"), ESP.getChipId());
  69. DEBUG_MSG_P(PSTR("[INIT] CPU frequency: %d MHz\n"), ESP.getCpuFreqMHz());
  70. DEBUG_MSG_P(PSTR("[INIT] SDK version: %s\n"), ESP.getSdkVersion());
  71. DEBUG_MSG_P(PSTR("[INIT] Core version: %s\n"), getCoreVersion().c_str());
  72. DEBUG_MSG_P(PSTR("[INIT] Core revision: %s\n"), getCoreRevision().c_str());
  73. DEBUG_MSG_P(PSTR("\n"));
  74. // -------------------------------------------------------------------------
  75. FlashMode_t mode = ESP.getFlashChipMode();
  76. DEBUG_MSG_P(PSTR("[INIT] Flash chip ID: 0x%06X\n"), ESP.getFlashChipId());
  77. DEBUG_MSG_P(PSTR("[INIT] Flash speed: %u Hz\n"), ESP.getFlashChipSpeed());
  78. 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");
  79. DEBUG_MSG_P(PSTR("\n"));
  80. DEBUG_MSG_P(PSTR("[INIT] Flash sector size: %8u bytes\n"), SPI_FLASH_SEC_SIZE);
  81. DEBUG_MSG_P(PSTR("[INIT] Flash size (CHIP): %8u bytes\n"), ESP.getFlashChipRealSize());
  82. DEBUG_MSG_P(PSTR("[INIT] Flash size (SDK): %8u bytes / %4d sectors\n"), ESP.getFlashChipSize(), sectors(ESP.getFlashChipSize()));
  83. DEBUG_MSG_P(PSTR("[INIT] Firmware size: %8u bytes / %4d sectors\n"), ESP.getSketchSize(), sectors(ESP.getSketchSize()));
  84. DEBUG_MSG_P(PSTR("[INIT] OTA size: %8u bytes / %4d sectors\n"), ESP.getFreeSketchSpace(), sectors(ESP.getFreeSketchSpace()));
  85. #if SPIFFS_SUPPORT
  86. FSInfo fs_info;
  87. bool fs = SPIFFS.info(fs_info);
  88. if (fs) {
  89. DEBUG_MSG_P(PSTR("[INIT] SPIFFS size: %8u bytes / %4d sectors\n"), fs_info.totalBytes, sectors(fs_info.totalBytes));
  90. }
  91. #else
  92. DEBUG_MSG_P(PSTR("[INIT] SPIFFS size: %8u bytes / %4d sectors\n"), 0, 0);
  93. #endif
  94. DEBUG_MSG_P(PSTR("[INIT] EEPROM size: %8u bytes / %4d sectors\n"), settingsMaxSize(), sectors(settingsMaxSize()));
  95. DEBUG_MSG_P(PSTR("[INIT] Empty space: %8u bytes / 4 sectors\n"), 4 * SPI_FLASH_SEC_SIZE);
  96. DEBUG_MSG_P(PSTR("\n"));
  97. // -------------------------------------------------------------------------
  98. #if SPIFFS_SUPPORT
  99. if (fs) {
  100. DEBUG_MSG_P(PSTR("[INIT] SPIFFS total size: %8u bytes\n"), fs_info.totalBytes);
  101. DEBUG_MSG_P(PSTR("[INIT] used size: %8u bytes\n"), fs_info.usedBytes);
  102. DEBUG_MSG_P(PSTR("[INIT] block size: %8u bytes\n"), fs_info.blockSize);
  103. DEBUG_MSG_P(PSTR("[INIT] page size: %8u bytes\n"), fs_info.pageSize);
  104. DEBUG_MSG_P(PSTR("[INIT] max files: %8u\n"), fs_info.maxOpenFiles);
  105. DEBUG_MSG_P(PSTR("[INIT] max length: %8u\n"), fs_info.maxPathLength);
  106. } else {
  107. DEBUG_MSG_P(PSTR("[INIT] No SPIFFS partition\n"));
  108. }
  109. DEBUG_MSG_P(PSTR("\n"));
  110. #endif
  111. // -------------------------------------------------------------------------
  112. DEBUG_MSG_P(PSTR("[INIT] MANUFACTURER: %s\n"), MANUFACTURER);
  113. DEBUG_MSG_P(PSTR("[INIT] DEVICE: %s\n"), DEVICE);
  114. DEBUG_MSG_P(PSTR("[INIT] SUPPORT:"));
  115. #if ALEXA_SUPPORT
  116. DEBUG_MSG_P(PSTR(" ALEXA"));
  117. #endif
  118. #if DEBUG_SERIAL_SUPPORT
  119. DEBUG_MSG_P(PSTR(" DEBUG_SERIAL"));
  120. #endif
  121. #if DEBUG_TELNET_SUPPORT
  122. DEBUG_MSG_P(PSTR(" DEBUG_TELNET"));
  123. #endif
  124. #if DEBUG_UDP_SUPPORT
  125. DEBUG_MSG_P(PSTR(" DEBUG_UDP"));
  126. #endif
  127. #if DOMOTICZ_SUPPORT
  128. DEBUG_MSG_P(PSTR(" DOMOTICZ"));
  129. #endif
  130. #if HOMEASSISTANT_SUPPORT
  131. DEBUG_MSG_P(PSTR(" HOMEASSISTANT"));
  132. #endif
  133. #if I2C_SUPPORT
  134. DEBUG_MSG_P(PSTR(" I2C"));
  135. #endif
  136. #if INFLUXDB_SUPPORT
  137. DEBUG_MSG_P(PSTR(" INFLUXDB"));
  138. #endif
  139. #if LLMNR_SUPPORT
  140. DEBUG_MSG_P(PSTR(" LLMNR"));
  141. #endif
  142. #if MDNS_SUPPORT
  143. DEBUG_MSG_P(PSTR(" MDNS"));
  144. #endif
  145. #if NETBIOS_SUPPORT
  146. DEBUG_MSG_P(PSTR(" NETBIOS"));
  147. #endif
  148. #if NOFUSS_SUPPORT
  149. DEBUG_MSG_P(PSTR(" NOFUSS"));
  150. #endif
  151. #if NTP_SUPPORT
  152. DEBUG_MSG_P(PSTR(" NTP"));
  153. #endif
  154. #if RF_SUPPORT
  155. DEBUG_MSG_P(PSTR(" RF"));
  156. #endif
  157. #if SPIFFS_SUPPORT
  158. DEBUG_MSG_P(PSTR(" SPIFFS"));
  159. #endif
  160. #if SSDP_SUPPORT
  161. DEBUG_MSG_P(PSTR(" SSDP"));
  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[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. DEBUG_MSG_P(PSTR("\n\n"));
  222. // -------------------------------------------------------------------------
  223. unsigned char reason = resetReason();
  224. if (reason > 0) {
  225. char buffer[32];
  226. strcpy_P(buffer, custom_reset_string[reason-1]);
  227. DEBUG_MSG_P(PSTR("[INIT] Last reset reason: %s\n"), buffer);
  228. } else {
  229. DEBUG_MSG_P(PSTR("[INIT] Last reset reason: %s\n"), (char *) ESP.getResetReason().c_str());
  230. }
  231. DEBUG_MSG_P(PSTR("[INIT] Free heap: %u bytes\n"), getFreeHeap());
  232. #if ADC_VCC_ENABLED
  233. DEBUG_MSG_P(PSTR("[INIT] Power: %d mV\n"), ESP.getVcc());
  234. #endif
  235. DEBUG_MSG_P(PSTR("[INIT] Power saving delay value: %lu ms\n"), _loopDelay);
  236. DEBUG_MSG_P(PSTR("\n"));
  237. }
  238. void setup() {
  239. // Init EEPROM, Serial and SPIFFS
  240. hardwareSetup();
  241. // Question system stability
  242. #if SYSTEM_CHECK_ENABLED
  243. systemCheck(false);
  244. #endif
  245. // Init persistance and terminal features
  246. settingsSetup();
  247. if (getSetting("hostname").length() == 0) {
  248. setSetting("hostname", getIdentifier());
  249. }
  250. // Cache loop delay value to speed things (recommended max 250ms)
  251. _loopDelay = atol(getSetting("loopDelay", LOOP_DELAY_TIME).c_str());
  252. // Show welcome message and system configuration
  253. welcome();
  254. // Basic modules, will always run
  255. wifiSetup();
  256. otaSetup();
  257. #if TELNET_SUPPORT
  258. telnetSetup();
  259. #endif
  260. // Do not run the next services if system is flagged stable
  261. #if SYSTEM_CHECK_ENABLED
  262. if (!systemCheck()) return;
  263. #endif
  264. // Init webserver required before any module that uses API
  265. #if WEB_SUPPORT
  266. webSetup();
  267. wsSetup();
  268. apiSetup();
  269. #endif
  270. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  271. lightSetup();
  272. #endif
  273. relaySetup();
  274. buttonSetup();
  275. ledSetup();
  276. #if MQTT_SUPPORT
  277. mqttSetup();
  278. #endif
  279. #if MDNS_SUPPORT
  280. mdnsSetup();
  281. #endif
  282. #if LLMNR_SUPPORT
  283. llmnrSetup();
  284. #endif
  285. #if NETBIOS_SUPPORT
  286. netbiosSetup();
  287. #endif
  288. #if SSDP_SUPPORT
  289. ssdpSetup();
  290. #endif
  291. #if NTP_SUPPORT
  292. ntpSetup();
  293. #endif
  294. #if I2C_SUPPORT
  295. i2cSetup();
  296. #if I2C_CLEAR_BUS
  297. i2cClearBus();
  298. #endif
  299. i2cScan();
  300. #endif
  301. #ifdef ITEAD_SONOFF_RFBRIDGE
  302. rfbSetup();
  303. #endif
  304. #if POWER_PROVIDER != POWER_PROVIDER_NONE
  305. powerSetup();
  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 RF_SUPPORT
  317. rfSetup();
  318. #endif
  319. #if IR_SUPPORT
  320. irSetup();
  321. #endif
  322. #if DOMOTICZ_SUPPORT
  323. domoticzSetup();
  324. #endif
  325. #if HOMEASSISTANT_SUPPORT
  326. haSetup();
  327. #endif
  328. sensorSetup();
  329. // Prepare configuration for version 2.0
  330. migrate();
  331. saveSettings();
  332. }
  333. void loop() {
  334. hardwareLoop();
  335. settingsLoop();
  336. wifiLoop();
  337. otaLoop();
  338. #if SYSTEM_CHECK_ENABLED
  339. systemCheckLoop();
  340. // Do not run the next services if system is flagged stable
  341. if (!systemCheck()) return;
  342. #endif
  343. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  344. lightLoop();
  345. #endif
  346. relayLoop();
  347. buttonLoop();
  348. ledLoop();
  349. #if MQTT_SUPPORT
  350. mqttLoop();
  351. #endif
  352. #ifdef ITEAD_SONOFF_RFBRIDGE
  353. rfbLoop();
  354. #endif
  355. #if POWER_PROVIDER != POWER_PROVIDER_NONE
  356. powerLoop();
  357. #endif
  358. #if SSDP_SUPPORT
  359. ssdpLoop();
  360. #endif
  361. #if NTP_SUPPORT
  362. ntpLoop();
  363. #endif
  364. #if ALEXA_SUPPORT
  365. alexaLoop();
  366. #endif
  367. #if NOFUSS_SUPPORT
  368. nofussLoop();
  369. #endif
  370. #if RF_SUPPORT
  371. rfLoop();
  372. #endif
  373. #if IR_SUPPORT
  374. irLoop();
  375. #endif
  376. sensorLoop();
  377. // Power saving delay
  378. delay(_loopDelay);
  379. }