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.

187 lines
4.8 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
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 "defaults.h"
  17. #include "version.h"
  18. #include "debug.h"
  19. // -----------------------------------------------------------------------------
  20. // PROTOTYPES
  21. // -----------------------------------------------------------------------------
  22. #include <NtpClientLib.h>
  23. #include <WebSocketsServer.h>
  24. #include "FS.h"
  25. String getSetting(const String& key, String 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 blink(unsigned long delayOff, unsigned long delayOn) {
  35. static unsigned long next = millis();
  36. static bool status = HIGH;
  37. if (next < millis()) {
  38. status = !status;
  39. digitalWrite(LED_PIN, status);
  40. next += ((status) ? delayOff : delayOn);
  41. }
  42. }
  43. void showStatus() {
  44. if (wifiConnected()) {
  45. blink(5000, 500);
  46. } else {
  47. blink(500, 500);
  48. }
  49. }
  50. void hardwareSetup() {
  51. Serial.begin(115200);
  52. pinMode(LED_PIN, OUTPUT);
  53. SPIFFS.begin();
  54. }
  55. void getFSVersion(char * buffer) {
  56. File h = SPIFFS.open(FS_VERSION_FILE, "r");
  57. if (!h) {
  58. DEBUG_MSG("[SPIFFS] Could not open file system version file.\n");
  59. strcpy(buffer, APP_VERSION);
  60. return;
  61. }
  62. size_t size = h.size();
  63. h.readBytes(buffer, size - 1);
  64. h.close();
  65. }
  66. void hardwareLoop() {
  67. showStatus();
  68. // Heartbeat
  69. static unsigned long last_heartbeat = 0;
  70. if (mqttConnected()) {
  71. if ((millis() - last_heartbeat > HEARTBEAT_INTERVAL) || (last_heartbeat == 0)) {
  72. last_heartbeat = millis();
  73. mqttSend((char *) MQTT_HEARTBEAT_TOPIC, (char *) "1");
  74. DEBUG_MSG("[BEAT] Free heap: %d\n", ESP.getFreeHeap());
  75. DEBUG_MSG("[NTP] Time: %s\n", (char *) NTP.getTimeDateString().c_str());
  76. }
  77. }
  78. }
  79. // -----------------------------------------------------------------------------
  80. // BOOTING
  81. // -----------------------------------------------------------------------------
  82. void welcome() {
  83. delay(2000);
  84. Serial.printf("%s %s\n", (char *) APP_NAME, (char *) APP_VERSION);
  85. Serial.printf("%s\n%s\n\n", (char *) APP_AUTHOR, (char *) APP_WEBSITE);
  86. //Serial.printf("Device: %s\n", (char *) getIdentifier().c_str());
  87. Serial.printf("ChipID: %06X\n", ESP.getChipId());
  88. Serial.printf("Last reset reason: %s\n", (char *) ESP.getResetReason().c_str());
  89. Serial.printf("Memory size: %d bytes\n", ESP.getFlashChipSize());
  90. Serial.printf("Free heap: %d bytes\n", ESP.getFreeHeap());
  91. FSInfo fs_info;
  92. if (SPIFFS.info(fs_info)) {
  93. Serial.printf("File system total size: %d bytes\n", fs_info.totalBytes);
  94. Serial.printf(" used size : %d bytes\n", fs_info.usedBytes);
  95. Serial.printf(" block size: %d bytes\n", fs_info.blockSize);
  96. Serial.printf(" page size : %d bytes\n", fs_info.pageSize);
  97. Serial.printf(" max files : %d\n", fs_info.maxOpenFiles);
  98. Serial.printf(" max length: %d\n", fs_info.maxPathLength);
  99. }
  100. Serial.println();
  101. Serial.println();
  102. }
  103. void setup() {
  104. hardwareSetup();
  105. buttonSetup();
  106. welcome();
  107. settingsSetup();
  108. setSetting("hostname", String() + getIdentifier());
  109. saveSettings();
  110. relaySetup();
  111. wifiSetup();
  112. otaSetup();
  113. mqttSetup();
  114. webServerSetup();
  115. webSocketSetup();
  116. ntpSetup();
  117. #if ENABLE_NOFUSS
  118. nofussSetup();
  119. #endif
  120. #if ENABLE_DHT
  121. dhtSetup();
  122. #endif
  123. #if ENABLE_RF
  124. rfSetup();
  125. #endif
  126. #if ENABLE_EMON
  127. powerMonitorSetup();
  128. #endif
  129. }
  130. void loop() {
  131. hardwareLoop();
  132. buttonLoop();
  133. settingsLoop();
  134. wifiLoop();
  135. otaLoop();
  136. mqttLoop();
  137. webServerLoop();
  138. webSocketLoop();
  139. ntpLoop();
  140. #if ENABLE_NOFUSS
  141. nofussLoop();
  142. #endif
  143. #if ENABLE_DHT
  144. dhtLoop();
  145. #endif
  146. #if ENABLE_RF
  147. rfLoop();
  148. #endif
  149. #if ENABLE_EMON
  150. powerMonitorLoop();
  151. #endif
  152. delay(1);
  153. }