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.

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