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.

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