httpServer.ino 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927
  1. #include "html.h"
  2. #include "html_conf.h"
  3. #include "html_confAdd.h"
  4. #include "html_confAdv.h"
  5. #include "html_confBas.h"
  6. #include "html_confDevWiFi.h"
  7. #include "html_confMqtt.h"
  8. #include "html_confWeb.h"
  9. #include "html_confTime.h"
  10. #include "html_confLog.h"
  11. #include "html_main.h"
  12. #include "html_redTemps.h"
  13. #include "html_sysinfo.h"
  14. #ifdef ENABLE_FEATURE_WEB_CONSOLE_WEBSOCKETS
  15. #include "html_console_ws.h"
  16. #endif
  17. #ifdef ENABLE_FEATURE_WEB_CONSOLE
  18. #include "html_console.h"
  19. #endif
  20. void httpServerHandleMainPage()
  21. {
  22. httpServerSendHtmlHeadChunked();
  23. httpServer.sendContent_P(html_main_script);
  24. httpServer.sendContent_P(html_head_end);
  25. httpServer.sendContent_P(html_bodytag_jsinit);
  26. httpServerSendHtmlBodyPageheadChunked();
  27. httpServer.sendContent_P(html_main_body);
  28. if (httpIsAuthenticatedAdmin())
  29. {
  30. httpServer.sendContent_P(html_main_body_adminonly);
  31. #ifdef ENABLE_FEATURE_WEB_CONSOLE
  32. if (confWeb.wConsole)
  33. httpServer.sendContent_P(html_main_body_adminonly_console);
  34. #endif
  35. #ifdef ENABLE_FEATURE_WEB_CONSOLE_WEBSOCKETS
  36. if (confWeb.wsConsole)
  37. httpServer.sendContent_P(html_main_body_adminonly_wsconsole);
  38. #endif
  39. }
  40. httpServer.sendContent_P(html_main_body2);
  41. httpServerSendHtmlFooterChunked();
  42. }
  43. void httpServerHandleRedTempsPage()
  44. {
  45. httpServerSendHtmlHeadChunked();
  46. httpServer.sendContent_P(html_redTemps_script);
  47. httpServer.sendContent_P(html_head_end);
  48. httpServer.sendContent_P(html_bodytag_jsinit);
  49. httpServerSendHtmlBodyPageheadChunked();
  50. httpServer.sendContent_P(html_redTemps_body);
  51. httpServerSendHtmlFooterChunked();
  52. }
  53. void httpServerHandleConfPage()
  54. {
  55. httpServerSendHtmlHeadChunked();
  56. httpServer.sendContent_P(html_head_end);
  57. httpServer.sendContent_P(html_bodytag);
  58. httpServerSendHtmlBodyPageheadChunked();
  59. httpServer.sendContent_P(html_conf_body);
  60. #ifdef ENABLE_FEATURE_WEB_CONSOLE
  61. if (confWeb.wConsole)
  62. httpServer.sendContent_P(html_conf_body_console);
  63. #endif
  64. #ifdef ENABLE_FEATURE_WEB_CONSOLE_WEBSOCKETS
  65. if (confWeb.wsConsole)
  66. httpServer.sendContent_P(html_conf_body_wsconsole);
  67. #endif
  68. httpServer.sendContent_P(html_conf_body_end);
  69. httpServerSendHtmlFooterChunked();
  70. }
  71. void httpServerHandleConfWebPage()
  72. {
  73. httpServerSendHtmlHeadChunked();
  74. httpServer.sendContent_P(html_confweb_script);
  75. httpServer.sendContent_P(html_head_end);
  76. httpServer.sendContent_P(html_bodytag_jsinit);
  77. httpServerSendHtmlBodyPageheadChunked();
  78. httpServer.sendContent_P(html_confweb_body);
  79. #ifdef ENABLE_FEATURE_WEB_CONSOLE_WEBSOCKETS
  80. httpServer.sendContent_P(html_confweb_body_wsconsole);
  81. #endif
  82. httpServer.sendContent_P(html_confweb_body2);
  83. httpServerSendHtmlFooterChunked();
  84. }
  85. void httpServerHandleConfMqttPage()
  86. {
  87. httpServerSendHtmlHeadChunked();
  88. httpServer.sendContent_P(html_confmqtt_script);
  89. httpServer.sendContent_P(html_head_end);
  90. httpServer.sendContent_P(html_bodytag_jsinit);
  91. httpServerSendHtmlBodyPageheadChunked();
  92. httpServer.sendContent_P(html_confmqtt_body);
  93. httpServerSendHtmlFooterChunked();
  94. }
  95. void httpServerHandleConfDevWiFiPage()
  96. {
  97. httpServerSendHtmlHeadChunked();
  98. httpServer.sendContent_P(html_confDevWiFi_script);
  99. httpServer.sendContent_P(html_head_end);
  100. httpServer.sendContent_P(html_bodytag_jsinit);
  101. httpServerSendHtmlBodyPageheadChunked();
  102. httpServer.sendContent_P(html_confDevWiFi_body);
  103. httpServerSendHtmlFooterChunked();
  104. }
  105. void httpServerHandleConfBasPage()
  106. {
  107. httpServerSendHtmlHeadChunked();
  108. httpServer.sendContent_P(html_confbas_script);
  109. httpServer.sendContent_P(html_head_end);
  110. httpServer.sendContent_P(html_bodytag_jsinit);
  111. httpServerSendHtmlBodyPageheadChunked();
  112. httpServer.sendContent_P(html_confbas_body);
  113. httpServerSendHtmlFooterChunked();
  114. }
  115. void httpServerHandleConfAdvPage()
  116. {
  117. httpServerSendHtmlHeadChunked();
  118. httpServer.sendContent_P(html_confadv_script);
  119. httpServer.sendContent_P(html_head_end);
  120. httpServer.sendContent_P(html_bodytag_jsinit);
  121. httpServerSendHtmlBodyPageheadChunked();
  122. httpServer.sendContent_P(html_confadv_body);
  123. httpServerSendHtmlFooterChunked();
  124. }
  125. void httpServerHandleConfAddPage()
  126. {
  127. httpServerSendHtmlHeadChunked();
  128. httpServer.sendContent_P(html_confadd_script);
  129. httpServer.sendContent_P(html_head_end);
  130. httpServer.sendContent_P(html_bodytag_jsinit);
  131. httpServerSendHtmlBodyPageheadChunked();
  132. httpServer.sendContent_P(html_confadd_body);
  133. httpServerSendHtmlFooterChunked();
  134. }
  135. void httpServerHandleConfTimePage()
  136. {
  137. httpServerSendHtmlHeadChunked();
  138. httpServer.sendContent_P(html_conftime_script);
  139. httpServer.sendContent_P(html_head_end);
  140. httpServer.sendContent_P(html_bodytag_jsinit);
  141. httpServerSendHtmlBodyPageheadChunked();
  142. httpServer.sendContent_P(html_conftime_body);
  143. httpServerSendHtmlFooterChunked();
  144. }
  145. void httpServerHandleConfLogPage()
  146. {
  147. httpServerSendHtmlHeadChunked();
  148. httpServer.sendContent_P(html_conflog_script);
  149. httpServer.sendContent_P(html_head_end);
  150. httpServer.sendContent_P(html_bodytag_jsinit);
  151. httpServerSendHtmlBodyPageheadChunked();
  152. httpServer.sendContent_P(html_conflog_body);
  153. httpServerSendHtmlFooterChunked();
  154. }
  155. #ifdef ENABLE_FEATURE_WEB_CONSOLE_WEBSOCKETS
  156. void httpServerHandleWSConsolePage()
  157. {
  158. httpServerSendHtmlHeadChunked();
  159. httpServer.sendContent_P(html_head_end);
  160. httpServer.sendContent_P(html_bodytag);
  161. httpServerSendHtmlBodyPageheadChunked();
  162. httpServer.sendContent_P(html_wsconsole_body);
  163. httpServerSendHtmlFooterChunked();
  164. }
  165. #endif
  166. void httpServerHandleConsolePage()
  167. {
  168. httpServerSendHtmlHeadChunked();
  169. httpServer.sendContent_P(html_head_end);
  170. httpServer.sendContent_P(html_bodytag);
  171. httpServerSendHtmlBodyPageheadChunked();
  172. httpServer.sendContent_P(html_console_body);
  173. httpServerSendHtmlFooterChunked();
  174. }
  175. void httpServerHandleConfSavedPage()
  176. {
  177. httpServerSendHtmlHeadChunked();
  178. httpServer.sendContent_P(html_head_end);
  179. httpServer.sendContent_P(html_bodytag);
  180. httpServerSendHtmlBodyPageheadChunked();
  181. httpServer.sendContent_P(html_confsaved_body);
  182. httpServerSendHtmlFooterChunked();
  183. }
  184. //void httpServerHandleConfSavedRestartPage() {
  185. // httpServerSendHtmlHeadChunked();
  186. // httpServer.sendContent_P(html_head_end);
  187. // httpServer.sendContent_P(html_bodytag);
  188. // httpServerSendHtmlBodyPageheadChunked();
  189. // httpServer.sendContent_P(html_confsavedrestart_body);
  190. // httpServerSendHtmlFooterChunked();
  191. //}
  192. void httpServerHandleRestartPage()
  193. {
  194. httpServerSendHtmlHeadChunked();
  195. httpServer.sendContent_P(html_head_end);
  196. httpServer.sendContent_P(html_bodytag);
  197. configChangedRestartRequired = false;
  198. httpServerSendHtmlBodyPageheadChunked();
  199. httpServer.sendContent_P(html_restarting_body);
  200. httpServerSendHtmlFooterChunked();
  201. }
  202. void httpServerHandleClearconfPage()
  203. {
  204. httpServerSendHtmlHeadChunked();
  205. httpServer.sendContent_P(html_head_end);
  206. httpServer.sendContent_P(html_bodytag);
  207. httpServerSendHtmlBodyPageheadChunked();
  208. httpServer.sendContent_P(html_clearconf_body);
  209. httpServerSendHtmlFooterChunked();
  210. }
  211. void httpServerHandleSysInfoPage()
  212. {
  213. httpServerSendHtmlHeadChunked();
  214. httpServer.sendContent_P(html_sysinfo_script);
  215. httpServer.sendContent_P(html_head_end);
  216. httpServer.sendContent_P(html_bodytag_jsinit);
  217. httpServerSendHtmlBodyPageheadChunked();
  218. httpServer.sendContent_P(html_sysinfo_body);
  219. httpServerSendHtmlFooterChunked();
  220. }
  221. void httpServerSendHtmlHeadChunked()
  222. {
  223. httpServer.setContentLength(CONTENT_LENGTH_UNKNOWN); //Enable Chunked Transfer
  224. httpServer.send(200, "text/html", html_head_part1);
  225. httpServer.sendContent_P(PGMStr_FIRMWARE_NAME);
  226. httpServer.sendContent(" - ");
  227. httpServer.sendContent(confDevWiFi.deviceName);
  228. httpServer.sendContent_P(html_head_part2);
  229. }
  230. void httpServerSendHtmlBodyPageheadChunked()
  231. {
  232. httpServer.sendContent_P(html_body_pagehead_part1);
  233. httpServer.sendContent_P(PGMStr_FIRMWARE_NAME);
  234. httpServer.sendContent(" - ");
  235. httpServer.sendContent(confDevWiFi.deviceName);
  236. httpServer.sendContent_P(html_body_pagehead_part2);
  237. if (configChangedRestartRequired)
  238. httpServer.sendContent_P(html_body_pagehead_confchangednote);
  239. }
  240. void httpServerSendHtmlFooterChunked()
  241. {
  242. httpServer.sendContent_P(html_footer1);
  243. httpServer.sendContent_P(html_footer_a1);
  244. httpServer.sendContent_P(PGMStr_FIRMWARE_URL);
  245. httpServer.sendContent_P(html_footer_a2);
  246. httpServer.sendContent_P(PGMStr_FIRMWARE_NAME);
  247. httpServer.sendContent_P(html_footer_ae);
  248. httpServer.sendContent(" ");
  249. httpServer.sendContent_P(PGMStr_FIRMWARE_VERSION);
  250. #ifdef DEBUGMODE
  251. httpServer.sendContent("&nbsp;<span style='color:red;font-weight:bold;'>DEBUG</span>");
  252. #endif
  253. httpServer.sendContent(" by ");
  254. httpServer.sendContent_P(html_footer_a1);
  255. httpServer.sendContent_P(PGMStr_FIRMWARE_COPYRIGHT_URL);
  256. httpServer.sendContent_P(html_footer_a2);
  257. httpServer.sendContent_P(PGMStr_FIRMWARE_COPYRIGHT);
  258. httpServer.sendContent_P(html_footer_ae);
  259. httpServer.sendContent_P(html_footer2);
  260. httpServer.sendContent("");
  261. httpServer.client().stop();
  262. }
  263. boolean httpIsAuthenticated()
  264. {
  265. if (WifiInApMode && strlen(confDevWiFi.WiFiAPModePassword) >= 8)
  266. return true;
  267. if (confWeb.http_user_auth)
  268. {
  269. boolean useAuth = false;
  270. boolean isAuthenticated = false;
  271. // user authentication enabled - only allow access without authentication if no admin password is set
  272. // or if user1 credentials are empty (access in "user mode")
  273. if (strlen(confWeb.http_pass) > 0 || strlen(confWeb.http_pass1) > 0 || strlen(confWeb.http_pass2) > 0)
  274. useAuth = true;
  275. if (strlen(confWeb.http_pass) == 0)
  276. {
  277. return true; // no admin password set - in this case authentication is off
  278. }
  279. if (useAuth)
  280. {
  281. // allow access if one set of valid credentials are given
  282. if ((strlen(confWeb.http_user) > 0 && strlen(confWeb.http_pass) > 0))
  283. {
  284. if (httpServer.authenticate(confWeb.http_user, confWeb.http_pass))
  285. isAuthenticated = true;
  286. }
  287. if ((strlen(confWeb.http_user1) > 0 && strlen(confWeb.http_pass1) > 0))
  288. {
  289. if (httpServer.authenticate(confWeb.http_user1, confWeb.http_pass1))
  290. isAuthenticated = true;
  291. }
  292. if ((strlen(confWeb.http_user2) > 0 && strlen(confWeb.http_pass2) > 0))
  293. {
  294. if (httpServer.authenticate(confWeb.http_user2, confWeb.http_pass2))
  295. isAuthenticated = true;
  296. }
  297. // also allow access if admin credentials are set but user1 password is empty and user-authentication is enabled
  298. if (strlen(confWeb.http_pass1) == 0)
  299. {
  300. isAuthenticated = true;
  301. }
  302. return isAuthenticated;
  303. }
  304. // allow access if all credentials are empty
  305. else
  306. return true;
  307. }
  308. // user authentication disabled - only allow access without password if no admin password is set
  309. else
  310. {
  311. if (strlen(confWeb.http_pass) > 0)
  312. {
  313. if (httpServer.authenticate(confWeb.http_user, confWeb.http_pass))
  314. return true;
  315. else
  316. return false;
  317. }
  318. else
  319. return true;
  320. }
  321. }
  322. boolean httpIsAuthenticatedAdmin()
  323. {
  324. if (WifiInApMode && strlen(confDevWiFi.WiFiAPModePassword) >= 8)
  325. return true;
  326. boolean auth = false;
  327. if ((strlen(confWeb.http_user) > 0 && strlen(confWeb.http_pass) > 0))
  328. auth = true;
  329. if (auth)
  330. {
  331. if (!httpServer.authenticate(confWeb.http_user, confWeb.http_pass))
  332. return false;
  333. else
  334. return true;
  335. }
  336. else
  337. return true;
  338. }
  339. void httpServerHandleNotFound()
  340. {
  341. httpServer.send(404, "text/plain", "404 NOT FOUND");
  342. }
  343. void httpSendUnauthorized()
  344. {
  345. httpServer.send(401, "text/plain", "UNAUTHORIZED");
  346. }
  347. void httpSend200OK()
  348. {
  349. httpServer.send(200, "text/plain", "OK");
  350. }
  351. boolean httpCheckToken()
  352. {
  353. if (confWeb.http_token[0] != '\0')
  354. { // dont accept empty token
  355. if (httpServer.hasArg("token"))
  356. {
  357. char buf[20];
  358. httpServer.arg("token").toCharArray(buf, 20);
  359. if (strcmp(buf, confWeb.http_token) == 0)
  360. return true;
  361. else
  362. return false;
  363. }
  364. else
  365. return false;
  366. }
  367. else
  368. return false;
  369. }
  370. void httpServerInit()
  371. {
  372. httpServer.on("/style.css", []() {
  373. httpServer.sendHeader("cache-control", "max-age=86400", false);
  374. httpServer.send_P(200, "text/css", html_stylesheet);
  375. });
  376. httpServer.on("/api", []() {
  377. if (!httpIsAuthenticated())
  378. return httpServer.requestAuthentication();
  379. else
  380. {
  381. if (confLog.logWebRequests || httpServer.args() > 0)
  382. {
  383. String addr = httpServer.client().remoteIP().toString();
  384. char buf[40];
  385. sprintf(buf, "WEB: /api (from %s)", addr.c_str());
  386. sendLog(buf, LOGLEVEL_INFO);
  387. }
  388. //sendLog("WEB: /api", LOGLEVEL_INFO);
  389. if (httpServer.hasArg("BtnPlus"))
  390. {
  391. setTempStepUp();
  392. sendLog(F("WEB: BtnPlus"), LOGLEVEL_INFO);
  393. } //if
  394. if (httpServer.hasArg("BtnMinus"))
  395. {
  396. setTempStepDown();
  397. sendLog(F("WEB: BtnMinus"), LOGLEVEL_INFO);
  398. } //if
  399. if (httpServer.hasArg("BtnPset0"))
  400. {
  401. setPresetTo(0);
  402. sendLog(F("WEB: BtnPset0"), LOGLEVEL_INFO);
  403. } //if
  404. if (httpServer.hasArg("BtnPset1"))
  405. {
  406. setPresetTo(1);
  407. sendLog(F("WEB: BtnPset1"), LOGLEVEL_INFO);
  408. } //if
  409. if (httpServer.hasArg("BtnPset2"))
  410. {
  411. setPresetTo(2);
  412. sendLog(F("WEB: BtnPset2"), LOGLEVEL_INFO);
  413. } //if
  414. if (httpServer.hasArg("BtnOn"))
  415. {
  416. setHeatingmodeTo(1);
  417. sendLog(F("WEB: BtnOn"), LOGLEVEL_INFO);
  418. } //if
  419. if (httpServer.hasArg("BtnOff"))
  420. {
  421. setHeatingmodeTo(0);
  422. sendLog(F("WEB: BtnOff"), LOGLEVEL_INFO);
  423. } //if
  424. if (httpServer.hasArg("BtnL1Plus"))
  425. {
  426. setTempLowStepUp();
  427. sendLog(F("WEB: BtnL1Plus"), LOGLEVEL_INFO);
  428. } //if
  429. if (httpServer.hasArg("BtnL1Minus"))
  430. {
  431. setTempLowStepDown();
  432. sendLog(F("WEB: BtnL1Minus"), LOGLEVEL_INFO);
  433. } //if
  434. if (httpServer.hasArg("BtnL2Plus"))
  435. {
  436. setTempLow2StepUp();
  437. sendLog(F("WEB: BtnL2Plus"), LOGLEVEL_INFO);
  438. } //if
  439. if (httpServer.hasArg("BtnL2Minus"))
  440. {
  441. setTempLow2StepDown();
  442. sendLog(F("WEB: BtnL2Minus"), LOGLEVEL_INFO);
  443. } //if
  444. if (httpServer.hasArg("setTemp"))
  445. {
  446. sendLog(F("WEB: /api?setTemp"), LOGLEVEL_INFO);
  447. char bufVal[20];
  448. httpServer.arg("setTemp").toCharArray(bufVal, 20);
  449. float valueFloat = round(atof(bufVal) * 2.0) / 2.0;
  450. setTempTo(valueFloat);
  451. }
  452. if (httpServer.hasArg("setPause"))
  453. {
  454. sendLog(F("WEB: /api?setPause"), LOGLEVEL_INFO);
  455. char bufVal[20];
  456. httpServer.arg("setPause").toCharArray(bufVal, 20);
  457. int valueInt = atoi(bufVal);
  458. setHeatingPause(valueInt);
  459. }
  460. if (httpServer.hasArg("setMode"))
  461. {
  462. sendLog(F("WEB: /api?setMode"), LOGLEVEL_INFO);
  463. char bufVal[20];
  464. httpServer.arg("setMode").toCharArray(bufVal, 20);
  465. int valueInt = atoi(bufVal);
  466. if (valueInt >= 0 && valueInt <= 1)
  467. setHeatingmodeTo(valueInt);
  468. }
  469. if (httpServer.hasArg("setPreset"))
  470. {
  471. sendLog(F("WEB: /api?setPreset"), LOGLEVEL_INFO);
  472. char bufVal[20];
  473. httpServer.arg("setPreset").toCharArray(bufVal, 20);
  474. int valueInt = atoi(bufVal);
  475. if (valueInt >= 0 && valueInt <= 2)
  476. setPresetTo(valueInt);
  477. }
  478. //char ch_currTemp[6];
  479. //char ch_currSetTemp[6];
  480. //dtostrf(currTemp, 1, 1, ch_currTemp );
  481. //dtostrf(setTemp, 1, 1, ch_currSetTemp );
  482. //build json object of program data
  483. StaticJsonDocument<850> json;
  484. json["devname"] = confDevWiFi.deviceName;
  485. json["ssid"] = WiFi.SSID();
  486. json["WiFiNum"] = persWM.getActiveWiFiNum();
  487. //json["uptime"] = uptimeStr;
  488. json["uptime"] = getUptimeStr();
  489. #ifdef ENABLE_FEATURE_NTP_TIME
  490. if (confTime.ntpEnable)
  491. {
  492. char buf[13];
  493. updateTime();
  494. if (lt.tm_year > 70)
  495. {
  496. strftime(buf, sizeof(buf), "%H:%M", &lt);
  497. json["time"] = buf;
  498. strftime(buf, sizeof(buf), "%d.%m.%Y", &lt);
  499. json["date"] = buf;
  500. }
  501. }
  502. #endif
  503. json["freeheap"] = ESP.getFreeHeap();
  504. mqtt_updateCurrentStateName();
  505. json["mqttstate"] = mqttCurrentStateName;
  506. json["mqtthost"] = confMqtt.mqtt_server;
  507. if (mqttReconnects < 1)
  508. json["mqttreconn"] = mqttReconnects;
  509. else
  510. json["mqttreconn"] = mqttReconnects - 1;
  511. json["setTemp"] = setTemp;
  512. json["currSetTemp"] = currSetTemp;
  513. json["actSetTemp"] = currActualSetTemp;
  514. json["temp"] = round1(currTemp);
  515. json["hum"] = int(currHum);
  516. uint8_t _heatingState;
  517. if(turnHeatingOn) _heatingState = 1;
  518. else if(heatingLockTime > 0) _heatingState = 2;
  519. else _heatingState = 0;
  520. json["heating"] = _heatingState;
  521. json["heatingLockTime"] = heatingLockTime;
  522. json["heatingPause"] = heatingPause;
  523. json["mode"] = heatingMode;
  524. json["modeName"] = currentModeName;
  525. json["pset"] = preset;
  526. json["psetName"] = currentPresetName;
  527. json["psetName0"] = confAdv.psetName0;
  528. json["psetName1"] = confAdv.psetName1;
  529. json["psetName2"] = confAdv.psetName2;
  530. json["tempLow"] = setTempLow;
  531. json["tempLow2"] = setTempLow2;
  532. json["outTemp"] = round1(outTemp);
  533. json["outHum"] = outHum;
  534. char jsonchar[750];
  535. serializeJson(json, jsonchar);
  536. httpServer.send(200, "application/json", jsonchar);
  537. }
  538. }); //httpServer.on /api
  539. httpServer.on("/confDataWeb", []() {
  540. if (!httpIsAuthenticatedAdmin())
  541. httpSendUnauthorized();
  542. else
  543. {
  544. //Serial.println("httpServer.on /confdata");
  545. //sendLog("WEB: /confDataWeb", LOGLEVEL_INFO);
  546. if (confLog.logWebRequests)
  547. {
  548. String addr = httpServer.client().remoteIP().toString();
  549. char buf[40];
  550. sprintf(buf, "WEB: /confDataWeb (from %s)", addr.c_str());
  551. sendLog(buf, LOGLEVEL_INFO);
  552. }
  553. StaticJsonDocument<1000> json;
  554. json["apiToken"] = confWeb.http_token;
  555. json["httpUA"] = confWeb.http_user;
  556. //json["httpPA"] = confWeb.http_pass;
  557. if (strlen(confWeb.http_pass) > 0)
  558. json["httpPA"] = "****";
  559. else
  560. json["httpPA"] = "";
  561. if (confWeb.http_user_auth)
  562. json["httpAuth"] = 1;
  563. else
  564. json["httpAuth"] = 0;
  565. json["httpU1"] = confWeb.http_user1;
  566. //json["httpP1"] = confWeb.http_pass1;
  567. if (strlen(confWeb.http_pass1) > 0)
  568. json["httpP1"] = "****";
  569. else
  570. json["httpP1"] = "";
  571. json["httpU2"] = confWeb.http_user2;
  572. //json["httpP2"] = confWeb.http_pass2;
  573. if (strlen(confWeb.http_pass2) > 0)
  574. json["httpP2"] = "****";
  575. else
  576. json["httpP2"] = "";
  577. if (confWeb.wConsole)
  578. json["wConsole"] = 1;
  579. else
  580. json["wConsole"] = 0;
  581. if (confWeb.wsConsole)
  582. json["wsConsole"] = 1;
  583. else
  584. json["wsConsole"] = 0;
  585. yield();
  586. char jsonchar[1000];
  587. serializeJson(json, jsonchar);
  588. httpServer.send(200, "application/json", jsonchar);
  589. }
  590. }); //httpServer.on /confdweb
  591. httpServer.on("/confDataMqtt", []() {
  592. if (!httpIsAuthenticatedAdmin())
  593. httpSendUnauthorized();
  594. else
  595. {
  596. //sendLog("WEB: /confDataMqtt", LOGLEVEL_INFO);
  597. if (confLog.logWebRequests)
  598. {
  599. String addr = httpServer.client().remoteIP().toString();
  600. char buf[40];
  601. sprintf(buf, "WEB: /confDataMqtt (from %s)", addr.c_str());
  602. sendLog(buf, LOGLEVEL_INFO);
  603. }
  604. StaticJsonDocument<1000> json;
  605. if (confMqtt.mqtt_enable)
  606. json["mqttEnable"] = 1;
  607. else
  608. json["mqttEnable"] = 0;
  609. json["mqttHost"] = confMqtt.mqtt_server;
  610. json["mqttPort"] = confMqtt.mqtt_port;
  611. json["mqttUser"] = confMqtt.mqtt_user;
  612. //json["mqttPass"] = confMqtt.mqtt_pass;
  613. if (strlen(confMqtt.mqtt_pass) > 0)
  614. json["mqttPass"] = "****";
  615. else
  616. json["mqttPass"] = "";
  617. json["inTop"] = confMqtt.mqtt_topic_in;
  618. json["outTop"] = confMqtt.mqtt_topic_out;
  619. if (confMqtt.mqtt_outRetain)
  620. json["outRet"] = 1;
  621. else
  622. json["outRet"] = 0;
  623. if (confMqtt.mqtt_outRetain_sensors)
  624. json["outRetSens"] = 1;
  625. else
  626. json["outRetSens"] = 0;
  627. json["outPubInt"] = confMqtt.mqtt_outPubInterval;
  628. json["outPubIntSens"] = confMqtt.mqtt_outPubInterval_sensors;
  629. json["willTop"] = confMqtt.mqtt_willTopic;
  630. json["willQos"] = confMqtt.mqtt_willQos;
  631. if (confMqtt.mqtt_willRetain)
  632. json["willRet"] = 1;
  633. else
  634. json["willRet"] = 0;
  635. json["willMsg"] = confMqtt.mqtt_willMsg;
  636. json["connMsg"] = confMqtt.mqtt_connMsg;
  637. if (confMqtt.mqtt_enable_heartbeat)
  638. json["hbEnable"] = 1;
  639. else
  640. json["hbEnable"] = 0;
  641. json["hbReconn"] = confMqtt.mqtt_heartbeat_maxage_reconnect / 60000;
  642. json["hbReboot"] = confMqtt.mqtt_heartbeat_maxage_reboot / 60000;
  643. yield();
  644. char jsonchar[1000];
  645. serializeJson(json, jsonchar);
  646. httpServer.send(200, "application/json", jsonchar);
  647. }
  648. }); //httpServer.on /confdmqtt
  649. httpServer.on("/setConfWeb", []() {
  650. if (!httpIsAuthenticatedAdmin())
  651. httpSendUnauthorized();
  652. else
  653. {
  654. //sendLog("WEB: /setConfWeb", LOGLEVEL_INFO);
  655. String addr = httpServer.client().remoteIP().toString();
  656. char buf[40];
  657. sprintf(buf, "WEB: /setConfWeb (from %s)", addr.c_str());
  658. sendLog(buf, LOGLEVEL_INFO);
  659. bool httpPASet, httpP1Set, httpP2Set;
  660. httpPASet = false;
  661. httpP1Set = false;
  662. httpP2Set = false;
  663. if (httpServer.hasArg("httpPASet"))
  664. httpPASet = true;
  665. if (httpServer.hasArg("httpP1Set"))
  666. httpP1Set = true;
  667. if (httpServer.hasArg("httpP2Set"))
  668. httpP2Set = true;
  669. for (int i = 0; i < httpServer.args(); i++)
  670. {
  671. char bufName[20];
  672. char bufValue[101];
  673. httpServer.argName(i).toCharArray(bufName, 20);
  674. httpServer.arg(i).toCharArray(bufValue, 101);
  675. if (strlen(bufName) > 0)
  676. {
  677. Serial.print("web update ");
  678. Serial.print(bufName);
  679. Serial.print(" = ");
  680. Serial.println(bufValue);
  681. if (strcmp(bufName, "httpUA") == 0 || strcmp(bufName, "httpPA") == 0)
  682. {
  683. if (httpPASet)
  684. setConfig(bufName, bufValue);
  685. }
  686. else if (strcmp(bufName, "httpU1") == 0 || strcmp(bufName, "httpP1") == 0)
  687. {
  688. if (httpP1Set)
  689. setConfig(bufName, bufValue);
  690. }
  691. else if (strcmp(bufName, "httpU2") == 0 || strcmp(bufName, "httpP2") == 0)
  692. {
  693. if (httpP2Set)
  694. setConfig(bufName, bufValue);
  695. }
  696. else if (strcmp(bufName, "httpPASet") != 0 && strcmp(bufName, "httpP1Set") != 0 && strcmp(bufName, "httpP2Set") != 0)
  697. setConfig(bufName, bufValue);
  698. //else setConfig(bufName, bufValue);
  699. }
  700. }
  701. yield();
  702. saveConfigWeb();
  703. yield();
  704. loadConfigWeb();
  705. httpServerHandleConfSavedPage();
  706. // yield();
  707. // ESP.restart();
  708. }
  709. }); //httpServer.on /setConfWeb
  710. httpServer.on("/setConfMqtt", []() {
  711. if (!httpIsAuthenticatedAdmin())
  712. httpSendUnauthorized();
  713. else
  714. {
  715. //Serial.println("httpServer.on /setConfMqtt");
  716. //sendLog("WEB: /setConfMqtt", LOGLEVEL_INFO);
  717. String addr = httpServer.client().remoteIP().toString();
  718. char buf[40];
  719. sprintf(buf, "WEB: /setConfMqtt (from %s)", addr.c_str());
  720. sendLog(buf, LOGLEVEL_INFO);
  721. bool mqttPassSet;
  722. mqttPassSet = false;
  723. if (httpServer.hasArg("mqttPassSet"))
  724. mqttPassSet = true;
  725. for (int i = 0; i < httpServer.args(); i++)
  726. {
  727. char bufName[20];
  728. char bufValue[101];
  729. httpServer.argName(i).toCharArray(bufName, 20);
  730. httpServer.arg(i).toCharArray(bufValue, 101);
  731. if (strlen(bufName) > 0)
  732. {
  733. Serial.print("web update ");
  734. Serial.print(bufName);
  735. Serial.print(" = ");
  736. Serial.println(bufValue);
  737. if (strcmp(bufName, "mqttUser") == 0 || strcmp(bufName, "mqttPass") == 0)
  738. {
  739. if (mqttPassSet)
  740. setConfig(bufName, bufValue);
  741. }
  742. else
  743. setConfig(bufName, bufValue);
  744. }
  745. //saveConfigMqttToFlash = true; // will be saved in next loop()
  746. //Serial.println("web triggered saveConfigMqttToFlash");
  747. }
  748. yield();
  749. saveConfigMqtt();
  750. yield();
  751. loadConfigMqtt();
  752. //httpServerHandleConfSavedRestartPage();
  753. httpServerHandleConfSavedPage();
  754. //restart();
  755. }
  756. }); //httpServer.on /setConfMqtt
  757. httpServer.on("/confDataDevWiFi", []() {
  758. if (!httpIsAuthenticated())
  759. return httpServer.requestAuthentication();
  760. else
  761. {
  762. //sendLog("WEB: /confDataDevWiFi", LOGLEVEL_INFO);
  763. if (confLog.logWebRequests)
  764. {
  765. String addr = httpServer.client().remoteIP().toString();
  766. char buf[40];
  767. sprintf(buf, "WEB: /confDataDevWiFi (from %s)", addr.c_str());
  768. sendLog(buf, LOGLEVEL_INFO);
  769. }
  770. //for (int i = 0; i < httpServer.args(); i++) {
  771. // char bufName[20];
  772. // char bufValue[101];
  773. // httpServer.argName(i).toCharArray(bufName, 20);
  774. // httpServer.arg(i).toCharArray(bufValue, 101);
  775. //
  776. // if (strlen(bufName) > 0) {
  777. // //Serial.print("web update ");
  778. // //Serial.print(bufName);
  779. // //Serial.print(" = ");
  780. // //Serial.println(bufValue);
  781. // setConfig(bufName, bufValue);
  782. // }
  783. // saveConfigToFlash = true; // will be saved in next loop()
  784. // //Serial.println("web triggered saveConfigToFlash");
  785. //}
  786. //yield();
  787. //build json object of program data
  788. StaticJsonDocument<800> json;
  789. json["devName"] = confDevWiFi.deviceName;
  790. json["hostName"] = confDevWiFi.hostName;
  791. json["SSID1"] = confDevWiFi.WiFiSSID1;
  792. //json["WPW1"] = confDevWiFi.WiFiPW1;
  793. if (strlen(confDevWiFi.WiFiPW1) > 0)
  794. json["WPW1"] = "****";
  795. else
  796. json["WPW1"] = "";
  797. json["SSID2"] = confDevWiFi.WiFiSSID2;
  798. //json["WPW2"] = confDevWiFi.WiFiPW2;
  799. if (strlen(confDevWiFi.WiFiPW2) > 0)
  800. json["WPW2"] = "****";
  801. else
  802. json["WPW2"] = "";
  803. json["SSIDAP"] = confDevWiFi.WiFiAPModeSSID;
  804. json["WPWAP"] = confDevWiFi.WiFiAPModePassword;
  805. json["WAPtout"] = confDevWiFi.WiFiAPModeTimeout;
  806. json["WConnCheck"] = confDevWiFi.WiFiConnCheckInterval;
  807. json["Wretry"] = confDevWiFi.WiFiRetryInterval;
  808. json["Wreboot"] = confDevWiFi.WiFiRebootOnNoConnect;
  809. yield();
  810. char jsonchar[1000];
  811. serializeJson(json, jsonchar);
  812. httpServer.send(200, "application/json", jsonchar);
  813. }
  814. }); //httpServer.on /confDataDevWiFi
  815. httpServer.on("/setConfDevWiFi", []() {
  816. if (!httpIsAuthenticatedAdmin())
  817. httpSendUnauthorized();
  818. else
  819. {
  820. //sendLog("WEB: /setConfDevWiFi", LOGLEVEL_INFO);
  821. String addr = httpServer.client().remoteIP().toString();
  822. char buf[40];
  823. sprintf(buf, "WEB: /setConfDevWiFi (from %s)", addr.c_str());
  824. sendLog(buf, LOGLEVEL_INFO);
  825. bool WPW1Set, WPW2Set, WPWAPSet;
  826. WPW1Set = false;
  827. WPW2Set = false;
  828. WPWAPSet = false;
  829. if (httpServer.hasArg("WPW1Set"))
  830. WPW1Set = true;
  831. if (httpServer.hasArg("WPW2Set"))
  832. WPW2Set = true;
  833. if (httpServer.hasArg("WPWAPSet"))
  834. WPWAPSet = true;
  835. for (int i = 0; i < httpServer.args(); i++)
  836. {
  837. char bufName[20];
  838. char bufValue[101];
  839. httpServer.argName(i).toCharArray(bufName, 20);
  840. httpServer.arg(i).toCharArray(bufValue, 101);
  841. if (strlen(bufName) > 0)
  842. {
  843. Serial.print("web update ");
  844. Serial.print(bufName);
  845. Serial.print(" = ");
  846. Serial.println(bufValue);
  847. if (strcmp(bufName, "SSID1") == 0 || strcmp(bufName, "WPW1") == 0)
  848. {
  849. if (WPW1Set)
  850. setConfig(bufName, bufValue);
  851. }
  852. else if (strcmp(bufName, "SSID2") == 0 || strcmp(bufName, "WPW2") == 0)
  853. {
  854. if (WPW2Set)
  855. setConfig(bufName, bufValue);
  856. }
  857. else if (strcmp(bufName, "WPWAP") == 0)
  858. {
  859. if (WPWAPSet)
  860. setConfig(bufName, bufValue);
  861. }
  862. else
  863. setConfig(bufName, bufValue);
  864. }
  865. }
  866. yield();
  867. saveConfigDevWiFi();
  868. yield();
  869. loadConfigDevWiFi();
  870. //httpServerHandleConfSavedRestartPage();
  871. httpServerHandleConfSavedPage();
  872. //restart();
  873. }
  874. }); //httpServer.on /setConfDev
  875. httpServer.on("/setConfBas", []() {
  876. if (!httpIsAuthenticatedAdmin())
  877. httpSendUnauthorized();
  878. else
  879. {
  880. //sendLog("WEB: /setConfBas", LOGLEVEL_INFO);
  881. String addr = httpServer.client().remoteIP().toString();
  882. char buf[40];
  883. sprintf(buf, "WEB: /setConfBas (from %s)", addr.c_str());
  884. sendLog(buf, LOGLEVEL_INFO);
  885. for (int i = 0; i < httpServer.args(); i++)
  886. {
  887. char bufName[20];
  888. char bufValue[101];
  889. httpServer.argName(i).toCharArray(bufName, 20);
  890. httpServer.arg(i).toCharArray(bufValue, 101);
  891. if (strlen(bufName) > 0)
  892. {
  893. Serial.print("web update ");
  894. Serial.print(bufName);
  895. Serial.print(" = ");
  896. Serial.println(bufValue);
  897. setConfig(bufName, bufValue);
  898. }
  899. }
  900. yield();
  901. saveConfigBas();
  902. yield();
  903. loadConfigBas();
  904. httpServerHandleConfSavedPage();
  905. }
  906. }); //httpServer.on /setConfBas
  907. httpServer.on("/setConfAdv", []() {
  908. if (!httpIsAuthenticatedAdmin())
  909. httpSendUnauthorized();
  910. else
  911. {
  912. //sendLog("WEB: /setConfAdv", LOGLEVEL_INFO);
  913. String addr = httpServer.client().remoteIP().toString();
  914. char buf[40];
  915. sprintf(buf, "WEB: /setConfAdv (from %s)", addr.c_str());
  916. sendLog(buf, LOGLEVEL_INFO);
  917. for (int i = 0; i < httpServer.args(); i++)
  918. {
  919. char bufName[20];
  920. char bufValue[101];
  921. httpServer.argName(i).toCharArray(bufName, 20);
  922. httpServer.arg(i).toCharArray(bufValue, 101);
  923. if (strlen(bufName) > 0)
  924. {
  925. Serial.print("web update ");
  926. Serial.print(bufName);
  927. Serial.print(" = ");
  928. Serial.println(bufValue);
  929. setConfig(bufName, bufValue);
  930. }
  931. }
  932. yield();
  933. saveConfigAdv();
  934. yield();
  935. loadConfigAdv();
  936. httpServerHandleConfSavedPage();
  937. }
  938. }); //httpServer.on /setConfAdv
  939. httpServer.on("/setConfAdd", []() {
  940. if (!httpIsAuthenticatedAdmin())
  941. httpSendUnauthorized();
  942. else
  943. {
  944. //sendLog("WEB: /setConfAdd", LOGLEVEL_INFO);
  945. String addr = httpServer.client().remoteIP().toString();
  946. char buf[40];
  947. sprintf(buf, "WEB: /setConfAdd (from %s)", addr.c_str());
  948. sendLog(buf, LOGLEVEL_INFO);
  949. for (int i = 0; i < httpServer.args(); i++)
  950. {
  951. char bufName[20];
  952. char bufValue[101];
  953. httpServer.argName(i).toCharArray(bufName, 20);
  954. httpServer.arg(i).toCharArray(bufValue, 101);
  955. if (strlen(bufName) > 0)
  956. {
  957. Serial.print("web update ");
  958. Serial.print(bufName);
  959. Serial.print(" = ");
  960. Serial.println(bufValue);
  961. setConfig(bufName, bufValue);
  962. }
  963. }
  964. yield();
  965. saveConfigAdd();
  966. yield();
  967. loadConfigAdd();
  968. httpServerHandleConfSavedPage();
  969. }
  970. }); //httpServer.on /setConfAdd
  971. #ifdef ENABLE_FEATURE_NTP_TIME
  972. httpServer.on("/setConfTime", []() {
  973. if (!httpIsAuthenticatedAdmin())
  974. httpSendUnauthorized();
  975. else
  976. {
  977. //sendLog("WEB: /setConfTime", LOGLEVEL_INFO);
  978. String addr = httpServer.client().remoteIP().toString();
  979. char buf[40];
  980. sprintf(buf, "WEB: /setConfTime (from %s)", addr.c_str());
  981. sendLog(buf, LOGLEVEL_INFO);
  982. for (int i = 0; i < httpServer.args(); i++)
  983. {
  984. char bufName[20];
  985. char bufValue[101];
  986. httpServer.argName(i).toCharArray(bufName, 20);
  987. httpServer.arg(i).toCharArray(bufValue, 101);
  988. if (strlen(bufName) > 0)
  989. {
  990. Serial.print("web update ");
  991. Serial.print(bufName);
  992. Serial.print(" = ");
  993. Serial.println(bufValue);
  994. setConfig(bufName, bufValue);
  995. }
  996. }
  997. yield();
  998. saveConfigTime();
  999. yield();
  1000. loadConfigTime();
  1001. httpServerHandleConfSavedPage();
  1002. }
  1003. }); //httpServer.on /setConfTime
  1004. #endif
  1005. httpServer.on("/setConfLog", []() {
  1006. if (!httpIsAuthenticatedAdmin())
  1007. httpSendUnauthorized();
  1008. else
  1009. {
  1010. //sendLog("WEB: /setConfLog", LOGLEVEL_INFO);
  1011. String addr = httpServer.client().remoteIP().toString();
  1012. char buf[40];
  1013. sprintf(buf, "WEB: /setConfLog (from %s)", addr.c_str());
  1014. sendLog(buf, LOGLEVEL_INFO);
  1015. for (int i = 0; i < httpServer.args(); i++)
  1016. {
  1017. char bufName[20];
  1018. char bufValue[101];
  1019. httpServer.argName(i).toCharArray(bufName, 20);
  1020. httpServer.arg(i).toCharArray(bufValue, 101);
  1021. if (strlen(bufName) > 0)
  1022. {
  1023. Serial.print("web update ");
  1024. Serial.print(bufName);
  1025. Serial.print(" = ");
  1026. Serial.println(bufValue);
  1027. setConfig(bufName, bufValue);
  1028. }
  1029. }
  1030. yield();
  1031. saveConfigLog();
  1032. yield();
  1033. loadConfigLog();
  1034. httpServerHandleConfSavedPage();
  1035. }
  1036. }); //httpServer.on /setConfLog
  1037. httpServer.on("/confDataBas", []() {
  1038. if (!httpIsAuthenticated())
  1039. return httpServer.requestAuthentication();
  1040. else
  1041. {
  1042. //sendLog("WEB: /confDataBas", LOGLEVEL_INFO);
  1043. //Serial.println("httpServer.on /confdata2");
  1044. if (confLog.logWebRequests)
  1045. {
  1046. String addr = httpServer.client().remoteIP().toString();
  1047. char buf[40];
  1048. sprintf(buf, "WEB: /confDataBas (from %s)", addr.c_str());
  1049. sendLog(buf, LOGLEVEL_INFO);
  1050. }
  1051. //for (int i = 0; i < httpServer.args(); i++) {
  1052. // char bufName[20];
  1053. // char bufValue[101];
  1054. // httpServer.argName(i).toCharArray(bufName, 20);
  1055. // httpServer.arg(i).toCharArray(bufValue, 101);
  1056. //
  1057. // if (strlen(bufName) > 0) {
  1058. // //Serial.print("web update ");
  1059. // //Serial.print(bufName);
  1060. // //Serial.print(" = ");
  1061. // //Serial.println(bufValue);
  1062. // setConfig(bufName, bufValue);
  1063. // }
  1064. // saveConfig2ToFlash = true;
  1065. // //Serial.println("web triggered saveConfig2ToFlash");
  1066. //}
  1067. //yield();
  1068. //build json object of program data
  1069. StaticJsonDocument<500> json;
  1070. if (confBas.autoSaveSetTemp)
  1071. json["autoSaveTemp"] = 1;
  1072. else
  1073. json["autoSaveTemp"] = 0;
  1074. if (confBas.autoSaveHeatingMode)
  1075. json["autoSaveMode"] = 1;
  1076. else
  1077. json["autoSaveMode"] = 0;
  1078. if (confBas.saveToMqttRetained)
  1079. json["saveToMqttRet"] = 1;
  1080. else
  1081. json["saveToMqttRet"] = 0;
  1082. json["tempMin"] = confBas.setTempMin;
  1083. json["tempMax"] = confBas.setTempMax;
  1084. json["measInt"] = confBas.measureInterval;
  1085. json["dispInt"] = confBas.displayInterval;
  1086. json["dispTout"] = confBas.displayTimeout;
  1087. if (confBas.PIR_enablesDisplay)
  1088. json["PIRenDisp"] = 1;
  1089. else
  1090. json["PIRenDisp"] = 0;
  1091. if (confBas.PIR_enablesDisplay_preset0only)
  1092. json["PIRenDispPs0"] = 1;
  1093. else
  1094. json["PIRenDispPs0"] = 0;
  1095. if (confBas.togglingTempHumAIDisplay)
  1096. json["togTHdisp"] = 1;
  1097. else
  1098. json["togTHdisp"] = 0;
  1099. yield();
  1100. char jsonchar[1000];
  1101. serializeJson(json, jsonchar);
  1102. httpServer.send(200, "application/json", jsonchar);
  1103. }
  1104. }); //httpServer.on /confdbas
  1105. httpServer.on("/confDataAdv", []() {
  1106. if (!httpIsAuthenticated())
  1107. return httpServer.requestAuthentication();
  1108. else
  1109. {
  1110. //sendLog("WEB: /confDataAdv", LOGLEVEL_INFO);
  1111. //Serial.println("httpServer.on /confdata2");
  1112. if (confLog.logWebRequests)
  1113. {
  1114. String addr = httpServer.client().remoteIP().toString();
  1115. char buf[40];
  1116. sprintf(buf, "WEB: /confDataAdv (from %s)", addr.c_str());
  1117. sendLog(buf, LOGLEVEL_INFO);
  1118. }
  1119. //for (int i = 0; i < httpServer.args(); i++) {
  1120. // char bufName[20];
  1121. // char bufValue[101];
  1122. // httpServer.argName(i).toCharArray(bufName, 20);
  1123. // httpServer.arg(i).toCharArray(bufValue, 101);
  1124. //
  1125. // if (strlen(bufName) > 0) {
  1126. // //Serial.print("web update ");
  1127. // //Serial.print(bufName);
  1128. // //Serial.print(" = ");
  1129. // //Serial.println(bufValue);
  1130. // setConfig(bufName, bufValue);
  1131. // }
  1132. // saveConfig2ToFlash = true;
  1133. // //Serial.println("web triggered saveConfig2ToFlash");
  1134. //}
  1135. //yield();
  1136. //build json object of program data
  1137. StaticJsonDocument<750> json;
  1138. json["minOffTime"] = confAdv.heatingMinOffTime;
  1139. json["tempDec"] = confAdv.setTempDecreaseVal;
  1140. json["hyst"] = confAdv.hysteresis;
  1141. json["tempCorr"] = confAdv.tempCorrVal;
  1142. json["humCorr"] = confAdv.humCorrVal;
  1143. json["offMsg"] = confAdv.offMessage;
  1144. json["modeName0"] = confAdv.modeName0;
  1145. json["modeName1"] = confAdv.modeName1;
  1146. json["psetName0"] = confAdv.psetName0;
  1147. json["psetName1"] = confAdv.psetName1;
  1148. json["psetName2"] = confAdv.psetName2;
  1149. json["iTempLab"] = confAdv.iTempLabel;
  1150. json["oTempLab"] = confAdv.oTempLabel;
  1151. json["pauseTout"] = confAdv.pauseTout;
  1152. yield();
  1153. char jsonchar[1000];
  1154. serializeJson(json, jsonchar);
  1155. httpServer.send(200, "application/json", jsonchar);
  1156. }
  1157. }); //httpServer.on /confdadv
  1158. httpServer.on("/confDataAdd", []() {
  1159. if (!httpIsAuthenticated())
  1160. return httpServer.requestAuthentication();
  1161. else
  1162. {
  1163. //sendLog("WEB: /confDataAdd", LOGLEVEL_INFO);
  1164. //Serial.println("httpServer.on /confdata2");
  1165. if (confLog.logWebRequests)
  1166. {
  1167. String addr = httpServer.client().remoteIP().toString();
  1168. char buf[40];
  1169. sprintf(buf, "WEB: /confDataAdd (from %s)", addr.c_str());
  1170. sendLog(buf, LOGLEVEL_INFO);
  1171. }
  1172. //for (int i = 0; i < httpServer.args(); i++) {
  1173. // char bufName[20];
  1174. // char bufValue[101];
  1175. // httpServer.argName(i).toCharArray(bufName, 20);
  1176. // httpServer.arg(i).toCharArray(bufValue, 101);
  1177. //
  1178. // if (strlen(bufName) > 0) {
  1179. // //Serial.print("web update ");
  1180. // //Serial.print(bufName);
  1181. // //Serial.print(" = ");
  1182. // //Serial.println(bufValue);
  1183. // setConfig(bufName, bufValue);
  1184. // }
  1185. // saveConfig2ToFlash = true;
  1186. // //Serial.println("web triggered saveConfig2ToFlash");
  1187. //}
  1188. //yield();
  1189. //build json object of program data
  1190. StaticJsonDocument<1000> json;
  1191. json["outTempTop"] = confAdd.outTemp_topic_in;
  1192. json["outHumTop"] = confAdd.outHum_topic_in;
  1193. json["PIRTop"] = confAdd.mqtt_topic_pir;
  1194. json["PIROnPld"] = confAdd.mqtt_payload_pir_on;
  1195. json["PIROffPld"] = confAdd.mqtt_payload_pir_off;
  1196. yield();
  1197. char jsonchar[1000];
  1198. serializeJson(json, jsonchar);
  1199. httpServer.send(200, "application/json", jsonchar);
  1200. }
  1201. }); //httpServer.on /confdadd
  1202. httpServer.on("/confDataTime", []() {
  1203. if (!httpIsAuthenticated())
  1204. return httpServer.requestAuthentication();
  1205. else
  1206. {
  1207. //sendLog("WEB: /confDataTime", LOGLEVEL_INFO);
  1208. //Serial.println("httpServer.on /confdata2");
  1209. if (confLog.logWebRequests)
  1210. {
  1211. String addr = httpServer.client().remoteIP().toString();
  1212. char buf[40];
  1213. sprintf(buf, "WEB: /confDataTime (from %s)", addr.c_str());
  1214. sendLog(buf, LOGLEVEL_INFO);
  1215. }
  1216. //for (int i = 0; i < httpServer.args(); i++) {
  1217. // char bufName[20];
  1218. // char bufValue[101];
  1219. // httpServer.argName(i).toCharArray(bufName, 20);
  1220. // httpServer.arg(i).toCharArray(bufValue, 101);
  1221. //
  1222. // if (strlen(bufName) > 0) {
  1223. // //Serial.print("web update ");
  1224. // //Serial.print(bufName);
  1225. // //Serial.print(" = ");
  1226. // //Serial.println(bufValue);
  1227. // setConfig(bufName, bufValue);
  1228. // }
  1229. // saveConfig2ToFlash = true;
  1230. // //Serial.println("web triggered saveConfig2ToFlash");
  1231. //}
  1232. //yield();
  1233. //build json object of program data
  1234. StaticJsonDocument<1000> json;
  1235. if (confTime.ntpEnable)
  1236. json["NTPEnable"] = 1;
  1237. else
  1238. json["NTPEnable"] = 0;
  1239. json["NTPServer1"] = confTime.ntpServer1;
  1240. json["NTPServer2"] = confTime.ntpServer2;
  1241. json["TZStr"] = confTime.timeZoneStr;
  1242. json["NTPSyncInt"] = confTime.ntpSyncInterval / 60;
  1243. yield();
  1244. char jsonchar[1000];
  1245. serializeJson(json, jsonchar);
  1246. httpServer.send(200, "application/json", jsonchar);
  1247. }
  1248. }); //httpServer.on /confDataTime
  1249. httpServer.on("/confDataLog", []() {
  1250. if (!httpIsAuthenticated())
  1251. return httpServer.requestAuthentication();
  1252. else
  1253. {
  1254. //sendLog("WEB: /confDataLog", LOGLEVEL_INFO);
  1255. if (confLog.logWebRequests)
  1256. {
  1257. String addr = httpServer.client().remoteIP().toString();
  1258. char buf[40];
  1259. sprintf(buf, "WEB: /confDataLog (from %s)", addr.c_str());
  1260. sendLog(buf, LOGLEVEL_INFO);
  1261. }
  1262. //for (int i = 0; i < httpServer.args(); i++) {
  1263. // char bufName[20];
  1264. // char bufValue[101];
  1265. // httpServer.argName(i).toCharArray(bufName, 20);
  1266. // httpServer.arg(i).toCharArray(bufValue, 101);
  1267. //
  1268. // if (strlen(bufName) > 0) {
  1269. // //Serial.print("web update ");
  1270. // //Serial.print(bufName);
  1271. // //Serial.print(" = ");
  1272. // //Serial.println(bufValue);
  1273. // setConfig(bufName, bufValue);
  1274. // }
  1275. // saveConfig2ToFlash = true;
  1276. // //Serial.println("web triggered saveConfig2ToFlash");
  1277. //}
  1278. //yield();
  1279. //build json object of program data
  1280. StaticJsonDocument<1000> json;
  1281. //if(confTime.ntpEnable) json["NTPEnable"] = 1;
  1282. //else json["NTPEnable"] = 0;
  1283. json["logLevSer"] = confLog.logLevelSerial;
  1284. json["logLevWeb"] = confLog.logLevelWeb;
  1285. json["logLevMqtt"] = confLog.logLevelMqtt;
  1286. if (confLog.logWebRequests)
  1287. json["logWebRequests"] = 1;
  1288. else
  1289. json["logWebRequests"] = 0;
  1290. yield();
  1291. char jsonchar[1000];
  1292. serializeJson(json, jsonchar);
  1293. httpServer.send(200, "application/json", jsonchar);
  1294. }
  1295. }); //httpServer.on /confDataLog
  1296. //get heap status, analog input value and all GPIO statuses in one json call
  1297. httpServer.on("/sysinfod", HTTP_GET, []() {
  1298. if (!httpIsAuthenticated())
  1299. return httpServer.requestAuthentication();
  1300. else
  1301. {
  1302. if (confLog.logWebRequests)
  1303. {
  1304. String addr = httpServer.client().remoteIP().toString();
  1305. char buf[40];
  1306. sprintf(buf, "WEB: /sysinfod (from %s)", addr.c_str());
  1307. sendLog(buf, LOGLEVEL_INFO);
  1308. }
  1309. char buf[40];
  1310. byte mac[6];
  1311. WiFi.macAddress(mac);
  1312. StaticJsonDocument<1000> json;
  1313. json["DevName"] = confDevWiFi.deviceName;
  1314. json["HostName"] = confDevWiFi.hostName;
  1315. sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", (uint8_t)mac[0], (uint8_t)mac[1], (uint8_t)mac[2], (uint8_t)mac[3], (uint8_t)mac[4], (uint8_t)mac[5]);
  1316. json["MAC"] = buf;
  1317. //sprintf(buf, "%s", WiFi.localIP().toString().c_str());
  1318. //json["IP"] = buf;
  1319. //sprintf(buf, "%s", WiFi.gatewayIP().toString().c_str());
  1320. //json["Gateway"] = buf;
  1321. json["IP"] = WiFi.localIP().toString();
  1322. json["GW"] = WiFi.gatewayIP().toString();
  1323. json["DNS"] = WiFi.dnsIP().toString();
  1324. json["SSID"] = WiFi.SSID();
  1325. json["WiFiConf"] = persWM.getActiveWiFiNum();
  1326. json["UpTime"] = getUptimeStr();
  1327. json["HeapFree"] = ESP.getFreeHeap();
  1328. json["HeapFragment"] = ESP.getHeapFragmentation();
  1329. json["HeapMaxBlock"] = ESP.getMaxFreeBlockSize();
  1330. json["ResetReason"] = ESP.getResetReason();
  1331. json["CoreVersion"] = ESP.getCoreVersion();
  1332. json["SDKVersion"] = ESP.getSdkVersion();
  1333. json["CPUfreq"] = ESP.getCpuFreqMHz();
  1334. json["SketchSize"] = ESP.getSketchSize();
  1335. json["FlashSize"] = ESP.getFlashChipRealSize();
  1336. sprintf(buf, "%s", PGMStr_FIRMWARE_NAME);
  1337. json["FWName"] = buf;
  1338. sprintf(buf, "%s", PGMStr_FIRMWARE_VERSION);
  1339. json["FWVer"] = buf;
  1340. sprintf(buf, "%s", PGMStr_FIRMWARE_COPYRIGHT);
  1341. json["FWCr"] = buf;
  1342. sprintf(buf, "%s", compile_date);
  1343. json["FWBuilt"] = buf;
  1344. #ifdef DEBUGMODE
  1345. json["FWDebug"] = "yes";
  1346. #else
  1347. json["FWDebug"] = "no";
  1348. #endif
  1349. #ifdef ENABLE_FEATURE_NTP_TIME
  1350. if (confTime.ntpEnable)
  1351. {
  1352. char buf[13];
  1353. updateTime();
  1354. if(lt.tm_year > 70) {
  1355. strftime(buf, sizeof(buf), "%H:%M", &lt);
  1356. json["Time"] = buf;
  1357. strftime(buf, sizeof(buf), "%d.%m.%Y", &lt);
  1358. json["Date"] = buf;
  1359. }
  1360. }
  1361. #endif
  1362. yield();
  1363. char jsonchar[1000];
  1364. serializeJson(json, jsonchar);
  1365. httpServer.send(200, "application/json", jsonchar);
  1366. }
  1367. }); //httpServer.on /sysinfod
  1368. httpServer.on("/sysinfo", HTTP_GET, []() {
  1369. if (!httpIsAuthenticated())
  1370. return httpServer.requestAuthentication();
  1371. else
  1372. {
  1373. if (confLog.logWebRequests)
  1374. {
  1375. String addr = httpServer.client().remoteIP().toString();
  1376. char buf[40];
  1377. sprintf(buf, "WEB: /sysinfo (from %s)", addr.c_str());
  1378. sendLog(buf, LOGLEVEL_INFO);
  1379. }
  1380. httpServerHandleSysInfoPage();
  1381. }
  1382. });
  1383. /*httpServer.on("/", []() {
  1384. if ( WifiInApMode ) {
  1385. httpServer.sendHeader("Location", "/wifi.htm", true);
  1386. httpServer.send(302);
  1387. }
  1388. else {
  1389. httpServer.sendHeader("Location", "/main", true);
  1390. httpServer.send(302);
  1391. }
  1392. });*/
  1393. httpServer.on("/", []() {
  1394. String addr = httpServer.client().remoteIP().toString();
  1395. if (confLog.logWebRequests || httpServer.args() > 0)
  1396. {
  1397. char buf[40];
  1398. sprintf(buf, "WEB: / (from %s)", addr.c_str());
  1399. sendLog(buf, LOGLEVEL_INFO);
  1400. }
  1401. if (httpServer.hasArg("restart"))
  1402. {
  1403. if (httpIsAuthenticated() || (!httpIsAuthenticated() && httpCheckToken()))
  1404. {
  1405. sendLog("WEB: /?restart", LOGLEVEL_INFO);
  1406. httpServerHandleRestartPage();
  1407. restart();
  1408. }
  1409. else
  1410. httpSendUnauthorized();
  1411. }
  1412. else if (httpServer.hasArg("mqttreconnect"))
  1413. {
  1414. if (httpIsAuthenticated() || (!httpIsAuthenticated() && httpCheckToken()))
  1415. {
  1416. //Serial.println("web triggered mqttreconnect");
  1417. sendLog("WEB: /?mqttreconnect", LOGLEVEL_INFO);
  1418. mqttReconnect();
  1419. httpServer.sendHeader("Location", "/", true);
  1420. httpServer.send(303);
  1421. }
  1422. else
  1423. httpSendUnauthorized();
  1424. }
  1425. else if (httpServer.hasArg("clearconf"))
  1426. {
  1427. if (httpIsAuthenticatedAdmin() || (!httpIsAuthenticatedAdmin() && httpCheckToken()))
  1428. {
  1429. sendLog("WEB: /?clearconf", LOGLEVEL_INFO);
  1430. httpServerHandleClearconfPage();
  1431. yield();
  1432. delay(5);
  1433. deleteConfig();
  1434. }
  1435. else
  1436. httpSendUnauthorized();
  1437. }
  1438. else if (httpServer.hasArg("clearwifi"))
  1439. {
  1440. if (httpIsAuthenticatedAdmin() || (!httpIsAuthenticatedAdmin() && httpCheckToken()))
  1441. {
  1442. sendLog("WEB: /?clearwifi", LOGLEVEL_INFO);
  1443. httpServerHandleClearconfPage();
  1444. yield();
  1445. delay(5);
  1446. confClearWiFiCredentials();
  1447. }
  1448. else
  1449. httpSendUnauthorized();
  1450. }
  1451. else if (httpServer.hasArg("clearcreds"))
  1452. {
  1453. if (httpIsAuthenticatedAdmin() || (!httpIsAuthenticatedAdmin() && httpCheckToken()))
  1454. {
  1455. sendLog("WEB: /?clearcreds", LOGLEVEL_INFO);
  1456. httpServerHandleClearconfPage();
  1457. yield();
  1458. delay(5);
  1459. confClearCredentials();
  1460. }
  1461. else
  1462. httpSendUnauthorized();
  1463. }
  1464. else if (!httpIsAuthenticated())
  1465. return httpServer.requestAuthentication();
  1466. else
  1467. {
  1468. if (confLog.logWebRequests)
  1469. sendLog("WEB: /", LOGLEVEL_INFO);
  1470. httpServerHandleMainPage();
  1471. }
  1472. });
  1473. httpServer.on("/conf", []() {
  1474. if (!httpIsAuthenticatedAdmin())
  1475. return httpServer.requestAuthentication();
  1476. else
  1477. {
  1478. //sendLog("WEB: /conf", LOGLEVEL_INFO);
  1479. if (confLog.logWebRequests)
  1480. {
  1481. String addr = httpServer.client().remoteIP().toString();
  1482. char buf[40];
  1483. sprintf(buf, "WEB: /conf (from %s)", addr.c_str());
  1484. sendLog(buf, LOGLEVEL_INFO);
  1485. }
  1486. httpServerHandleConfPage();
  1487. }
  1488. });
  1489. httpServer.on("/confweb", []() {
  1490. if (!httpIsAuthenticatedAdmin())
  1491. return httpServer.requestAuthentication();
  1492. else
  1493. {
  1494. //sendLog("WEB: /confweb", LOGLEVEL_INFO);
  1495. if (confLog.logWebRequests)
  1496. {
  1497. String addr = httpServer.client().remoteIP().toString();
  1498. char buf[40];
  1499. sprintf(buf, "WEB: /confweb (from %s)", addr.c_str());
  1500. sendLog(buf, LOGLEVEL_INFO);
  1501. }
  1502. httpServerHandleConfWebPage();
  1503. }
  1504. });
  1505. httpServer.on("/confmqtt", []() {
  1506. if (!httpIsAuthenticatedAdmin())
  1507. return httpServer.requestAuthentication();
  1508. else
  1509. {
  1510. //sendLog("WEB: /confmqtt", LOGLEVEL_INFO);
  1511. if (confLog.logWebRequests)
  1512. {
  1513. String addr = httpServer.client().remoteIP().toString();
  1514. char buf[40];
  1515. sprintf(buf, "WEB: /confmqtt (from %s)", addr.c_str());
  1516. sendLog(buf, LOGLEVEL_INFO);
  1517. }
  1518. httpServerHandleConfMqttPage();
  1519. }
  1520. });
  1521. httpServer.on("/confdevwifi", []() {
  1522. if (!httpIsAuthenticatedAdmin())
  1523. return httpServer.requestAuthentication();
  1524. else
  1525. {
  1526. //sendLog("WEB: /confdevwifi", LOGLEVEL_INFO);
  1527. if (confLog.logWebRequests)
  1528. {
  1529. String addr = httpServer.client().remoteIP().toString();
  1530. char buf[40];
  1531. sprintf(buf, "WEB: /confdevwifi (from %s)", addr.c_str());
  1532. sendLog(buf, LOGLEVEL_INFO);
  1533. }
  1534. httpServerHandleConfDevWiFiPage();
  1535. }
  1536. });
  1537. httpServer.on("/confbas", []() {
  1538. if (!httpIsAuthenticatedAdmin())
  1539. return httpServer.requestAuthentication();
  1540. else
  1541. {
  1542. //sendLog("WEB: /confbas", LOGLEVEL_INFO);
  1543. if (confLog.logWebRequests)
  1544. {
  1545. String addr = httpServer.client().remoteIP().toString();
  1546. char buf[40];
  1547. sprintf(buf, "WEB: /confbas (from %s)", addr.c_str());
  1548. sendLog(buf, LOGLEVEL_INFO);
  1549. }
  1550. httpServerHandleConfBasPage();
  1551. }
  1552. });
  1553. httpServer.on("/confadv", []() {
  1554. if (!httpIsAuthenticatedAdmin())
  1555. return httpServer.requestAuthentication();
  1556. else
  1557. {
  1558. //sendLog("WEB: /confadv", LOGLEVEL_INFO);
  1559. if (confLog.logWebRequests)
  1560. {
  1561. String addr = httpServer.client().remoteIP().toString();
  1562. char buf[40];
  1563. sprintf(buf, "WEB: /confadv (from %s)", addr.c_str());
  1564. sendLog(buf, LOGLEVEL_INFO);
  1565. }
  1566. httpServerHandleConfAdvPage();
  1567. }
  1568. });
  1569. httpServer.on("/confadd", []() {
  1570. if (!httpIsAuthenticatedAdmin())
  1571. return httpServer.requestAuthentication();
  1572. else
  1573. {
  1574. //sendLog("WEB: /confadd", LOGLEVEL_INFO);
  1575. if (confLog.logWebRequests)
  1576. {
  1577. String addr = httpServer.client().remoteIP().toString();
  1578. char buf[40];
  1579. sprintf(buf, "WEB: /confadd (from %s)", addr.c_str());
  1580. sendLog(buf, LOGLEVEL_INFO);
  1581. }
  1582. httpServerHandleConfAddPage();
  1583. }
  1584. });
  1585. httpServer.on("/conftime", []() {
  1586. if (!httpIsAuthenticatedAdmin())
  1587. return httpServer.requestAuthentication();
  1588. else
  1589. {
  1590. //sendLog("WEB: /conftime", LOGLEVEL_INFO);
  1591. if (confLog.logWebRequests)
  1592. {
  1593. String addr = httpServer.client().remoteIP().toString();
  1594. char buf[40];
  1595. sprintf(buf, "WEB: /conftime (from %s)", addr.c_str());
  1596. sendLog(buf, LOGLEVEL_INFO);
  1597. }
  1598. httpServerHandleConfTimePage();
  1599. }
  1600. });
  1601. httpServer.on("/conflog", []() {
  1602. if (!httpIsAuthenticatedAdmin())
  1603. return httpServer.requestAuthentication();
  1604. else
  1605. {
  1606. //sendLog("WEB: /conflog", LOGLEVEL_INFO);
  1607. if (confLog.logWebRequests)
  1608. {
  1609. String addr = httpServer.client().remoteIP().toString();
  1610. char buf[40];
  1611. sprintf(buf, "WEB: /conflog (from %s)", addr.c_str());
  1612. sendLog(buf, LOGLEVEL_INFO);
  1613. }
  1614. httpServerHandleConfLogPage();
  1615. }
  1616. });
  1617. httpServer.on("/redTemps", []() {
  1618. if (!httpIsAuthenticatedAdmin())
  1619. return httpServer.requestAuthentication();
  1620. else
  1621. {
  1622. //sendLog("WEB: /redTemps", LOGLEVEL_INFO);
  1623. if (confLog.logWebRequests)
  1624. {
  1625. String addr = httpServer.client().remoteIP().toString();
  1626. char buf[40];
  1627. sprintf(buf, "WEB: /redTemps (from %s)", addr.c_str());
  1628. sendLog(buf, LOGLEVEL_INFO);
  1629. }
  1630. httpServerHandleRedTempsPage();
  1631. }
  1632. });
  1633. #ifdef ENABLE_FEATURE_WEB_CONSOLE_WEBSOCKETS
  1634. if (confWeb.wsConsole)
  1635. {
  1636. httpServer.on("/wsconsole", []() {
  1637. if (!httpIsAuthenticatedAdmin())
  1638. return httpServer.requestAuthentication();
  1639. else
  1640. {
  1641. //sendLog("WEB: /wsconsole", LOGLEVEL_INFO);
  1642. if (confLog.logWebRequests)
  1643. {
  1644. String addr = httpServer.client().remoteIP().toString();
  1645. char buf[40];
  1646. sprintf(buf, "WEB: /wsconsole (from %s)", addr.c_str());
  1647. sendLog(buf, LOGLEVEL_INFO);
  1648. }
  1649. wsValidSessionId = millis();
  1650. char sidbuf[20];
  1651. sprintf(sidbuf, "sessionId=%lu|", wsValidSessionId);
  1652. httpServer.sendHeader("Set-Cookie", sidbuf);
  1653. httpServerHandleWSConsolePage();
  1654. }
  1655. });
  1656. httpServer.on("/wsapp.js", []() {
  1657. httpServer.sendHeader("cache-control", "max-age=86400", false);
  1658. httpServer.send_P(200, "text/javascript", js_wsapp);
  1659. });
  1660. }
  1661. #endif
  1662. httpServer.onNotFound([]() {
  1663. String addr = httpServer.client().remoteIP().toString();
  1664. if (confLog.logWebRequests)
  1665. {
  1666. char buf[40];
  1667. sprintf(buf, "WEB: 404 (from %s)", addr.c_str());
  1668. sendLog(buf, LOGLEVEL_INFO);
  1669. }
  1670. httpServerHandleNotFound();
  1671. }); //httpServer.onNotFound
  1672. #ifdef ENABLE_FEATURE_WEB_CONSOLE
  1673. if (confWeb.wConsole)
  1674. {
  1675. httpServer.on("/console", []() {
  1676. if (!httpIsAuthenticatedAdmin())
  1677. return httpServer.requestAuthentication();
  1678. else
  1679. {
  1680. //sendLog("WEB: /console", LOGLEVEL_INFO);
  1681. if (confLog.logWebRequests)
  1682. {
  1683. String addr = httpServer.client().remoteIP().toString();
  1684. char buf[40];
  1685. sprintf(buf, "WEB: /console (from %s)", addr.c_str());
  1686. sendLog(buf, LOGLEVEL_INFO);
  1687. }
  1688. httpServerHandleConsolePage();
  1689. }
  1690. });
  1691. httpServer.on("/webcsapi", []() {
  1692. if (!httpIsAuthenticatedAdmin())
  1693. return httpServer.requestAuthentication();
  1694. else
  1695. {
  1696. if (httpServer.hasArg("cmd"))
  1697. {
  1698. char buf[101];
  1699. httpServer.arg("cmd").toCharArray(buf, 100);
  1700. strlcpy(cmdPayload, buf, sizeof(cmdPayload));
  1701. cmdInQueue = true;
  1702. evalCmd();
  1703. }
  1704. if (confLog.logWebRequests)
  1705. {
  1706. String addr = httpServer.client().remoteIP().toString();
  1707. char buf[40];
  1708. sprintf(buf, "WEB: /conflog (from %s)", addr.c_str());
  1709. sendLog(buf, LOGLEVEL_INFO);
  1710. }
  1711. httpServer.send(200, "text/plain", webLogBuffer);
  1712. }
  1713. });
  1714. httpServer.on("/csapp.js", []() {
  1715. httpServer.sendHeader("cache-control", "max-age=86400", false);
  1716. httpServer.send_P(200, "text/javascript", js_csapp);
  1717. });
  1718. }
  1719. #endif
  1720. // HTTP Updater at /update
  1721. #ifdef ENABLE_FEATURE_HTTP_UPDATER
  1722. httpUpdater.setup(&httpServer, "/update", confWeb.http_user, confWeb.http_pass);
  1723. #endif
  1724. httpServer.begin();
  1725. }