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.

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