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.

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