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.

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