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.

333 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 ADC_VCC_ENABLED
  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 ADC_VCC_ENABLED
  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. #if WEB_SUPPORT
  191. webSetup();
  192. #endif
  193. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  194. lightSetup();
  195. #endif
  196. relaySetup();
  197. buttonSetup();
  198. ledSetup();
  199. delay(500);
  200. wifiSetup();
  201. otaSetup();
  202. mqttSetup();
  203. ntpSetup();
  204. #ifdef ITEAD_SONOFF_RFBRIDGE
  205. rfbSetup();
  206. #endif
  207. #if I2C_SUPPORT
  208. i2cSetup();
  209. #endif
  210. #if ALEXA_SUPPORT
  211. alexaSetup();
  212. #endif
  213. #if NOFUSS_SUPPORT
  214. nofussSetup();
  215. #endif
  216. #if INFLUXDB_SUPPORT
  217. influxDBSetup();
  218. #endif
  219. #if HLW8012_SUPPORT
  220. hlw8012Setup();
  221. #endif
  222. #if DS18B20_SUPPORT
  223. dsSetup();
  224. #endif
  225. #if ANALOG_SUPPORT
  226. analogSetup();
  227. #endif
  228. #if DHT_SUPPORT
  229. dhtSetup();
  230. #endif
  231. #if RF_SUPPORT
  232. rfSetup();
  233. #endif
  234. #if EMON_SUPPORT
  235. powerMonitorSetup();
  236. #endif
  237. #if DOMOTICZ_SUPPORT
  238. domoticzSetup();
  239. #endif
  240. // Prepare configuration for version 2.0
  241. hwUpwardsCompatibility();
  242. }
  243. void loop() {
  244. hardwareLoop();
  245. buttonLoop();
  246. relayLoop();
  247. ledLoop();
  248. wifiLoop();
  249. otaLoop();
  250. mqttLoop();
  251. ntpLoop();
  252. #ifdef ITEAD_SONOFF_RFBRIDGE
  253. rfbLoop();
  254. #endif
  255. #if TERMINAL_SUPPORT
  256. settingsLoop();
  257. #endif
  258. #if ALEXA_SUPPORT
  259. alexaLoop();
  260. #endif
  261. #if NOFUSS_SUPPORT
  262. nofussLoop();
  263. #endif
  264. #if HLW8012_SUPPORT
  265. hlw8012Loop();
  266. #endif
  267. #if DS18B20_SUPPORT
  268. dsLoop();
  269. #endif
  270. #if ANALOG_SUPPORT
  271. analogLoop();
  272. #endif
  273. #if DHT_SUPPORT
  274. dhtLoop();
  275. #endif
  276. #if RF_SUPPORT
  277. rfLoop();
  278. #endif
  279. #if EMON_SUPPORT
  280. powerMonitorLoop();
  281. #endif
  282. }