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.

591 lines
18 KiB

6 years ago
6 years ago
  1. /*
  2. UTILS MODULE
  3. Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #include <Ticker.h>
  6. Ticker _defer_reset;
  7. String getIdentifier() {
  8. char buffer[20];
  9. snprintf_P(buffer, sizeof(buffer), PSTR("%s-%06X"), APP_NAME, ESP.getChipId());
  10. return String(buffer);
  11. }
  12. void setDefaultHostname() {
  13. if (strlen(HOSTNAME) > 0) {
  14. setSetting("hostname", HOSTNAME);
  15. } else {
  16. setSetting("hostname", getIdentifier());
  17. }
  18. }
  19. void setBoardName() {
  20. #ifndef ESPURNA_CORE
  21. setSetting("boardName", DEVICE_NAME);
  22. #endif
  23. }
  24. String getBoardName() {
  25. return getSetting("boardName", DEVICE_NAME);
  26. }
  27. String getCoreVersion() {
  28. String version = ESP.getCoreVersion();
  29. #ifdef ARDUINO_ESP8266_RELEASE
  30. if (version.equals("00000000")) {
  31. version = String(ARDUINO_ESP8266_RELEASE);
  32. }
  33. #endif
  34. version.replace("_", ".");
  35. return version;
  36. }
  37. String getCoreRevision() {
  38. #ifdef ARDUINO_ESP8266_GIT_VER
  39. return String(ARDUINO_ESP8266_GIT_VER);
  40. #else
  41. return String("");
  42. #endif
  43. }
  44. unsigned long maxSketchSpace() {
  45. return (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
  46. }
  47. // WTF
  48. // Calling ESP.getFreeHeap() is making the system crash on a specific
  49. // AiLight bulb, but anywhere else...
  50. unsigned int getFreeHeap() {
  51. if (getSetting("wtfHeap", 0).toInt() == 1) return 9999;
  52. return ESP.getFreeHeap();
  53. }
  54. String buildTime() {
  55. const char time_now[] = __TIME__; // hh:mm:ss
  56. unsigned int hour = atoi(&time_now[0]);
  57. unsigned int minute = atoi(&time_now[3]);
  58. unsigned int second = atoi(&time_now[6]);
  59. const char date_now[] = __DATE__; // Mmm dd yyyy
  60. const char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  61. unsigned int month = 0;
  62. for ( int i = 0; i < 12; i++ ) {
  63. if (strncmp(date_now, months[i], 3) == 0 ) {
  64. month = i + 1;
  65. break;
  66. }
  67. }
  68. unsigned int day = atoi(&date_now[3]);
  69. unsigned int year = atoi(&date_now[7]);
  70. char buffer[20];
  71. snprintf_P(
  72. buffer, sizeof(buffer), PSTR("%04d-%02d-%02d %02d:%02d:%02d"),
  73. year, month, day, hour, minute, second
  74. );
  75. return String(buffer);
  76. }
  77. unsigned long getUptime() {
  78. static unsigned long last_uptime = 0;
  79. static unsigned char uptime_overflows = 0;
  80. if (millis() < last_uptime) ++uptime_overflows;
  81. last_uptime = millis();
  82. unsigned long uptime_seconds = uptime_overflows * (UPTIME_OVERFLOW / 1000) + (last_uptime / 1000);
  83. return uptime_seconds;
  84. }
  85. #if HEARTBEAT_ENABLED
  86. void heartbeat() {
  87. unsigned long uptime_seconds = getUptime();
  88. unsigned int free_heap = getFreeHeap();
  89. #if MQTT_SUPPORT
  90. bool serial = !mqttConnected();
  91. #else
  92. bool serial = true;
  93. #endif
  94. // -------------------------------------------------------------------------
  95. // Serial
  96. // -------------------------------------------------------------------------
  97. if (serial) {
  98. DEBUG_MSG_P(PSTR("[MAIN] Uptime: %lu seconds\n"), uptime_seconds);
  99. DEBUG_MSG_P(PSTR("[MAIN] Free heap: %lu bytes\n"), free_heap);
  100. #if ADC_MODE_VALUE == ADC_VCC
  101. DEBUG_MSG_P(PSTR("[MAIN] Power: %lu mV\n"), ESP.getVcc());
  102. #endif
  103. #if NTP_SUPPORT
  104. if (ntpSynced()) DEBUG_MSG_P(PSTR("[MAIN] Time: %s\n"), (char *) ntpDateTime().c_str());
  105. #endif
  106. }
  107. // -------------------------------------------------------------------------
  108. // MQTT
  109. // -------------------------------------------------------------------------
  110. #if MQTT_SUPPORT
  111. if (!serial) {
  112. #if (HEARTBEAT_REPORT_INTERVAL)
  113. mqttSend(MQTT_TOPIC_INTERVAL, HEARTBEAT_INTERVAL / 1000);
  114. #endif
  115. #if (HEARTBEAT_REPORT_APP)
  116. mqttSend(MQTT_TOPIC_APP, APP_NAME);
  117. #endif
  118. #if (HEARTBEAT_REPORT_VERSION)
  119. mqttSend(MQTT_TOPIC_VERSION, APP_VERSION);
  120. #endif
  121. #if (HEARTBEAT_REPORT_BOARD)
  122. mqttSend(MQTT_TOPIC_BOARD, getBoardName().c_str());
  123. #endif
  124. #if (HEARTBEAT_REPORT_HOSTNAME)
  125. mqttSend(MQTT_TOPIC_HOSTNAME, getSetting("hostname").c_str());
  126. #endif
  127. #if (HEARTBEAT_REPORT_IP)
  128. mqttSend(MQTT_TOPIC_IP, getIP().c_str());
  129. #endif
  130. #if (HEARTBEAT_REPORT_MAC)
  131. mqttSend(MQTT_TOPIC_MAC, WiFi.macAddress().c_str());
  132. #endif
  133. #if (HEARTBEAT_REPORT_RSSI)
  134. mqttSend(MQTT_TOPIC_RSSI, String(WiFi.RSSI()).c_str());
  135. #endif
  136. #if (HEARTBEAT_REPORT_UPTIME)
  137. mqttSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str());
  138. #endif
  139. #if (HEARTBEAT_REPORT_DATETIME) && (NTP_SUPPORT)
  140. if (ntpSynced()) mqttSend(MQTT_TOPIC_DATETIME, ntpDateTime().c_str());
  141. #endif
  142. #if (HEARTBEAT_REPORT_FREEHEAP)
  143. mqttSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str());
  144. #endif
  145. #if (HEARTBEAT_REPORT_RELAY)
  146. relayMQTT();
  147. #endif
  148. #if (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE) & (HEARTBEAT_REPORT_LIGHT)
  149. lightMQTT();
  150. #endif
  151. #if (HEARTBEAT_REPORT_VCC)
  152. #if ADC_MODE_VALUE == ADC_VCC
  153. mqttSend(MQTT_TOPIC_VCC, String(ESP.getVcc()).c_str());
  154. #endif
  155. #endif
  156. #if (HEARTBEAT_REPORT_STATUS)
  157. mqttSend(MQTT_TOPIC_STATUS, MQTT_STATUS_ONLINE, true);
  158. #endif
  159. #if (LOADAVG_REPORT)
  160. mqttSend(MQTT_TOPIC_LOADAVG, String(systemLoadAverage()).c_str());
  161. #endif
  162. }
  163. #endif
  164. // -------------------------------------------------------------------------
  165. // InfluxDB
  166. // -------------------------------------------------------------------------
  167. #if INFLUXDB_SUPPORT
  168. #if (HEARTBEAT_REPORT_UPTIME)
  169. idbSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str());
  170. #endif
  171. #if (HEARTBEAT_REPORT_FREEHEAP)
  172. idbSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str());
  173. #endif
  174. #endif
  175. }
  176. #endif /// HEARTBEAT_ENABLED
  177. // -----------------------------------------------------------------------------
  178. // INFO
  179. // -----------------------------------------------------------------------------
  180. extern "C" uint32_t _SPIFFS_start;
  181. extern "C" uint32_t _SPIFFS_end;
  182. unsigned int info_bytes2sectors(size_t size) {
  183. return (int) (size + SPI_FLASH_SEC_SIZE - 1) / SPI_FLASH_SEC_SIZE;
  184. }
  185. unsigned long info_ota_space() {
  186. return (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
  187. }
  188. unsigned long info_filesystem_space() {
  189. return ((uint32_t)&_SPIFFS_end - (uint32_t)&_SPIFFS_start);
  190. }
  191. unsigned long info_eeprom_space() {
  192. return EEPROMr.reserved() * SPI_FLASH_SEC_SIZE;
  193. }
  194. void _info_print_memory_layout_line(const char * name, unsigned long bytes, bool reset) {
  195. static unsigned long index = 0;
  196. if (reset) index = 0;
  197. if (0 == bytes) return;
  198. unsigned int _sectors = info_bytes2sectors(bytes);
  199. DEBUG_MSG_P(PSTR("[INIT] %-20s: %8lu bytes / %4d sectors (%4d to %4d)\n"), name, bytes, _sectors, index, index + _sectors - 1);
  200. index += _sectors;
  201. }
  202. void _info_print_memory_layout_line(const char * name, unsigned long bytes) {
  203. _info_print_memory_layout_line(name, bytes, false);
  204. }
  205. void info() {
  206. DEBUG_MSG_P(PSTR("\n\n"));
  207. DEBUG_MSG_P(PSTR("[INIT] %s %s\n"), (char *) APP_NAME, (char *) APP_VERSION);
  208. DEBUG_MSG_P(PSTR("[INIT] %s\n"), (char *) APP_AUTHOR);
  209. DEBUG_MSG_P(PSTR("[INIT] %s\n\n"), (char *) APP_WEBSITE);
  210. DEBUG_MSG_P(PSTR("[INIT] CPU chip ID: 0x%06X\n"), ESP.getChipId());
  211. DEBUG_MSG_P(PSTR("[INIT] CPU frequency: %u MHz\n"), ESP.getCpuFreqMHz());
  212. DEBUG_MSG_P(PSTR("[INIT] SDK version: %s\n"), ESP.getSdkVersion());
  213. DEBUG_MSG_P(PSTR("[INIT] Core version: %s\n"), getCoreVersion().c_str());
  214. DEBUG_MSG_P(PSTR("[INIT] Core revision: %s\n"), getCoreRevision().c_str());
  215. DEBUG_MSG_P(PSTR("\n"));
  216. // -------------------------------------------------------------------------
  217. FlashMode_t mode = ESP.getFlashChipMode();
  218. DEBUG_MSG_P(PSTR("[INIT] Flash chip ID: 0x%06X\n"), ESP.getFlashChipId());
  219. DEBUG_MSG_P(PSTR("[INIT] Flash speed: %u Hz\n"), ESP.getFlashChipSpeed());
  220. DEBUG_MSG_P(PSTR("[INIT] Flash mode: %s\n"), mode == FM_QIO ? "QIO" : mode == FM_QOUT ? "QOUT" : mode == FM_DIO ? "DIO" : mode == FM_DOUT ? "DOUT" : "UNKNOWN");
  221. DEBUG_MSG_P(PSTR("\n"));
  222. _info_print_memory_layout_line("Flash size (CHIP)", ESP.getFlashChipRealSize(), true);
  223. _info_print_memory_layout_line("Flash size (SDK)", ESP.getFlashChipSize(), true);
  224. _info_print_memory_layout_line("Reserved", 1 * SPI_FLASH_SEC_SIZE, true);
  225. _info_print_memory_layout_line("Firmware size", ESP.getSketchSize());
  226. _info_print_memory_layout_line("Max OTA size", info_ota_space());
  227. _info_print_memory_layout_line("SPIFFS size", info_filesystem_space());
  228. _info_print_memory_layout_line("EEPROM size", info_eeprom_space());
  229. _info_print_memory_layout_line("Reserved", 4 * SPI_FLASH_SEC_SIZE);
  230. DEBUG_MSG_P(PSTR("\n"));
  231. DEBUG_MSG_P(PSTR("[INIT] EEPROM sectors: %s\n"), (char *) eepromSectors().c_str());
  232. DEBUG_MSG_P(PSTR("\n"));
  233. // -------------------------------------------------------------------------
  234. #if SPIFFS_SUPPORT
  235. FSInfo fs_info;
  236. bool fs = SPIFFS.info(fs_info);
  237. if (fs) {
  238. DEBUG_MSG_P(PSTR("[INIT] SPIFFS total size: %8u bytes / %4d sectors\n"), fs_info.totalBytes, sectors(fs_info.totalBytes));
  239. DEBUG_MSG_P(PSTR("[INIT] used size: %8u bytes\n"), fs_info.usedBytes);
  240. DEBUG_MSG_P(PSTR("[INIT] block size: %8u bytes\n"), fs_info.blockSize);
  241. DEBUG_MSG_P(PSTR("[INIT] page size: %8u bytes\n"), fs_info.pageSize);
  242. DEBUG_MSG_P(PSTR("[INIT] max files: %8u\n"), fs_info.maxOpenFiles);
  243. DEBUG_MSG_P(PSTR("[INIT] max length: %8u\n"), fs_info.maxPathLength);
  244. } else {
  245. DEBUG_MSG_P(PSTR("[INIT] No SPIFFS partition\n"));
  246. }
  247. DEBUG_MSG_P(PSTR("\n"));
  248. #endif
  249. // -------------------------------------------------------------------------
  250. DEBUG_MSG_P(PSTR("[INIT] BOARD: %s\n"), getBoardName().c_str());
  251. DEBUG_MSG_P(PSTR("[INIT] SUPPORT:"));
  252. #if ALEXA_SUPPORT
  253. DEBUG_MSG_P(PSTR(" ALEXA"));
  254. #endif
  255. #if BROKER_SUPPORT
  256. DEBUG_MSG_P(PSTR(" BROKER"));
  257. #endif
  258. #if DEBUG_SERIAL_SUPPORT
  259. DEBUG_MSG_P(PSTR(" DEBUG_SERIAL"));
  260. #endif
  261. #if DEBUG_TELNET_SUPPORT
  262. DEBUG_MSG_P(PSTR(" DEBUG_TELNET"));
  263. #endif
  264. #if DEBUG_UDP_SUPPORT
  265. DEBUG_MSG_P(PSTR(" DEBUG_UDP"));
  266. #endif
  267. #if DEBUG_WEB_SUPPORT
  268. DEBUG_MSG_P(PSTR(" DEBUG_WEB"));
  269. #endif
  270. #if DOMOTICZ_SUPPORT
  271. DEBUG_MSG_P(PSTR(" DOMOTICZ"));
  272. #endif
  273. #if HOMEASSISTANT_SUPPORT
  274. DEBUG_MSG_P(PSTR(" HOMEASSISTANT"));
  275. #endif
  276. #if I2C_SUPPORT
  277. DEBUG_MSG_P(PSTR(" I2C"));
  278. #endif
  279. #if INFLUXDB_SUPPORT
  280. DEBUG_MSG_P(PSTR(" INFLUXDB"));
  281. #endif
  282. #if LLMNR_SUPPORT
  283. DEBUG_MSG_P(PSTR(" LLMNR"));
  284. #endif
  285. #if MDNS_SERVER_SUPPORT
  286. DEBUG_MSG_P(PSTR(" MDNS_SERVER"));
  287. #endif
  288. #if MDNS_CLIENT_SUPPORT
  289. DEBUG_MSG_P(PSTR(" MDNS_CLIENT"));
  290. #endif
  291. #if MQTT_SUPPORT
  292. DEBUG_MSG_P(PSTR(" MQTT"));
  293. #endif
  294. #if NETBIOS_SUPPORT
  295. DEBUG_MSG_P(PSTR(" NETBIOS"));
  296. #endif
  297. #if NOFUSS_SUPPORT
  298. DEBUG_MSG_P(PSTR(" NOFUSS"));
  299. #endif
  300. #if NTP_SUPPORT
  301. DEBUG_MSG_P(PSTR(" NTP"));
  302. #endif
  303. #if RF_SUPPORT
  304. DEBUG_MSG_P(PSTR(" RF"));
  305. #endif
  306. #if SCHEDULER_SUPPORT
  307. DEBUG_MSG_P(PSTR(" SCHEDULER"));
  308. #endif
  309. #if SENSOR_SUPPORT
  310. DEBUG_MSG_P(PSTR(" SENSOR"));
  311. #endif
  312. #if SPIFFS_SUPPORT
  313. DEBUG_MSG_P(PSTR(" SPIFFS"));
  314. #endif
  315. #if SSDP_SUPPORT
  316. DEBUG_MSG_P(PSTR(" SSDP"));
  317. #endif
  318. #if TELNET_SUPPORT
  319. DEBUG_MSG_P(PSTR(" TELNET"));
  320. #endif
  321. #if TERMINAL_SUPPORT
  322. DEBUG_MSG_P(PSTR(" TERMINAL"));
  323. #endif
  324. #if THINGSPEAK_SUPPORT
  325. DEBUG_MSG_P(PSTR(" THINGSPEAK"));
  326. #endif
  327. #if UART_MQTT_SUPPORT
  328. DEBUG_MSG_P(PSTR(" UART_MQTT"));
  329. #endif
  330. #if WEB_SUPPORT
  331. DEBUG_MSG_P(PSTR(" WEB"));
  332. #endif
  333. #if SENSOR_SUPPORT
  334. DEBUG_MSG_P(PSTR("\n"));
  335. DEBUG_MSG_P(PSTR("[INIT] SENSORS:"));
  336. #if AM2320_SUPPORT
  337. DEBUG_MSG_P(PSTR(" AM2320_I2C"));
  338. #endif
  339. #if ANALOG_SUPPORT
  340. DEBUG_MSG_P(PSTR(" ANALOG"));
  341. #endif
  342. #if BH1750_SUPPORT
  343. DEBUG_MSG_P(PSTR(" BH1750"));
  344. #endif
  345. #if BMX280_SUPPORT
  346. DEBUG_MSG_P(PSTR(" BMX280"));
  347. #endif
  348. #if CSE7766_SUPPORT
  349. DEBUG_MSG_P(PSTR(" CSE7766"));
  350. #endif
  351. #if DALLAS_SUPPORT
  352. DEBUG_MSG_P(PSTR(" DALLAS"));
  353. #endif
  354. #if DHT_SUPPORT
  355. DEBUG_MSG_P(PSTR(" DHTXX"));
  356. #endif
  357. #if DIGITAL_SUPPORT
  358. DEBUG_MSG_P(PSTR(" DIGITAL"));
  359. #endif
  360. #if ECH1560_SUPPORT
  361. DEBUG_MSG_P(PSTR(" ECH1560"));
  362. #endif
  363. #if EMON_ADC121_SUPPORT
  364. DEBUG_MSG_P(PSTR(" EMON_ADC121"));
  365. #endif
  366. #if EMON_ADS1X15_SUPPORT
  367. DEBUG_MSG_P(PSTR(" EMON_ADX1X15"));
  368. #endif
  369. #if EMON_ANALOG_SUPPORT
  370. DEBUG_MSG_P(PSTR(" EMON_ANALOG"));
  371. #endif
  372. #if EVENTS_SUPPORT
  373. DEBUG_MSG_P(PSTR(" EVENTS"));
  374. #endif
  375. #if GUVAS12SD_SUPPORT
  376. DEBUG_MSG_P(PSTR(" GUVAS12SD"));
  377. #endif
  378. #if HCSR04_SUPPORT
  379. DEBUG_MSG_P(PSTR(" HCSR04"));
  380. #endif
  381. #if HLW8012_SUPPORT
  382. DEBUG_MSG_P(PSTR(" HLW8012"));
  383. #endif
  384. #if MHZ19_SUPPORT
  385. DEBUG_MSG_P(PSTR(" MHZ19"));
  386. #endif
  387. #if PMSX003_SUPPORT
  388. DEBUG_MSG_P(PSTR(" PMSX003"));
  389. #endif
  390. #if PZEM004T_SUPPORT
  391. DEBUG_MSG_P(PSTR(" PZEM004T"));
  392. #endif
  393. #if SENSEAIR_SUPPORT
  394. DEBUG_MSG_P(PSTR(" SENSEAIR"));
  395. #endif
  396. #if SHT3X_I2C_SUPPORT
  397. DEBUG_MSG_P(PSTR(" SHT3X_I2C"));
  398. #endif
  399. #if SI7021_SUPPORT
  400. DEBUG_MSG_P(PSTR(" SI7021"));
  401. #endif
  402. #if TMP3X_SUPPORT
  403. DEBUG_MSG_P(PSTR(" TMP3X"));
  404. #endif
  405. #if V9261F_SUPPORT
  406. DEBUG_MSG_P(PSTR(" V9261F"));
  407. #endif
  408. #endif // SENSOR_SUPPORT
  409. DEBUG_MSG_P(PSTR("\n\n"));
  410. // -------------------------------------------------------------------------
  411. unsigned char reason = resetReason();
  412. if (reason > 0) {
  413. char buffer[32];
  414. strcpy_P(buffer, custom_reset_string[reason-1]);
  415. DEBUG_MSG_P(PSTR("[INIT] Last reset reason: %s\n"), buffer);
  416. } else {
  417. DEBUG_MSG_P(PSTR("[INIT] Last reset reason: %s\n"), (char *) ESP.getResetReason().c_str());
  418. }
  419. DEBUG_MSG_P(PSTR("[INIT] Settings size: %u bytes\n"), settingsSize());
  420. DEBUG_MSG_P(PSTR("[INIT] Free heap: %u bytes\n"), getFreeHeap());
  421. #if ADC_MODE_VALUE == ADC_VCC
  422. DEBUG_MSG_P(PSTR("[INIT] Power: %u mV\n"), ESP.getVcc());
  423. #endif
  424. DEBUG_MSG_P(PSTR("[INIT] Power saving delay value: %lu ms\n"), systemLoopDelay());
  425. #if SYSTEM_CHECK_ENABLED
  426. if (!systemCheck()) DEBUG_MSG_P(PSTR("\n[INIT] Device is in SAFE MODE\n"));
  427. #endif
  428. DEBUG_MSG_P(PSTR("\n"));
  429. }
  430. // -----------------------------------------------------------------------------
  431. // SSL
  432. // -----------------------------------------------------------------------------
  433. #if ASYNC_TCP_SSL_ENABLED
  434. bool sslCheckFingerPrint(const char * fingerprint) {
  435. return (strlen(fingerprint) == 59);
  436. }
  437. bool sslFingerPrintArray(const char * fingerprint, unsigned char * bytearray) {
  438. // check length (20 2-character digits ':' or ' ' separated => 20*2+19 = 59)
  439. if (!sslCheckFingerPrint(fingerprint)) return false;
  440. // walk the fingerprint
  441. for (unsigned int i=0; i<20; i++) {
  442. bytearray[i] = strtol(fingerprint + 3*i, NULL, 16);
  443. }
  444. return true;
  445. }
  446. bool sslFingerPrintChar(const char * fingerprint, char * destination) {
  447. // check length (20 2-character digits ':' or ' ' separated => 20*2+19 = 59)
  448. if (!sslCheckFingerPrint(fingerprint)) return false;
  449. // copy it
  450. strncpy(destination, fingerprint, 59);
  451. // walk the fingerprint replacing ':' for ' '
  452. for (unsigned char i = 0; i<59; i++) {
  453. if (destination[i] == ':') destination[i] = ' ';
  454. }
  455. return true;
  456. }
  457. #endif
  458. // -----------------------------------------------------------------------------
  459. // Reset
  460. // -----------------------------------------------------------------------------
  461. unsigned char resetReason() {
  462. static unsigned char status = 255;
  463. if (status == 255) {
  464. status = EEPROMr.read(EEPROM_CUSTOM_RESET);
  465. if (status > 0) resetReason(0);
  466. if (status > CUSTOM_RESET_MAX) status = 0;
  467. }
  468. return status;
  469. }
  470. void resetReason(unsigned char reason) {
  471. EEPROMr.write(EEPROM_CUSTOM_RESET, reason);
  472. EEPROMr.commit();
  473. }
  474. void reset(unsigned char reason) {
  475. resetReason(reason);
  476. ESP.restart();
  477. }
  478. void deferredReset(unsigned long delay, unsigned char reason) {
  479. _defer_reset.once_ms(delay, reset, reason);
  480. }
  481. // -----------------------------------------------------------------------------
  482. char * ltrim(char * s) {
  483. char *p = s;
  484. while ((unsigned char) *p == ' ') ++p;
  485. return p;
  486. }
  487. double roundTo(double num, unsigned char positions) {
  488. double multiplier = 1;
  489. while (positions-- > 0) multiplier *= 10;
  490. return round(num * multiplier) / multiplier;
  491. }
  492. void nice_delay(unsigned long ms) {
  493. unsigned long start = millis();
  494. while (millis() - start < ms) delay(1);
  495. }
  496. // This method is called by the SDK to know where to connect the ADC
  497. int __get_adc_mode() {
  498. return (int) (ADC_MODE_VALUE);
  499. }