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.

182 lines
4.7 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. String getSetting(const String& key, String defaultValue = "");
  25. bool relayStatus(unsigned char id, bool status, bool report = true);
  26. // -----------------------------------------------------------------------------
  27. // METHODS
  28. // -----------------------------------------------------------------------------
  29. String getIdentifier() {
  30. char identifier[20];
  31. sprintf(identifier, "%s_%06X", DEVICE, ESP.getChipId());
  32. return String(identifier);
  33. }
  34. void hardwareSetup() {
  35. Serial.begin(SERIAL_BAUDRATE);
  36. SPIFFS.begin();
  37. }
  38. void getFSVersion(char * buffer) {
  39. File h = SPIFFS.open(FS_VERSION_FILE, "r");
  40. if (!h) {
  41. DEBUG_MSG("[SPIFFS] Could not open file system version file.\n");
  42. strcpy(buffer, APP_VERSION);
  43. return;
  44. }
  45. size_t size = h.size();
  46. h.readBytes(buffer, size - 1);
  47. h.close();
  48. }
  49. void hardwareLoop() {
  50. // Heartbeat
  51. static unsigned long last_heartbeat = 0;
  52. if (mqttConnected()) {
  53. if ((millis() - last_heartbeat > HEARTBEAT_INTERVAL) || (last_heartbeat == 0)) {
  54. last_heartbeat = millis();
  55. mqttSend((char *) MQTT_HEARTBEAT_TOPIC, (char *) "1");
  56. DEBUG_MSG("[BEAT] Free heap: %d\n", ESP.getFreeHeap());
  57. DEBUG_MSG("[NTP] Time: %s\n", (char *) NTP.getTimeDateString().c_str());
  58. }
  59. }
  60. }
  61. // -----------------------------------------------------------------------------
  62. // BOOTING
  63. // -----------------------------------------------------------------------------
  64. void welcome() {
  65. delay(2000);
  66. DEBUG_MSG("%s %s\n", (char *) APP_NAME, (char *) APP_VERSION);
  67. DEBUG_MSG("%s\n%s\n\n", (char *) APP_AUTHOR, (char *) APP_WEBSITE);
  68. //DEBUG_MSG("Device: %s\n", (char *) getIdentifier().c_str());
  69. DEBUG_MSG("ChipID: %06X\n", ESP.getChipId());
  70. DEBUG_MSG("Last reset reason: %s\n", (char *) ESP.getResetReason().c_str());
  71. DEBUG_MSG("Memory size: %d bytes\n", ESP.getFlashChipSize());
  72. DEBUG_MSG("Free heap: %d bytes\n", ESP.getFreeHeap());
  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. DEBUG_MSG("\n\n");
  83. }
  84. void setup() {
  85. hardwareSetup();
  86. welcome();
  87. settingsSetup();
  88. if (getSetting("hostname").length() == 0) {
  89. setSetting("hostname", String() + getIdentifier());
  90. saveSettings();
  91. }
  92. relaySetup();
  93. buttonSetup();
  94. ledSetup();
  95. wifiSetup();
  96. otaSetup();
  97. mqttSetup();
  98. webSetup();
  99. ntpSetup();
  100. #if ENABLE_FAUXMO
  101. fauxmoSetup();
  102. #endif
  103. #if ENABLE_NOFUSS
  104. nofussSetup();
  105. #endif
  106. #if ENABLE_POW
  107. powSetup();
  108. #endif
  109. #if ENABLE_DS18B20
  110. dsSetup();
  111. #endif
  112. #if ENABLE_DHT
  113. dhtSetup();
  114. #endif
  115. #if ENABLE_RF
  116. rfSetup();
  117. #endif
  118. #if ENABLE_EMON
  119. powerMonitorSetup();
  120. #endif
  121. }
  122. void loop() {
  123. hardwareLoop();
  124. buttonLoop();
  125. ledLoop();
  126. wifiLoop();
  127. otaLoop();
  128. mqttLoop();
  129. ntpLoop();
  130. #ifndef SONOFF_DUAL
  131. settingsLoop();
  132. #endif
  133. #if ENABLE_NOFUSS
  134. nofussLoop();
  135. #endif
  136. #if ENABLE_POW
  137. powLoop();
  138. #endif
  139. #if ENABLE_DS18B20
  140. dsLoop();
  141. #endif
  142. #if ENABLE_DHT
  143. dhtLoop();
  144. #endif
  145. #if ENABLE_RF
  146. rfLoop();
  147. #endif
  148. #if ENABLE_EMON
  149. powerMonitorLoop();
  150. #endif
  151. yield();
  152. }