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.

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