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.

246 lines
6.7 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 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. 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("%s %s\n"), (char *) APP_NAME, (char *) APP_VERSION);
  51. DEBUG_MSG_P(PSTR("%s\n%s\n\n"), (char *) APP_AUTHOR, (char *) APP_WEBSITE);
  52. DEBUG_MSG_P(PSTR("CPU chip ID: 0x%06X\n"), ESP.getChipId());
  53. DEBUG_MSG_P(PSTR("CPU frequency: %d MHz\n"), ESP.getCpuFreqMHz());
  54. DEBUG_MSG_P(PSTR("SDK version: %s\n"), ESP.getSdkVersion());
  55. DEBUG_MSG_P(PSTR("Core version: %s\n"), ESP.getCoreVersion().c_str());
  56. DEBUG_MSG_P(PSTR("\n"));
  57. FlashMode_t mode = ESP.getFlashChipMode();
  58. DEBUG_MSG_P(PSTR("Flash chip ID: 0x%06X\n"), ESP.getFlashChipId());
  59. DEBUG_MSG_P(PSTR("Flash speed: %u Hz\n"), ESP.getFlashChipSpeed());
  60. DEBUG_MSG_P(PSTR("Flash mode: %s\n"), mode == FM_QIO ? "QIO" : mode == FM_QOUT ? "QOUT" : mode == FM_DIO ? "DIO" : mode == FM_DOUT ? "DOUT" : "UNKNOWN");
  61. DEBUG_MSG_P(PSTR("\n"));
  62. DEBUG_MSG_P(PSTR("Flash sector size: %8u bytes\n"), SPI_FLASH_SEC_SIZE);
  63. DEBUG_MSG_P(PSTR("Flash size (CHIP): %8u bytes\n"), ESP.getFlashChipRealSize());
  64. DEBUG_MSG_P(PSTR("Flash size (SDK): %8u bytes / %4d sectors\n"), ESP.getFlashChipSize(), sectors(ESP.getFlashChipSize()));
  65. DEBUG_MSG_P(PSTR("Firmware size: %8u bytes / %4d sectors\n"), ESP.getSketchSize(), sectors(ESP.getSketchSize()));
  66. DEBUG_MSG_P(PSTR("OTA size: %8u bytes / %4d sectors\n"), ESP.getFreeSketchSpace(), sectors(ESP.getFreeSketchSpace()));
  67. #if SPIFFS_SUPPORT
  68. FSInfo fs_info;
  69. bool fs = SPIFFS.info(fs_info);
  70. if (fs) {
  71. DEBUG_MSG_P(PSTR("SPIFFS size: %8u bytes / %4d sectors\n"), fs_info.totalBytes, sectors(fs_info.totalBytes));
  72. }
  73. #else
  74. DEBUG_MSG_P(PSTR("SPIFFS size: %8u bytes / %4d sectors\n"), 0, 0);
  75. #endif
  76. DEBUG_MSG_P(PSTR("EEPROM size: %8u bytes / %4d sectors\n"), settingsMaxSize(), sectors(settingsMaxSize()));
  77. DEBUG_MSG_P(PSTR("Empty space: %8u bytes / 4 sectors\n"), 4 * SPI_FLASH_SEC_SIZE);
  78. #if SPIFFS_SUPPORT
  79. if (fs) {
  80. DEBUG_MSG_P(PSTR("\n"));
  81. DEBUG_MSG_P(PSTR("SPIFFS total size: %8u bytes\n"), fs_info.totalBytes);
  82. DEBUG_MSG_P(PSTR(" used size: %8u bytes\n"), fs_info.usedBytes);
  83. DEBUG_MSG_P(PSTR(" block size: %8u bytes\n"), fs_info.blockSize);
  84. DEBUG_MSG_P(PSTR(" page size: %8u bytes\n"), fs_info.pageSize);
  85. DEBUG_MSG_P(PSTR(" max files: %8u\n"), fs_info.maxOpenFiles);
  86. DEBUG_MSG_P(PSTR(" max length: %8u\n"), fs_info.maxPathLength);
  87. }
  88. #endif
  89. DEBUG_MSG_P(PSTR("\n"));
  90. unsigned char custom_reset = customReset();
  91. if (custom_reset > 0) {
  92. char buffer[32];
  93. strcpy_P(buffer, custom_reset_string[custom_reset-1]);
  94. DEBUG_MSG_P(PSTR("Last reset reason: %s\n"), buffer);
  95. } else {
  96. DEBUG_MSG_P(PSTR("Last reset reason: %s\n"), (char *) ESP.getResetReason().c_str());
  97. }
  98. DEBUG_MSG_P(PSTR("Free heap: %u bytes\n"), ESP.getFreeHeap());
  99. DEBUG_MSG_P(PSTR("\n\n"));
  100. }
  101. void setup() {
  102. hardwareSetup();
  103. welcome();
  104. settingsSetup();
  105. if (getSetting("hostname").length() == 0) {
  106. setSetting("hostname", getIdentifier());
  107. saveSettings();
  108. }
  109. #if WEB_SUPPORT
  110. webSetup();
  111. #endif
  112. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  113. lightSetup();
  114. #endif
  115. relaySetup();
  116. buttonSetup();
  117. ledSetup();
  118. delay(500);
  119. wifiSetup();
  120. otaSetup();
  121. mqttSetup();
  122. #ifdef ITEAD_SONOFF_RFBRIDGE
  123. rfbSetup();
  124. #endif
  125. #if NTP_SUPPORT
  126. ntpSetup();
  127. #endif
  128. #if I2C_SUPPORT
  129. i2cSetup();
  130. #endif
  131. #if ALEXA_SUPPORT
  132. alexaSetup();
  133. #endif
  134. #if NOFUSS_SUPPORT
  135. nofussSetup();
  136. #endif
  137. #if INFLUXDB_SUPPORT
  138. influxDBSetup();
  139. #endif
  140. #if HLW8012_SUPPORT
  141. hlw8012Setup();
  142. #endif
  143. #if DS18B20_SUPPORT
  144. dsSetup();
  145. #endif
  146. #if ANALOG_SUPPORT
  147. analogSetup();
  148. #endif
  149. #if DHT_SUPPORT
  150. dhtSetup();
  151. #endif
  152. #if RF_SUPPORT
  153. rfSetup();
  154. #endif
  155. #if EMON_SUPPORT
  156. powerMonitorSetup();
  157. #endif
  158. #if DOMOTICZ_SUPPORT
  159. domoticzSetup();
  160. #endif
  161. // Prepare configuration for version 2.0
  162. hwUpwardsCompatibility();
  163. }
  164. void loop() {
  165. hardwareLoop();
  166. buttonLoop();
  167. relayLoop();
  168. ledLoop();
  169. wifiLoop();
  170. otaLoop();
  171. mqttLoop();
  172. #ifdef ITEAD_SONOFF_RFBRIDGE
  173. rfbLoop();
  174. #endif
  175. #if NTP_SUPPORT
  176. ntpLoop();
  177. #endif
  178. #if TERMINAL_SUPPORT
  179. settingsLoop();
  180. #endif
  181. #if ALEXA_SUPPORT
  182. alexaLoop();
  183. #endif
  184. #if NOFUSS_SUPPORT
  185. nofussLoop();
  186. #endif
  187. #if HLW8012_SUPPORT
  188. hlw8012Loop();
  189. #endif
  190. #if DS18B20_SUPPORT
  191. dsLoop();
  192. #endif
  193. #if ANALOG_SUPPORT
  194. analogLoop();
  195. #endif
  196. #if DHT_SUPPORT
  197. dhtLoop();
  198. #endif
  199. #if RF_SUPPORT
  200. rfLoop();
  201. #endif
  202. #if EMON_SUPPORT
  203. powerMonitorLoop();
  204. #endif
  205. }