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.

340 lines
9.5 KiB

8 years ago
7 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. String getIdentifier() {
  21. char buffer[20];
  22. snprintf_P(buffer, sizeof(buffer), PSTR("%s_%06X"), DEVICE, ESP.getChipId());
  23. return String(buffer);
  24. }
  25. void heartbeat() {
  26. static unsigned long last_uptime = 0;
  27. static unsigned char uptime_overflows = 0;
  28. if (millis() < last_uptime) ++uptime_overflows;
  29. last_uptime = millis();
  30. unsigned long uptime_seconds = uptime_overflows * (UPTIME_OVERFLOW / 1000) + (last_uptime / 1000);
  31. unsigned int free_heap = ESP.getFreeHeap();
  32. #if NTP_SUPPORT
  33. DEBUG_MSG_P(PSTR("[MAIN] Time: %s\n"), (char *) ntpDateTime().c_str());
  34. #endif
  35. if (!mqttConnected()) {
  36. DEBUG_MSG_P(PSTR("[MAIN] Uptime: %ld seconds\n"), uptime_seconds);
  37. DEBUG_MSG_P(PSTR("[MAIN] Free heap: %d bytes\n"), free_heap);
  38. #if ADC_VCC_ENABLED
  39. DEBUG_MSG_P(PSTR("[MAIN] Power: %d mV\n"), ESP.getVcc());
  40. #endif
  41. }
  42. #if (HEARTBEAT_REPORT_INTERVAL)
  43. mqttSend(MQTT_TOPIC_INTERVAL, HEARTBEAT_INTERVAL / 1000);
  44. #endif
  45. #if (HEARTBEAT_REPORT_APP)
  46. mqttSend(MQTT_TOPIC_APP, APP_NAME);
  47. #endif
  48. #if (HEARTBEAT_REPORT_VERSION)
  49. mqttSend(MQTT_TOPIC_VERSION, APP_VERSION);
  50. #endif
  51. #if (HEARTBEAT_REPORT_HOSTNAME)
  52. mqttSend(MQTT_TOPIC_HOSTNAME, getSetting("hostname").c_str());
  53. #endif
  54. #if (HEARTBEAT_REPORT_IP)
  55. mqttSend(MQTT_TOPIC_IP, getIP().c_str());
  56. #endif
  57. #if (HEARTBEAT_REPORT_MAC)
  58. mqttSend(MQTT_TOPIC_MAC, WiFi.macAddress().c_str());
  59. #endif
  60. #if (HEARTBEAT_REPORT_RSSI)
  61. mqttSend(MQTT_TOPIC_RSSI, String(WiFi.RSSI()).c_str());
  62. #endif
  63. #if (HEARTBEAT_REPORT_UPTIME)
  64. mqttSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str());
  65. #if INFLUXDB_SUPPORT
  66. influxDBSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str());
  67. #endif
  68. #endif
  69. #if (HEARTBEAT_REPORT_FREEHEAP)
  70. mqttSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str());
  71. #if INFLUXDB_SUPPORT
  72. influxDBSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str());
  73. #endif
  74. #endif
  75. #if (HEARTBEAT_REPORT_RELAY)
  76. relayMQTT();
  77. #endif
  78. #if (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE) & (HEARTBEAT_REPORT_LIGHT)
  79. lightMQTT();
  80. #endif
  81. #if (HEARTBEAT_REPORT_VCC)
  82. #if ADC_VCC_ENABLED
  83. mqttSend(MQTT_TOPIC_VCC, String(ESP.getVcc()).c_str());
  84. #endif
  85. #endif
  86. #if (HEARTBEAT_REPORT_STATUS)
  87. mqttSend(MQTT_TOPIC_STATUS, MQTT_STATUS_ONLINE, true);
  88. #endif
  89. }
  90. void customReset(unsigned char status) {
  91. EEPROM.write(EEPROM_CUSTOM_RESET, status);
  92. EEPROM.commit();
  93. }
  94. unsigned char customReset() {
  95. static unsigned char status = 255;
  96. if (status == 255) {
  97. status = EEPROM.read(EEPROM_CUSTOM_RESET);
  98. if (status > 0) customReset(0);
  99. if (status > CUSTOM_RESET_MAX) status = 0;
  100. }
  101. return status;
  102. }
  103. void hardwareSetup() {
  104. EEPROM.begin(EEPROM_SIZE);
  105. #if DEBUG_SERIAL_SUPPORT
  106. DEBUG_PORT.begin(SERIAL_BAUDRATE);
  107. #if DEBUG_ESP_WIFI
  108. DEBUG_PORT.setDebugOutput(true);
  109. #endif
  110. #elif defined(SERIAL_BAUDRATE)
  111. Serial.begin(SERIAL_BAUDRATE);
  112. #endif
  113. #if SPIFFS_SUPPORT
  114. SPIFFS.begin();
  115. #endif
  116. }
  117. void hardwareLoop() {
  118. // Heartbeat
  119. static unsigned long last_uptime = 0;
  120. if ((millis() - last_uptime > HEARTBEAT_INTERVAL) || (last_uptime == 0)) {
  121. last_uptime = millis();
  122. heartbeat();
  123. }
  124. }
  125. // -----------------------------------------------------------------------------
  126. // BOOTING
  127. // -----------------------------------------------------------------------------
  128. unsigned int sectors(size_t size) {
  129. return (int) (size + SPI_FLASH_SEC_SIZE - 1) / SPI_FLASH_SEC_SIZE;
  130. }
  131. void welcome() {
  132. DEBUG_MSG_P(PSTR("\n\n"));
  133. DEBUG_MSG_P(PSTR("%s %s\n"), (char *) APP_NAME, (char *) APP_VERSION);
  134. DEBUG_MSG_P(PSTR("%s\n%s\n\n"), (char *) APP_AUTHOR, (char *) APP_WEBSITE);
  135. DEBUG_MSG_P(PSTR("CPU chip ID: 0x%06X\n"), ESP.getChipId());
  136. DEBUG_MSG_P(PSTR("CPU frequency: %d MHz\n"), ESP.getCpuFreqMHz());
  137. DEBUG_MSG_P(PSTR("SDK version: %s\n"), ESP.getSdkVersion());
  138. DEBUG_MSG_P(PSTR("Core version: %s\n"), ESP.getCoreVersion().c_str());
  139. DEBUG_MSG_P(PSTR("\n"));
  140. FlashMode_t mode = ESP.getFlashChipMode();
  141. DEBUG_MSG_P(PSTR("Flash chip ID: 0x%06X\n"), ESP.getFlashChipId());
  142. DEBUG_MSG_P(PSTR("Flash speed: %u Hz\n"), ESP.getFlashChipSpeed());
  143. 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");
  144. DEBUG_MSG_P(PSTR("\n"));
  145. DEBUG_MSG_P(PSTR("Flash sector size: %8u bytes\n"), SPI_FLASH_SEC_SIZE);
  146. DEBUG_MSG_P(PSTR("Flash size (CHIP): %8u bytes\n"), ESP.getFlashChipRealSize());
  147. DEBUG_MSG_P(PSTR("Flash size (SDK): %8u bytes / %4d sectors\n"), ESP.getFlashChipSize(), sectors(ESP.getFlashChipSize()));
  148. DEBUG_MSG_P(PSTR("Firmware size: %8u bytes / %4d sectors\n"), ESP.getSketchSize(), sectors(ESP.getSketchSize()));
  149. DEBUG_MSG_P(PSTR("OTA size: %8u bytes / %4d sectors\n"), ESP.getFreeSketchSpace(), sectors(ESP.getFreeSketchSpace()));
  150. #if SPIFFS_SUPPORT
  151. FSInfo fs_info;
  152. bool fs = SPIFFS.info(fs_info);
  153. if (fs) {
  154. DEBUG_MSG_P(PSTR("SPIFFS size: %8u bytes / %4d sectors\n"), fs_info.totalBytes, sectors(fs_info.totalBytes));
  155. }
  156. #else
  157. DEBUG_MSG_P(PSTR("SPIFFS size: %8u bytes / %4d sectors\n"), 0, 0);
  158. #endif
  159. DEBUG_MSG_P(PSTR("EEPROM size: %8u bytes / %4d sectors\n"), settingsMaxSize(), sectors(settingsMaxSize()));
  160. DEBUG_MSG_P(PSTR("Empty space: %8u bytes / 4 sectors\n"), 4 * SPI_FLASH_SEC_SIZE);
  161. #if SPIFFS_SUPPORT
  162. if (fs) {
  163. DEBUG_MSG_P(PSTR("\n"));
  164. DEBUG_MSG_P(PSTR("SPIFFS total size: %8u bytes\n"), fs_info.totalBytes);
  165. DEBUG_MSG_P(PSTR(" used size: %8u bytes\n"), fs_info.usedBytes);
  166. DEBUG_MSG_P(PSTR(" block size: %8u bytes\n"), fs_info.blockSize);
  167. DEBUG_MSG_P(PSTR(" page size: %8u bytes\n"), fs_info.pageSize);
  168. DEBUG_MSG_P(PSTR(" max files: %8u\n"), fs_info.maxOpenFiles);
  169. DEBUG_MSG_P(PSTR(" max length: %8u\n"), fs_info.maxPathLength);
  170. }
  171. #endif
  172. DEBUG_MSG_P(PSTR("\n"));
  173. unsigned char custom_reset = customReset();
  174. if (custom_reset > 0) {
  175. char buffer[32];
  176. strcpy_P(buffer, custom_reset_string[custom_reset-1]);
  177. DEBUG_MSG_P(PSTR("Last reset reason: %s\n"), buffer);
  178. } else {
  179. DEBUG_MSG_P(PSTR("Last reset reason: %s\n"), (char *) ESP.getResetReason().c_str());
  180. }
  181. DEBUG_MSG_P(PSTR("Free heap: %u bytes\n"), ESP.getFreeHeap());
  182. DEBUG_MSG_P(PSTR("\n\n"));
  183. }
  184. void setup() {
  185. hardwareSetup();
  186. welcome();
  187. settingsSetup();
  188. if (getSetting("hostname").length() == 0) {
  189. setSetting("hostname", getIdentifier());
  190. saveSettings();
  191. }
  192. #if WEB_SUPPORT
  193. webSetup();
  194. #endif
  195. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  196. lightSetup();
  197. #endif
  198. relaySetup();
  199. buttonSetup();
  200. ledSetup();
  201. delay(500);
  202. wifiSetup();
  203. otaSetup();
  204. mqttSetup();
  205. #ifdef ITEAD_SONOFF_RFBRIDGE
  206. rfbSetup();
  207. #endif
  208. #if NTP_SUPPORT
  209. ntpSetup();
  210. #endif
  211. #if I2C_SUPPORT
  212. i2cSetup();
  213. #endif
  214. #if ALEXA_SUPPORT
  215. alexaSetup();
  216. #endif
  217. #if NOFUSS_SUPPORT
  218. nofussSetup();
  219. #endif
  220. #if INFLUXDB_SUPPORT
  221. influxDBSetup();
  222. #endif
  223. #if HLW8012_SUPPORT
  224. hlw8012Setup();
  225. #endif
  226. #if DS18B20_SUPPORT
  227. dsSetup();
  228. #endif
  229. #if ANALOG_SUPPORT
  230. analogSetup();
  231. #endif
  232. #if DHT_SUPPORT
  233. dhtSetup();
  234. #endif
  235. #if RF_SUPPORT
  236. rfSetup();
  237. #endif
  238. #if EMON_SUPPORT
  239. powerMonitorSetup();
  240. #endif
  241. #if DOMOTICZ_SUPPORT
  242. domoticzSetup();
  243. #endif
  244. // Prepare configuration for version 2.0
  245. hwUpwardsCompatibility();
  246. }
  247. void loop() {
  248. hardwareLoop();
  249. buttonLoop();
  250. relayLoop();
  251. ledLoop();
  252. wifiLoop();
  253. otaLoop();
  254. mqttLoop();
  255. #ifdef ITEAD_SONOFF_RFBRIDGE
  256. rfbLoop();
  257. #endif
  258. #if NTP_SUPPORT
  259. ntpLoop();
  260. #endif
  261. #if TERMINAL_SUPPORT
  262. settingsLoop();
  263. #endif
  264. #if ALEXA_SUPPORT
  265. alexaLoop();
  266. #endif
  267. #if NOFUSS_SUPPORT
  268. nofussLoop();
  269. #endif
  270. #if HLW8012_SUPPORT
  271. hlw8012Loop();
  272. #endif
  273. #if DS18B20_SUPPORT
  274. dsLoop();
  275. #endif
  276. #if ANALOG_SUPPORT
  277. analogLoop();
  278. #endif
  279. #if DHT_SUPPORT
  280. dhtLoop();
  281. #endif
  282. #if RF_SUPPORT
  283. rfLoop();
  284. #endif
  285. #if EMON_SUPPORT
  286. powerMonitorLoop();
  287. #endif
  288. }