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.

179 lines
4.7 KiB

8 years ago
8 years ago
7 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-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 <Arduino.h>
  16. #include "config/all.h"
  17. // -----------------------------------------------------------------------------
  18. // PROTOTYPES
  19. // -----------------------------------------------------------------------------
  20. #include <NtpClientLib.h>
  21. #include <ESPAsyncWebServer.h>
  22. #include <AsyncMqttClient.h>
  23. void mqttRegister(void (*callback)(unsigned int, const char *, const char *));
  24. template<typename T> bool setSetting(const String& key, T value);
  25. template<typename T> String getSetting(const String& key, T defaultValue);
  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 hardwareLoop() {
  39. // Heartbeat
  40. static unsigned long last_heartbeat = 0;
  41. if (mqttConnected()) {
  42. if ((millis() - last_heartbeat > HEARTBEAT_INTERVAL) || (last_heartbeat == 0)) {
  43. last_heartbeat = millis();
  44. mqttSend(MQTT_HEARTBEAT_TOPIC, "1");
  45. DEBUG_MSG("[BEAT] Free heap: %d\n", ESP.getFreeHeap());
  46. DEBUG_MSG("[NTP] Time: %s\n", (char *) NTP.getTimeDateString().c_str());
  47. }
  48. }
  49. }
  50. // -----------------------------------------------------------------------------
  51. // BOOTING
  52. // -----------------------------------------------------------------------------
  53. void welcome() {
  54. delay(2000);
  55. DEBUG_MSG("%s %s\n", (char *) APP_NAME, (char *) APP_VERSION);
  56. DEBUG_MSG("%s\n%s\n\n", (char *) APP_AUTHOR, (char *) APP_WEBSITE);
  57. //DEBUG_MSG("Device: %s\n", (char *) getIdentifier().c_str());
  58. DEBUG_MSG("ChipID: %06X\n", ESP.getChipId());
  59. DEBUG_MSG("CPU frequency: %d MHz\n", ESP.getCpuFreqMHz());
  60. DEBUG_MSG("Last reset reason: %s\n", (char *) ESP.getResetReason().c_str());
  61. DEBUG_MSG("Memory size: %d bytes\n", ESP.getFlashChipSize());
  62. DEBUG_MSG("Free heap: %d bytes\n", ESP.getFreeHeap());
  63. DEBUG_MSG("Firmware size: %d bytes\n", ESP.getSketchSize());
  64. DEBUG_MSG("Free firmware space: %d bytes\n", ESP.getFreeSketchSpace());
  65. FSInfo fs_info;
  66. if (SPIFFS.info(fs_info)) {
  67. DEBUG_MSG("File system total size: %d bytes\n", fs_info.totalBytes);
  68. DEBUG_MSG(" used size : %d bytes\n", fs_info.usedBytes);
  69. DEBUG_MSG(" block size: %d bytes\n", fs_info.blockSize);
  70. DEBUG_MSG(" page size : %d bytes\n", fs_info.pageSize);
  71. DEBUG_MSG(" max files : %d\n", fs_info.maxOpenFiles);
  72. DEBUG_MSG(" max length: %d\n", fs_info.maxPathLength);
  73. }
  74. DEBUG_MSG("\n\n");
  75. }
  76. void setup() {
  77. hardwareSetup();
  78. welcome();
  79. settingsSetup();
  80. if (getSetting("hostname").length() == 0) {
  81. setSetting("hostname", getIdentifier());
  82. saveSettings();
  83. }
  84. relaySetup();
  85. buttonSetup();
  86. ledSetup();
  87. wifiSetup();
  88. otaSetup();
  89. mqttSetup();
  90. webSetup();
  91. ntpSetup();
  92. #if ENABLE_DOMOTICZ
  93. domoticzSetup();
  94. #endif
  95. #if ENABLE_FAUXMO
  96. fauxmoSetup();
  97. #endif
  98. #if ENABLE_NOFUSS
  99. nofussSetup();
  100. #endif
  101. #if ENABLE_POW
  102. powSetup();
  103. #endif
  104. #if ENABLE_DS18B20
  105. dsSetup();
  106. #endif
  107. #if ENABLE_DHT
  108. dhtSetup();
  109. #endif
  110. #if ENABLE_RF
  111. rfSetup();
  112. #endif
  113. #if ENABLE_EMON
  114. powerMonitorSetup();
  115. #endif
  116. }
  117. void loop() {
  118. hardwareLoop();
  119. buttonLoop();
  120. ledLoop();
  121. wifiLoop();
  122. otaLoop();
  123. mqttLoop();
  124. ntpLoop();
  125. #if ENABLE_FAUXMO
  126. fauxmoLoop();
  127. #endif
  128. #ifndef SONOFF_DUAL
  129. settingsLoop();
  130. #endif
  131. #if ENABLE_NOFUSS
  132. nofussLoop();
  133. #endif
  134. #if ENABLE_POW
  135. powLoop();
  136. #endif
  137. #if ENABLE_DS18B20
  138. dsLoop();
  139. #endif
  140. #if ENABLE_DHT
  141. dhtLoop();
  142. #endif
  143. #if ENABLE_RF
  144. rfLoop();
  145. #endif
  146. #if ENABLE_EMON
  147. powerMonitorLoop();
  148. #endif
  149. }