httpServer.ino 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  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_console.h"
  14. void httpServerHandleMainPage() {
  15. httpServerSendHtmlHeadChunked();
  16. httpServer.sendContent_P(html_main_script);
  17. httpServer.sendContent_P(html_head_end);
  18. httpServer.sendContent_P(html_bodytag_jsinit);
  19. httpServerSendHtmlBodyPageheadChunked();
  20. httpServer.sendContent_P(html_main_body);
  21. if (httpIsAuthenticatedAdmin()) {
  22. httpServer.sendContent_P(html_main_body_adminonly);
  23. if(confWeb.enableConsole) httpServer.sendContent_P(html_main_body_adminonly_console);
  24. }
  25. httpServer.sendContent_P(html_main_body2);
  26. httpServerSendHtmlFooterChunked();
  27. }
  28. void httpServerHandleRedTempsPage() {
  29. httpServerSendHtmlHeadChunked();
  30. httpServer.sendContent_P(html_redTemps_script);
  31. httpServer.sendContent_P(html_head_end);
  32. httpServer.sendContent_P(html_bodytag_jsinit);
  33. httpServerSendHtmlBodyPageheadChunked();
  34. httpServer.sendContent_P(html_redTemps_body);
  35. httpServerSendHtmlFooterChunked();
  36. }
  37. void httpServerHandleConfPage() {
  38. httpServerSendHtmlHeadChunked();
  39. httpServer.sendContent_P(html_head_end);
  40. httpServer.sendContent_P(html_bodytag);
  41. httpServerSendHtmlBodyPageheadChunked();
  42. httpServer.sendContent_P(html_conf_body);
  43. if(confWeb.enableConsole) httpServer.sendContent_P(html_conf_body_console);
  44. httpServer.sendContent_P(html_conf_body_end);
  45. httpServerSendHtmlFooterChunked();
  46. }
  47. void httpServerHandleConfWebPage() {
  48. httpServerSendHtmlHeadChunked();
  49. httpServer.sendContent_P(html_confweb_script);
  50. httpServer.sendContent_P(html_head_end);
  51. httpServer.sendContent_P(html_bodytag_jsinit);
  52. httpServerSendHtmlBodyPageheadChunked();
  53. httpServer.sendContent_P(html_confweb_body);
  54. httpServerSendHtmlFooterChunked();
  55. }
  56. void httpServerHandleConfMqttPage() {
  57. httpServerSendHtmlHeadChunked();
  58. httpServer.sendContent_P(html_confmqtt_script);
  59. httpServer.sendContent_P(html_head_end);
  60. httpServer.sendContent_P(html_bodytag_jsinit);
  61. httpServerSendHtmlBodyPageheadChunked();
  62. httpServer.sendContent_P(html_confmqtt_body);
  63. httpServerSendHtmlFooterChunked();
  64. }
  65. void httpServerHandleConfDevWiFiPage() {
  66. httpServerSendHtmlHeadChunked();
  67. httpServer.sendContent_P(html_confDevWiFi_script);
  68. httpServer.sendContent_P(html_head_end);
  69. httpServer.sendContent_P(html_bodytag_jsinit);
  70. httpServerSendHtmlBodyPageheadChunked();
  71. httpServer.sendContent_P(html_confDevWiFi_body);
  72. httpServerSendHtmlFooterChunked();
  73. }
  74. void httpServerHandleConfBasPage() {
  75. httpServerSendHtmlHeadChunked();
  76. httpServer.sendContent_P(html_confbas_script);
  77. httpServer.sendContent_P(html_head_end);
  78. httpServer.sendContent_P(html_bodytag_jsinit);
  79. httpServerSendHtmlBodyPageheadChunked();
  80. httpServer.sendContent_P(html_confbas_body);
  81. httpServerSendHtmlFooterChunked();
  82. }
  83. void httpServerHandleConfAdvPage() {
  84. httpServerSendHtmlHeadChunked();
  85. httpServer.sendContent_P(html_confadv_script);
  86. httpServer.sendContent_P(html_head_end);
  87. httpServer.sendContent_P(html_bodytag_jsinit);
  88. httpServerSendHtmlBodyPageheadChunked();
  89. httpServer.sendContent_P(html_confadv_body);
  90. httpServerSendHtmlFooterChunked();
  91. }
  92. void httpServerHandleConfAddPage() {
  93. httpServerSendHtmlHeadChunked();
  94. httpServer.sendContent_P(html_confadd_script);
  95. httpServer.sendContent_P(html_head_end);
  96. httpServer.sendContent_P(html_bodytag_jsinit);
  97. httpServerSendHtmlBodyPageheadChunked();
  98. httpServer.sendContent_P(html_confadd_body);
  99. httpServerSendHtmlFooterChunked();
  100. }
  101. void httpServerHandleConfTimePage() {
  102. httpServerSendHtmlHeadChunked();
  103. httpServer.sendContent_P(html_conftime_script);
  104. httpServer.sendContent_P(html_head_end);
  105. httpServer.sendContent_P(html_bodytag_jsinit);
  106. httpServerSendHtmlBodyPageheadChunked();
  107. httpServer.sendContent_P(html_conftime_body);
  108. httpServerSendHtmlFooterChunked();
  109. }
  110. void httpServerHandleConfLogPage() {
  111. httpServerSendHtmlHeadChunked();
  112. httpServer.sendContent_P(html_conflog_script);
  113. httpServer.sendContent_P(html_head_end);
  114. httpServer.sendContent_P(html_bodytag_jsinit);
  115. httpServerSendHtmlBodyPageheadChunked();
  116. httpServer.sendContent_P(html_conflog_body);
  117. httpServerSendHtmlFooterChunked();
  118. }
  119. void httpServerHandleConsolePage() {
  120. httpServerSendHtmlHeadChunked();
  121. httpServer.sendContent_P(html_head_end);
  122. httpServer.sendContent_P(html_bodytag);
  123. httpServerSendHtmlBodyPageheadChunked();
  124. httpServer.sendContent_P(html_console_body);
  125. httpServerSendHtmlFooterChunked();
  126. }
  127. void httpServerHandleConfSavedPage() {
  128. httpServerSendHtmlHeadChunked();
  129. httpServer.sendContent_P(html_head_end);
  130. httpServer.sendContent_P(html_bodytag);
  131. httpServerSendHtmlBodyPageheadChunked();
  132. httpServer.sendContent_P(html_confsaved_body);
  133. httpServerSendHtmlFooterChunked();
  134. }
  135. void httpServerHandleConfSavedRestartPage() {
  136. httpServerSendHtmlHeadChunked();
  137. httpServer.sendContent_P(html_head_end);
  138. httpServer.sendContent_P(html_bodytag);
  139. httpServerSendHtmlBodyPageheadChunked();
  140. httpServer.sendContent_P(html_confsavedrestart_body);
  141. httpServerSendHtmlFooterChunked();
  142. }
  143. void httpServerHandleRestartPage() {
  144. httpServerSendHtmlHeadChunked();
  145. httpServer.sendContent_P(html_head_end);
  146. httpServer.sendContent_P(html_bodytag);
  147. httpServerSendHtmlBodyPageheadChunked();
  148. httpServer.sendContent_P(html_restarting_body);
  149. httpServerSendHtmlFooterChunked();
  150. }
  151. void httpServerHandleClearconfPage() {
  152. httpServerSendHtmlHeadChunked();
  153. httpServer.sendContent_P(html_head_end);
  154. httpServer.sendContent_P(html_bodytag);
  155. httpServerSendHtmlBodyPageheadChunked();
  156. httpServer.sendContent_P(html_clearconf_body);
  157. httpServerSendHtmlFooterChunked();
  158. }
  159. void httpServerSendHtmlHeadChunked() {
  160. httpServer.setContentLength(CONTENT_LENGTH_UNKNOWN); //Enable Chunked Transfer
  161. httpServer.send(200, "text/html", html_head_part1);
  162. httpServer.sendContent(FIRMWARE_NAME);
  163. httpServer.sendContent(" - ");
  164. httpServer.sendContent(confDevWiFi.deviceName);
  165. httpServer.sendContent_P(html_head_part2);
  166. }
  167. void httpServerSendHtmlBodyPageheadChunked() {
  168. httpServer.sendContent_P(html_body_pagehead_part1);
  169. httpServer.sendContent(FIRMWARE_NAME);
  170. httpServer.sendContent(" - ");
  171. httpServer.sendContent(confDevWiFi.deviceName);
  172. httpServer.sendContent_P(html_body_pagehead_part2);
  173. if(config_was_changed) httpServer.sendContent_P(html_body_pagehead_confchangednote);
  174. }
  175. void httpServerSendHtmlFooterChunked() {
  176. httpServer.sendContent_P(html_footer1);
  177. httpServer.sendContent("<a href='");
  178. httpServer.sendContent(FIRMWARE_URL);
  179. httpServer.sendContent("' target='_blank' style='color:#AAA;'>");
  180. httpServer.sendContent(FIRMWARE_NAME);
  181. httpServer.sendContent("</a> v");
  182. httpServer.sendContent(FIRMWARE_VERSION);
  183. #ifdef DEBUGMODE
  184. httpServer.sendContent("&nbsp;<span style='color:red;font-weight:bold;'>DEBUG</span>");
  185. #endif
  186. httpServer.sendContent(" by <a href='");
  187. httpServer.sendContent(FIRMWARE_COPYRIGHT_URL);
  188. httpServer.sendContent("' target='_blank' style='color:#AAA;'>");
  189. httpServer.sendContent(FIRMWARE_COPYRIGHT);
  190. httpServer.sendContent("</a>");
  191. httpServer.sendContent_P(html_footer2);
  192. httpServer.sendContent("");
  193. httpServer.client().stop();
  194. }
  195. boolean httpIsAuthenticated() {
  196. if (confWeb.http_user_auth) {
  197. boolean useAuth = false;
  198. boolean isAuthenticated = false;
  199. // user authentication enabled - only allow access without authentication if no admin credentials are set
  200. // or if user1 credentials are empty
  201. if ((strlen(confWeb.http_user) > 0 && strlen(confWeb.http_pass) > 0) || (strlen(confWeb.http_user1) > 0 && strlen(confWeb.http_pass1) > 0) || (strlen(confWeb.http_user2) > 0 && strlen(confWeb.http_pass2) > 0)) useAuth = true;
  202. if (useAuth) {
  203. // allow access if one set of valid credentials are given
  204. if ((strlen(confWeb.http_user) > 0 && strlen(confWeb.http_pass) > 0)) {
  205. if (httpServer.authenticate(confWeb.http_user, confWeb.http_pass)) isAuthenticated = true;
  206. }
  207. if ((strlen(confWeb.http_user1) > 0 && strlen(confWeb.http_pass1) > 0)) {
  208. if (httpServer.authenticate(confWeb.http_user1, confWeb.http_pass1)) isAuthenticated = true;
  209. }
  210. if ((strlen(confWeb.http_user2) > 0 && strlen(confWeb.http_pass2) > 0)) {
  211. if (httpServer.authenticate(confWeb.http_user2, confWeb.http_pass2)) isAuthenticated = true;
  212. }
  213. // also allow access if admin credentials are set but user1 credentials are empty and user-authentication is enabled
  214. if ((strlen(confWeb.http_user1) == 0 && strlen(confWeb.http_pass1) == 0)) {
  215. isAuthenticated = true;
  216. }
  217. return isAuthenticated;
  218. }
  219. // allow access if all credentials are empty
  220. else return true;
  221. }
  222. // user authentication disabled - only allow access without password if no admin credentials are set
  223. else {
  224. if ((strlen(confWeb.http_user) > 0 && strlen(confWeb.http_pass) > 0)) {
  225. if (httpServer.authenticate(confWeb.http_user, confWeb.http_pass)) return true;
  226. else return false;
  227. }
  228. else return true;
  229. }
  230. }
  231. boolean httpIsAuthenticatedAdmin() {
  232. boolean auth = false;
  233. if ((strlen(confWeb.http_user) > 0 && strlen(confWeb.http_pass) > 0)) auth = true;
  234. if (auth) {
  235. if (!httpServer.authenticate(confWeb.http_user, confWeb.http_pass)) return false;
  236. else return true;
  237. }
  238. else return true;
  239. }
  240. void httpServerHandleNotFound() {
  241. httpServer.send(404, "text/plain", "404 NOT FOUND");
  242. }
  243. void httpSendUnauthorized() {
  244. httpServer.send (401, "text/plain", "UNAUTHORIZED");
  245. }
  246. void httpSend200OK() {
  247. httpServer.send (200, "text/plain", "OK");
  248. }
  249. boolean httpCheckToken() {
  250. if (confWeb.http_token[0] != '\0') { // dont accept empty token
  251. if (httpServer.hasArg("token")) {
  252. char buf[20];
  253. httpServer.arg("token").toCharArray(buf, 20);
  254. if (strcmp(buf, confWeb.http_token) == 0) return true;
  255. else return false;
  256. }
  257. else return false;
  258. }
  259. else return false;
  260. }
  261. void httpServerInit() {
  262. httpServer.on("/style.css", []() {
  263. httpServer.sendHeader("cache-control", "max-age=86400", false);
  264. httpServer.send_P(200, "text/css", html_stylesheet);
  265. });
  266. httpServer.on("/wsapp.js", []() {
  267. httpServer.sendHeader("cache-control", "max-age=86400", false);
  268. httpServer.send_P(200, "text/javascript", js_wsapp);
  269. });
  270. httpServer.on("/api", []() {
  271. if (!httpIsAuthenticated()) return httpServer.requestAuthentication();
  272. else {
  273. String addr = httpServer.client().remoteIP().toString();
  274. char buf[40];
  275. sprintf(buf, "WEB: /api (from %s)", addr.c_str());
  276. sendLog(buf, LOGLEVEL_INFO);
  277. //sendLog("WEB: /api", LOGLEVEL_INFO);
  278. if (httpServer.hasArg("BtnPlus")) {
  279. setTempStepUp();
  280. sendLog(F("WEB: Btn +"), LOGLEVEL_INFO);
  281. } //if
  282. if (httpServer.hasArg("BtnMinus")) {
  283. setTempStepDown();
  284. sendLog(F("WEB: Btn -"), LOGLEVEL_INFO);
  285. } //if
  286. if (httpServer.hasArg("BtnPset0")) {
  287. setPresetTo(0);
  288. sendLog(F("WEB: Btn PSet0"), LOGLEVEL_INFO);
  289. } //if
  290. if (httpServer.hasArg("BtnPset1")) {
  291. setPresetTo(1);
  292. sendLog(F("WEB: Btn PSet1"), LOGLEVEL_INFO);
  293. } //if
  294. if (httpServer.hasArg("BtnPset2")) {
  295. setPresetTo(2);
  296. sendLog(F("WEB: Btn PSet2"), LOGLEVEL_INFO);
  297. } //if
  298. if (httpServer.hasArg("BtnOn")) {
  299. setHeatingmodeTo(1);
  300. sendLog(F("WEB: Btn ON"), LOGLEVEL_INFO);
  301. } //if
  302. if (httpServer.hasArg("BtnOff")) {
  303. setHeatingmodeTo(0);
  304. sendLog(F("WEB: Btn OFF"), LOGLEVEL_INFO);
  305. } //if
  306. if (httpServer.hasArg("BtnL1Plus")) {
  307. setTempLowStepUp();
  308. sendLog(F("WEB: Btn L1+"), LOGLEVEL_INFO);
  309. } //if
  310. if (httpServer.hasArg("BtnL1Minus")) {
  311. setTempLowStepDown();
  312. sendLog(F("WEB: Btn L1-"), LOGLEVEL_INFO);
  313. } //if
  314. if (httpServer.hasArg("BtnL2Plus")) {
  315. setTempLow2StepUp();
  316. sendLog(F("WEB: Btn L2+"), LOGLEVEL_INFO);
  317. } //if
  318. if (httpServer.hasArg("BtnL2Minus")) {
  319. setTempLow2StepDown();
  320. sendLog(F("WEB: Btn L2-"), LOGLEVEL_INFO);
  321. } //if
  322. if (httpServer.hasArg("setTemp")) {
  323. sendLog(F("WEB: api?setTemp"), LOGLEVEL_INFO);
  324. char bufVal[20];
  325. httpServer.arg("setTemp").toCharArray(bufVal, 20);
  326. float valueFloat = round(atof(bufVal) * 2.0) / 2.0;
  327. setTempTo(valueFloat);
  328. }
  329. if (httpServer.hasArg("setMode")) {
  330. sendLog(F("WEB: api?setMode"), LOGLEVEL_INFO);
  331. char bufVal[20];
  332. httpServer.arg("setMode").toCharArray(bufVal, 20);
  333. int valueInt = atoi(bufVal);
  334. if (valueInt >= 0 && valueInt <= 1) setHeatingmodeTo(valueInt);
  335. }
  336. if (httpServer.hasArg("setPreset")) {
  337. sendLog(F("WEB: api?setPreset"), LOGLEVEL_INFO);
  338. char bufVal[20];
  339. httpServer.arg("setPreset").toCharArray(bufVal, 20);
  340. int valueInt = atoi(bufVal);
  341. if (valueInt >= 0 && valueInt <= 2) setPresetTo(valueInt);
  342. }
  343. //char ch_currTemp[6];
  344. //char ch_currSetTemp[6];
  345. //dtostrf(currTemp, 1, 1, ch_currTemp );
  346. //dtostrf(setTemp, 1, 1, ch_currSetTemp );
  347. //build json object of program data
  348. StaticJsonDocument<700> json;
  349. json["devname"] = confDevWiFi.deviceName;
  350. json["ssid"] = WiFi.SSID();
  351. json["WiFiNum"] = persWM.getActiveWiFiNum();
  352. json["uptime"] = uptimeStr;
  353. //json["uptime"] = NTP.getUptimeString();
  354. if(confTime.ntpEnable) {
  355. char buf[13];
  356. updateTime();
  357. strftime(buf, sizeof(buf), "%H:%M", &lt);
  358. json["time"] = buf;
  359. strftime(buf, sizeof(buf), "%d.%m.%Y", &lt);
  360. json["date"] = buf;
  361. }
  362. json["freeheap"] = ESP.getFreeHeap();
  363. mqtt_updateCurrentStateName();
  364. json["mqttstate"] = mqttCurrentStateName;
  365. //if (mqttclient.state() == -4) json["mqttstate"] = "CONNECTION_TIMEOUT";
  366. //else if (mqttclient.state() == -3) json["mqttstate"] = "CONNECTION_LOST";
  367. //else if (mqttclient.state() == -2) json["mqttstate"] = "CONNECT_FAILED";
  368. //else if (mqttclient.state() == -1) json["mqttstate"] = "DISCONNECTED";
  369. //else if (mqttclient.state() == 0) json["mqttstate"] = "CONNECTED";
  370. //else if (mqttclient.state() == 1) json["mqttstate"] = "CONNECT_BAD_PROTOCOL";
  371. //else if (mqttclient.state() == 2) json["mqttstate"] = "CONNECT_BAD_CLIENT_ID";
  372. //else if (mqttclient.state() == 3) json["mqttstate"] = "CONNECT_UNAVAILABLE";
  373. //else if (mqttclient.state() == 4) json["mqttstate"] = "CONNECT_BAD_CREDENTIALS";
  374. //else if (mqttclient.state() == 5) json["mqttstate"] = "CONNECT_UNAUTHORIZED";
  375. //if (mqttclient.state() == -4) json["mqttstate"] = PGMStr_MQTTStateM4;
  376. //else if (mqttclient.state() == -3) json["mqttstate"] = PGMStr_MQTTStateM3;
  377. //else if (mqttclient.state() == -2) json["mqttstate"] = PGMStr_MQTTStateM2;
  378. //else if (mqttclient.state() == -1) json["mqttstate"] = PGMStr_MQTTStateM1;
  379. //else if (mqttclient.state() == 0) json["mqttstate"] = PGMStr_MQTTState0;
  380. //else if (mqttclient.state() == 1) json["mqttstate"] = PGMStr_MQTTState1;
  381. //else if (mqttclient.state() == 2) json["mqttstate"] = PGMStr_MQTTState2;
  382. //else if (mqttclient.state() == 3) json["mqttstate"] = PGMStr_MQTTState3;
  383. //else if (mqttclient.state() == 4) json["mqttstate"] = PGMStr_MQTTState4;
  384. //else if (mqttclient.state() == 5) json["mqttstate"] = PGMStr_MQTTState5;
  385. json["mqtthost"] = confMqtt.mqtt_server;
  386. if (mqttReconnects < 1) json["mqttreconn"] = mqttReconnects;
  387. else json["mqttreconn"] = mqttReconnects - 1;
  388. json["setTemp"] = setTemp;
  389. json["currSetTemp"] = currSetTemp;
  390. json["temp"] = round1(currTemp);
  391. json["hum"] = int(currHum);
  392. json["heating"] = turnHeatingOn;
  393. json["mode"] = heatingMode;
  394. json["modeName"] = currentModeName;
  395. json["pset"] = preset;
  396. json["psetName"] = currentPresetName;
  397. json["psetName0"] = confAdv.psetName0;
  398. json["psetName1"] = confAdv.psetName1;
  399. json["psetName2"] = confAdv.psetName2;
  400. json["tempLow"] = setTempLow;
  401. json["tempLow2"] = setTempLow2;
  402. json["outTemp"] = round1(outTemp);
  403. json["outHum"] = outHum;
  404. char jsonchar[500];
  405. serializeJson(json, jsonchar);
  406. httpServer.send(200, "application/json", jsonchar);
  407. }
  408. }); //httpServer.on /api
  409. //httpServer.on("/setTemp", []() {
  410. // if ( httpIsAuthenticated() || (!httpIsAuthenticated() && httpCheckToken()) ) {
  411. // //Serial.println("web triggered setTemp");
  412. // sendLog(F("WEB: /setTemp"), LOGLEVEL_INFO);
  413. // if (httpServer.hasArg("value")) {
  414. // char bufVal[20];
  415. // httpServer.arg("value").toCharArray(bufVal, 20);
  416. // float valueFloat = round(atof(bufVal) * 2.0) / 2.0;
  417. // setTempTo(valueFloat);
  418. // httpSend200OK();
  419. // }
  420. // } //if
  421. // else httpSendUnauthorized();
  422. //});
  423. //httpServer.on("/setMode", []() {
  424. // if ( httpIsAuthenticated() || (!httpIsAuthenticated() && httpCheckToken()) ) {
  425. // //Serial.println("web triggered setMode");
  426. // sendLog(F("WEB: /setMode"), LOGLEVEL_INFO);
  427. // if (httpServer.hasArg("value")) {
  428. // char bufVal[20];
  429. // httpServer.arg("value").toCharArray(bufVal, 20);
  430. // int valueInt = atoi(bufVal);
  431. // if (valueInt >= 0 && valueInt <= 1) setHeatingmodeTo(valueInt);
  432. // httpSend200OK();
  433. // }
  434. // } //if
  435. // else httpSendUnauthorized();
  436. //});
  437. //httpServer.on("/setPreset", []() {
  438. // if ( httpIsAuthenticated() || (!httpIsAuthenticated() && httpCheckToken()) ) {
  439. // //Serial.println("web triggered setPreset");
  440. // sendLog(F("WEB: /setPreset"), LOGLEVEL_INFO);
  441. // if (httpServer.hasArg("value")) {
  442. // char bufVal[20];
  443. // httpServer.arg("value").toCharArray(bufVal, 20);
  444. // int valueInt = atoi(bufVal);
  445. // if (valueInt >= 0 && valueInt <= 2) setPresetTo(valueInt);
  446. // httpSend200OK();
  447. // }
  448. // } //if
  449. // else httpSendUnauthorized();
  450. //});
  451. httpServer.on("/confDataWeb", []() {
  452. if (!httpIsAuthenticatedAdmin()) httpSendUnauthorized();
  453. else {
  454. //Serial.println("httpServer.on /confdata");
  455. //sendLog("WEB: /confDataWeb", LOGLEVEL_INFO);
  456. String addr = httpServer.client().remoteIP().toString();
  457. char buf[40];
  458. sprintf(buf, "WEB: /confDataWeb (from %s)", addr.c_str());
  459. sendLog(buf, LOGLEVEL_INFO);
  460. StaticJsonDocument<1000> json;
  461. json["apiToken"] = confWeb.http_token;
  462. json["httpUA"] = confWeb.http_user;
  463. //json["httpPA"] = confWeb.http_pass;
  464. if(strlen(confWeb.http_pass) > 0) json["httpPA"] = "****";
  465. else json["httpPA"] = "";
  466. if (confWeb.http_user_auth) json["httpAuth"] = 1;
  467. else json["httpAuth"] = 0;
  468. json["httpU1"] = confWeb.http_user1;
  469. //json["httpP1"] = confWeb.http_pass1;
  470. if(strlen(confWeb.http_pass1) > 0) json["httpP1"] = "****";
  471. else json["httpP1"] = "";
  472. json["httpU2"] = confWeb.http_user2;
  473. //json["httpP2"] = confWeb.http_pass2;
  474. if(strlen(confWeb.http_pass2) > 0) json["httpP2"] = "****";
  475. else json["httpP2"] = "";
  476. if (confWeb.enableConsole) json["enableConsole"] = 1;
  477. else json["enableConsole"] = 0;
  478. yield();
  479. char jsonchar[1000];
  480. serializeJson(json, jsonchar);
  481. httpServer.send(200, "application/json", jsonchar);
  482. }
  483. }); //httpServer.on /confdweb
  484. httpServer.on("/confDataMqtt", []() {
  485. if (!httpIsAuthenticatedAdmin()) httpSendUnauthorized();
  486. else {
  487. //sendLog("WEB: /confDataMqtt", LOGLEVEL_INFO);
  488. String addr = httpServer.client().remoteIP().toString();
  489. char buf[40];
  490. sprintf(buf, "WEB: /confDataMqtt (from %s)", addr.c_str());
  491. sendLog(buf, LOGLEVEL_INFO);
  492. StaticJsonDocument<1000> json;
  493. if (confMqtt.mqtt_enable) json["mqttEnable"] = 1;
  494. else json["mqttEnable"] = 0;
  495. json["mqttHost"] = confMqtt.mqtt_server;
  496. json["mqttPort"] = confMqtt.mqtt_port;
  497. json["mqttUser"] = confMqtt.mqtt_user;
  498. //json["mqttPass"] = confMqtt.mqtt_pass;
  499. if(strlen(confMqtt.mqtt_pass) > 0) json["mqttPass"] = "****";
  500. else json["mqttPass"] = "";
  501. json["inTop"] = confMqtt.mqtt_topic_in;
  502. json["outTop"] = confMqtt.mqtt_topic_out;
  503. if (confMqtt.mqtt_outRetain) json["outRet"] = 1;
  504. else json["outRet"] = 0;
  505. if (confMqtt.mqtt_outRetain_sensors) json["outRetSens"] = 1;
  506. else json["outRetSens"] = 0;
  507. json["outPubInt"] = confMqtt.mqtt_outPubInterval;
  508. json["outPubIntSens"] = confMqtt.mqtt_outPubInterval_sensors;
  509. json["willTop"] = confMqtt.mqtt_willTopic;
  510. json["willQos"] = confMqtt.mqtt_willQos;
  511. if (confMqtt.mqtt_willRetain) json["willRet"] = 1;
  512. else json["willRet"] = 0;
  513. json["willMsg"] = confMqtt.mqtt_willMsg;
  514. json["connMsg"] = confMqtt.mqtt_connMsg;
  515. if (confMqtt.mqtt_enable_heartbeat) json["hbEnable"] = 1;
  516. else json["hbEnable"] = 0;
  517. json["hbReconn"] = confMqtt.mqtt_heartbeat_maxage_reconnect / 60000;
  518. json["hbReboot"] = confMqtt.mqtt_heartbeat_maxage_reboot / 60000;
  519. yield();
  520. char jsonchar[1000];
  521. serializeJson(json, jsonchar);
  522. httpServer.send(200, "application/json", jsonchar);
  523. }
  524. }); //httpServer.on /confdmqtt
  525. httpServer.on("/setConfWeb", []() {
  526. if (!httpIsAuthenticatedAdmin()) httpSendUnauthorized();
  527. else {
  528. //sendLog("WEB: /setConfWeb", LOGLEVEL_INFO);
  529. String addr = httpServer.client().remoteIP().toString();
  530. char buf[40];
  531. sprintf(buf, "WEB: /setConfWeb (from %s)", addr.c_str());
  532. sendLog(buf, LOGLEVEL_INFO);
  533. bool httpPASet, httpP1Set, httpP2Set;
  534. httpPASet = false;
  535. httpP1Set = false;
  536. httpP2Set = false;
  537. if (httpServer.hasArg("httpPASet")) httpPASet = true;
  538. if (httpServer.hasArg("httpP1Set")) httpP1Set = true;
  539. if (httpServer.hasArg("httpP2Set")) httpP2Set = true;
  540. for (int i = 0; i < httpServer.args(); i++) {
  541. char bufName[20];
  542. char bufValue[101];
  543. httpServer.argName(i).toCharArray(bufName, 20);
  544. httpServer.arg(i).toCharArray(bufValue, 101);
  545. if (strlen(bufName) > 0) {
  546. Serial.print("web update ");
  547. Serial.print(bufName);
  548. Serial.print(" = ");
  549. Serial.println(bufValue);
  550. if (strcmp(bufName, "httpUA") == 0 || strcmp(bufName, "httpPA") == 0) {
  551. if (httpPASet) setConfig(bufName, bufValue);
  552. }
  553. else if (strcmp(bufName, "httpU1") == 0 || strcmp(bufName, "httpP1") == 0) {
  554. if (httpP1Set) setConfig(bufName, bufValue);
  555. }
  556. else if (strcmp(bufName, "httpU2") == 0 || strcmp(bufName, "httpP2") == 0) {
  557. if (httpP2Set) setConfig(bufName, bufValue);
  558. }
  559. else if (strcmp(bufName, "httpPASet") != 0 && strcmp(bufName, "httpP1Set") != 0 && strcmp(bufName, "httpP2Set") != 0) setConfig(bufName, bufValue);
  560. //else setConfig(bufName, bufValue);
  561. }
  562. //saveConfigWebToFlash = true; // will be saved in next loop()
  563. //Serial.println("web triggered saveConfigWebToFlash");
  564. }
  565. yield();
  566. saveConfigWeb();
  567. httpServerHandleConfSavedPage();
  568. config_was_changed = true;
  569. // yield();
  570. // ESP.restart();
  571. }
  572. }); //httpServer.on /setConfWeb
  573. httpServer.on("/setConfMqtt", []() {
  574. if (!httpIsAuthenticatedAdmin()) httpSendUnauthorized();
  575. else {
  576. //Serial.println("httpServer.on /setConfMqtt");
  577. //sendLog("WEB: /setConfMqtt", LOGLEVEL_INFO);
  578. String addr = httpServer.client().remoteIP().toString();
  579. char buf[40];
  580. sprintf(buf, "WEB: /setConfMqtt (from %s)", addr.c_str());
  581. sendLog(buf, LOGLEVEL_INFO);
  582. bool mqttPassSet;
  583. mqttPassSet = false;
  584. if (httpServer.hasArg("mqttPassSet")) mqttPassSet = true;
  585. for (int i = 0; i < httpServer.args(); i++) {
  586. char bufName[20];
  587. char bufValue[101];
  588. httpServer.argName(i).toCharArray(bufName, 20);
  589. httpServer.arg(i).toCharArray(bufValue, 101);
  590. if (strlen(bufName) > 0) {
  591. Serial.print("web update ");
  592. Serial.print(bufName);
  593. Serial.print(" = ");
  594. Serial.println(bufValue);
  595. if (strcmp(bufName, "mqttUser") == 0 || strcmp(bufName, "mqttPass") == 0) {
  596. if (mqttPassSet) setConfig(bufName, bufValue);
  597. }
  598. else setConfig(bufName, bufValue);
  599. }
  600. //saveConfigMqttToFlash = true; // will be saved in next loop()
  601. //Serial.println("web triggered saveConfigMqttToFlash");
  602. }
  603. yield();
  604. saveConfigMqtt();
  605. httpServerHandleConfSavedRestartPage();
  606. config_was_changed = true;
  607. restart();
  608. }
  609. }); //httpServer.on /setConfMqtt
  610. httpServer.on("/confDataDevWiFi", []() {
  611. if (!httpIsAuthenticated()) return httpServer.requestAuthentication();
  612. else {
  613. //sendLog("WEB: /confDataDevWiFi", LOGLEVEL_INFO);
  614. String addr = httpServer.client().remoteIP().toString();
  615. char buf[40];
  616. sprintf(buf, "WEB: /confDataDevWiFi (from %s)", addr.c_str());
  617. sendLog(buf, LOGLEVEL_INFO);
  618. for (int i = 0; i < httpServer.args(); i++) {
  619. char bufName[20];
  620. char bufValue[101];
  621. httpServer.argName(i).toCharArray(bufName, 20);
  622. httpServer.arg(i).toCharArray(bufValue, 101);
  623. if (strlen(bufName) > 0) {
  624. //Serial.print("web update ");
  625. //Serial.print(bufName);
  626. //Serial.print(" = ");
  627. //Serial.println(bufValue);
  628. setConfig(bufName, bufValue);
  629. }
  630. saveConfigToFlash = true; // will be saved in next loop()
  631. //Serial.println("web triggered saveConfigToFlash");
  632. }
  633. yield();
  634. //build json object of program data
  635. StaticJsonDocument<800> json;
  636. json["devName"] = confDevWiFi.deviceName;
  637. json["hostName"] = confDevWiFi.hostName;
  638. json["SSID1"] = confDevWiFi.WiFiSSID1;
  639. //json["WPW1"] = confDevWiFi.WiFiPW1;
  640. if(strlen(confDevWiFi.WiFiPW1) > 0) json["WPW1"] = "****";
  641. else json["WPW1"] = "";
  642. json["SSID2"] = confDevWiFi.WiFiSSID2;
  643. //json["WPW2"] = confDevWiFi.WiFiPW2;
  644. if(strlen(confDevWiFi.WiFiPW2) > 0) json["WPW2"] = "****";
  645. else json["WPW2"] = "";
  646. json["SSIDAP"] = confDevWiFi.WiFiAPModeSSID;
  647. json["WPWAP"] = confDevWiFi.WiFiAPModePassword;
  648. json["WAPtout"] = confDevWiFi.WiFiAPModeTimeout;
  649. json["WConnCheck"] = confDevWiFi.WiFiConnCheckInterval;
  650. json["Wretry"] = confDevWiFi.WiFiRetryInterval;
  651. json["Wreboot"] = confDevWiFi.WiFiRebootOnNoConnect;
  652. yield();
  653. char jsonchar[1000];
  654. serializeJson(json, jsonchar);
  655. httpServer.send(200, "application/json", jsonchar);
  656. }
  657. }); //httpServer.on /getConf/DevWiFi
  658. httpServer.on("/setConfDevWiFi", []() {
  659. if (!httpIsAuthenticatedAdmin()) httpSendUnauthorized();
  660. else {
  661. //sendLog("WEB: /setConfDevWiFi", LOGLEVEL_INFO);
  662. String addr = httpServer.client().remoteIP().toString();
  663. char buf[40];
  664. sprintf(buf, "WEB: /setConfDevWiFi (from %s)", addr.c_str());
  665. sendLog(buf, LOGLEVEL_INFO);
  666. bool WPW1Set, WPW2Set, WPWAPSet;
  667. WPW1Set = false;
  668. WPW2Set = false;
  669. WPWAPSet = false;
  670. if (httpServer.hasArg("WPW1Set")) WPW1Set = true;
  671. if (httpServer.hasArg("WPW2Set")) WPW2Set = true;
  672. if (httpServer.hasArg("WPWAPSet")) WPWAPSet = true;
  673. for (int i = 0; i < httpServer.args(); i++) {
  674. char bufName[20];
  675. char bufValue[101];
  676. httpServer.argName(i).toCharArray(bufName, 20);
  677. httpServer.arg(i).toCharArray(bufValue, 101);
  678. if (strlen(bufName) > 0) {
  679. Serial.print("web update ");
  680. Serial.print(bufName);
  681. Serial.print(" = ");
  682. Serial.println(bufValue);
  683. if (strcmp(bufName, "SSID1") == 0 || strcmp(bufName, "WPW1") == 0) {
  684. if (WPW1Set) setConfig(bufName, bufValue);
  685. }
  686. else if (strcmp(bufName, "SSID2") == 0 || strcmp(bufName, "WPW2") == 0) {
  687. if (WPW2Set) setConfig(bufName, bufValue);
  688. }
  689. else if (strcmp(bufName, "WPWAP") == 0) {
  690. if (WPWAPSet) setConfig(bufName, bufValue);
  691. }
  692. else setConfig(bufName, bufValue);
  693. }
  694. }
  695. yield();
  696. saveConfigDevWiFi();
  697. httpServerHandleConfSavedRestartPage();
  698. config_was_changed = true;
  699. restart();
  700. }
  701. }); //httpServer.on /setConfDev
  702. httpServer.on("/setConfBas", []() {
  703. if (!httpIsAuthenticatedAdmin()) httpSendUnauthorized();
  704. else {
  705. //sendLog("WEB: /setConfBas", LOGLEVEL_INFO);
  706. String addr = httpServer.client().remoteIP().toString();
  707. char buf[40];
  708. sprintf(buf, "WEB: /setConfBas (from %s)", addr.c_str());
  709. sendLog(buf, LOGLEVEL_INFO);
  710. for (int i = 0; i < httpServer.args(); i++) {
  711. char bufName[20];
  712. char bufValue[101];
  713. httpServer.argName(i).toCharArray(bufName, 20);
  714. httpServer.arg(i).toCharArray(bufValue, 101);
  715. if (strlen(bufName) > 0) {
  716. Serial.print("web update ");
  717. Serial.print(bufName);
  718. Serial.print(" = ");
  719. Serial.println(bufValue);
  720. setConfig(bufName, bufValue);
  721. }
  722. }
  723. yield();
  724. saveConfigBas();
  725. httpServerHandleConfSavedPage();
  726. config_was_changed = true;
  727. // delay(10);
  728. // yield();
  729. // ESP.restart();
  730. }
  731. }); //httpServer.on /setConfBas
  732. httpServer.on("/setConfAdv", []() {
  733. if (!httpIsAuthenticatedAdmin()) httpSendUnauthorized();
  734. else {
  735. //sendLog("WEB: /setConfAdv", LOGLEVEL_INFO);
  736. String addr = httpServer.client().remoteIP().toString();
  737. char buf[40];
  738. sprintf(buf, "WEB: /setConfAdv (from %s)", addr.c_str());
  739. sendLog(buf, LOGLEVEL_INFO);
  740. for (int i = 0; i < httpServer.args(); i++) {
  741. char bufName[20];
  742. char bufValue[101];
  743. httpServer.argName(i).toCharArray(bufName, 20);
  744. httpServer.arg(i).toCharArray(bufValue, 101);
  745. if (strlen(bufName) > 0) {
  746. Serial.print("web update ");
  747. Serial.print(bufName);
  748. Serial.print(" = ");
  749. Serial.println(bufValue);
  750. setConfig(bufName, bufValue);
  751. }
  752. }
  753. yield();
  754. saveConfigAdv();
  755. httpServerHandleConfSavedPage();
  756. config_was_changed = true;
  757. // delay(10);
  758. // yield();
  759. // ESP.restart();
  760. }
  761. }); //httpServer.on /setConfAdv
  762. httpServer.on("/setConfAdd", []() {
  763. if (!httpIsAuthenticatedAdmin()) httpSendUnauthorized();
  764. else {
  765. //sendLog("WEB: /setConfAdd", LOGLEVEL_INFO);
  766. String addr = httpServer.client().remoteIP().toString();
  767. char buf[40];
  768. sprintf(buf, "WEB: /setConfAdd (from %s)", addr.c_str());
  769. sendLog(buf, LOGLEVEL_INFO);
  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. if (strlen(bufName) > 0) {
  776. Serial.print("web update ");
  777. Serial.print(bufName);
  778. Serial.print(" = ");
  779. Serial.println(bufValue);
  780. setConfig(bufName, bufValue);
  781. }
  782. }
  783. yield();
  784. saveConfigAdd();
  785. httpServerHandleConfSavedPage();
  786. config_was_changed = true;
  787. // delay(10);
  788. // yield();
  789. // ESP.restart();
  790. }
  791. }); //httpServer.on /setConfAdd
  792. httpServer.on("/setConfTime", []() {
  793. if (!httpIsAuthenticatedAdmin()) httpSendUnauthorized();
  794. else {
  795. //sendLog("WEB: /setConfTime", LOGLEVEL_INFO);
  796. String addr = httpServer.client().remoteIP().toString();
  797. char buf[40];
  798. sprintf(buf, "WEB: /setConfTime (from %s)", addr.c_str());
  799. sendLog(buf, LOGLEVEL_INFO);
  800. for (int i = 0; i < httpServer.args(); i++) {
  801. char bufName[20];
  802. char bufValue[101];
  803. httpServer.argName(i).toCharArray(bufName, 20);
  804. httpServer.arg(i).toCharArray(bufValue, 101);
  805. if (strlen(bufName) > 0) {
  806. Serial.print("web update ");
  807. Serial.print(bufName);
  808. Serial.print(" = ");
  809. Serial.println(bufValue);
  810. setConfig(bufName, bufValue);
  811. }
  812. }
  813. yield();
  814. saveConfigTime();
  815. httpServerHandleConfSavedPage();
  816. config_was_changed = true;
  817. // delay(10);
  818. // yield();
  819. // ESP.restart();
  820. }
  821. }); //httpServer.on /setConfTime
  822. httpServer.on("/setConfLog", []() {
  823. if (!httpIsAuthenticatedAdmin()) httpSendUnauthorized();
  824. else {
  825. //sendLog("WEB: /setConfLog", LOGLEVEL_INFO);
  826. String addr = httpServer.client().remoteIP().toString();
  827. char buf[40];
  828. sprintf(buf, "WEB: /setConfLog (from %s)", addr.c_str());
  829. sendLog(buf, LOGLEVEL_INFO);
  830. for (int i = 0; i < httpServer.args(); i++) {
  831. char bufName[20];
  832. char bufValue[101];
  833. httpServer.argName(i).toCharArray(bufName, 20);
  834. httpServer.arg(i).toCharArray(bufValue, 101);
  835. if (strlen(bufName) > 0) {
  836. Serial.print("web update ");
  837. Serial.print(bufName);
  838. Serial.print(" = ");
  839. Serial.println(bufValue);
  840. setConfig(bufName, bufValue);
  841. }
  842. }
  843. yield();
  844. saveConfigLog();
  845. httpServerHandleConfSavedPage();
  846. config_was_changed = true;
  847. // delay(10);
  848. // yield();
  849. // ESP.restart();
  850. }
  851. }); //httpServer.on /setConfLog
  852. httpServer.on("/confDataBas", []() {
  853. if (!httpIsAuthenticated()) return httpServer.requestAuthentication();
  854. else {
  855. //sendLog("WEB: /confDataBas", LOGLEVEL_INFO);
  856. //Serial.println("httpServer.on /confdata2");
  857. String addr = httpServer.client().remoteIP().toString();
  858. char buf[40];
  859. sprintf(buf, "WEB: /confDataBas (from %s)", addr.c_str());
  860. sendLog(buf, LOGLEVEL_INFO);
  861. for (int i = 0; i < httpServer.args(); i++) {
  862. char bufName[20];
  863. char bufValue[101];
  864. httpServer.argName(i).toCharArray(bufName, 20);
  865. httpServer.arg(i).toCharArray(bufValue, 101);
  866. if (strlen(bufName) > 0) {
  867. //Serial.print("web update ");
  868. //Serial.print(bufName);
  869. //Serial.print(" = ");
  870. //Serial.println(bufValue);
  871. setConfig(bufName, bufValue);
  872. }
  873. saveConfig2ToFlash = true;
  874. //Serial.println("web triggered saveConfig2ToFlash");
  875. }
  876. yield();
  877. //build json object of program data
  878. StaticJsonDocument<500> json;
  879. if (confBas.autoSaveSetTemp) json["autoSaveTemp"] = 1;
  880. else json["autoSaveTemp"] = 0;
  881. if (confBas.autoSaveHeatingMode) json["autoSaveMode"] = 1;
  882. else json["autoSaveMode"] = 0;
  883. if (confBas.saveToMqttRetained) json["saveToMqttRet"] = 1;
  884. else json["saveToMqttRet"] = 0;
  885. json["tempMin"] = confBas.setTempMin;
  886. json["tempMax"] = confBas.setTempMax;
  887. json["measInt"] = confBas.measureInterval;
  888. json["dispInt"] = confBas.displayInterval;
  889. json["dispTout"] = confBas.displayTimeout;
  890. if (confBas.PIR_enablesDisplay) json["PIRenDisp"] = 1;
  891. else json["PIRenDisp"] = 0;
  892. if (confBas.PIR_enablesDisplay_preset0only) json["PIRenDispPs0"] = 1;
  893. else json["PIRenDispPs0"] = 0;
  894. if (confBas.togglingTempHumAIDisplay) json["togTHdisp"] = 1;
  895. else json["togTHdisp"] = 0;
  896. yield();
  897. char jsonchar[1000];
  898. serializeJson(json, jsonchar);
  899. httpServer.send(200, "application/json", jsonchar);
  900. }
  901. }); //httpServer.on /confdbas
  902. httpServer.on("/confDataAdv", []() {
  903. if (!httpIsAuthenticated()) return httpServer.requestAuthentication();
  904. else {
  905. //sendLog("WEB: /confDataAdv", LOGLEVEL_INFO);
  906. //Serial.println("httpServer.on /confdata2");
  907. String addr = httpServer.client().remoteIP().toString();
  908. char buf[40];
  909. sprintf(buf, "WEB: /confDataAdv (from %s)", addr.c_str());
  910. sendLog(buf, LOGLEVEL_INFO);
  911. for (int i = 0; i < httpServer.args(); i++) {
  912. char bufName[20];
  913. char bufValue[101];
  914. httpServer.argName(i).toCharArray(bufName, 20);
  915. httpServer.arg(i).toCharArray(bufValue, 101);
  916. if (strlen(bufName) > 0) {
  917. //Serial.print("web update ");
  918. //Serial.print(bufName);
  919. //Serial.print(" = ");
  920. //Serial.println(bufValue);
  921. setConfig(bufName, bufValue);
  922. }
  923. saveConfig2ToFlash = true;
  924. //Serial.println("web triggered saveConfig2ToFlash");
  925. }
  926. yield();
  927. //build json object of program data
  928. StaticJsonDocument<600> json;
  929. json["minOffTime"] = confAdv.heatingMinOffTime;
  930. json["tempDec"] = confAdv.setTempDecreaseVal;
  931. json["hyst"] = confAdv.hysteresis;
  932. json["tempCorr"] = confAdv.tempCorrVal;
  933. json["humCorr"] = confAdv.humCorrVal;
  934. json["offMsg"] = confAdv.offMessage;
  935. json["modeName0"] = confAdv.modeName0;
  936. json["modeName1"] = confAdv.modeName1;
  937. json["psetName0"] = confAdv.psetName0;
  938. json["psetName1"] = confAdv.psetName1;
  939. json["psetName2"] = confAdv.psetName2;
  940. json["iTempLab"] = confAdv.iTempLabel;
  941. json["oTempLab"] = confAdv.oTempLabel;
  942. yield();
  943. char jsonchar[1000];
  944. serializeJson(json, jsonchar);
  945. httpServer.send(200, "application/json", jsonchar);
  946. }
  947. }); //httpServer.on /confdadv
  948. httpServer.on("/confDataAdd", []() {
  949. if (!httpIsAuthenticated()) return httpServer.requestAuthentication();
  950. else {
  951. //sendLog("WEB: /confDataAdd", LOGLEVEL_INFO);
  952. //Serial.println("httpServer.on /confdata2");
  953. String addr = httpServer.client().remoteIP().toString();
  954. char buf[40];
  955. sprintf(buf, "WEB: /confDataAdd (from %s)", addr.c_str());
  956. sendLog(buf, LOGLEVEL_INFO);
  957. for (int i = 0; i < httpServer.args(); i++) {
  958. char bufName[20];
  959. char bufValue[101];
  960. httpServer.argName(i).toCharArray(bufName, 20);
  961. httpServer.arg(i).toCharArray(bufValue, 101);
  962. if (strlen(bufName) > 0) {
  963. //Serial.print("web update ");
  964. //Serial.print(bufName);
  965. //Serial.print(" = ");
  966. //Serial.println(bufValue);
  967. setConfig(bufName, bufValue);
  968. }
  969. saveConfig2ToFlash = true;
  970. //Serial.println("web triggered saveConfig2ToFlash");
  971. }
  972. yield();
  973. //build json object of program data
  974. StaticJsonDocument<1000> json;
  975. json["outTempTop"] = confAdd.outTemp_topic_in;
  976. json["outHumTop"] = confAdd.outHum_topic_in;
  977. json["PIRTop"] = confAdd.mqtt_topic_pir;
  978. json["PIROnPld"] = confAdd.mqtt_payload_pir_on;
  979. json["PIROffPld"] = confAdd.mqtt_payload_pir_off;
  980. yield();
  981. char jsonchar[1000];
  982. serializeJson(json, jsonchar);
  983. httpServer.send(200, "application/json", jsonchar);
  984. }
  985. }); //httpServer.on /confdadd
  986. httpServer.on("/confDataTime", []() {
  987. if (!httpIsAuthenticated()) return httpServer.requestAuthentication();
  988. else {
  989. //sendLog("WEB: /confDataTime", LOGLEVEL_INFO);
  990. //Serial.println("httpServer.on /confdata2");
  991. String addr = httpServer.client().remoteIP().toString();
  992. char buf[40];
  993. sprintf(buf, "WEB: /confDataTime (from %s)", addr.c_str());
  994. sendLog(buf, LOGLEVEL_INFO);
  995. for (int i = 0; i < httpServer.args(); i++) {
  996. char bufName[20];
  997. char bufValue[101];
  998. httpServer.argName(i).toCharArray(bufName, 20);
  999. httpServer.arg(i).toCharArray(bufValue, 101);
  1000. if (strlen(bufName) > 0) {
  1001. //Serial.print("web update ");
  1002. //Serial.print(bufName);
  1003. //Serial.print(" = ");
  1004. //Serial.println(bufValue);
  1005. setConfig(bufName, bufValue);
  1006. }
  1007. saveConfig2ToFlash = true;
  1008. //Serial.println("web triggered saveConfig2ToFlash");
  1009. }
  1010. yield();
  1011. //build json object of program data
  1012. StaticJsonDocument<1000> json;
  1013. if(confTime.ntpEnable) json["NTPEnable"] = 1;
  1014. else json["NTPEnable"] = 0;
  1015. json["NTPServer1"] = confTime.ntpServer1;
  1016. json["NTPServer2"] = confTime.ntpServer2;
  1017. json["TZStr"] = confTime.timeZoneStr;
  1018. json["NTPSyncInt"] = confTime.ntpSyncInterval;
  1019. yield();
  1020. char jsonchar[1000];
  1021. serializeJson(json, jsonchar);
  1022. httpServer.send(200, "application/json", jsonchar);
  1023. }
  1024. }); //httpServer.on /confDataTime
  1025. httpServer.on("/confDataLog", []() {
  1026. if (!httpIsAuthenticated()) return httpServer.requestAuthentication();
  1027. else {
  1028. //sendLog("WEB: /confDataLog", LOGLEVEL_INFO);
  1029. String addr = httpServer.client().remoteIP().toString();
  1030. char buf[40];
  1031. sprintf(buf, "WEB: /confDataLog (from %s)", addr.c_str());
  1032. sendLog(buf, LOGLEVEL_INFO);
  1033. for (int i = 0; i < httpServer.args(); i++) {
  1034. char bufName[20];
  1035. char bufValue[101];
  1036. httpServer.argName(i).toCharArray(bufName, 20);
  1037. httpServer.arg(i).toCharArray(bufValue, 101);
  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<1000> json;
  1051. //if(confTime.ntpEnable) json["NTPEnable"] = 1;
  1052. //else json["NTPEnable"] = 0;
  1053. json["logLevSer"] = confLog.logLevelSerial;
  1054. json["logLevWeb"] = confLog.logLevelWeb;
  1055. json["logLevMqtt"] = confLog.logLevelMqtt;
  1056. yield();
  1057. char jsonchar[1000];
  1058. serializeJson(json, jsonchar);
  1059. httpServer.send(200, "application/json", jsonchar);
  1060. }
  1061. }); //httpServer.on /confDataLog
  1062. //get heap status, analog input value and all GPIO statuses in one json call
  1063. httpServer.on("/sysinfo", HTTP_GET, []() {
  1064. if (!httpIsAuthenticated()) return httpServer.requestAuthentication();
  1065. else {
  1066. String addr = httpServer.client().remoteIP().toString();
  1067. char buf[40];
  1068. sprintf(buf, "WEB: /sysinfo (from %s)", addr.c_str());
  1069. sendLog(buf, LOGLEVEL_INFO);
  1070. StaticJsonDocument<600> json;
  1071. json["Devname"] = confDevWiFi.deviceName;
  1072. json["Ssid"] = WiFi.SSID();
  1073. json["WiFiNum"] = persWM.getActiveWiFiNum();
  1074. json["Uptime"] = uptimeStr;
  1075. json["HeapFree"] = ESP.getFreeHeap();
  1076. json["HeapFragment"] = ESP.getHeapFragmentation();
  1077. json["HeapMaxBlock"] = ESP.getMaxFreeBlockSize();
  1078. json["ResetReason"] = ESP.getResetReason();
  1079. json["CoreVersion"] = ESP.getCoreVersion();
  1080. json["SDKVersion"] = ESP.getSdkVersion();
  1081. json["CPUfreq"] = ESP.getCpuFreqMHz();
  1082. json["SketchSize"] = ESP.getSketchSize();
  1083. json["FlashSize"] = ESP.getFlashChipRealSize();
  1084. if(confTime.ntpEnable) {
  1085. char buf[13];
  1086. updateTime();
  1087. strftime(buf, sizeof(buf), "%H:%M", &lt);
  1088. json["time"] = buf;
  1089. strftime(buf, sizeof(buf), "%d.%m.%Y", &lt);
  1090. json["date"] = buf;
  1091. }
  1092. yield();
  1093. char jsonchar[1000];
  1094. serializeJson(json, jsonchar);
  1095. httpServer.send(200, "application/json", jsonchar);
  1096. }
  1097. }); //httpServer.on /sysinfo
  1098. /*httpServer.on("/", []() {
  1099. if ( WifiInApMode ) {
  1100. httpServer.sendHeader("Location", "/wifi.htm", true);
  1101. httpServer.send(302);
  1102. }
  1103. else {
  1104. httpServer.sendHeader("Location", "/main", true);
  1105. httpServer.send(302);
  1106. }
  1107. });*/
  1108. httpServer.on("/", []() {
  1109. String addr = httpServer.client().remoteIP().toString();
  1110. char buf[40];
  1111. sprintf(buf, "WEB: / (from %s)", addr.c_str());
  1112. sendLog(buf, LOGLEVEL_INFO);
  1113. if (httpServer.hasArg("restart")) {
  1114. if (httpIsAuthenticated() || (!httpIsAuthenticated() && httpCheckToken()) ) {
  1115. sendLog("WEB: /?restart", LOGLEVEL_INFO);
  1116. httpServerHandleRestartPage();
  1117. restart();
  1118. }
  1119. else httpSendUnauthorized();
  1120. }
  1121. else if (httpServer.hasArg("mqttreconnect")) {
  1122. if ( httpIsAuthenticated() || (!httpIsAuthenticated() && httpCheckToken()) ) {
  1123. //Serial.println("web triggered mqttreconnect");
  1124. sendLog("WEB: /?mqttreconnect", LOGLEVEL_INFO);
  1125. mqttReconnect();
  1126. httpServer.sendHeader("Location", "/", true);
  1127. httpServer.send(303);
  1128. }
  1129. else httpSendUnauthorized();
  1130. }
  1131. else if (httpServer.hasArg("clearconf")) {
  1132. if (httpIsAuthenticatedAdmin() || (!httpIsAuthenticatedAdmin() && httpCheckToken()) ) {
  1133. sendLog("WEB: /?clearconf", LOGLEVEL_INFO);
  1134. httpServerHandleClearconfPage();
  1135. yield();
  1136. delay(5);
  1137. deleteConfig();
  1138. }
  1139. else httpSendUnauthorized();
  1140. }
  1141. else if (!httpIsAuthenticated()) return httpServer.requestAuthentication();
  1142. else {
  1143. sendLog("WEB: /", LOGLEVEL_INFO);
  1144. httpServerHandleMainPage();
  1145. }
  1146. });
  1147. httpServer.on("/conf", []() {
  1148. if (!httpIsAuthenticatedAdmin()) return httpServer.requestAuthentication();
  1149. else {
  1150. //sendLog("WEB: /conf", LOGLEVEL_INFO);
  1151. String addr = httpServer.client().remoteIP().toString();
  1152. char buf[40];
  1153. sprintf(buf, "WEB: /conf (from %s)", addr.c_str());
  1154. sendLog(buf, LOGLEVEL_INFO);
  1155. httpServerHandleConfPage();
  1156. }
  1157. });
  1158. httpServer.on("/confweb", []() {
  1159. if (!httpIsAuthenticatedAdmin()) return httpServer.requestAuthentication();
  1160. else {
  1161. //sendLog("WEB: /confweb", LOGLEVEL_INFO);
  1162. String addr = httpServer.client().remoteIP().toString();
  1163. char buf[40];
  1164. sprintf(buf, "WEB: /confweb (from %s)", addr.c_str());
  1165. sendLog(buf, LOGLEVEL_INFO);
  1166. httpServerHandleConfWebPage();
  1167. }
  1168. });
  1169. httpServer.on("/confmqtt", []() {
  1170. if (!httpIsAuthenticatedAdmin()) return httpServer.requestAuthentication();
  1171. else {
  1172. //sendLog("WEB: /confmqtt", LOGLEVEL_INFO);
  1173. String addr = httpServer.client().remoteIP().toString();
  1174. char buf[40];
  1175. sprintf(buf, "WEB: /confmqtt (from %s)", addr.c_str());
  1176. sendLog(buf, LOGLEVEL_INFO);
  1177. httpServerHandleConfMqttPage();
  1178. }
  1179. });
  1180. httpServer.on("/confdevwifi", []() {
  1181. if (!httpIsAuthenticatedAdmin()) return httpServer.requestAuthentication();
  1182. else {
  1183. //sendLog("WEB: /confdevwifi", LOGLEVEL_INFO);
  1184. String addr = httpServer.client().remoteIP().toString();
  1185. char buf[40];
  1186. sprintf(buf, "WEB: /confdevwifi (from %s)", addr.c_str());
  1187. sendLog(buf, LOGLEVEL_INFO);
  1188. httpServerHandleConfDevWiFiPage();
  1189. }
  1190. });
  1191. httpServer.on("/confbas", []() {
  1192. if (!httpIsAuthenticatedAdmin()) return httpServer.requestAuthentication();
  1193. else {
  1194. //sendLog("WEB: /confbas", LOGLEVEL_INFO);
  1195. String addr = httpServer.client().remoteIP().toString();
  1196. char buf[40];
  1197. sprintf(buf, "WEB: /confbas (from %s)", addr.c_str());
  1198. sendLog(buf, LOGLEVEL_INFO);
  1199. httpServerHandleConfBasPage();
  1200. }
  1201. });
  1202. httpServer.on("/confadv", []() {
  1203. if (!httpIsAuthenticatedAdmin()) return httpServer.requestAuthentication();
  1204. else {
  1205. //sendLog("WEB: /confadv", LOGLEVEL_INFO);
  1206. String addr = httpServer.client().remoteIP().toString();
  1207. char buf[40];
  1208. sprintf(buf, "WEB: /confadv (from %s)", addr.c_str());
  1209. sendLog(buf, LOGLEVEL_INFO);
  1210. httpServerHandleConfAdvPage();
  1211. }
  1212. });
  1213. httpServer.on("/confadd", []() {
  1214. if (!httpIsAuthenticatedAdmin()) return httpServer.requestAuthentication();
  1215. else {
  1216. //sendLog("WEB: /confadd", LOGLEVEL_INFO);
  1217. String addr = httpServer.client().remoteIP().toString();
  1218. char buf[40];
  1219. sprintf(buf, "WEB: /confadd (from %s)", addr.c_str());
  1220. sendLog(buf, LOGLEVEL_INFO);
  1221. httpServerHandleConfAddPage();
  1222. }
  1223. });
  1224. httpServer.on("/conftime", []() {
  1225. if (!httpIsAuthenticatedAdmin()) return httpServer.requestAuthentication();
  1226. else {
  1227. //sendLog("WEB: /conftime", LOGLEVEL_INFO);
  1228. String addr = httpServer.client().remoteIP().toString();
  1229. char buf[40];
  1230. sprintf(buf, "WEB: /conftime (from %s)", addr.c_str());
  1231. sendLog(buf, LOGLEVEL_INFO);
  1232. httpServerHandleConfTimePage();
  1233. }
  1234. });
  1235. httpServer.on("/conflog", []() {
  1236. if (!httpIsAuthenticatedAdmin()) return httpServer.requestAuthentication();
  1237. else {
  1238. //sendLog("WEB: /conflog", LOGLEVEL_INFO);
  1239. String addr = httpServer.client().remoteIP().toString();
  1240. char buf[40];
  1241. sprintf(buf, "WEB: /conflog (from %s)", addr.c_str());
  1242. sendLog(buf, LOGLEVEL_INFO);
  1243. httpServerHandleConfLogPage();
  1244. }
  1245. });
  1246. httpServer.on("/redTemps", []() {
  1247. if (!httpIsAuthenticatedAdmin()) return httpServer.requestAuthentication();
  1248. else {
  1249. //sendLog("WEB: /redTemps", LOGLEVEL_INFO);
  1250. String addr = httpServer.client().remoteIP().toString();
  1251. char buf[40];
  1252. sprintf(buf, "WEB: /redTemps (from %s)", addr.c_str());
  1253. sendLog(buf, LOGLEVEL_INFO);
  1254. httpServerHandleRedTempsPage();
  1255. }
  1256. });
  1257. if(confWeb.enableConsole) {
  1258. httpServer.on("/console", []() {
  1259. if (!httpIsAuthenticatedAdmin()) return httpServer.requestAuthentication();
  1260. else {
  1261. //sendLog("WEB: /console", LOGLEVEL_INFO);
  1262. String addr = httpServer.client().remoteIP().toString();
  1263. char buf[40];
  1264. sprintf(buf, "WEB: /console (from %s)", addr.c_str());
  1265. sendLog(buf, LOGLEVEL_INFO);
  1266. wsValidSessionId = millis();
  1267. char sidbuf[20];
  1268. sprintf(sidbuf, "sessionId=%lu|", wsValidSessionId);
  1269. httpServer.sendHeader("Set-Cookie",sidbuf);
  1270. httpServerHandleConsolePage();
  1271. }
  1272. });
  1273. }
  1274. httpServer.onNotFound([]() {
  1275. String addr = httpServer.client().remoteIP().toString();
  1276. char buf[40];
  1277. sprintf(buf, "WEB: 404 (from %s)", addr.c_str());
  1278. sendLog(buf, LOGLEVEL_INFO);
  1279. httpServerHandleNotFound();
  1280. }); //httpServer.onNotFound
  1281. // HTTP Updater at /update
  1282. httpUpdater.setup(&httpServer, "/update", confWeb.http_user, confWeb.http_pass);
  1283. httpServer.begin();
  1284. }