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.

201 lines
5.2 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_VCC_REPORT
  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_VCC_REPORT)
  59. #if ENABLE_VCC_REPORT
  60. mqttSend(MQTT_VCC_TOPIC, String(ESP.getVcc()).c_str());
  61. #endif
  62. #endif
  63. }
  64. }
  65. // -----------------------------------------------------------------------------
  66. // BOOTING
  67. // -----------------------------------------------------------------------------
  68. void welcome() {
  69. DEBUG_MSG("%s %s\n", (char *) APP_NAME, (char *) APP_VERSION);
  70. DEBUG_MSG("%s\n%s\n\n", (char *) APP_AUTHOR, (char *) APP_WEBSITE);
  71. DEBUG_MSG("ChipID: %06X\n", ESP.getChipId());
  72. DEBUG_MSG("CPU frequency: %d MHz\n", ESP.getCpuFreqMHz());
  73. DEBUG_MSG("Last reset reason: %s\n", (char *) ESP.getResetReason().c_str());
  74. DEBUG_MSG("Memory size: %d bytes\n", ESP.getFlashChipSize());
  75. DEBUG_MSG("Free heap: %d bytes\n", ESP.getFreeHeap());
  76. DEBUG_MSG("Firmware size: %d bytes\n", ESP.getSketchSize());
  77. DEBUG_MSG("Free firmware space: %d bytes\n", ESP.getFreeSketchSpace());
  78. #if not EMBEDDED_WEB
  79. FSInfo fs_info;
  80. if (SPIFFS.info(fs_info)) {
  81. DEBUG_MSG("File system total size: %d bytes\n", fs_info.totalBytes);
  82. DEBUG_MSG(" used size : %d bytes\n", fs_info.usedBytes);
  83. DEBUG_MSG(" block size: %d bytes\n", fs_info.blockSize);
  84. DEBUG_MSG(" page size : %d bytes\n", fs_info.pageSize);
  85. DEBUG_MSG(" max files : %d\n", fs_info.maxOpenFiles);
  86. DEBUG_MSG(" max length: %d\n", fs_info.maxPathLength);
  87. }
  88. #endif
  89. DEBUG_MSG("\n\n");
  90. }
  91. void setup() {
  92. hardwareSetup();
  93. welcome();
  94. settingsSetup();
  95. if (getSetting("hostname").length() == 0) {
  96. setSetting("hostname", getIdentifier());
  97. saveSettings();
  98. }
  99. webSetup();
  100. relaySetup();
  101. buttonSetup();
  102. ledSetup();
  103. delay(500);
  104. wifiSetup();
  105. otaSetup();
  106. mqttSetup();
  107. ntpSetup();
  108. #if ENABLE_I2C
  109. i2cSetup();
  110. #endif
  111. #if ENABLE_FAUXMO
  112. fauxmoSetup();
  113. #endif
  114. #if ENABLE_NOFUSS
  115. nofussSetup();
  116. #endif
  117. #if ENABLE_POW
  118. powSetup();
  119. #endif
  120. #if ENABLE_DS18B20
  121. dsSetup();
  122. #endif
  123. #if ENABLE_DHT
  124. dhtSetup();
  125. #endif
  126. #if ENABLE_RF
  127. rfSetup();
  128. #endif
  129. #if ENABLE_EMON
  130. powerMonitorSetup();
  131. #endif
  132. }
  133. void loop() {
  134. hardwareLoop();
  135. buttonLoop();
  136. ledLoop();
  137. wifiLoop();
  138. otaLoop();
  139. mqttLoop();
  140. ntpLoop();
  141. #if ENABLE_FAUXMO
  142. fauxmoLoop();
  143. #endif
  144. #ifndef SONOFF_DUAL
  145. settingsLoop();
  146. #endif
  147. #if ENABLE_NOFUSS
  148. nofussLoop();
  149. #endif
  150. #if ENABLE_POW
  151. powLoop();
  152. #endif
  153. #if ENABLE_DS18B20
  154. dsLoop();
  155. #endif
  156. #if ENABLE_DHT
  157. dhtLoop();
  158. #endif
  159. #if ENABLE_RF
  160. rfLoop();
  161. #endif
  162. #if ENABLE_EMON
  163. powerMonitorLoop();
  164. #endif
  165. }