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.

195 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. delay(500);
  98. wifiSetup();
  99. otaSetup();
  100. mqttSetup();
  101. ntpSetup();
  102. #if ENABLE_I2C
  103. i2cSetup();
  104. #endif
  105. #if ENABLE_FAUXMO
  106. fauxmoSetup();
  107. #endif
  108. #if ENABLE_NOFUSS
  109. nofussSetup();
  110. #endif
  111. #if ENABLE_POW
  112. powSetup();
  113. #endif
  114. #if ENABLE_DS18B20
  115. dsSetup();
  116. #endif
  117. #if ENABLE_DHT
  118. dhtSetup();
  119. #endif
  120. #if ENABLE_RF
  121. rfSetup();
  122. #endif
  123. #if ENABLE_EMON
  124. powerMonitorSetup();
  125. #endif
  126. }
  127. void loop() {
  128. hardwareLoop();
  129. buttonLoop();
  130. ledLoop();
  131. wifiLoop();
  132. otaLoop();
  133. mqttLoop();
  134. ntpLoop();
  135. #if ENABLE_FAUXMO
  136. fauxmoLoop();
  137. #endif
  138. #ifndef SONOFF_DUAL
  139. settingsLoop();
  140. #endif
  141. #if ENABLE_NOFUSS
  142. nofussLoop();
  143. #endif
  144. #if ENABLE_POW
  145. powLoop();
  146. #endif
  147. #if ENABLE_DS18B20
  148. dsLoop();
  149. #endif
  150. #if ENABLE_DHT
  151. dhtLoop();
  152. #endif
  153. #if ENABLE_RF
  154. rfLoop();
  155. #endif
  156. #if ENABLE_EMON
  157. powerMonitorLoop();
  158. #endif
  159. }