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.

322 lines
9.1 KiB

8 years ago
7 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. #include <EEPROM.h>
  17. // -----------------------------------------------------------------------------
  18. // METHODS
  19. // -----------------------------------------------------------------------------
  20. String getIdentifier() {
  21. char identifier[20];
  22. sprintf_P(identifier, PSTR("%s_%06X"), DEVICE, ESP.getChipId());
  23. return String(identifier);
  24. }
  25. void heartbeat() {
  26. static unsigned long last_uptime = 0;
  27. static unsigned char uptime_overflows = 0;
  28. if (millis() < last_uptime) ++uptime_overflows;
  29. last_uptime = millis();
  30. unsigned long uptime_seconds = uptime_overflows * (UPTIME_OVERFLOW / 1000) + (last_uptime / 1000);
  31. unsigned int free_heap = ESP.getFreeHeap();
  32. DEBUG_MSG_P(PSTR("[MAIN] Time: %s\n"), (char *) ntpDateTime().c_str());
  33. if (!mqttConnected()) {
  34. DEBUG_MSG_P(PSTR("[MAIN] Uptime: %ld seconds\n"), uptime_seconds);
  35. DEBUG_MSG_P(PSTR("[MAIN] Free heap: %d bytes\n"), free_heap);
  36. #if ENABLE_ADC_VCC
  37. DEBUG_MSG_P(PSTR("[MAIN] Power: %d mV\n"), ESP.getVcc());
  38. #endif
  39. }
  40. #if (HEARTBEAT_REPORT_INTERVAL)
  41. mqttSend(MQTT_TOPIC_INTERVAL, HEARTBEAT_INTERVAL / 1000);
  42. #endif
  43. #if (HEARTBEAT_REPORT_APP)
  44. mqttSend(MQTT_TOPIC_APP, APP_NAME);
  45. #endif
  46. #if (HEARTBEAT_REPORT_VERSION)
  47. mqttSend(MQTT_TOPIC_VERSION, APP_VERSION);
  48. #endif
  49. #if (HEARTBEAT_REPORT_HOSTNAME)
  50. mqttSend(MQTT_TOPIC_HOSTNAME, getSetting("hostname").c_str());
  51. #endif
  52. #if (HEARTBEAT_REPORT_IP)
  53. mqttSend(MQTT_TOPIC_IP, getIP().c_str());
  54. #endif
  55. #if (HEARTBEAT_REPORT_MAC)
  56. mqttSend(MQTT_TOPIC_MAC, WiFi.macAddress().c_str());
  57. #endif
  58. #if (HEARTBEAT_REPORT_RSSI)
  59. mqttSend(MQTT_TOPIC_RSSI, String(WiFi.RSSI()).c_str());
  60. #endif
  61. #if (HEARTBEAT_REPORT_UPTIME)
  62. mqttSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str());
  63. #if ENABLE_INFLUXDB
  64. influxDBSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str());
  65. #endif
  66. #endif
  67. #if (HEARTBEAT_REPORT_FREEHEAP)
  68. mqttSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str());
  69. #if ENABLE_INFLUXDB
  70. influxDBSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str());
  71. #endif
  72. #endif
  73. #if (HEARTBEAT_REPORT_RELAY)
  74. relayMQTT();
  75. #endif
  76. #if (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE) & (HEARTBEAT_REPORT_LIGHT)
  77. lightMQTT();
  78. #endif
  79. #if (HEARTBEAT_REPORT_VCC)
  80. #if ENABLE_ADC_VCC
  81. mqttSend(MQTT_TOPIC_VCC, String(ESP.getVcc()).c_str());
  82. #endif
  83. #endif
  84. #if (HEARTBEAT_REPORT_STATUS)
  85. mqttSend(MQTT_TOPIC_STATUS, MQTT_STATUS_ONLINE, true);
  86. #endif
  87. }
  88. void customReset(unsigned char status) {
  89. EEPROM.write(EEPROM_CUSTOM_RESET, status);
  90. EEPROM.commit();
  91. }
  92. unsigned char customReset() {
  93. static unsigned char status = 255;
  94. if (status == 255) {
  95. status = EEPROM.read(EEPROM_CUSTOM_RESET);
  96. if (status > 0) customReset(0);
  97. if (status > CUSTOM_RESET_MAX) status = 0;
  98. }
  99. return status;
  100. }
  101. void hardwareSetup() {
  102. EEPROM.begin(EEPROM_SIZE);
  103. #if ENABLE_SERIAL_DEBUG
  104. DEBUG_PORT.begin(SERIAL_BAUDRATE);
  105. if (customReset() == CUSTOM_RESET_HARDWARE) {
  106. DEBUG_PORT.setDebugOutput(true);
  107. }
  108. #elif defined(SERIAL_BAUDRATE)
  109. Serial.begin(SERIAL_BAUDRATE);
  110. #endif
  111. #if ENABLE_SPIFFS
  112. SPIFFS.begin();
  113. #endif
  114. }
  115. void hardwareLoop() {
  116. // Heartbeat
  117. static unsigned long last_uptime = 0;
  118. if ((millis() - last_uptime > HEARTBEAT_INTERVAL) || (last_uptime == 0)) {
  119. last_uptime = millis();
  120. heartbeat();
  121. }
  122. }
  123. // -----------------------------------------------------------------------------
  124. // BOOTING
  125. // -----------------------------------------------------------------------------
  126. unsigned int sectors(size_t size) {
  127. return (int) (size + SPI_FLASH_SEC_SIZE - 1) / SPI_FLASH_SEC_SIZE;
  128. }
  129. void welcome() {
  130. DEBUG_MSG_P(PSTR("\n\n"));
  131. DEBUG_MSG_P(PSTR("%s %s\n"), (char *) APP_NAME, (char *) APP_VERSION);
  132. DEBUG_MSG_P(PSTR("%s\n%s\n\n"), (char *) APP_AUTHOR, (char *) APP_WEBSITE);
  133. DEBUG_MSG_P(PSTR("CPU chip ID: 0x%06X\n"), ESP.getChipId());
  134. DEBUG_MSG_P(PSTR("CPU frequency: %d MHz\n"), ESP.getCpuFreqMHz());
  135. DEBUG_MSG_P(PSTR("SDK version: %s\n"), ESP.getSdkVersion());
  136. DEBUG_MSG_P(PSTR("Core version: %s\n"), ESP.getCoreVersion().c_str());
  137. DEBUG_MSG_P(PSTR("\n"));
  138. FlashMode_t mode = ESP.getFlashChipMode();
  139. DEBUG_MSG_P(PSTR("Flash chip ID: 0x%06X\n"), ESP.getFlashChipId());
  140. DEBUG_MSG_P(PSTR("Flash speed: %u Hz\n"), ESP.getFlashChipSpeed());
  141. DEBUG_MSG_P(PSTR("Flash mode: %s\n"), mode == FM_QIO ? "QIO" : mode == FM_QOUT ? "QOUT" : mode == FM_DIO ? "DIO" : mode == FM_DOUT ? "DOUT" : "UNKNOWN");
  142. DEBUG_MSG_P(PSTR("\n"));
  143. DEBUG_MSG_P(PSTR("Flash sector size: %8u bytes\n"), SPI_FLASH_SEC_SIZE);
  144. DEBUG_MSG_P(PSTR("Flash size (SDK): %8u bytes\n"), ESP.getFlashChipSize());
  145. DEBUG_MSG_P(PSTR("Flash size (CHIP): %8u bytes / %4d sectors\n"), ESP.getFlashChipRealSize(), sectors(ESP.getFlashChipRealSize()));
  146. DEBUG_MSG_P(PSTR("Firmware size: %8u bytes / %4d sectors\n"), ESP.getSketchSize(), sectors(ESP.getSketchSize()));
  147. DEBUG_MSG_P(PSTR("OTA size: %8u bytes / %4d sectors\n"), ESP.getFreeSketchSpace(), sectors(ESP.getFreeSketchSpace()));
  148. DEBUG_MSG_P(PSTR("EEPROM size: %8u bytes / %4d sectors\n"), settingsMaxSize(), sectors(settingsMaxSize()));
  149. DEBUG_MSG_P(PSTR("Empty space: %8u bytes / 4 sectors\n"), 4 * SPI_FLASH_SEC_SIZE);
  150. DEBUG_MSG_P(PSTR("\n"));
  151. unsigned char custom_reset = customReset();
  152. if (custom_reset > 0) {
  153. char buffer[32];
  154. strcpy_P(buffer, custom_reset_string[custom_reset-1]);
  155. DEBUG_MSG_P(PSTR("Last reset reason: %s\n"), buffer);
  156. } else {
  157. DEBUG_MSG_P(PSTR("Last reset reason: %s\n"), (char *) ESP.getResetReason().c_str());
  158. }
  159. DEBUG_MSG_P(PSTR("Free heap: %u bytes\n"), ESP.getFreeHeap());
  160. #if ENABLE_SPIFFS
  161. FSInfo fs_info;
  162. if (SPIFFS.info(fs_info)) {
  163. DEBUG_MSG_P(PSTR("\n"));
  164. DEBUG_MSG_P(PSTR("File system total size: %d bytes\n"), fs_info.totalBytes);
  165. DEBUG_MSG_P(PSTR(" used size : %d bytes\n"), fs_info.usedBytes);
  166. DEBUG_MSG_P(PSTR(" block size: %d bytes\n"), fs_info.blockSize);
  167. DEBUG_MSG_P(PSTR(" page size : %d bytes\n"), fs_info.pageSize);
  168. DEBUG_MSG_P(PSTR(" max files : %d\n"), fs_info.maxOpenFiles);
  169. DEBUG_MSG_P(PSTR(" max length: %d\n"), fs_info.maxPathLength);
  170. }
  171. #endif
  172. DEBUG_MSG_P(PSTR("\n\n"));
  173. }
  174. void setup() {
  175. hardwareSetup();
  176. welcome();
  177. settingsSetup();
  178. if (getSetting("hostname").length() == 0) {
  179. setSetting("hostname", getIdentifier());
  180. saveSettings();
  181. }
  182. webSetup();
  183. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  184. lightSetup();
  185. #endif
  186. relaySetup();
  187. buttonSetup();
  188. ledSetup();
  189. delay(500);
  190. wifiSetup();
  191. otaSetup();
  192. mqttSetup();
  193. ntpSetup();
  194. #ifdef ITEAD_SONOFF_RFBRIDGE
  195. rfbSetup();
  196. #endif
  197. #if ENABLE_I2C
  198. i2cSetup();
  199. #endif
  200. #if ENABLE_FAUXMO
  201. fauxmoSetup();
  202. #endif
  203. #if ENABLE_NOFUSS
  204. nofussSetup();
  205. #endif
  206. #if ENABLE_INFLUXDB
  207. influxDBSetup();
  208. #endif
  209. #if ENABLE_HLW8012
  210. hlw8012Setup();
  211. #endif
  212. #if ENABLE_DS18B20
  213. dsSetup();
  214. #endif
  215. #if ENABLE_ANALOG
  216. analogSetup();
  217. #endif
  218. #if ENABLE_DHT
  219. dhtSetup();
  220. #endif
  221. #if ENABLE_RF
  222. rfSetup();
  223. #endif
  224. #if ENABLE_EMON
  225. powerMonitorSetup();
  226. #endif
  227. #if ENABLE_DOMOTICZ
  228. domoticzSetup();
  229. #endif
  230. // Prepare configuration for version 2.0
  231. hwUpwardsCompatibility();
  232. }
  233. void loop() {
  234. hardwareLoop();
  235. buttonLoop();
  236. relayLoop();
  237. ledLoop();
  238. wifiLoop();
  239. otaLoop();
  240. mqttLoop();
  241. ntpLoop();
  242. #ifdef ITEAD_SONOFF_RFBRIDGE
  243. rfbLoop();
  244. #endif
  245. #if ENABLE_TERMINAL
  246. settingsLoop();
  247. #endif
  248. #if ENABLE_FAUXMO
  249. fauxmoLoop();
  250. #endif
  251. #if ENABLE_NOFUSS
  252. nofussLoop();
  253. #endif
  254. #if ENABLE_HLW8012
  255. hlw8012Loop();
  256. #endif
  257. #if ENABLE_DS18B20
  258. dsLoop();
  259. #endif
  260. #if ENABLE_ANALOG
  261. analogLoop();
  262. #endif
  263. #if ENABLE_DHT
  264. dhtLoop();
  265. #endif
  266. #if ENABLE_RF
  267. rfLoop();
  268. #endif
  269. #if ENABLE_EMON
  270. powerMonitorLoop();
  271. #endif
  272. }