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.

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