httpServer.ino 56 KB

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