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