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.

228 lines
5.6 KiB

  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. extern "C" {
  16. #include "user_interface.h"
  17. }
  18. #include <Arduino.h>
  19. #include "version.h"
  20. #include "defaults.h"
  21. #include "FS.h"
  22. #include "Config.h"
  23. // -----------------------------------------------------------------------------
  24. // Methods
  25. // -----------------------------------------------------------------------------
  26. void getCompileTime(char * buffer) {
  27. int day, month, year, hour, minute, second;
  28. // parse date
  29. String tmp = String(__DATE__);
  30. day = tmp.substring(4,6).toInt();
  31. year = tmp.substring(7).toInt();
  32. tmp = tmp.substring(0,3);
  33. if (tmp.equals("Jan")) month = 1;
  34. if (tmp.equals("Feb")) month = 2;
  35. if (tmp.equals("Mar")) month = 3;
  36. if (tmp.equals("Apr")) month = 4;
  37. if (tmp.equals("May")) month = 5;
  38. if (tmp.equals("Jun")) month = 6;
  39. if (tmp.equals("Jul")) month = 7;
  40. if (tmp.equals("Aug")) month = 8;
  41. if (tmp.equals("Sep")) month = 9;
  42. if (tmp.equals("Oct")) month = 10;
  43. if (tmp.equals("Nov")) month = 11;
  44. if (tmp.equals("Dec")) month = 12;
  45. // parse time
  46. tmp = String(__TIME__);
  47. hour = tmp.substring(0,2).toInt();
  48. minute = tmp.substring(3,5).toInt();
  49. second = tmp.substring(6,8).toInt();
  50. sprintf(buffer, "%d%02d%02d%02d%02d%02d", year, month, day, hour, minute, second);
  51. buffer[14] = 0;
  52. }
  53. String getIdentifier() {
  54. char identifier[20];
  55. sprintf(identifier, "%s_%06X", DEVICE, ESP.getChipId());
  56. return String(identifier);
  57. }
  58. void blink(unsigned long delayOff, unsigned long delayOn) {
  59. static unsigned long next = millis();
  60. static bool status = HIGH;
  61. if (next < millis()) {
  62. status = !status;
  63. digitalWrite(LED_PIN, status);
  64. next += ((status) ? delayOff : delayOn);
  65. }
  66. }
  67. void showStatus() {
  68. if (wifiConnected()) {
  69. blink(5000, 500);
  70. } else {
  71. blink(500, 500);
  72. }
  73. }
  74. void hardwareSetup() {
  75. Serial.begin(115200);
  76. pinMode(LED_PIN, OUTPUT);
  77. SPIFFS.begin();
  78. }
  79. void getFSVersion(char * buffer) {
  80. File h = SPIFFS.open(FS_VERSION_FILE, "r");
  81. if (!h) {
  82. #ifdef DEBUG
  83. Serial.println(F("[SPIFFS] Could not open file system version file."));
  84. #endif
  85. strcpy(buffer, APP_VERSION);
  86. return;
  87. }
  88. size_t size = h.size();
  89. h.readBytes(buffer, size - 1);
  90. h.close();
  91. }
  92. void hardwareLoop() {
  93. showStatus();
  94. // Heartbeat
  95. static unsigned long last_heartbeat = 0;
  96. if (millis() - last_heartbeat > HEARTBEAT_INTERVAL) {
  97. last_heartbeat = millis();
  98. mqttSend((char *) MQTT_HEARTBEAT_TOPIC, (char *) "1");
  99. #ifdef DEBUG
  100. Serial.print(F("[BEAT] Free heap: "));
  101. Serial.println(ESP.getFreeHeap());
  102. #endif
  103. }
  104. }
  105. // -----------------------------------------------------------------------------
  106. // Booting
  107. // -----------------------------------------------------------------------------
  108. void welcome() {
  109. char buffer[BUFFER_SIZE];
  110. getCompileTime(buffer);
  111. Serial.println();
  112. Serial.println();
  113. Serial.print(APP_NAME);
  114. Serial.print(F(" "));
  115. Serial.print(APP_VERSION);
  116. Serial.print(F(" built "));
  117. Serial.println(buffer);
  118. Serial.println(APP_AUTHOR);
  119. Serial.println(APP_WEBSITE);
  120. Serial.println();
  121. Serial.print(F("Device: "));
  122. Serial.println(getIdentifier());
  123. Serial.print(F("Last reset reason: "));
  124. Serial.println(ESP.getResetReason());
  125. Serial.print(F("Memory size: "));
  126. Serial.print(ESP.getFlashChipSize());
  127. Serial.println(F(" bytes"));
  128. Serial.print(F("Free heap: "));
  129. Serial.print(ESP.getFreeHeap());
  130. Serial.println(F(" bytes"));
  131. FSInfo fs_info;
  132. if (SPIFFS.info(fs_info)) {
  133. Serial.print(F("File system total size: "));
  134. Serial.print(fs_info.totalBytes);
  135. Serial.println(F(" bytes"));
  136. Serial.print(F("File system used size : "));
  137. Serial.print(fs_info.usedBytes);
  138. Serial.println(F(" bytes"));
  139. }
  140. Serial.println();
  141. }
  142. void setup() {
  143. hardwareSetup();
  144. relaySetup();
  145. buttonSetup();
  146. delay(1000);
  147. welcome();
  148. config.load();
  149. // At the moment I am overriding any possible hostname stored in EEPROM
  150. // with the generated one until I have a way to change them from the
  151. // configuration interface
  152. config.hostname = getIdentifier();
  153. wifi_station_set_hostname((char *) config.hostname.c_str());
  154. wifiSetup();
  155. otaSetup();
  156. mqttSetup();
  157. webServerSetup();
  158. #if ENABLE_NOFUSS
  159. nofussSetup();
  160. #endif
  161. #if ENABLE_RF
  162. rfSetup();
  163. #endif
  164. #if ENABLE_DHT
  165. dhtSetup();
  166. #endif
  167. #if ENABLE_EMON
  168. powerMonitorSetup();
  169. #endif
  170. }
  171. void loop() {
  172. wifiLoop();
  173. hardwareLoop();
  174. buttonLoop();
  175. otaLoop();
  176. mqttLoop();
  177. webServerLoop();
  178. #if ENABLE_NOFUSS
  179. nofussLoop();
  180. #endif
  181. #if ENABLE_RF
  182. rfLoop();
  183. #endif
  184. #if ENABLE_DHT
  185. dhtLoop();
  186. #endif
  187. #if ENABLE_EMON
  188. powerMonitorLoop();
  189. #endif
  190. delay(1);
  191. }