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.

360 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. // System check
  42. static bool checked = false;
  43. if (!checked && (millis() > CRASH_SAFE_TIME)) {
  44. // Check system as stable
  45. systemCheck(true);
  46. checked = true;
  47. }
  48. // Heartbeat
  49. static unsigned long last_uptime = 0;
  50. if ((millis() - last_uptime > HEARTBEAT_INTERVAL) || (last_uptime == 0)) {
  51. last_uptime = millis();
  52. heartbeat();
  53. }
  54. }
  55. // -----------------------------------------------------------------------------
  56. // BOOTING
  57. // -----------------------------------------------------------------------------
  58. unsigned int sectors(size_t size) {
  59. return (int) (size + SPI_FLASH_SEC_SIZE - 1) / SPI_FLASH_SEC_SIZE;
  60. }
  61. void welcome() {
  62. DEBUG_MSG_P(PSTR("\n\n"));
  63. DEBUG_MSG_P(PSTR("[INIT] %s %s\n"), (char *) APP_NAME, (char *) APP_VERSION);
  64. DEBUG_MSG_P(PSTR("[INIT] %s\n"), (char *) APP_AUTHOR);
  65. DEBUG_MSG_P(PSTR("[INIT] %s\n\n"), (char *) APP_WEBSITE);
  66. DEBUG_MSG_P(PSTR("[INIT] CPU chip ID: 0x%06X\n"), ESP.getChipId());
  67. DEBUG_MSG_P(PSTR("[INIT] CPU frequency: %d MHz\n"), ESP.getCpuFreqMHz());
  68. DEBUG_MSG_P(PSTR("[INIT] SDK version: %s\n"), ESP.getSdkVersion());
  69. DEBUG_MSG_P(PSTR("[INIT] Core version: %s\n"), ESP.getCoreVersion().c_str());
  70. DEBUG_MSG_P(PSTR("\n"));
  71. // -------------------------------------------------------------------------
  72. FlashMode_t mode = ESP.getFlashChipMode();
  73. DEBUG_MSG_P(PSTR("[INIT] Flash chip ID: 0x%06X\n"), ESP.getFlashChipId());
  74. DEBUG_MSG_P(PSTR("[INIT] Flash speed: %u Hz\n"), ESP.getFlashChipSpeed());
  75. 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");
  76. DEBUG_MSG_P(PSTR("\n"));
  77. DEBUG_MSG_P(PSTR("[INIT] Flash sector size: %8u bytes\n"), SPI_FLASH_SEC_SIZE);
  78. DEBUG_MSG_P(PSTR("[INIT] Flash size (CHIP): %8u bytes\n"), ESP.getFlashChipRealSize());
  79. DEBUG_MSG_P(PSTR("[INIT] Flash size (SDK): %8u bytes / %4d sectors\n"), ESP.getFlashChipSize(), sectors(ESP.getFlashChipSize()));
  80. DEBUG_MSG_P(PSTR("[INIT] Firmware size: %8u bytes / %4d sectors\n"), ESP.getSketchSize(), sectors(ESP.getSketchSize()));
  81. DEBUG_MSG_P(PSTR("[INIT] OTA size: %8u bytes / %4d sectors\n"), ESP.getFreeSketchSpace(), sectors(ESP.getFreeSketchSpace()));
  82. #if SPIFFS_SUPPORT
  83. FSInfo fs_info;
  84. bool fs = SPIFFS.info(fs_info);
  85. if (fs) {
  86. DEBUG_MSG_P(PSTR("[INIT] SPIFFS size: %8u bytes / %4d sectors\n"), fs_info.totalBytes, sectors(fs_info.totalBytes));
  87. }
  88. #else
  89. DEBUG_MSG_P(PSTR("[INIT] SPIFFS size: %8u bytes / %4d sectors\n"), 0, 0);
  90. #endif
  91. DEBUG_MSG_P(PSTR("[INIT] EEPROM size: %8u bytes / %4d sectors\n"), settingsMaxSize(), sectors(settingsMaxSize()));
  92. DEBUG_MSG_P(PSTR("[INIT] Empty space: %8u bytes / 4 sectors\n"), 4 * SPI_FLASH_SEC_SIZE);
  93. DEBUG_MSG_P(PSTR("\n"));
  94. // -------------------------------------------------------------------------
  95. #if SPIFFS_SUPPORT
  96. if (fs) {
  97. DEBUG_MSG_P(PSTR("[INIT] SPIFFS total size: %8u bytes\n"), fs_info.totalBytes);
  98. DEBUG_MSG_P(PSTR("[INIT] used size: %8u bytes\n"), fs_info.usedBytes);
  99. DEBUG_MSG_P(PSTR("[INIT] block size: %8u bytes\n"), fs_info.blockSize);
  100. DEBUG_MSG_P(PSTR("[INIT] page size: %8u bytes\n"), fs_info.pageSize);
  101. DEBUG_MSG_P(PSTR("[INIT] max files: %8u\n"), fs_info.maxOpenFiles);
  102. DEBUG_MSG_P(PSTR("[INIT] max length: %8u\n"), fs_info.maxPathLength);
  103. } else {
  104. DEBUG_MSG_P(PSTR("[INIT] No SPIFFS partition\n"));
  105. }
  106. DEBUG_MSG_P(PSTR("\n"));
  107. #endif
  108. // -------------------------------------------------------------------------
  109. DEBUG_MSG_P(PSTR("[INIT] MANUFACTURER: %s\n"), MANUFACTURER);
  110. DEBUG_MSG_P(PSTR("[INIT] DEVICE: %s\n"), DEVICE);
  111. DEBUG_MSG_P(PSTR("[INIT] SUPPORT:"));
  112. #if ALEXA_SUPPORT
  113. DEBUG_MSG_P(PSTR(" ALEXA"));
  114. #endif
  115. #if ANALOG_SUPPORT
  116. DEBUG_MSG_P(PSTR(" ANALOG"));
  117. #endif
  118. #if COUNTER_SUPPORT
  119. DEBUG_MSG_P(PSTR(" COUNTER"));
  120. #endif
  121. #if DEBUG_SERIAL_SUPPORT
  122. DEBUG_MSG_P(PSTR(" DEBUG_SERIAL"));
  123. #endif
  124. #if DEBUG_TELNET_SUPPORT
  125. DEBUG_MSG_P(PSTR(" DEBUG_TELNET"));
  126. #endif
  127. #if DEBUG_UDP_SUPPORT
  128. DEBUG_MSG_P(PSTR(" DEBUG_UDP"));
  129. #endif
  130. #if DHT_SUPPORT
  131. DEBUG_MSG_P(PSTR(" DHT"));
  132. #endif
  133. #if DOMOTICZ_SUPPORT
  134. DEBUG_MSG_P(PSTR(" DOMOTICZ"));
  135. #endif
  136. #if DS18B20_SUPPORT
  137. DEBUG_MSG_P(PSTR(" DS18B20"));
  138. #endif
  139. #if HOMEASSISTANT_SUPPORT
  140. DEBUG_MSG_P(PSTR(" HOMEASSISTANT"));
  141. #endif
  142. #if I2C_SUPPORT
  143. DEBUG_MSG_P(PSTR(" I2C"));
  144. #endif
  145. #if INFLUXDB_SUPPORT
  146. DEBUG_MSG_P(PSTR(" INFLUXDB"));
  147. #endif
  148. #if MDNS_SUPPORT
  149. DEBUG_MSG_P(PSTR(" MDNS"));
  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 custom_reset = customReset();
  175. if (custom_reset > 0) {
  176. char buffer[32];
  177. strcpy_P(buffer, custom_reset_string[custom_reset-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("\n"));
  187. }
  188. void setup() {
  189. // Init EEPROM, Serial and SPIFFS
  190. hardwareSetup();
  191. // Question system stability
  192. systemCheck(false);
  193. // Show welcome message and system configuration
  194. welcome();
  195. // Init persistance and terminal features
  196. settingsSetup();
  197. if (getSetting("hostname").length() == 0) {
  198. setSetting("hostname", getIdentifier());
  199. }
  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 (!systemCheck()) return;
  207. #if WEB_SUPPORT
  208. webSetup();
  209. #endif
  210. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  211. lightSetup();
  212. #endif
  213. relaySetup();
  214. buttonSetup();
  215. ledSetup();
  216. mqttSetup();
  217. #ifdef ITEAD_SONOFF_RFBRIDGE
  218. rfbSetup();
  219. #endif
  220. #if POWER_PROVIDER != POWER_PROVIDER_NONE
  221. powerSetup();
  222. #endif
  223. #if NTP_SUPPORT
  224. ntpSetup();
  225. #endif
  226. #if I2C_SUPPORT
  227. i2cSetup();
  228. #endif
  229. #if ALEXA_SUPPORT
  230. alexaSetup();
  231. #endif
  232. #if NOFUSS_SUPPORT
  233. nofussSetup();
  234. #endif
  235. #if INFLUXDB_SUPPORT
  236. influxDBSetup();
  237. #endif
  238. #if DS18B20_SUPPORT
  239. dsSetup();
  240. #endif
  241. #if ANALOG_SUPPORT
  242. analogSetup();
  243. #endif
  244. #if COUNTER_SUPPORT
  245. counterSetup();
  246. #endif
  247. #if DHT_SUPPORT
  248. dhtSetup();
  249. #endif
  250. #if RF_SUPPORT
  251. rfSetup();
  252. #endif
  253. #if DOMOTICZ_SUPPORT
  254. domoticzSetup();
  255. #endif
  256. // Prepare configuration for version 2.0
  257. hwUpwardsCompatibility();
  258. saveSettings();
  259. }
  260. void loop() {
  261. hardwareLoop();
  262. settingsLoop();
  263. wifiLoop();
  264. otaLoop();
  265. // Do not run the next services if system is flagged stable
  266. if (!systemCheck()) return;
  267. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  268. lightLoop();
  269. #endif
  270. buttonLoop();
  271. relayLoop();
  272. ledLoop();
  273. mqttLoop();
  274. #ifdef ITEAD_SONOFF_RFBRIDGE
  275. rfbLoop();
  276. #endif
  277. #if POWER_PROVIDER != POWER_PROVIDER_NONE
  278. powerLoop();
  279. #endif
  280. #if NTP_SUPPORT
  281. ntpLoop();
  282. #endif
  283. #if ALEXA_SUPPORT
  284. alexaLoop();
  285. #endif
  286. #if NOFUSS_SUPPORT
  287. nofussLoop();
  288. #endif
  289. #if DS18B20_SUPPORT
  290. dsLoop();
  291. #endif
  292. #if ANALOG_SUPPORT
  293. analogLoop();
  294. #endif
  295. #if COUNTER_SUPPORT
  296. counterLoop();
  297. #endif
  298. #if DHT_SUPPORT
  299. dhtLoop();
  300. #endif
  301. #if RF_SUPPORT
  302. rfLoop();
  303. #endif
  304. }