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.

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