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.

322 lines
8.8 KiB

8 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
8 years ago
8 years ago
8 years ago
7 years ago
8 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. }
  34. void hardwareLoop() {
  35. // Heartbeat
  36. static unsigned long last_uptime = 0;
  37. if ((millis() - last_uptime > HEARTBEAT_INTERVAL) || (last_uptime == 0)) {
  38. last_uptime = millis();
  39. heartbeat();
  40. }
  41. }
  42. // -----------------------------------------------------------------------------
  43. // BOOTING
  44. // -----------------------------------------------------------------------------
  45. unsigned int sectors(size_t size) {
  46. return (int) (size + SPI_FLASH_SEC_SIZE - 1) / SPI_FLASH_SEC_SIZE;
  47. }
  48. void welcome() {
  49. DEBUG_MSG_P(PSTR("\n\n"));
  50. DEBUG_MSG_P(PSTR("[INIT] %s %s\n"), (char *) APP_NAME, (char *) APP_VERSION);
  51. DEBUG_MSG_P(PSTR("[INIT] %s\n"), (char *) APP_AUTHOR);
  52. DEBUG_MSG_P(PSTR("[INIT] %s\n\n"), (char *) APP_WEBSITE);
  53. DEBUG_MSG_P(PSTR("[INIT] CPU chip ID: 0x%06X\n"), ESP.getChipId());
  54. DEBUG_MSG_P(PSTR("[INIT] CPU frequency: %d MHz\n"), ESP.getCpuFreqMHz());
  55. DEBUG_MSG_P(PSTR("[INIT] SDK version: %s\n"), ESP.getSdkVersion());
  56. DEBUG_MSG_P(PSTR("[INIT] Core version: %s\n"), ESP.getCoreVersion().c_str());
  57. DEBUG_MSG_P(PSTR("\n"));
  58. // -------------------------------------------------------------------------
  59. FlashMode_t mode = ESP.getFlashChipMode();
  60. DEBUG_MSG_P(PSTR("[INIT] Flash chip ID: 0x%06X\n"), ESP.getFlashChipId());
  61. DEBUG_MSG_P(PSTR("[INIT] Flash speed: %u Hz\n"), ESP.getFlashChipSpeed());
  62. 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");
  63. DEBUG_MSG_P(PSTR("\n"));
  64. DEBUG_MSG_P(PSTR("[INIT] Flash sector size: %8u bytes\n"), SPI_FLASH_SEC_SIZE);
  65. DEBUG_MSG_P(PSTR("[INIT] Flash size (CHIP): %8u bytes\n"), ESP.getFlashChipRealSize());
  66. DEBUG_MSG_P(PSTR("[INIT] Flash size (SDK): %8u bytes / %4d sectors\n"), ESP.getFlashChipSize(), sectors(ESP.getFlashChipSize()));
  67. DEBUG_MSG_P(PSTR("[INIT] Firmware size: %8u bytes / %4d sectors\n"), ESP.getSketchSize(), sectors(ESP.getSketchSize()));
  68. DEBUG_MSG_P(PSTR("[INIT] OTA size: %8u bytes / %4d sectors\n"), ESP.getFreeSketchSpace(), sectors(ESP.getFreeSketchSpace()));
  69. #if SPIFFS_SUPPORT
  70. FSInfo fs_info;
  71. bool fs = SPIFFS.info(fs_info);
  72. if (fs) {
  73. DEBUG_MSG_P(PSTR("[INIT] SPIFFS size: %8u bytes / %4d sectors\n"), fs_info.totalBytes, sectors(fs_info.totalBytes));
  74. }
  75. #else
  76. DEBUG_MSG_P(PSTR("[INIT] SPIFFS size: %8u bytes / %4d sectors\n"), 0, 0);
  77. #endif
  78. DEBUG_MSG_P(PSTR("[INIT] EEPROM size: %8u bytes / %4d sectors\n"), settingsMaxSize(), sectors(settingsMaxSize()));
  79. DEBUG_MSG_P(PSTR("[INIT] Empty space: %8u bytes / 4 sectors\n"), 4 * SPI_FLASH_SEC_SIZE);
  80. DEBUG_MSG_P(PSTR("\n"));
  81. // -------------------------------------------------------------------------
  82. #if SPIFFS_SUPPORT
  83. if (fs) {
  84. DEBUG_MSG_P(PSTR("[INIT] SPIFFS total size: %8u bytes\n"), fs_info.totalBytes);
  85. DEBUG_MSG_P(PSTR("[INIT] used size: %8u bytes\n"), fs_info.usedBytes);
  86. DEBUG_MSG_P(PSTR("[INIT] block size: %8u bytes\n"), fs_info.blockSize);
  87. DEBUG_MSG_P(PSTR("[INIT] page size: %8u bytes\n"), fs_info.pageSize);
  88. DEBUG_MSG_P(PSTR("[INIT] max files: %8u\n"), fs_info.maxOpenFiles);
  89. DEBUG_MSG_P(PSTR("[INIT] max length: %8u\n"), fs_info.maxPathLength);
  90. DEBUG_MSG_P(PSTR("\n"));
  91. }
  92. #endif
  93. // -------------------------------------------------------------------------
  94. DEBUG_MSG_P(PSTR("[INIT] MANUFACTURER: %s\n"), MANUFACTURER);
  95. DEBUG_MSG_P(PSTR("[INIT] DEVICE: %s\n"), DEVICE);
  96. DEBUG_MSG_P(PSTR("[INIT] SUPPORT:"));
  97. #if ALEXA_SUPPORT
  98. DEBUG_MSG_P(PSTR(" ALEXA"));
  99. #endif
  100. #if ANALOG_SUPPORT
  101. DEBUG_MSG_P(PSTR(" ANALOG"));
  102. #endif
  103. #if COUNTER_SUPPORT
  104. DEBUG_MSG_P(PSTR(" COUNTER"));
  105. #endif
  106. #if DEBUG_SERIAL_SUPPORT
  107. DEBUG_MSG_P(PSTR(" DEBUG_SERIAL"));
  108. #endif
  109. #if DEBUG_UDP_SUPPORT
  110. DEBUG_MSG_P(PSTR(" DEBUG_UDP"));
  111. #endif
  112. #if DHT_SUPPORT
  113. DEBUG_MSG_P(PSTR(" DHT"));
  114. #endif
  115. #if DOMOTICZ_SUPPORT
  116. DEBUG_MSG_P(PSTR(" DOMOTICZ"));
  117. #endif
  118. #if DS18B20_SUPPORT
  119. DEBUG_MSG_P(PSTR(" DS18B20"));
  120. #endif
  121. #if EMON_SUPPORT
  122. DEBUG_MSG_P(PSTR(" EMON"));
  123. #endif
  124. #if HOMEASSISTANT_SUPPORT
  125. DEBUG_MSG_P(PSTR(" HOMEASSISTANT"));
  126. #endif
  127. #if I2C_SUPPORT
  128. DEBUG_MSG_P(PSTR(" I2C"));
  129. #endif
  130. #if INFLUXDB_SUPPORT
  131. DEBUG_MSG_P(PSTR(" INFLUXDB"));
  132. #endif
  133. #if MDNS_SUPPORT
  134. DEBUG_MSG_P(PSTR(" MDNS"));
  135. #endif
  136. #if NOFUSS_SUPPORT
  137. DEBUG_MSG_P(PSTR(" NOFUSS"));
  138. #endif
  139. #if NTP_SUPPORT
  140. DEBUG_MSG_P(PSTR(" NTP"));
  141. #endif
  142. #if RF_SUPPORT
  143. DEBUG_MSG_P(PSTR(" RF"));
  144. #endif
  145. #if SPIFFS_SUPPORT
  146. DEBUG_MSG_P(PSTR(" SPIFFS"));
  147. #endif
  148. #if TERMINAL_SUPPORT
  149. DEBUG_MSG_P(PSTR(" TERMINAL"));
  150. #endif
  151. #if WEB_SUPPORT
  152. DEBUG_MSG_P(PSTR(" WEB"));
  153. #endif
  154. DEBUG_MSG_P(PSTR("\n\n"));
  155. // -------------------------------------------------------------------------
  156. unsigned char custom_reset = customReset();
  157. if (custom_reset > 0) {
  158. char buffer[32];
  159. strcpy_P(buffer, custom_reset_string[custom_reset-1]);
  160. DEBUG_MSG_P(PSTR("[INIT] Last reset reason: %s\n"), buffer);
  161. } else {
  162. DEBUG_MSG_P(PSTR("[INIT] Last reset reason: %s\n"), (char *) ESP.getResetReason().c_str());
  163. }
  164. DEBUG_MSG_P(PSTR("[INIT] Free heap: %u bytes\n"), ESP.getFreeHeap());
  165. DEBUG_MSG_P(PSTR("\n"));
  166. }
  167. void setup() {
  168. hardwareSetup();
  169. welcome();
  170. settingsSetup();
  171. if (getSetting("hostname").length() == 0) {
  172. setSetting("hostname", getIdentifier());
  173. saveSettings();
  174. }
  175. #if WEB_SUPPORT
  176. webSetup();
  177. #endif
  178. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  179. lightSetup();
  180. #endif
  181. relaySetup();
  182. buttonSetup();
  183. ledSetup();
  184. delay(500);
  185. wifiSetup();
  186. otaSetup();
  187. mqttSetup();
  188. #ifdef ITEAD_SONOFF_RFBRIDGE
  189. rfbSetup();
  190. #endif
  191. #if NTP_SUPPORT
  192. ntpSetup();
  193. #endif
  194. #if I2C_SUPPORT
  195. i2cSetup();
  196. #endif
  197. #if ALEXA_SUPPORT
  198. alexaSetup();
  199. #endif
  200. #if NOFUSS_SUPPORT
  201. nofussSetup();
  202. #endif
  203. #if INFLUXDB_SUPPORT
  204. influxDBSetup();
  205. #endif
  206. #if HLW8012_SUPPORT
  207. hlw8012Setup();
  208. #endif
  209. #if DS18B20_SUPPORT
  210. dsSetup();
  211. #endif
  212. #if ANALOG_SUPPORT
  213. analogSetup();
  214. #endif
  215. #if COUNTER_SUPPORT
  216. counterSetup();
  217. #endif
  218. #if DHT_SUPPORT
  219. dhtSetup();
  220. #endif
  221. #if RF_SUPPORT
  222. rfSetup();
  223. #endif
  224. #if EMON_SUPPORT
  225. powerMonitorSetup();
  226. #endif
  227. #if DOMOTICZ_SUPPORT
  228. domoticzSetup();
  229. #endif
  230. // Prepare configuration for version 2.0
  231. hwUpwardsCompatibility();
  232. }
  233. void loop() {
  234. hardwareLoop();
  235. buttonLoop();
  236. relayLoop();
  237. ledLoop();
  238. wifiLoop();
  239. otaLoop();
  240. mqttLoop();
  241. settingsLoop();
  242. #ifdef ITEAD_SONOFF_RFBRIDGE
  243. rfbLoop();
  244. #endif
  245. #if NTP_SUPPORT
  246. ntpLoop();
  247. #endif
  248. #if ALEXA_SUPPORT
  249. alexaLoop();
  250. #endif
  251. #if NOFUSS_SUPPORT
  252. nofussLoop();
  253. #endif
  254. #if HLW8012_SUPPORT
  255. hlw8012Loop();
  256. #endif
  257. #if DS18B20_SUPPORT
  258. dsLoop();
  259. #endif
  260. #if ANALOG_SUPPORT
  261. analogLoop();
  262. #endif
  263. #if COUNTER_SUPPORT
  264. counterLoop();
  265. #endif
  266. #if DHT_SUPPORT
  267. dhtLoop();
  268. #endif
  269. #if RF_SUPPORT
  270. rfLoop();
  271. #endif
  272. #if EMON_SUPPORT
  273. powerMonitorLoop();
  274. #endif
  275. }