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.

330 lines
9.4 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 INFLUXDB_SUPPORT
  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 INFLUXDB_SUPPORT
  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 DEBUG_SERIAL_SUPPORT
  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 SPIFFS_SUPPORT
  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. #if SPIFFS_SUPPORT
  149. FSInfo fs_info;
  150. bool fs = SPIFFS.info(fs_info);
  151. if (fs) {
  152. DEBUG_MSG_P(PSTR("SPIFFS size: %8u bytes / %4d sectors\n"), fs_info.totalBytes, sectors(fs_info.totalBytes));
  153. }
  154. #else
  155. DEBUG_MSG_P(PSTR("SPIFFS size: %8u bytes / %4d sectors\n"), 0, 0);
  156. #endif
  157. DEBUG_MSG_P(PSTR("EEPROM size: %8u bytes / %4d sectors\n"), settingsMaxSize(), sectors(settingsMaxSize()));
  158. DEBUG_MSG_P(PSTR("Empty space: %8u bytes / 4 sectors\n"), 4 * SPI_FLASH_SEC_SIZE);
  159. #if SPIFFS_SUPPORT
  160. if (fs) {
  161. DEBUG_MSG_P(PSTR("\n"));
  162. DEBUG_MSG_P(PSTR("SPIFFS total size: %8u bytes\n"), fs_info.totalBytes);
  163. DEBUG_MSG_P(PSTR(" used size: %8u bytes\n"), fs_info.usedBytes);
  164. DEBUG_MSG_P(PSTR(" block size: %8u bytes\n"), fs_info.blockSize);
  165. DEBUG_MSG_P(PSTR(" page size: %8u bytes\n"), fs_info.pageSize);
  166. DEBUG_MSG_P(PSTR(" max files: %8u\n"), fs_info.maxOpenFiles);
  167. DEBUG_MSG_P(PSTR(" max length: %8u\n"), fs_info.maxPathLength);
  168. }
  169. #endif
  170. DEBUG_MSG_P(PSTR("\n"));
  171. unsigned char custom_reset = customReset();
  172. if (custom_reset > 0) {
  173. char buffer[32];
  174. strcpy_P(buffer, custom_reset_string[custom_reset-1]);
  175. DEBUG_MSG_P(PSTR("Last reset reason: %s\n"), buffer);
  176. } else {
  177. DEBUG_MSG_P(PSTR("Last reset reason: %s\n"), (char *) ESP.getResetReason().c_str());
  178. }
  179. DEBUG_MSG_P(PSTR("Free heap: %u bytes\n"), ESP.getFreeHeap());
  180. DEBUG_MSG_P(PSTR("\n\n"));
  181. }
  182. void setup() {
  183. hardwareSetup();
  184. welcome();
  185. settingsSetup();
  186. if (getSetting("hostname").length() == 0) {
  187. setSetting("hostname", getIdentifier());
  188. saveSettings();
  189. }
  190. webSetup();
  191. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  192. lightSetup();
  193. #endif
  194. relaySetup();
  195. buttonSetup();
  196. ledSetup();
  197. delay(500);
  198. wifiSetup();
  199. otaSetup();
  200. mqttSetup();
  201. ntpSetup();
  202. #ifdef ITEAD_SONOFF_RFBRIDGE
  203. rfbSetup();
  204. #endif
  205. #if I2C_SUPPORT
  206. i2cSetup();
  207. #endif
  208. #if ALEXA_SUPPORT
  209. alexaSetup();
  210. #endif
  211. #if NOFUSS_SUPPORT
  212. nofussSetup();
  213. #endif
  214. #if INFLUXDB_SUPPORT
  215. influxDBSetup();
  216. #endif
  217. #if HLW8012_SUPPORT
  218. hlw8012Setup();
  219. #endif
  220. #if DS18B20_SUPPORT
  221. dsSetup();
  222. #endif
  223. #if ANALOG_SUPPORT
  224. analogSetup();
  225. #endif
  226. #if DHT_SUPPORT
  227. dhtSetup();
  228. #endif
  229. #if RF_SUPPORT
  230. rfSetup();
  231. #endif
  232. #if EMON_SUPPORT
  233. powerMonitorSetup();
  234. #endif
  235. #if DOMOTICZ_SUPPORT
  236. domoticzSetup();
  237. #endif
  238. // Prepare configuration for version 2.0
  239. hwUpwardsCompatibility();
  240. }
  241. void loop() {
  242. hardwareLoop();
  243. buttonLoop();
  244. relayLoop();
  245. ledLoop();
  246. wifiLoop();
  247. otaLoop();
  248. mqttLoop();
  249. ntpLoop();
  250. #ifdef ITEAD_SONOFF_RFBRIDGE
  251. rfbLoop();
  252. #endif
  253. #if TERMINAL_SUPPORT
  254. settingsLoop();
  255. #endif
  256. #if ALEXA_SUPPORT
  257. alexaLoop();
  258. #endif
  259. #if NOFUSS_SUPPORT
  260. nofussLoop();
  261. #endif
  262. #if HLW8012_SUPPORT
  263. hlw8012Loop();
  264. #endif
  265. #if DS18B20_SUPPORT
  266. dsLoop();
  267. #endif
  268. #if ANALOG_SUPPORT
  269. analogLoop();
  270. #endif
  271. #if DHT_SUPPORT
  272. dhtLoop();
  273. #endif
  274. #if RF_SUPPORT
  275. rfLoop();
  276. #endif
  277. #if EMON_SUPPORT
  278. powerMonitorLoop();
  279. #endif
  280. }