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.

186 lines
5.0 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
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. /*
  2. ESPurna
  3. Copyright (C) 2016 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 <Arduino.h>
  16. #include "config/all.h"
  17. // -----------------------------------------------------------------------------
  18. // PROTOTYPES
  19. // -----------------------------------------------------------------------------
  20. #include <NtpClientLib.h>
  21. #include <ESPAsyncWebServer.h>
  22. #include <AsyncMqttClient.h>
  23. #include "FS.h"
  24. void mqttRegister(void (*callback)(unsigned int, const char *, const char *));
  25. template<typename T> bool setSetting(const String& key, T value);
  26. template<typename T> String getSetting(const String& key, T defaultValue);
  27. // -----------------------------------------------------------------------------
  28. // METHODS
  29. // -----------------------------------------------------------------------------
  30. String getIdentifier() {
  31. char identifier[20];
  32. sprintf(identifier, "%s_%06X", DEVICE, ESP.getChipId());
  33. return String(identifier);
  34. }
  35. void hardwareSetup() {
  36. Serial.begin(SERIAL_BAUDRATE);
  37. SPIFFS.begin();
  38. }
  39. void getFSVersion(char * buffer) {
  40. File h = SPIFFS.open(FS_VERSION_FILE, "r");
  41. if (!h) {
  42. DEBUG_MSG("[SPIFFS] Could not open file system version file.\n");
  43. strcpy(buffer, APP_VERSION);
  44. return;
  45. }
  46. size_t size = h.size();
  47. h.readBytes(buffer, size - 1);
  48. h.close();
  49. }
  50. void hardwareLoop() {
  51. // Heartbeat
  52. static unsigned long last_heartbeat = 0;
  53. if (mqttConnected()) {
  54. if ((millis() - last_heartbeat > HEARTBEAT_INTERVAL) || (last_heartbeat == 0)) {
  55. last_heartbeat = millis();
  56. mqttSend((char *) MQTT_HEARTBEAT_TOPIC, (char *) "1");
  57. DEBUG_MSG("[BEAT] Free heap: %d\n", ESP.getFreeHeap());
  58. DEBUG_MSG("[NTP] Time: %s\n", (char *) NTP.getTimeDateString().c_str());
  59. }
  60. }
  61. }
  62. // -----------------------------------------------------------------------------
  63. // BOOTING
  64. // -----------------------------------------------------------------------------
  65. void welcome() {
  66. delay(2000);
  67. DEBUG_MSG("%s %s\n", (char *) APP_NAME, (char *) APP_VERSION);
  68. DEBUG_MSG("%s\n%s\n\n", (char *) APP_AUTHOR, (char *) APP_WEBSITE);
  69. //DEBUG_MSG("Device: %s\n", (char *) getIdentifier().c_str());
  70. DEBUG_MSG("ChipID: %06X\n", ESP.getChipId());
  71. DEBUG_MSG("CPU frequency: %d MHz\n", ESP.getCpuFreqMHz());
  72. DEBUG_MSG("Last reset reason: %s\n", (char *) ESP.getResetReason().c_str());
  73. DEBUG_MSG("Memory size: %d bytes\n", ESP.getFlashChipSize());
  74. DEBUG_MSG("Free heap: %d bytes\n", ESP.getFreeHeap());
  75. DEBUG_MSG("Firmware size: %d bytes\n", ESP.getSketchSize());
  76. DEBUG_MSG("Free firmware space: %d bytes\n", ESP.getFreeSketchSpace());
  77. FSInfo fs_info;
  78. if (SPIFFS.info(fs_info)) {
  79. DEBUG_MSG("File system total size: %d bytes\n", fs_info.totalBytes);
  80. DEBUG_MSG(" used size : %d bytes\n", fs_info.usedBytes);
  81. DEBUG_MSG(" block size: %d bytes\n", fs_info.blockSize);
  82. DEBUG_MSG(" page size : %d bytes\n", fs_info.pageSize);
  83. DEBUG_MSG(" max files : %d\n", fs_info.maxOpenFiles);
  84. DEBUG_MSG(" max length: %d\n", fs_info.maxPathLength);
  85. }
  86. DEBUG_MSG("\n\n");
  87. }
  88. void setup() {
  89. hardwareSetup();
  90. welcome();
  91. settingsSetup();
  92. if (getSetting("hostname").length() == 0) {
  93. setSetting("hostname", getIdentifier());
  94. saveSettings();
  95. }
  96. relaySetup();
  97. buttonSetup();
  98. ledSetup();
  99. wifiSetup();
  100. otaSetup();
  101. mqttSetup();
  102. webSetup();
  103. ntpSetup();
  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. #ifndef SONOFF_DUAL
  135. settingsLoop();
  136. #endif
  137. #if ENABLE_NOFUSS
  138. nofussLoop();
  139. #endif
  140. #if ENABLE_POW
  141. powLoop();
  142. #endif
  143. #if ENABLE_DS18B20
  144. dsLoop();
  145. #endif
  146. #if ENABLE_DHT
  147. dhtLoop();
  148. #endif
  149. #if ENABLE_RF
  150. rfLoop();
  151. #endif
  152. #if ENABLE_EMON
  153. powerMonitorLoop();
  154. #endif
  155. yield();
  156. }