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.

378 lines
10 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
  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 buffer[20];
  22. snprintf_P(buffer, sizeof(buffer), PSTR("%s_%06X"), DEVICE, ESP.getChipId());
  23. return String(buffer);
  24. }
  25. String buildTime() {
  26. const char time_now[] = __TIME__; // hh:mm:ss
  27. unsigned int hour = atoi(&time_now[0]);
  28. unsigned int minute = atoi(&time_now[3]);
  29. unsigned int second = atoi(&time_now[6]);
  30. const char date_now[] = __DATE__; // Mmm dd yyyy
  31. const char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  32. unsigned int month = 0;
  33. for ( int i = 0; i < 12; i++ ) {
  34. if (strncmp(date_now, months[i], 3) == 0 ) {
  35. month = i + 1;
  36. break;
  37. }
  38. }
  39. unsigned int day = atoi(&date_now[3]);
  40. unsigned int year = atoi(&date_now[7]);
  41. char buffer[20];
  42. snprintf_P(
  43. buffer, sizeof(buffer), PSTR("%04d/%02d/%02d %02d:%02d:%02d"),
  44. year, month, day, hour, minute, second
  45. );
  46. return String(buffer);
  47. }
  48. unsigned long getUptime() {
  49. static unsigned long last_uptime = 0;
  50. static unsigned char uptime_overflows = 0;
  51. if (millis() < last_uptime) ++uptime_overflows;
  52. last_uptime = millis();
  53. unsigned long uptime_seconds = uptime_overflows * (UPTIME_OVERFLOW / 1000) + (last_uptime / 1000);
  54. return uptime_seconds;
  55. }
  56. void heartbeat() {
  57. unsigned long uptime_seconds = getUptime();
  58. unsigned int free_heap = ESP.getFreeHeap();
  59. #if NTP_SUPPORT
  60. DEBUG_MSG_P(PSTR("[MAIN] Time: %s\n"), (char *) ntpDateTime().c_str());
  61. #endif
  62. if (!mqttConnected()) {
  63. DEBUG_MSG_P(PSTR("[MAIN] Uptime: %ld seconds\n"), uptime_seconds);
  64. DEBUG_MSG_P(PSTR("[MAIN] Free heap: %d bytes\n"), free_heap);
  65. #if ADC_VCC_ENABLED
  66. DEBUG_MSG_P(PSTR("[MAIN] Power: %d mV\n"), ESP.getVcc());
  67. #endif
  68. }
  69. #if (HEARTBEAT_REPORT_INTERVAL)
  70. mqttSend(MQTT_TOPIC_INTERVAL, HEARTBEAT_INTERVAL / 1000);
  71. #endif
  72. #if (HEARTBEAT_REPORT_APP)
  73. mqttSend(MQTT_TOPIC_APP, APP_NAME);
  74. #endif
  75. #if (HEARTBEAT_REPORT_VERSION)
  76. mqttSend(MQTT_TOPIC_VERSION, APP_VERSION);
  77. #endif
  78. #if (HEARTBEAT_REPORT_HOSTNAME)
  79. mqttSend(MQTT_TOPIC_HOSTNAME, getSetting("hostname").c_str());
  80. #endif
  81. #if (HEARTBEAT_REPORT_IP)
  82. mqttSend(MQTT_TOPIC_IP, getIP().c_str());
  83. #endif
  84. #if (HEARTBEAT_REPORT_MAC)
  85. mqttSend(MQTT_TOPIC_MAC, WiFi.macAddress().c_str());
  86. #endif
  87. #if (HEARTBEAT_REPORT_RSSI)
  88. mqttSend(MQTT_TOPIC_RSSI, String(WiFi.RSSI()).c_str());
  89. #endif
  90. #if (HEARTBEAT_REPORT_UPTIME)
  91. mqttSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str());
  92. #if INFLUXDB_SUPPORT
  93. influxDBSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str());
  94. #endif
  95. #endif
  96. #if (HEARTBEAT_REPORT_FREEHEAP)
  97. mqttSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str());
  98. #if INFLUXDB_SUPPORT
  99. influxDBSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str());
  100. #endif
  101. #endif
  102. #if (HEARTBEAT_REPORT_RELAY)
  103. relayMQTT();
  104. #endif
  105. #if (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE) & (HEARTBEAT_REPORT_LIGHT)
  106. lightMQTT();
  107. #endif
  108. #if (HEARTBEAT_REPORT_VCC)
  109. #if ADC_VCC_ENABLED
  110. mqttSend(MQTT_TOPIC_VCC, String(ESP.getVcc()).c_str());
  111. #endif
  112. #endif
  113. #if (HEARTBEAT_REPORT_STATUS)
  114. mqttSend(MQTT_TOPIC_STATUS, MQTT_STATUS_ONLINE, true);
  115. #endif
  116. }
  117. void customReset(unsigned char status) {
  118. EEPROM.write(EEPROM_CUSTOM_RESET, status);
  119. EEPROM.commit();
  120. }
  121. unsigned char customReset() {
  122. static unsigned char status = 255;
  123. if (status == 255) {
  124. status = EEPROM.read(EEPROM_CUSTOM_RESET);
  125. if (status > 0) customReset(0);
  126. if (status > CUSTOM_RESET_MAX) status = 0;
  127. }
  128. return status;
  129. }
  130. void hardwareSetup() {
  131. EEPROM.begin(EEPROM_SIZE);
  132. #if DEBUG_SERIAL_SUPPORT
  133. DEBUG_PORT.begin(SERIAL_BAUDRATE);
  134. #if DEBUG_ESP_WIFI
  135. DEBUG_PORT.setDebugOutput(true);
  136. #endif
  137. #elif defined(SERIAL_BAUDRATE)
  138. Serial.begin(SERIAL_BAUDRATE);
  139. #endif
  140. #if SPIFFS_SUPPORT
  141. SPIFFS.begin();
  142. #endif
  143. }
  144. void hardwareLoop() {
  145. // Heartbeat
  146. static unsigned long last_uptime = 0;
  147. if ((millis() - last_uptime > HEARTBEAT_INTERVAL) || (last_uptime == 0)) {
  148. last_uptime = millis();
  149. heartbeat();
  150. }
  151. }
  152. // -----------------------------------------------------------------------------
  153. // BOOTING
  154. // -----------------------------------------------------------------------------
  155. unsigned int sectors(size_t size) {
  156. return (int) (size + SPI_FLASH_SEC_SIZE - 1) / SPI_FLASH_SEC_SIZE;
  157. }
  158. void welcome() {
  159. DEBUG_MSG_P(PSTR("\n\n"));
  160. DEBUG_MSG_P(PSTR("%s %s\n"), (char *) APP_NAME, (char *) APP_VERSION);
  161. DEBUG_MSG_P(PSTR("%s\n%s\n\n"), (char *) APP_AUTHOR, (char *) APP_WEBSITE);
  162. DEBUG_MSG_P(PSTR("CPU chip ID: 0x%06X\n"), ESP.getChipId());
  163. DEBUG_MSG_P(PSTR("CPU frequency: %d MHz\n"), ESP.getCpuFreqMHz());
  164. DEBUG_MSG_P(PSTR("SDK version: %s\n"), ESP.getSdkVersion());
  165. DEBUG_MSG_P(PSTR("Core version: %s\n"), ESP.getCoreVersion().c_str());
  166. DEBUG_MSG_P(PSTR("\n"));
  167. FlashMode_t mode = ESP.getFlashChipMode();
  168. DEBUG_MSG_P(PSTR("Flash chip ID: 0x%06X\n"), ESP.getFlashChipId());
  169. DEBUG_MSG_P(PSTR("Flash speed: %u Hz\n"), ESP.getFlashChipSpeed());
  170. 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");
  171. DEBUG_MSG_P(PSTR("\n"));
  172. DEBUG_MSG_P(PSTR("Flash sector size: %8u bytes\n"), SPI_FLASH_SEC_SIZE);
  173. DEBUG_MSG_P(PSTR("Flash size (CHIP): %8u bytes\n"), ESP.getFlashChipRealSize());
  174. DEBUG_MSG_P(PSTR("Flash size (SDK): %8u bytes / %4d sectors\n"), ESP.getFlashChipSize(), sectors(ESP.getFlashChipSize()));
  175. DEBUG_MSG_P(PSTR("Firmware size: %8u bytes / %4d sectors\n"), ESP.getSketchSize(), sectors(ESP.getSketchSize()));
  176. DEBUG_MSG_P(PSTR("OTA size: %8u bytes / %4d sectors\n"), ESP.getFreeSketchSpace(), sectors(ESP.getFreeSketchSpace()));
  177. #if SPIFFS_SUPPORT
  178. FSInfo fs_info;
  179. bool fs = SPIFFS.info(fs_info);
  180. if (fs) {
  181. DEBUG_MSG_P(PSTR("SPIFFS size: %8u bytes / %4d sectors\n"), fs_info.totalBytes, sectors(fs_info.totalBytes));
  182. }
  183. #else
  184. DEBUG_MSG_P(PSTR("SPIFFS size: %8u bytes / %4d sectors\n"), 0, 0);
  185. #endif
  186. DEBUG_MSG_P(PSTR("EEPROM size: %8u bytes / %4d sectors\n"), settingsMaxSize(), sectors(settingsMaxSize()));
  187. DEBUG_MSG_P(PSTR("Empty space: %8u bytes / 4 sectors\n"), 4 * SPI_FLASH_SEC_SIZE);
  188. #if SPIFFS_SUPPORT
  189. if (fs) {
  190. DEBUG_MSG_P(PSTR("\n"));
  191. DEBUG_MSG_P(PSTR("SPIFFS total size: %8u bytes\n"), fs_info.totalBytes);
  192. DEBUG_MSG_P(PSTR(" used size: %8u bytes\n"), fs_info.usedBytes);
  193. DEBUG_MSG_P(PSTR(" block size: %8u bytes\n"), fs_info.blockSize);
  194. DEBUG_MSG_P(PSTR(" page size: %8u bytes\n"), fs_info.pageSize);
  195. DEBUG_MSG_P(PSTR(" max files: %8u\n"), fs_info.maxOpenFiles);
  196. DEBUG_MSG_P(PSTR(" max length: %8u\n"), fs_info.maxPathLength);
  197. }
  198. #endif
  199. DEBUG_MSG_P(PSTR("\n"));
  200. unsigned char custom_reset = customReset();
  201. if (custom_reset > 0) {
  202. char buffer[32];
  203. strcpy_P(buffer, custom_reset_string[custom_reset-1]);
  204. DEBUG_MSG_P(PSTR("Last reset reason: %s\n"), buffer);
  205. } else {
  206. DEBUG_MSG_P(PSTR("Last reset reason: %s\n"), (char *) ESP.getResetReason().c_str());
  207. }
  208. DEBUG_MSG_P(PSTR("Free heap: %u bytes\n"), ESP.getFreeHeap());
  209. DEBUG_MSG_P(PSTR("\n\n"));
  210. }
  211. void setup() {
  212. hardwareSetup();
  213. welcome();
  214. settingsSetup();
  215. if (getSetting("hostname").length() == 0) {
  216. setSetting("hostname", getIdentifier());
  217. saveSettings();
  218. }
  219. #if WEB_SUPPORT
  220. webSetup();
  221. #endif
  222. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  223. lightSetup();
  224. #endif
  225. relaySetup();
  226. buttonSetup();
  227. ledSetup();
  228. delay(500);
  229. wifiSetup();
  230. otaSetup();
  231. mqttSetup();
  232. #ifdef ITEAD_SONOFF_RFBRIDGE
  233. rfbSetup();
  234. #endif
  235. #if NTP_SUPPORT
  236. ntpSetup();
  237. #endif
  238. #if I2C_SUPPORT
  239. i2cSetup();
  240. #endif
  241. #if ALEXA_SUPPORT
  242. alexaSetup();
  243. #endif
  244. #if NOFUSS_SUPPORT
  245. nofussSetup();
  246. #endif
  247. #if INFLUXDB_SUPPORT
  248. influxDBSetup();
  249. #endif
  250. #if HLW8012_SUPPORT
  251. hlw8012Setup();
  252. #endif
  253. #if DS18B20_SUPPORT
  254. dsSetup();
  255. #endif
  256. #if ANALOG_SUPPORT
  257. analogSetup();
  258. #endif
  259. #if DHT_SUPPORT
  260. dhtSetup();
  261. #endif
  262. #if RF_SUPPORT
  263. rfSetup();
  264. #endif
  265. #if EMON_SUPPORT
  266. powerMonitorSetup();
  267. #endif
  268. #if DOMOTICZ_SUPPORT
  269. domoticzSetup();
  270. #endif
  271. // Prepare configuration for version 2.0
  272. hwUpwardsCompatibility();
  273. }
  274. void loop() {
  275. hardwareLoop();
  276. buttonLoop();
  277. relayLoop();
  278. ledLoop();
  279. wifiLoop();
  280. otaLoop();
  281. mqttLoop();
  282. #ifdef ITEAD_SONOFF_RFBRIDGE
  283. rfbLoop();
  284. #endif
  285. #if NTP_SUPPORT
  286. ntpLoop();
  287. #endif
  288. #if TERMINAL_SUPPORT
  289. settingsLoop();
  290. #endif
  291. #if ALEXA_SUPPORT
  292. alexaLoop();
  293. #endif
  294. #if NOFUSS_SUPPORT
  295. nofussLoop();
  296. #endif
  297. #if HLW8012_SUPPORT
  298. hlw8012Loop();
  299. #endif
  300. #if DS18B20_SUPPORT
  301. dsLoop();
  302. #endif
  303. #if ANALOG_SUPPORT
  304. analogLoop();
  305. #endif
  306. #if DHT_SUPPORT
  307. dhtLoop();
  308. #endif
  309. #if RF_SUPPORT
  310. rfLoop();
  311. #endif
  312. #if EMON_SUPPORT
  313. powerMonitorLoop();
  314. #endif
  315. }