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.

302 lines
7.9 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
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 identifier[20];
  22. sprintf(identifier, "%s_%06X", DEVICE, ESP.getChipId());
  23. return String(identifier);
  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. DEBUG_MSG_P(PSTR("[MAIN] Time: %s\n"), (char *) ntpDateTime().c_str());
  33. if (!mqttConnected()) {
  34. DEBUG_MSG_P(PSTR("[MAIN] Uptime: %ld seconds\n"), uptime_seconds);
  35. DEBUG_MSG_P(PSTR("[MAIN] Free heap: %d bytes\n"), free_heap);
  36. #if ENABLE_ADC_VCC
  37. DEBUG_MSG_P(PSTR("[MAIN] Power: %d mV\n"), ESP.getVcc());
  38. #endif
  39. }
  40. #if (HEARTBEAT_REPORT_INTERVAL)
  41. mqttSend(MQTT_TOPIC_INTERVAL, HEARTBEAT_INTERVAL / 1000);
  42. #endif
  43. #if (HEARTBEAT_REPORT_APP)
  44. mqttSend(MQTT_TOPIC_APP, APP_NAME);
  45. #endif
  46. #if (HEARTBEAT_REPORT_VERSION)
  47. mqttSend(MQTT_TOPIC_VERSION, APP_VERSION);
  48. #endif
  49. #if (HEARTBEAT_REPORT_HOSTNAME)
  50. mqttSend(MQTT_TOPIC_HOSTNAME, getSetting("hostname").c_str());
  51. #endif
  52. #if (HEARTBEAT_REPORT_IP)
  53. mqttSend(MQTT_TOPIC_IP, getIP().c_str());
  54. #endif
  55. #if (HEARTBEAT_REPORT_MAC)
  56. mqttSend(MQTT_TOPIC_MAC, WiFi.macAddress().c_str());
  57. #endif
  58. #if (HEARTBEAT_REPORT_RSSI)
  59. mqttSend(MQTT_TOPIC_RSSI, String(WiFi.RSSI()).c_str());
  60. #endif
  61. #if (HEARTBEAT_REPORT_UPTIME)
  62. mqttSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str());
  63. #if ENABLE_INFLUXDB
  64. influxDBSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str());
  65. #endif
  66. #endif
  67. #if (HEARTBEAT_REPORT_FREEHEAP)
  68. mqttSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str());
  69. #if ENABLE_INFLUXDB
  70. influxDBSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str());
  71. #endif
  72. #endif
  73. #if (HEARTBEAT_REPORT_RELAY)
  74. relayMQTT();
  75. #endif
  76. #if (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE) & (HEARTBEAT_REPORT_LIGHT)
  77. lightMQTT();
  78. #endif
  79. #if (HEARTBEAT_REPORT_VCC)
  80. #if ENABLE_ADC_VCC
  81. mqttSend(MQTT_TOPIC_VCC, String(ESP.getVcc()).c_str());
  82. #endif
  83. #endif
  84. #if (HEARTBEAT_REPORT_STATUS)
  85. mqttSend(MQTT_TOPIC_STATUS, MQTT_STATUS_ONLINE, true);
  86. #endif
  87. }
  88. void customReset(unsigned char status) {
  89. EEPROM.write(EEPROM_CUSTOM_RESET, status);
  90. EEPROM.commit();
  91. }
  92. unsigned char customReset() {
  93. static unsigned char status = 255;
  94. if (status == 255) {
  95. status = EEPROM.read(EEPROM_CUSTOM_RESET);
  96. if (status > 0) customReset(0);
  97. if (status > CUSTOM_RESET_MAX) status = 0;
  98. }
  99. return status;
  100. }
  101. void hardwareSetup() {
  102. EEPROM.begin(4096);
  103. #ifdef DEBUG_PORT
  104. DEBUG_PORT.begin(SERIAL_BAUDRATE);
  105. if (customReset() == CUSTOM_RESET_HARDWARE) {
  106. DEBUG_PORT.setDebugOutput(true);
  107. }
  108. #elif defined(SERIAL_BAUDRATE)
  109. Serial.begin(SERIAL_BAUDRATE);
  110. #endif
  111. #if not EMBEDDED_WEB
  112. SPIFFS.begin();
  113. #endif
  114. }
  115. void hardwareLoop() {
  116. // Heartbeat
  117. static unsigned long last_uptime = 0;
  118. if ((millis() - last_uptime > HEARTBEAT_INTERVAL) || (last_uptime == 0)) {
  119. last_uptime = millis();
  120. heartbeat();
  121. }
  122. }
  123. // -----------------------------------------------------------------------------
  124. // BOOTING
  125. // -----------------------------------------------------------------------------
  126. void welcome() {
  127. DEBUG_MSG_P(PSTR("%s %s\n"), (char *) APP_NAME, (char *) APP_VERSION);
  128. DEBUG_MSG_P(PSTR("%s\n%s\n\n"), (char *) APP_AUTHOR, (char *) APP_WEBSITE);
  129. DEBUG_MSG_P(PSTR("ChipID: %06X\n"), ESP.getChipId());
  130. DEBUG_MSG_P(PSTR("CPU frequency: %d MHz\n"), ESP.getCpuFreqMHz());
  131. unsigned char custom_reset = customReset();
  132. if (custom_reset > 0) {
  133. char buffer[32];
  134. strcpy_P(buffer, custom_reset_string[custom_reset-1]);
  135. DEBUG_MSG_P(PSTR("Last reset reason: %s\n"), buffer);
  136. } else {
  137. DEBUG_MSG_P(PSTR("Last reset reason: %s\n"), (char *) ESP.getResetReason().c_str());
  138. }
  139. DEBUG_MSG_P(PSTR("Memory size (SDK): %d bytes\n"), ESP.getFlashChipSize());
  140. DEBUG_MSG_P(PSTR("Memory size (CHIP): %d bytes\n"), ESP.getFlashChipRealSize());
  141. DEBUG_MSG_P(PSTR("Free heap: %d bytes\n"), ESP.getFreeHeap());
  142. DEBUG_MSG_P(PSTR("Firmware size: %d bytes\n"), ESP.getSketchSize());
  143. DEBUG_MSG_P(PSTR("Free firmware space: %d bytes\n"), ESP.getFreeSketchSpace());
  144. #if not EMBEDDED_WEB
  145. FSInfo fs_info;
  146. if (SPIFFS.info(fs_info)) {
  147. DEBUG_MSG_P(PSTR("File system total size: %d bytes\n"), fs_info.totalBytes);
  148. DEBUG_MSG_P(PSTR(" used size : %d bytes\n"), fs_info.usedBytes);
  149. DEBUG_MSG_P(PSTR(" block size: %d bytes\n"), fs_info.blockSize);
  150. DEBUG_MSG_P(PSTR(" page size : %d bytes\n"), fs_info.pageSize);
  151. DEBUG_MSG_P(PSTR(" max files : %d\n"), fs_info.maxOpenFiles);
  152. DEBUG_MSG_P(PSTR(" max length: %d\n"), fs_info.maxPathLength);
  153. }
  154. #endif
  155. DEBUG_MSG_P(PSTR("\n\n"));
  156. }
  157. void setup() {
  158. hardwareSetup();
  159. welcome();
  160. settingsSetup();
  161. if (getSetting("hostname").length() == 0) {
  162. setSetting("hostname", getIdentifier());
  163. saveSettings();
  164. }
  165. webSetup();
  166. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  167. lightSetup();
  168. #endif
  169. relaySetup();
  170. buttonSetup();
  171. ledSetup();
  172. delay(500);
  173. wifiSetup();
  174. otaSetup();
  175. mqttSetup();
  176. ntpSetup();
  177. #ifdef SONOFF_RFBRIDGE
  178. rfbSetup();
  179. #endif
  180. #if ENABLE_I2C
  181. i2cSetup();
  182. #endif
  183. #if ENABLE_FAUXMO
  184. fauxmoSetup();
  185. #endif
  186. #if ENABLE_NOFUSS
  187. nofussSetup();
  188. #endif
  189. #if ENABLE_INFLUXDB
  190. influxDBSetup();
  191. #endif
  192. #if ENABLE_HLW8012
  193. hlw8012Setup();
  194. #endif
  195. #if ENABLE_DS18B20
  196. dsSetup();
  197. #endif
  198. #if ENABLE_ANALOG
  199. analogSetup();
  200. #endif
  201. #if ENABLE_DHT
  202. dhtSetup();
  203. #endif
  204. #if ENABLE_RF
  205. rfSetup();
  206. #endif
  207. #if ENABLE_EMON
  208. powerMonitorSetup();
  209. #endif
  210. #if ENABLE_DOMOTICZ
  211. domoticzSetup();
  212. #endif
  213. // Prepare configuration for version 2.0
  214. hwUpwardsCompatibility();
  215. }
  216. void loop() {
  217. hardwareLoop();
  218. buttonLoop();
  219. relayLoop();
  220. ledLoop();
  221. wifiLoop();
  222. otaLoop();
  223. mqttLoop();
  224. ntpLoop();
  225. #if ENABLE_FAUXMO
  226. fauxmoLoop();
  227. #endif
  228. #if !defined(SONOFF_DUAL) & !defined(SONOFF_RFBRIDGE)
  229. settingsLoop();
  230. #endif
  231. #ifdef SONOFF_RFBRIDGE
  232. rfbLoop();
  233. #endif
  234. #if ENABLE_NOFUSS
  235. nofussLoop();
  236. #endif
  237. #if ENABLE_HLW8012
  238. hlw8012Loop();
  239. #endif
  240. #if ENABLE_DS18B20
  241. dsLoop();
  242. #endif
  243. #if ENABLE_ANALOG
  244. analogLoop();
  245. #endif
  246. #if ENABLE_DHT
  247. dhtLoop();
  248. #endif
  249. #if ENABLE_RF
  250. rfLoop();
  251. #endif
  252. #if ENABLE_EMON
  253. powerMonitorLoop();
  254. #endif
  255. }