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.

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