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.

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