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.

244 lines
6.4 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 *) NTP.getTimeDateString().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 (MQTT_REPORT_INTERVAL)
  41. mqttSend(MQTT_TOPIC_INTERVAL, HEARTBEAT_INTERVAL / 1000);
  42. #endif
  43. #if (MQTT_REPORT_APP)
  44. mqttSend(MQTT_TOPIC_APP, APP_NAME);
  45. #endif
  46. #if (MQTT_REPORT_VERSION)
  47. mqttSend(MQTT_TOPIC_VERSION, APP_VERSION);
  48. #endif
  49. #if (MQTT_REPORT_HOSTNAME)
  50. mqttSend(MQTT_TOPIC_HOSTNAME, getSetting("hostname").c_str());
  51. #endif
  52. #if (MQTT_REPORT_IP)
  53. mqttSend(MQTT_TOPIC_IP, getIP().c_str());
  54. #endif
  55. #if (MQTT_REPORT_MAC)
  56. mqttSend(MQTT_TOPIC_MAC, WiFi.macAddress().c_str());
  57. #endif
  58. #if (MQTT_REPORT_RSSI)
  59. mqttSend(MQTT_TOPIC_RSSI, String(WiFi.RSSI()).c_str());
  60. #endif
  61. #if (MQTT_REPORT_UPTIME)
  62. mqttSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str());
  63. #endif
  64. #if (MQTT_REPORT_FREEHEAP)
  65. mqttSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str());
  66. #endif
  67. #if (MQTT_REPORT_RELAY)
  68. relayMQTT();
  69. #endif
  70. #if (MQTT_REPORT_VCC)
  71. #if ENABLE_ADC_VCC
  72. mqttSend(MQTT_TOPIC_VCC, String(ESP.getVcc()).c_str());
  73. #endif
  74. #endif
  75. #if (MQTT_REPORT_STATUS)
  76. mqttSend(MQTT_TOPIC_STATUS, MQTT_STATUS_ONLINE);
  77. #endif
  78. }
  79. void hardwareSetup() {
  80. EEPROM.begin(4096);
  81. Serial.begin(SERIAL_BAUDRATE);
  82. #if not EMBEDDED_WEB
  83. SPIFFS.begin();
  84. #endif
  85. }
  86. void hardwareLoop() {
  87. // Heartbeat
  88. static unsigned long last_uptime = 0;
  89. if ((millis() - last_uptime > HEARTBEAT_INTERVAL) || (last_uptime == 0)) {
  90. last_uptime = millis();
  91. heartbeat();
  92. }
  93. }
  94. // -----------------------------------------------------------------------------
  95. // BOOTING
  96. // -----------------------------------------------------------------------------
  97. void welcome() {
  98. DEBUG_MSG_P(PSTR("%s %s\n"), (char *) APP_NAME, (char *) APP_VERSION);
  99. DEBUG_MSG_P(PSTR("%s\n%s\n\n"), (char *) APP_AUTHOR, (char *) APP_WEBSITE);
  100. DEBUG_MSG_P(PSTR("ChipID: %06X\n"), ESP.getChipId());
  101. DEBUG_MSG_P(PSTR("CPU frequency: %d MHz\n"), ESP.getCpuFreqMHz());
  102. DEBUG_MSG_P(PSTR("Last reset reason: %s\n"), (char *) ESP.getResetReason().c_str());
  103. DEBUG_MSG_P(PSTR("Memory size: %d bytes\n"), ESP.getFlashChipSize());
  104. DEBUG_MSG_P(PSTR("Free heap: %d bytes\n"), ESP.getFreeHeap());
  105. DEBUG_MSG_P(PSTR("Firmware size: %d bytes\n"), ESP.getSketchSize());
  106. DEBUG_MSG_P(PSTR("Free firmware space: %d bytes\n"), ESP.getFreeSketchSpace());
  107. #if not EMBEDDED_WEB
  108. FSInfo fs_info;
  109. if (SPIFFS.info(fs_info)) {
  110. DEBUG_MSG_P(PSTR("File system total size: %d bytes\n"), fs_info.totalBytes);
  111. DEBUG_MSG_P(PSTR(" used size : %d bytes\n"), fs_info.usedBytes);
  112. DEBUG_MSG_P(PSTR(" block size: %d bytes\n"), fs_info.blockSize);
  113. DEBUG_MSG_P(PSTR(" page size : %d bytes\n"), fs_info.pageSize);
  114. DEBUG_MSG_P(PSTR(" max files : %d\n"), fs_info.maxOpenFiles);
  115. DEBUG_MSG_P(PSTR(" max length: %d\n"), fs_info.maxPathLength);
  116. }
  117. #endif
  118. DEBUG_MSG_P(PSTR("\n\n"));
  119. }
  120. void setup() {
  121. hardwareSetup();
  122. welcome();
  123. settingsSetup();
  124. if (getSetting("hostname").length() == 0) {
  125. setSetting("hostname", getIdentifier());
  126. saveSettings();
  127. }
  128. webSetup();
  129. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  130. lightSetup();
  131. #endif
  132. relaySetup();
  133. buttonSetup();
  134. ledSetup();
  135. delay(500);
  136. wifiSetup();
  137. otaSetup();
  138. mqttSetup();
  139. ntpSetup();
  140. #if ENABLE_I2C
  141. i2cSetup();
  142. #endif
  143. #if ENABLE_FAUXMO
  144. fauxmoSetup();
  145. #endif
  146. #if ENABLE_NOFUSS
  147. nofussSetup();
  148. #endif
  149. #if ENABLE_POW
  150. powSetup();
  151. #endif
  152. #if ENABLE_DS18B20
  153. dsSetup();
  154. #endif
  155. #if ENABLE_ANALOG
  156. analogSetup();
  157. #endif
  158. #if ENABLE_DHT
  159. dhtSetup();
  160. #endif
  161. #if ENABLE_RF
  162. rfSetup();
  163. #endif
  164. #if ENABLE_EMON
  165. powerMonitorSetup();
  166. #endif
  167. // Prepare configuration for version 2.0
  168. hwUpwardsCompatibility();
  169. }
  170. void loop() {
  171. hardwareLoop();
  172. buttonLoop();
  173. relayLoop();
  174. ledLoop();
  175. wifiLoop();
  176. otaLoop();
  177. mqttLoop();
  178. ntpLoop();
  179. #if ENABLE_FAUXMO
  180. fauxmoLoop();
  181. #endif
  182. #ifndef SONOFF_DUAL
  183. settingsLoop();
  184. #endif
  185. #if ENABLE_NOFUSS
  186. nofussLoop();
  187. #endif
  188. #if ENABLE_POW
  189. powLoop();
  190. #endif
  191. #if ENABLE_DS18B20
  192. dsLoop();
  193. #endif
  194. #if ENABLE_ANALOG
  195. analogLoop();
  196. #endif
  197. #if ENABLE_DHT
  198. dhtLoop();
  199. #endif
  200. #if ENABLE_RF
  201. rfLoop();
  202. #endif
  203. #if ENABLE_EMON
  204. powerMonitorLoop();
  205. #endif
  206. }