_wwwServer.ino 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452
  1. // 1-channel LoRa Gateway for ESP8266
  2. // Copyright (c) 2016, 2017, 2018, 2019 Maarten Westenberg version for ESP8266
  3. // Version 6.1.0
  4. // Date: 2019-10-20
  5. //
  6. // based on work done by many people and making use of several libraries.
  7. //
  8. // All rights reserved. This program and the accompanying materials
  9. // are made available under the terms of the MIT License
  10. // which accompanies this distribution, and is available at
  11. // https://opensource.org/licenses/mit-license.php
  12. //
  13. // NO WARRANTY OF ANY KIND IS PROVIDED
  14. //
  15. // Author: Maarten Westenberg (mw12554@hotmail.com)
  16. //
  17. // This file contains the webserver code for the ESP Single Channel Gateway.
  18. // Note:
  19. // The ESP Webserver works with Strings to display HTML content.
  20. // Care must be taken that not all data is output to the webserver in one string
  21. // as this will use a LOT of memory and possibly kill the heap (cause system
  22. // crash or other unreliable behaviour.
  23. // Instead, output of the various tables on the webpage should be displayed in
  24. // chunks so that strings are limited in size.
  25. // Be aware that using no strings but only sendContent() calls has its own
  26. // disadvantage that these calls take a lot of time and cause the page to be
  27. // displayed like an old typewriter.
  28. // So, the trick is to make chucks that are sent to the website by using
  29. // a response String but not make those Strings too big.
  30. //
  31. // Also, selecting too many options for Statistics, display, Hopping channels
  32. // etc makes the gateway more sluggish and may impact the available memory and
  33. // thus its performance and reliability. It's up to the user to select wisely!
  34. //
  35. // --------------------------------------------------------------------------------
  36. // PRINT IP
  37. // Output the 4-byte IP address for easy printing.
  38. // As this function is also used by _otaServer.ino do not put in #define
  39. // --------------------------------------------------------------------------------
  40. static void printIP(IPAddress ipa, const char sep, String& response)
  41. {
  42. response+=(IPAddress)ipa[0]; response+=sep;
  43. response+=(IPAddress)ipa[1]; response+=sep;
  44. response+=(IPAddress)ipa[2]; response+=sep;
  45. response+=(IPAddress)ipa[3];
  46. }
  47. //
  48. // The remainder of the file ONLY works is A_SERVER=1 is set.
  49. //
  50. #if A_SERVER==1
  51. // ================================================================================
  52. // WEBSERVER DECLARATIONS
  53. // ================================================================================
  54. // None at the moment
  55. // ================================================================================
  56. // WEBSERVER FUNCTIONS
  57. // ================================================================================
  58. // --------------------------------------------------------------------------------
  59. // Used by all functions requiring user confirmation
  60. // Displays a menu by user and two buttons "OK" and "CANCEL"
  61. // The function returns true for OK and false for CANCEL
  62. // Usage: Call this function ONCE during startup to declare and init
  63. // the ynDialog JavaScript function, and call the function
  64. // from the program when needed.
  65. // Paramters of the JavaScript function:
  66. // s: Th strig contining the question to be answered
  67. // o: The OK tab for the webpage where to go to
  68. // c: The Cancel string (optional)
  69. // --------------------------------------------------------------------------------
  70. boolean YesNo()
  71. {
  72. boolean ret = false;
  73. String response = "";
  74. response += "<script>";
  75. response += "var ch = \"\"; "; // Init choice
  76. response += "function ynDialog(s,y) {";
  77. response += " try { adddlert(s); }";
  78. response += " catch(err) {";
  79. response += " ch = \" \" + s + \".\\n\\n\"; ";
  80. response += " ch += \"Click OK to continue,\\n\"; ";
  81. response += " ch += \"or Cancel\\n\\n\"; ";
  82. response += " if(!confirm(ch)) { ";
  83. response += " javascript:window.location.reload(true);";
  84. response += " } ";
  85. response += " else { ";
  86. response += " document.location.href = '/'+y; ";
  87. response += " } ";
  88. response += " }";
  89. response += "}";
  90. response += "</script>";
  91. server.sendContent(response);
  92. // Put something like this in the ESP program
  93. // response += "<input type=\"button\" value=\"YesNo\" onclick=\"ynDialog()\" />";
  94. return(ret);
  95. }
  96. // --------------------------------------------------------------------------------
  97. // WWWFILE
  98. // This function will open a pop-up in the browser and then
  99. // display the contents of a file in that window
  100. // Output is sent to server.sendContent()
  101. // Parameters:
  102. // fn; String with filename
  103. // Returns:
  104. // <none>
  105. // --------------------------------------------------------------------------------
  106. void wwwFile(String fn) {
  107. if (!SPIFFS.exists(fn)) {
  108. #if DUSB>=1
  109. Serial.print(F("wwwFile:: ERROR: file not found="));
  110. Serial.println(fn);
  111. #endif
  112. return;
  113. }
  114. #if DUSB>=2
  115. else {
  116. Serial.print(F("wwwFile:: File existist= "));
  117. Serial.println(fn);
  118. }
  119. #endif
  120. #if DUSB>=1
  121. File f = SPIFFS.open(fn, "r"); // Open the file for reading
  122. int j;
  123. for (j=0; j<LOGFILEREC; j++) {
  124. String s=f.readStringUntil('\n');
  125. if (s.length() == 0) {
  126. Serial.print(F("wwwFile:: String length 0"));
  127. break;
  128. }
  129. server.sendContent(s.substring(12)); // Skip the first 12 Gateway specific binary characters
  130. server.sendContent("\n");
  131. yield();
  132. }
  133. #endif
  134. }
  135. // --------------------------------------------------------------------------------
  136. // Button function Docu, display the documentation pages.
  137. // This is a button on the top of the GUI screen.
  138. // --------------------------------------------------------------------------------
  139. void buttonDocu()
  140. {
  141. String response = "";
  142. response += "<script>";
  143. response += "var txt = \"\";";
  144. response += "function showDocu() {";
  145. response += " try { adddlert(\"Welcome,\"); }";
  146. response += " catch(err) {";
  147. response += " txt = \"Do you want the documentation page.\\n\\n\"; ";
  148. response += " txt += \"Click OK to continue viewing documentation,\\n\"; ";
  149. response += " txt += \"or Cancel to return to the home page.\\n\\n\"; ";
  150. response += " if(confirm(txt)) { ";
  151. response += " document.location.href = \"https://things4u.github.io/UserGuide/One%20Channel%20Gateway/Introduction%205.html\"; ";
  152. response += " }";
  153. response += " }";
  154. response += "}";
  155. response += "</script>";
  156. server.sendContent(response);
  157. }
  158. // --------------------------------------------------------------------------------
  159. // Button function Log displays logfiles.
  160. // This is a button on the top of the GUI screen
  161. // --------------------------------------------------------------------------------
  162. void buttonLog()
  163. {
  164. String response = "";
  165. String fn = "";
  166. int i = 0;
  167. while (i< LOGFILEMAX ) {
  168. fn = "/log-" + String(gwayConfig.logFileNo - i);
  169. wwwFile(fn); // Display the file contents in the browser
  170. i++;
  171. }
  172. server.sendContent(response);
  173. }
  174. // --------------------------------------------------------------------------------
  175. // Navigate webpage by buttons. This method has some advantages:
  176. // - Less time/cpu usage
  177. // - Less memory usage <a href=\"SPEED=160\">
  178. // --------------------------------------------------------------------------------
  179. static void wwwButtons()
  180. {
  181. String response = "";
  182. String mode = (gwayConfig.expert ? "Basic Mode" : "Expert Mode");
  183. YesNo(); // Init the Yes/No function
  184. buttonDocu();
  185. response += "<input type=\"button\" value=\"Documentation\" onclick=\"showDocu()\" >";
  186. response += "<a href=\"EXPERT\" download><button type=\"button\">" + mode + "</button></a>";
  187. response += "<a href=\"LOG\" download><button type=\"button\">Log Files</button></a>";
  188. server.sendContent(response); // Send to the screen
  189. }
  190. // --------------------------------------------------------------------------------
  191. // SET ESP8266 WEB SERVER VARIABLES
  192. //
  193. // This funtion implements the WiFi Webserver (very simple one). The purpose
  194. // of this server is to receive simple admin commands, and execute these
  195. // results which are sent back to the web client.
  196. // Commands: DEBUG, ADDRESS, IP, CONFIG, GETTIME, SETTIME
  197. // The webpage is completely built response and then printed on screen.
  198. //
  199. // Parameters:
  200. // cmd: Contains a character array with the command to execute
  201. // arg: Contains the parameter value of that command
  202. // Returns:
  203. // <none>
  204. // --------------------------------------------------------------------------------
  205. static void setVariables(const char *cmd, const char *arg) {
  206. // DEBUG settings; These can be used as a single argument
  207. if (strcmp(cmd, "DEBUG")==0) { // Set debug level 0-2
  208. if (atoi(arg) == 1) {
  209. debug = (debug+1)%4;
  210. }
  211. else if (atoi(arg) == -1) {
  212. debug = (debug+3)%4;
  213. }
  214. writeGwayCfg(CONFIGFILE); // Save configuration to file
  215. }
  216. if (strcmp(cmd, "CAD")==0) { // Set -cad on=1 or off=0
  217. _cad=(bool)atoi(arg);
  218. writeGwayCfg(CONFIGFILE); // Save configuration to file
  219. }
  220. if (strcmp(cmd, "HOP")==0) { // Set -hop on=1 or off=0
  221. _hop=(bool)atoi(arg);
  222. if (! _hop) {
  223. ifreq=0;
  224. setFreq(freqs[ifreq].upFreq);
  225. rxLoraModem();
  226. sf = SF7;
  227. cadScanner();
  228. }
  229. writeGwayCfg(CONFIGFILE); // Save configuration to file
  230. }
  231. if (strcmp(cmd, "DELAY")==0) { // Set delay usecs
  232. txDelay+=atoi(arg)*1000;
  233. }
  234. // SF; Handle Spreading Factor Settings
  235. if (strcmp(cmd, "SF")==0) {
  236. uint8_t sfi = sf;
  237. if (atoi(arg) == 1) {
  238. if (sf>=SF12) sf=SF7; else sf= (sf_t)((int)sf+1);
  239. }
  240. else if (atoi(arg) == -1) {
  241. if (sf<=SF7) sf=SF12; else sf= (sf_t)((int)sf-1);
  242. }
  243. rxLoraModem(); // Reset the radio with the new spreading factor
  244. writeGwayCfg(CONFIGFILE); // Save configuration to file
  245. }
  246. // FREQ; Handle Frequency Settings
  247. if (strcmp(cmd, "FREQ")==0) {
  248. uint8_t nf = sizeof(freqs)/ sizeof(freqs[0]); // Number of frequency elements in array
  249. // Compute frequency index
  250. if (atoi(arg) == 1) {
  251. if (ifreq==(nf-1)) ifreq=0; else ifreq++;
  252. }
  253. else if (atoi(arg) == -1) {
  254. Serial.println("down");
  255. if (ifreq==0) ifreq=(nf-1); else ifreq--;
  256. }
  257. setFreq(freqs[ifreq].upFreq);
  258. rxLoraModem(); // Reset the radio with the new frequency
  259. writeGwayCfg(CONFIGFILE); // Save configuration to file
  260. }
  261. //if (strcmp(cmd, "GETTIME")==0) { Serial.println(F("gettime tbd")); } // Get the local time
  262. //if (strcmp(cmd, "SETTIME")==0) { Serial.println(F("settime tbd")); } // Set the local time
  263. if (strcmp(cmd, "HELP")==0) { Serial.println(F("Display Help Topics")); }
  264. #if GATEWAYNODE==1
  265. if (strcmp(cmd, "NODE")==0) { // Set node on=1 or off=0
  266. gwayConfig.isNode =(bool)atoi(arg);
  267. writeGwayCfg(CONFIGFILE); // Save configuration to file
  268. }
  269. if (strcmp(cmd, "FCNT")==0) {
  270. frameCount=0;
  271. rxLoraModem(); // Reset the radio with the new frequency
  272. writeGwayCfg(CONFIGFILE);
  273. }
  274. #endif
  275. #if WIFIMANAGER==1
  276. if (strcmp(cmd, "NEWSSID")==0) {
  277. WiFiManager wifiManager;
  278. strcpy(wpa[0].login,"");
  279. strcpy(wpa[0].passw,"");
  280. WiFi.disconnect();
  281. wifiManager.autoConnect(AP_NAME, AP_PASSWD );
  282. }
  283. #endif
  284. #if A_OTA==1
  285. if (strcmp(cmd, "UPDATE")==0) {
  286. if (atoi(arg) == 1) {
  287. updateOtaa();
  288. }
  289. }
  290. #endif
  291. #if A_REFRESH==1
  292. if (strcmp(cmd, "REFR")==0) { // Set refresh on=1 or off=0
  293. gwayConfig.refresh =(bool)atoi(arg);
  294. writeGwayCfg(CONFIGFILE); // Save configuration to file
  295. }
  296. #endif
  297. }
  298. // --------------------------------------------------------------------------------
  299. // OPEN WEB PAGE
  300. // This is the init function for opening the webpage
  301. //
  302. // --------------------------------------------------------------------------------
  303. static void openWebPage()
  304. {
  305. ++gwayConfig.views; // increment number of views
  306. #if A_REFRESH==1
  307. //server.client().stop(); // Experimental, stop webserver in case something is still running!
  308. #endif
  309. String response="";
  310. server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
  311. server.sendHeader("Pragma", "no-cache");
  312. server.sendHeader("Expires", "-1");
  313. // init webserver, fill the webpage
  314. // NOTE: The page is renewed every _WWW_INTERVAL seconds, please adjust in ESP-sc-gway.h
  315. //
  316. server.setContentLength(CONTENT_LENGTH_UNKNOWN);
  317. server.send(200, "text/html", "");
  318. #if A_REFRESH==1
  319. if (gwayConfig.refresh) {
  320. response += String() + "<!DOCTYPE HTML><HTML><HEAD><meta http-equiv='refresh' content='"+_WWW_INTERVAL+";http://";
  321. printIP((IPAddress)WiFi.localIP(),'.',response);
  322. response += "'><TITLE>ESP8266 1ch Gateway</TITLE>";
  323. }
  324. else {
  325. response += String() + "<!DOCTYPE HTML><HTML><HEAD><TITLE>ESP8266 1ch Gateway</TITLE>";
  326. }
  327. #else
  328. response += String() + "<!DOCTYPE HTML><HTML><HEAD><TITLE>ESP8266 1ch Gateway</TITLE>";
  329. #endif
  330. response += "<META HTTP-EQUIV='CONTENT-TYPE' CONTENT='text/html; charset=UTF-8'>";
  331. response += "<META NAME='AUTHOR' CONTENT='M. Westenberg (mw1554@hotmail.com)'>";
  332. response += "<style>.thead {background-color:green; color:white;} ";
  333. response += ".cell {border: 1px solid black;}";
  334. response += ".config_table {max_width:100%; min-width:400px; width:98%; border:1px solid black; border-collapse:collapse;}";
  335. response += "</style></HEAD><BODY>";
  336. response +="<h1>ESP Gateway Config</h1>";
  337. response +="<p style='font-size:10px;'>";
  338. response +="Version: "; response+=VERSION;
  339. response +="<br>ESP alive since "; // STARTED ON
  340. stringTime(startTime, response);
  341. response +=", Uptime: "; // UPTIME
  342. uint32_t secs = millis()/1000;
  343. uint16_t days = secs / 86400; // Determine number of days
  344. uint8_t _hour = hour(secs);
  345. uint8_t _minute = minute(secs);
  346. uint8_t _second = second(secs);
  347. response += String() + days + "-";
  348. if (_hour < 10) response += "0";
  349. response += String() + _hour + ":";
  350. if (_minute < 10) response += "0";
  351. response += String() + _minute + ":";
  352. if (_second < 10) response += "0";
  353. response += String() + _second;
  354. response +="<br>Current time "; // CURRENT TIME
  355. stringTime(now(), response);
  356. response +="<br>";
  357. response +="</p>";
  358. server.sendContent(response);
  359. }
  360. // --------------------------------------------------------------------------------
  361. // H2 Gateway Settings
  362. //
  363. // Display the configuration and settings data. This is an interactive setting
  364. // allowing the user to set CAD, HOP, Debug and several other operating parameters
  365. //
  366. // --------------------------------------------------------------------------------
  367. static void gatewaySettings()
  368. {
  369. String response="";
  370. String bg="";
  371. response +="<h2>Gateway Settings</h2>";
  372. response +="<table class=\"config_table\">";
  373. response +="<tr>";
  374. response +="<th class=\"thead\">Setting</th>";
  375. response +="<th colspan=\"2\" style=\"background-color: green; color: white; width:120px;\">Value</th>";
  376. response +="<th colspan=\"4\" style=\"background-color: green; color: white; width:100px;\">Set</th>";
  377. response +="</tr>";
  378. bg = " background-color: ";
  379. bg += ( _cad ? "LightGreen" : "orange" );
  380. response +="<tr><td class=\"cell\">CAD</td>";
  381. response +="<td colspan=\"2\" style=\"border: 1px solid black;"; response += bg; response += "\">";
  382. response += ( _cad ? "ON" : "OFF" );
  383. response +="<td style=\"border: 1px solid black; width:40px;\"><a href=\"CAD=1\"><button>ON</button></a></td>";
  384. response +="<td style=\"border: 1px solid black; width:40px;\"><a href=\"CAD=0\"><button>OFF</button></a></td>";
  385. response +="</tr>";
  386. bg = " background-color: ";
  387. bg += ( _hop ? "LightGreen" : "orange" );
  388. response +="<tr><td class=\"cell\">HOP</td>";
  389. response +="<td colspan=\"2\" style=\"border: 1px solid black;"; response += bg; response += "\">";
  390. response += ( _hop ? "ON" : "OFF" );
  391. response +="<td style=\"border: 1px solid black; width:40px;\"><a href=\"HOP=1\"><button>ON</button></a></td>";
  392. response +="<td style=\"border: 1px solid black; width:40px;\"><a href=\"HOP=0\"><button>OFF</button></a></td>";
  393. response +="</tr>";
  394. response +="<tr><td class=\"cell\">SF Setting</td><td class=\"cell\" colspan=\"2\">";
  395. if (_cad) {
  396. response += "AUTO</td>";
  397. }
  398. else {
  399. response += sf;
  400. response +="<td class=\"cell\"><a href=\"SF=-1\"><button>-</button></a></td>";
  401. response +="<td class=\"cell\"><a href=\"SF=1\"><button>+</button></a></td>";
  402. }
  403. response +="</tr>";
  404. // Channel
  405. response +="<tr><td class=\"cell\">Channel</td>";
  406. response +="<td class=\"cell\" colspan=\"2\">";
  407. if (_hop) {
  408. response += "AUTO</td>";
  409. }
  410. else {
  411. response += String() + ifreq;
  412. response +="</td>";
  413. response +="<td class=\"cell\"><a href=\"FREQ=-1\"><button>-</button></a></td>";
  414. response +="<td class=\"cell\"><a href=\"FREQ=1\"><button>+</button></a></td>";
  415. }
  416. response +="</tr>";
  417. // Debugging options, only when DUSB is set, otherwise no
  418. // serial activity
  419. #if DUSB>=1
  420. response +="<tr><td class=\"cell\">Debug level</td><td class=\"cell\" colspan=\"2\">";
  421. response +=debug;
  422. response +="</td>";
  423. response +="<td class=\"cell\"><a href=\"DEBUG=-1\"><button>-</button></a></td>";
  424. response +="<td class=\"cell\"><a href=\"DEBUG=1\"><button>+</button></a></td>";
  425. response +="</tr>";
  426. response +="<tr><td class=\"cell\">Debug pattern</td>";
  427. bg = ( (pdebug & P_SCAN) ? "LightGreen" : "orange" );
  428. response +="<td class=\"cell\" style=\"border: 1px solid black; width:20px; background-color: ";
  429. response += bg; response += "\">";
  430. response +="<a href=\"PDEBUG=SCAN\">";
  431. response +="<button>SCN</button></a></td>";
  432. bg = ( (pdebug & P_CAD) ? "LightGreen" : "orange" );
  433. response +="<td class=\"cell\" style=\"border: 1px solid black; width:20px; background-color: ";
  434. response += bg; response += "\">";
  435. response +="<a href=\"PDEBUG=CAD\">";
  436. response +="<button>CAD</button></a></td>";
  437. bg = ( (pdebug & P_RX) ? "LightGreen" : "orange" );
  438. response +="<td class=\"cell\" style=\"border: 1px solid black; width:20px; background-color: ";
  439. response += bg; response += "\">";
  440. response +="<a href=\"PDEBUG=RX\">";
  441. response +="<button>RX</button></a></td>";
  442. bg = ( (pdebug & P_TX) ? "LightGreen" : "orange" );
  443. response +="<td class=\"cell\" style=\"border: 1px solid black; width:20px; background-color: ";
  444. response += bg; response += "\">";
  445. response +="<a href=\"PDEBUG=TX\">";
  446. response +="<button>TX</button></a></td>";
  447. response += "</tr>";
  448. // Use a second Line
  449. response +="<tr><td class=\"cell\"></td>";
  450. bg = ( (pdebug & P_PRE) ? "LightGreen" : "orange" );
  451. response +="<td class=\"cell\" style=\"border: 1px solid black; width:20px; background-color: ";
  452. response += bg; response += "\">";
  453. response +="<a href=\"PDEBUG=PRE\">";
  454. response +="<button>PRE</button></a></td>";
  455. bg = ( (pdebug & P_MAIN) ? "LightGreen" : "orange" );
  456. response +="<td class=\"cell\" style=\"border: 1px solid black; width:20px; background-color: ";
  457. response += bg; response += "\">";
  458. response +="<a href=\"PDEBUG=MAIN\">";
  459. response +="<button>MAI</button></a></td>";
  460. bg = ( (pdebug & P_GUI) ? "LightGreen" : "orange" );
  461. response +="<td class=\"cell\" style=\"border: 1px solid black; width:20px; background-color: ";
  462. response += bg; response += "\">";
  463. response +="<a href=\"PDEBUG=GUI\">";
  464. response +="<button>GUI</button></a></td>";
  465. bg = ( (pdebug & P_RADIO) ? "LightGreen" : "orange" );
  466. response +="<td class=\"cell\" style=\"border: 1px solid black; width:20px; background-color: ";
  467. response += bg; response += "\">";
  468. response +="<a href=\"PDEBUG=RADIO\">";
  469. response +="<button>RDIO</button></a></td>";
  470. response +="</tr>";
  471. #endif
  472. // Serial Debugging
  473. response +="<tr><td class=\"cell\">Usb Debug</td><td class=\"cell\" colspan=\"2\">";
  474. response +=DUSB;
  475. response +="</td>";
  476. //response +="<td class=\"cell\"> </td>";
  477. //response +="<td class=\"cell\"> </td>";
  478. response +="</tr>";
  479. #if GATEWAYNODE==1
  480. response +="<tr><td class=\"cell\">Framecounter Internal Sensor</td>";
  481. response +="<td class=\"cell\" colspan=\"2\">";
  482. response +=frameCount;
  483. response +="</td><td colspan=\"2\" style=\"border: 1px solid black;\">";
  484. response +="<button><a href=\"/FCNT\">RESET</a></button></td>";
  485. response +="</tr>";
  486. bg = " background-color: ";
  487. bg += ( (gwayConfig.isNode == 1) ? "LightGreen" : "orange" );
  488. response +="<tr><td class=\"cell\">Gateway Node</td>";
  489. response +="<td class=\"cell\" style=\"border: 1px solid black;" + bg + "\">";
  490. response += ( (gwayConfig.isNode == true) ? "ON" : "OFF" );
  491. response +="<td style=\"border: 1px solid black; width:40px;\"><a href=\"NODE=1\"><button>ON</button></a></td>";
  492. response +="<td style=\"border: 1px solid black; width:40px;\"><a href=\"NODE=0\"><button>OFF</button></a></td>";
  493. response +="</tr>";
  494. #endif
  495. #if A_REFRESH==1
  496. bg = " background-color: ";
  497. bg += ( (gwayConfig.refresh == 1) ? "LightGreen" : "orange" );
  498. response +="<tr><td class=\"cell\">WWW Refresh</td>";
  499. response +="<td class=\"cell\" colspan=\"2\" style=\"border: 1px solid black; " + bg + "\">";
  500. response += ( (gwayConfig.refresh == 1) ? "ON" : "OFF" );
  501. response +="<td style=\"border: 1px solid black; width:40px;\"><a href=\"REFR=1\"><button>ON</button></a></td>";
  502. response +="<td style=\"border: 1px solid black; width:40px;\"><a href=\"REFR=0\"><button>OFF</button></a></td>";
  503. response +="</tr>";
  504. #endif
  505. // Reset Accesspoint
  506. #if WIFIMANAGER==1
  507. response +="<tr><td><tr><td>";
  508. response +="Click <a href=\"/NEWSSID\">here</a> to reset accesspoint<br>";
  509. response +="</td><td></td></tr>";
  510. #endif
  511. // Update Firmware all statistics
  512. response +="<tr><td class=\"cell\">Update Firmware</td><td colspan=\"2\"></td>";
  513. response +="<td class=\"cell\" colspan=\"2\" class=\"cell\"><a href=\"/UPDATE=1\"><button>UPDATE</button></a></td></tr>";
  514. // Format the Filesystem
  515. response +="<tr><td class=\"cell\">Format SPIFFS</td>";
  516. response +=String() + "<td class=\"cell\" colspan=\"2\" >"+""+"</td>";
  517. response +="<td colspan=\"2\" class=\"cell\"><input type=\"button\" value=\"FORMAT\" onclick=\"ynDialog(\'Do you really want to format?\',\'FORMAT\')\" /></td></tr>";
  518. // Reset all statistics
  519. #if STATISTICS >= 1
  520. response +="<tr><td class=\"cell\">Statistics</td>";
  521. response +=String() + "<td class=\"cell\" colspan=\"2\" >"+statc.resets+"</td>";
  522. response +="<td colspan=\"2\" class=\"cell\"><input type=\"button\" value=\"RESET\" onclick=\"ynDialog(\'Do you really want to reset statistics?\',\'RESET\')\" /></td></tr>";
  523. // Reset
  524. response +="<tr><td class=\"cell\">Boots and Resets</td>";
  525. response +=String() + "<td class=\"cell\" colspan=\"2\" >"+gwayConfig.boots+"</td>";
  526. response +="<td colspan=\"2\" class=\"cell\"><input type=\"button\" value=\"RESET\" onclick=\"ynDialog(\'Do you want to reset boots?\',\'BOOT\')\" /></td></tr>";
  527. #endif
  528. response +="</table>";
  529. server.sendContent(response);
  530. }
  531. // --------------------------------------------------------------------------------
  532. // H2 Package Statistics
  533. //
  534. // This section display a matrix on the screen where everay channel and spreading
  535. // factor is displayed.
  536. // --------------------------------------------------------------------------------
  537. static void statisticsData()
  538. {
  539. String response="";
  540. // Header
  541. response +="<h2>Package Statistics</h2>";
  542. response +="<table class=\"config_table\">";
  543. response +="<tr><th class=\"thead\">Counter</th>";
  544. #if STATISTICS == 3
  545. response +="<th class=\"thead\">C 0</th>";
  546. response +="<th class=\"thead\">C 1</th>";
  547. response +="<th class=\"thead\">C 2</th>";
  548. #endif
  549. response +="<th class=\"thead\">Pkgs</th>";
  550. response +="<th class=\"thead\">Pkgs/hr</th>";
  551. response +="</tr>";
  552. //
  553. // Table rows
  554. //
  555. response +="<tr><td class=\"cell\">Packages Downlink</td>";
  556. #if STATISTICS == 3
  557. response +="<td class=\"cell\">" + String(statc.msg_down_0) + "</td>";
  558. response +="<td class=\"cell\">" + String(statc.msg_down_1) + "</td>";
  559. response +="<td class=\"cell\">" + String(statc.msg_down_2) + "</td>";
  560. #endif
  561. response += "<td class=\"cell\">" + String(statc.msg_down) + "</td>";
  562. response +="<td class=\"cell\"></td></tr>";
  563. response +="<tr><td class=\"cell\">Packages Uplink Total</td>";
  564. #if STATISTICS == 3
  565. response +="<td class=\"cell\">" + String(statc.msg_ttl_0) + "</td>";
  566. response +="<td class=\"cell\">" + String(statc.msg_ttl_1) + "</td>";
  567. response +="<td class=\"cell\">" + String(statc.msg_ttl_2) + "</td>";
  568. #endif
  569. response +="<td class=\"cell\">" + String(statc.msg_ttl) + "</td>";
  570. response +="<td class=\"cell\">" + String((statc.msg_ttl*3600)/(now() - startTime)) + "</td></tr>";
  571. response +="<tr><td class=\"cell\">Packages Uplink OK </td>";
  572. #if STATISTICS == 3
  573. response +="<td class=\"cell\">" + String(statc.msg_ok_0) + "</td>";
  574. response +="<td class=\"cell\">" + String(statc.msg_ok_1) + "</td>";
  575. response +="<td class=\"cell\">" + String(statc.msg_ok_2) + "</td>";
  576. #endif
  577. response +="<td class=\"cell\">" + String(statc.msg_ok) + "</td>";
  578. response +="<td class=\"cell\"></td></tr>";
  579. // Provide a table with all the SF data including percentage of messsages
  580. #if STATISTICS == 2
  581. response +="<tr><td class=\"cell\">SF7 rcvd</td>";
  582. response +="<td class=\"cell\">"; response +=statc.sf7;
  583. response +="<td class=\"cell\">"; response += String(statc.msg_ttl>0 ? 100*statc.sf7/statc.msg_ttl : 0)+" %";
  584. response +="</td></tr>";
  585. response +="<tr><td class=\"cell\">SF8 rcvd</td>";
  586. response +="<td class=\"cell\">"; response +=statc.sf8;
  587. response +="<td class=\"cell\">"; response += String(statc.msg_ttl>0 ? 100*statc.sf8/statc.msg_ttl : 0)+" %";
  588. response +="</td></tr>";
  589. response +="<tr><td class=\"cell\">SF9 rcvd</td>";
  590. response +="<td class=\"cell\">"; response +=statc.sf9;
  591. response +="<td class=\"cell\">"; response += String(statc.msg_ttl>0 ? 100*statc.sf9/statc.msg_ttl : 0)+" %";
  592. response +="</td></tr>";
  593. response +="<tr><td class=\"cell\">SF10 rcvd</td>";
  594. response +="<td class=\"cell\">"; response +=statc.sf10;
  595. response +="<td class=\"cell\">"; response += String(statc.msg_ttl>0 ? 100*statc.sf10/statc.msg_ttl : 0)+" %";
  596. response +="</td></tr>";
  597. response +="<tr><td class=\"cell\">SF11 rcvd</td>";
  598. response +="<td class=\"cell\">"; response +=statc.sf11;
  599. response +="<td class=\"cell\">"; response += String(statc.msg_ttl>0 ? 100*statc.sf11/statc.msg_ttl : 0)+" %";
  600. response +="</td></tr>";
  601. response +="<tr><td class=\"cell\">SF12 rcvd</td>";
  602. response +="<td class=\"cell\">"; response +=statc.sf12;
  603. response +="<td class=\"cell\">"; response += String(statc.msg_ttl>0 ? 100*statc.sf12/statc.msg_ttl : 0)+" %";
  604. response +="</td></tr>";
  605. #endif
  606. #if STATISTICS == 3
  607. response +="<tr><td class=\"cell\">SF7 rcvd</td>";
  608. response +="<td class=\"cell\">"; response +=statc.sf7_0;
  609. response +="<td class=\"cell\">"; response +=statc.sf7_1;
  610. response +="<td class=\"cell\">"; response +=statc.sf7_2;
  611. response +="<td class=\"cell\">"; response +=statc.sf7;
  612. response +="<td class=\"cell\">"; response += String(statc.msg_ttl>0 ? 100*statc.sf7/statc.msg_ttl : 0)+" %";
  613. response +="</td></tr>";
  614. response +="<tr><td class=\"cell\">SF8 rcvd</td>";
  615. response +="<td class=\"cell\">"; response +=statc.sf8_0;
  616. response +="<td class=\"cell\">"; response +=statc.sf8_1;
  617. response +="<td class=\"cell\">"; response +=statc.sf8_2;
  618. response +="<td class=\"cell\">"; response +=statc.sf8;
  619. response +="<td class=\"cell\">"; response += String(statc.msg_ttl>0 ? 100*statc.sf8/statc.msg_ttl : 0)+" %";
  620. response +="</td></tr>";
  621. response +="<tr><td class=\"cell\">SF9 rcvd</td>";
  622. response +="<td class=\"cell\">"; response +=statc.sf9_0;
  623. response +="<td class=\"cell\">"; response +=statc.sf9_1;
  624. response +="<td class=\"cell\">"; response +=statc.sf9_2;
  625. response +="<td class=\"cell\">"; response +=statc.sf9;
  626. response +="<td class=\"cell\">"; response += String(statc.msg_ttl>0 ? 100*statc.sf9/statc.msg_ttl : 0)+" %";
  627. response +="</td></tr>";
  628. response +="<tr><td class=\"cell\">SF10 rcvd</td>";
  629. response +="<td class=\"cell\">"; response +=statc.sf10_0;
  630. response +="<td class=\"cell\">"; response +=statc.sf10_1;
  631. response +="<td class=\"cell\">"; response +=statc.sf10_2;
  632. response +="<td class=\"cell\">"; response +=statc.sf10;
  633. response +="<td class=\"cell\">"; response += String(statc.msg_ttl>0 ? 100*statc.sf10/statc.msg_ttl : 0)+" %";
  634. response +="</td></tr>";
  635. response +="<tr><td class=\"cell\">SF11 rcvd</td>";
  636. response +="<td class=\"cell\">"; response +=statc.sf11_0;
  637. response +="<td class=\"cell\">"; response +=statc.sf11_1;
  638. response +="<td class=\"cell\">"; response +=statc.sf11_2;
  639. response +="<td class=\"cell\">"; response +=statc.sf11;
  640. response +="<td class=\"cell\">"; response += String(statc.msg_ttl>0 ? 100*statc.sf11/statc.msg_ttl : 0)+" %";
  641. response +="</td></tr>";
  642. response +="<tr><td class=\"cell\">SF12 rcvd</td>";
  643. response +="<td class=\"cell\">"; response +=statc.sf12_0;
  644. response +="<td class=\"cell\">"; response +=statc.sf12_1;
  645. response +="<td class=\"cell\">"; response +=statc.sf12_1;
  646. response +="<td class=\"cell\">"; response +=statc.sf12;
  647. response +="<td class=\"cell\">"; response += String(statc.msg_ttl>0 ? 100*statc.sf12/statc.msg_ttl : 0)+" %";
  648. response +="</td></tr>";
  649. #endif
  650. response +="</table>";
  651. server.sendContent(response);
  652. }
  653. // --------------------------------------------------------------------------------
  654. // Message History
  655. // If enabled, display the sensor messageHistory on the current webserver Page.
  656. // In this GUI section a number of statr[x] records are displayed such as:
  657. //
  658. // Time, The time the sensor message was received
  659. // Node, the DevAddr or even Node name for Trusted nodes,
  660. // Data (Localserver), when _LOCALSERVER is enabled contains decoded data
  661. // C, Channel frequency on which the sensor was received
  662. // Freq, The frequency of the channel
  663. // SF, Spreading Factor
  664. // pRSSI, Packet RSSI
  665. //
  666. // Parameters:
  667. // - <none>
  668. // Returns:
  669. // - <none>
  670. // --------------------------------------------------------------------------------
  671. static void messageHistory()
  672. {
  673. #if STATISTICS >= 1
  674. String response="";
  675. response += "<h2>Message History</h2>";
  676. response += "<table class=\"config_table\">";
  677. response += "<tr>";
  678. response += "<th class=\"thead\">Time</th>";
  679. response += "<th class=\"thead\">Node</th>";
  680. #if _LOCALSERVER==1
  681. response += "<th class=\"thead\">Data</th>";
  682. #endif
  683. response += "<th class=\"thead\" style=\"width: 20px;\">C</th>";
  684. response += "<th class=\"thead\">Freq</th>";
  685. response += "<th class=\"thead\" style=\"width: 40px;\">SF</th>";
  686. response += "<th class=\"thead\" style=\"width: 50px;\">pRSSI</th>";
  687. #if RSSI==1
  688. if (debug > 1) {
  689. response += "<th class=\"thead\" style=\"width: 50px;\">RSSI</th>";
  690. }
  691. #endif
  692. response += "</tr>";
  693. server.sendContent(response);
  694. for (int i=0; i<MAX_STAT; i++) {
  695. if (statr[i].sf == 0) break;
  696. response = "";
  697. response += String() + "<tr><td class=\"cell\">"; // Tmst
  698. stringTime((statr[i].tmst), response); // XXX Change tmst not to be millis() dependent
  699. response += "</td>";
  700. response += String() + "<td class=\"cell\">"; // Node
  701. if (SerialName((char *)(& (statr[i].node)), response) < 0) { // works with TRUSTED_NODES >= 1
  702. printHEX((char *)(& (statr[i].node)),' ',response); // else
  703. }
  704. response += "</td>";
  705. #if _LOCALSERVER==1
  706. response += String() + "<td class=\"cell\">"; // Data
  707. for (int j=0; j<statr[i].datal; j++) {
  708. if (statr[i].data[j] <0x10) response+= "0";
  709. response += String(statr[i].data[j],HEX) + " ";
  710. }
  711. response += "</td>";
  712. #endif
  713. response += String() + "<td class=\"cell\">" + statr[i].ch + "</td>";
  714. response += String() + "<td class=\"cell\">" + freqs[statr[i].ch].upFreq + "</td>";
  715. response += String() + "<td class=\"cell\">" + statr[i].sf + "</td>";
  716. response += String() + "<td class=\"cell\">" + statr[i].prssi + "</td>";
  717. #if RSSI==1
  718. if (debug >= 2) {
  719. response += String() + "<td class=\"cell\">" + statr[i].rssi + "</td>";
  720. }
  721. #endif
  722. response += "</tr>";
  723. server.sendContent(response);
  724. }
  725. server.sendContent("</table>");
  726. #endif
  727. }
  728. // --------------------------------------------------------------------------------
  729. // SEND WEB PAGE()
  730. // Call the webserver and send the standard content and the content that is
  731. // passed by the parameter. Each time a variable is changed, this function is
  732. // called to display the webpage again/
  733. //
  734. // NOTE: This is the only place where yield() or delay() calls are used.
  735. //
  736. // --------------------------------------------------------------------------------
  737. void sendWebPage(const char *cmd, const char *arg)
  738. {
  739. openWebPage(); yield(); // Do the initial website setup
  740. wwwButtons(); // Display buttons such as Documentation, Mode, Logfiles
  741. setVariables(cmd,arg); yield(); // Read Webserver commands from line
  742. statisticsData(); yield(); // Node statistics
  743. messageHistory(); yield(); // Display the sensor history, message statistics
  744. gatewaySettings(); yield(); // Display web configuration
  745. wifiConfig(); yield(); // WiFi specific parameters
  746. systemStatus(); yield(); // System statistics such as heap etc.
  747. interruptData(); yield(); // Display interrupts only when debug >= 2
  748. websiteFooter(); yield();
  749. server.client().stop();
  750. }
  751. // --------------------------------------------------------------------------------
  752. // setupWWW is the main function for webserver functions/
  753. // SetupWWW function called by main setup() program to setup webserver
  754. // It does actually not much more than installing all the callback handlers
  755. // for messages sent to the webserver
  756. //
  757. // Implemented is an interface like:
  758. // http://<server>/<Variable>=<value>
  759. //
  760. // --------------------------------------------------------------------------------
  761. void setupWWW()
  762. {
  763. server.begin(); // Start the webserver
  764. // -----------------
  765. // BUTTONS, define what should happen with the buttons we press on the homepage
  766. server.on("/", []() {
  767. sendWebPage("",""); // Send the webPage string
  768. server.sendHeader("Location", String("/"), true);
  769. server.send ( 302, "text/plain", "");
  770. });
  771. server.on("/HELP", []() {
  772. sendWebPage("HELP",""); // Send the webPage string
  773. server.sendHeader("Location", String("/"), true);
  774. server.send ( 302, "text/plain", "");
  775. });
  776. // Format the filesystem
  777. server.on("/FORMAT", []() {
  778. Serial.print(F("FORMAT ..."));
  779. SPIFFS.format(); // Normally disabled. Enable only when SPIFFS corrupt
  780. initConfig(&gwayConfig);
  781. writeConfig( CONFIGFILE, &gwayConfig);
  782. #if DUSB>=1
  783. Serial.println(F("DONE"));
  784. #endif
  785. server.sendHeader("Location", String("/"), true);
  786. server.send ( 302, "text/plain", "");
  787. });
  788. // Reset the statistics
  789. server.on("/RESET", []() {
  790. Serial.println(F("RESET"));
  791. startTime= now() - 1; // Reset all timers too
  792. statc.msg_ttl = 0; // Reset package statistics
  793. statc.msg_ok = 0;
  794. statc.msg_down = 0;
  795. #if STATISTICS >= 3
  796. statc.msg_ttl_0 = 0;
  797. statc.msg_ttl_1 = 0;
  798. statc.msg_ttl_2 = 0;
  799. statc.msg_ok_0 = 0;
  800. statc.msg_ok_1 = 0;
  801. statc.msg_ok_2 = 0;
  802. statc.msg_down_0 = 0;
  803. statc.msg_down_1 = 0;
  804. statc.msg_down_2 = 0;
  805. #endif
  806. #if STATISTICS >= 1
  807. for (int i=0; i<MAX_STAT; i++) { statr[i].sf = 0; }
  808. #if STATISTICS >= 2
  809. statc.sf7 = 0;
  810. statc.sf8 = 0;
  811. statc.sf9 = 0;
  812. statc.sf10= 0;
  813. statc.sf11= 0;
  814. statc.sf12= 0;
  815. statc.resets= 0;
  816. writeGwayCfg(CONFIGFILE);
  817. #if STATISTICS >= 3
  818. statc.sf7_0 = 0; statc.sf7_1 = 0; statc.sf7_2 = 0;
  819. statc.sf8_0 = 0; statc.sf8_1 = 0; statc.sf8_2 = 0;
  820. statc.sf9_0 = 0; statc.sf9_1 = 0; statc.sf9_2 = 0;
  821. statc.sf10_0= 0; statc.sf10_1= 0; statc.sf10_2= 0;
  822. statc.sf11_0= 0; statc.sf11_1= 0; statc.sf11_2= 0;
  823. statc.sf12_0= 0; statc.sf12_1= 0; statc.sf12_2= 0;
  824. #endif
  825. #endif
  826. #endif
  827. server.sendHeader("Location", String("/"), true);
  828. server.send ( 302, "text/plain", "");
  829. });
  830. // Reset the boot counter
  831. server.on("/BOOT", []() {
  832. Serial.println(F("BOOT"));
  833. #if STATISTICS >= 2
  834. gwayConfig.boots = 0;
  835. gwayConfig.wifis = 0;
  836. gwayConfig.views = 0;
  837. gwayConfig.ntpErr = 0; // NTP errors
  838. gwayConfig.ntpErrTime = 0; // NTP last error time
  839. gwayConfig.ntps = 0; // Number of NTP calls
  840. #endif
  841. gwayConfig.reents = 0; // Re-entrance
  842. writeGwayCfg(CONFIGFILE);
  843. server.sendHeader("Location", String("/"), true);
  844. server.send ( 302, "text/plain", "");
  845. });
  846. server.on("/NEWSSID", []() {
  847. sendWebPage("NEWSSID",""); // Send the webPage string
  848. server.sendHeader("Location", String("/"), true);
  849. server.send ( 302, "text/plain", "");
  850. });
  851. // Set debug parameter
  852. server.on("/DEBUG=-1", []() { // Set debug level 0-2
  853. debug = (debug+3)%4;
  854. writeGwayCfg(CONFIGFILE); // Save configuration to file
  855. server.sendHeader("Location", String("/"), true);
  856. server.send ( 302, "text/plain", "");
  857. });
  858. server.on("/DEBUG=1", []() {
  859. debug = (debug+1)%4;
  860. writeGwayCfg(CONFIGFILE); // Save configuration to file
  861. server.sendHeader("Location", String("/"), true);
  862. server.send ( 302, "text/plain", "");
  863. });
  864. // Set PDEBUG parameter
  865. //
  866. server.on("/PDEBUG=SCAN", []() { // Set debug level 0-2
  867. pdebug ^= P_SCAN;
  868. writeGwayCfg(CONFIGFILE); // Save configuration to file
  869. server.sendHeader("Location", String("/"), true);
  870. server.send ( 302, "text/plain", "");
  871. });
  872. server.on("/PDEBUG=CAD", []() { // Set debug level 0-2
  873. pdebug ^= P_CAD;
  874. writeGwayCfg(CONFIGFILE); // Save configuration to file
  875. server.sendHeader("Location", String("/"), true);
  876. server.send ( 302, "text/plain", "");
  877. });
  878. server.on("/PDEBUG=RX", []() { // Set debug level 0-2
  879. pdebug ^= P_RX;
  880. writeGwayCfg(CONFIGFILE); // Save configuration to file
  881. server.sendHeader("Location", String("/"), true);
  882. server.send ( 302, "text/plain", "");
  883. });
  884. server.on("/PDEBUG=TX", []() { // Set debug level 0-2
  885. pdebug ^= P_TX;
  886. writeGwayCfg(CONFIGFILE); // Save configuration to file
  887. server.sendHeader("Location", String("/"), true);
  888. server.send ( 302, "text/plain", "");
  889. });
  890. server.on("/PDEBUG=PRE", []() { // Set debug level 0-2
  891. pdebug ^= P_PRE;
  892. writeGwayCfg(CONFIGFILE); // Save configuration to file
  893. server.sendHeader("Location", String("/"), true);
  894. server.send ( 302, "text/plain", "");
  895. });
  896. server.on("/PDEBUG=MAIN", []() { // Set debug level 0-2
  897. pdebug ^= P_MAIN;
  898. writeGwayCfg(CONFIGFILE); // Save configuration to file
  899. server.sendHeader("Location", String("/"), true);
  900. server.send ( 302, "text/plain", "");
  901. });
  902. server.on("/PDEBUG=GUI", []() { // Set debug level 0-2
  903. pdebug ^= P_GUI;
  904. writeGwayCfg(CONFIGFILE); // Save configuration to file
  905. server.sendHeader("Location", String("/"), true);
  906. server.send ( 302, "text/plain", "");
  907. });
  908. server.on("/PDEBUG=RADIO", []() { // Set debug level 0-2
  909. pdebug ^= P_RADIO;
  910. writeGwayCfg(CONFIGFILE); // Save configuration to file
  911. server.sendHeader("Location", String("/"), true);
  912. server.send ( 302, "text/plain", "");
  913. });
  914. // Set delay in microseconds
  915. server.on("/DELAY=1", []() {
  916. txDelay+=5000;
  917. server.sendHeader("Location", String("/"), true);
  918. server.send ( 302, "text/plain", "");
  919. });
  920. server.on("/DELAY=-1", []() {
  921. txDelay-=5000;
  922. server.sendHeader("Location", String("/"), true);
  923. server.send ( 302, "text/plain", "");
  924. });
  925. // Spreading Factor setting
  926. server.on("/SF=1", []() {
  927. if (sf>=SF12) sf=SF7; else sf= (sf_t)((int)sf+1);
  928. server.sendHeader("Location", String("/"), true);
  929. server.send ( 302, "text/plain", "");
  930. });
  931. server.on("/SF=-1", []() {
  932. if (sf<=SF7) sf=SF12; else sf= (sf_t)((int)sf-1);
  933. server.sendHeader("Location", String("/"), true);
  934. server.send ( 302, "text/plain", "");
  935. });
  936. // Set Frequency of the GateWay node
  937. server.on("/FREQ=1", []() {
  938. uint8_t nf = sizeof(freqs)/sizeof(freqs[0]); // Number of elements in array
  939. #if DUSB==2
  940. Serial.print("FREQ==1:: For freq[0] sizeof vector=");
  941. Serial.print(sizeof(freqs[0]));
  942. Serial.println();
  943. #endif
  944. if (ifreq==(nf-1)) ifreq=0; else ifreq++;
  945. server.sendHeader("Location", String("/"), true);
  946. server.send ( 302, "text/plain", "");
  947. });
  948. server.on("/FREQ=-1", []() {
  949. uint8_t nf = sizeof(freqs)/sizeof(freqs[0]); // Number of elements in array
  950. if (ifreq==0) ifreq=(nf-1); else ifreq--;
  951. server.sendHeader("Location", String("/"), true);
  952. server.send ( 302, "text/plain", "");
  953. });
  954. // Set CAD function off/on
  955. server.on("/CAD=1", []() {
  956. _cad=(bool)1;
  957. writeGwayCfg(CONFIGFILE); // Save configuration to file
  958. server.sendHeader("Location", String("/"), true);
  959. server.send ( 302, "text/plain", "");
  960. });
  961. server.on("/CAD=0", []() {
  962. _cad=(bool)0;
  963. writeGwayCfg(CONFIGFILE); // Save configuration to file
  964. server.sendHeader("Location", String("/"), true);
  965. server.send ( 302, "text/plain", "");
  966. });
  967. // GatewayNode
  968. server.on("/NODE=1", []() {
  969. #if GATEWAYNODE==1
  970. gwayConfig.isNode =(bool)1;
  971. writeGwayCfg(CONFIGFILE); // Save configuration to file
  972. #endif
  973. server.sendHeader("Location", String("/"), true);
  974. server.send ( 302, "text/plain", "");
  975. });
  976. server.on("/NODE=0", []() {
  977. #if GATEWAYNODE==1
  978. gwayConfig.isNode =(bool)0;
  979. writeGwayCfg(CONFIGFILE); // Save configuration to file
  980. #endif
  981. server.sendHeader("Location", String("/"), true);
  982. server.send ( 302, "text/plain", "");
  983. });
  984. #if GATEWAYNODE==1
  985. // Framecounter of the Gateway node
  986. server.on("/FCNT", []() {
  987. frameCount=0;
  988. rxLoraModem(); // Reset the radio with the new frequency
  989. writeGwayCfg(CONFIGFILE);
  990. //sendWebPage("",""); // Send the webPage string
  991. server.sendHeader("Location", String("/"), true);
  992. server.send ( 302, "text/plain", "");
  993. });
  994. #endif
  995. // WWW Page refresh function
  996. server.on("/REFR=1", []() { // WWW page auto refresh ON
  997. #if A_REFRESH==1
  998. gwayConfig.refresh =1;
  999. writeGwayCfg(CONFIGFILE); // Save configuration to file
  1000. #endif
  1001. server.sendHeader("Location", String("/"), true);
  1002. server.send ( 302, "text/plain", "");
  1003. });
  1004. server.on("/REFR=0", []() { // WWW page auto refresh OFF
  1005. #if A_REFRESH==1
  1006. gwayConfig.refresh =0;
  1007. writeGwayCfg(CONFIGFILE); // Save configuration to file
  1008. #endif
  1009. server.sendHeader("Location", String("/"), true);
  1010. server.send ( 302, "text/plain", "");
  1011. });
  1012. // Switch off/on the HOP functions
  1013. server.on("/HOP=1", []() {
  1014. _hop=true;
  1015. server.sendHeader("Location", String("/"), true);
  1016. server.send ( 302, "text/plain", "");
  1017. });
  1018. server.on("/HOP=0", []() {
  1019. _hop=false;
  1020. ifreq=0;
  1021. setFreq(freqs[ifreq].upFreq);
  1022. rxLoraModem();
  1023. server.sendHeader("Location", String("/"), true);
  1024. server.send ( 302, "text/plain", "");
  1025. });
  1026. #if !defined ESP32_ARCH
  1027. // Change speed to 160 MHz
  1028. server.on("/SPEED=80", []() {
  1029. system_update_cpu_freq(80);
  1030. server.sendHeader("Location", String("/"), true);
  1031. server.send ( 302, "text/plain", "");
  1032. });
  1033. server.on("/SPEED=160", []() {
  1034. system_update_cpu_freq(160);
  1035. server.sendHeader("Location", String("/"), true);
  1036. server.send ( 302, "text/plain", "");
  1037. });
  1038. #endif
  1039. // Display Documentation pages
  1040. server.on("/DOCU", []() {
  1041. server.sendHeader("Location", String("/"), true);
  1042. buttonDocu();
  1043. server.send ( 302, "text/plain", "");
  1044. });
  1045. server.on("/LOG", []() {
  1046. server.sendHeader("Location", String("/"), true);
  1047. #if DUSB>=1
  1048. Serial.println(F("LOG button"));
  1049. #endif
  1050. buttonLog();
  1051. server.send ( 302, "text/plain", "");
  1052. });
  1053. // Display Expert mode or Simple mode
  1054. server.on("/EXPERT", []() {
  1055. server.sendHeader("Location", String("/"), true);
  1056. gwayConfig.expert = bool(1 - (int) gwayConfig.expert) ;
  1057. server.send ( 302, "text/plain", "");
  1058. });
  1059. // Update the sketch. Not yet implemented
  1060. server.on("/UPDATE=1", []() {
  1061. #if A_OTA==1
  1062. updateOtaa();
  1063. #endif
  1064. server.sendHeader("Location", String("/"), true);
  1065. server.send ( 302, "text/plain", "");
  1066. });
  1067. // -----------
  1068. // This section from version 4.0.7 defines what PART of the
  1069. // webpage is shown based on the buttons pressed by the user
  1070. // Maybe not all information should be put on the screen since it
  1071. // may take too much time to serve all information before a next
  1072. // package interrupt arrives at the gateway
  1073. Serial.print(F("WWW Server started on port "));
  1074. Serial.println(A_SERVERPORT);
  1075. return;
  1076. } // setupWWW
  1077. // --------------------------------------------------------------------------------
  1078. // WIFI CONFIG
  1079. // wifiConfig() displays the most important Wifi parameters gathered
  1080. //
  1081. // --------------------------------------------------------------------------------
  1082. static void wifiConfig()
  1083. {
  1084. if (gwayConfig.expert) {
  1085. String response="";
  1086. response +="<h2>WiFi Config</h2>";
  1087. response +="<table class=\"config_table\">";
  1088. response +="<tr><th class=\"thead\">Parameter</th><th class=\"thead\">Value</th></tr>";
  1089. response +="<tr><td class=\"cell\">WiFi host</td><td class=\"cell\">";
  1090. #if ESP32_ARCH==1
  1091. response +=WiFi.getHostname(); response+="</tr>";
  1092. #else
  1093. response +=wifi_station_get_hostname(); response+="</tr>";
  1094. #endif
  1095. response +="<tr><td class=\"cell\">WiFi SSID</td><td class=\"cell\">";
  1096. response +=WiFi.SSID(); response+="</tr>";
  1097. response +="<tr><td class=\"cell\">IP Address</td><td class=\"cell\">";
  1098. printIP((IPAddress)WiFi.localIP(),'.',response);
  1099. response +="</tr>";
  1100. response +="<tr><td class=\"cell\">IP Gateway</td><td class=\"cell\">";
  1101. printIP((IPAddress)WiFi.gatewayIP(),'.',response);
  1102. response +="</tr>";
  1103. response +="<tr><td class=\"cell\">NTP Server</td><td class=\"cell\">"; response+=NTP_TIMESERVER; response+="</tr>";
  1104. response +="<tr><td class=\"cell\">LoRa Router</td><td class=\"cell\">"; response+=_TTNSERVER; response+="</tr>";
  1105. response +="<tr><td class=\"cell\">LoRa Router IP</td><td class=\"cell\">";
  1106. printIP((IPAddress)ttnServer,'.',response);
  1107. response +="</tr>";
  1108. #ifdef _THINGSERVER
  1109. response +="<tr><td class=\"cell\">LoRa Router 2</td><td class=\"cell\">"; response+=_THINGSERVER;
  1110. response += String() + ":" + _THINGPORT + "</tr>";
  1111. response +="<tr><td class=\"cell\">LoRa Router 2 IP</td><td class=\"cell\">";
  1112. printIP((IPAddress)thingServer,'.',response);
  1113. response +="</tr>";
  1114. #endif
  1115. response +="</table>";
  1116. server.sendContent(response);
  1117. } // gwayConfig.expert
  1118. } // wifiConfig
  1119. // --------------------------------------------------------------------------------
  1120. // H2 systemStatus
  1121. // systemStatus is additional and only available in the expert mode.
  1122. // It provides a number of system specific data such as heap size etc.
  1123. // --------------------------------------------------------------------------------
  1124. static void systemStatus()
  1125. {
  1126. if (gwayConfig.expert) {
  1127. String response="";
  1128. response +="<h2>System Status</h2>";
  1129. response +="<table class=\"config_table\">";
  1130. response +="<tr>";
  1131. response +="<th class=\"thead\">Parameter</th>";
  1132. response +="<th class=\"thead\">Value</th>";
  1133. response +="<th colspan=\"2\" class=\"thead\">Set</th>";
  1134. response +="</tr>";
  1135. response +="<tr><td style=\"border: 1px solid black; width:120px;\">Gateway ID</td>";
  1136. response +="<td class=\"cell\">";
  1137. if (MAC_array[0]< 0x10) response +='0'; response +=String(MAC_array[0],HEX); // The MAC array is always returned in lowercase
  1138. if (MAC_array[1]< 0x10) response +='0'; response +=String(MAC_array[1],HEX);
  1139. if (MAC_array[2]< 0x10) response +='0'; response +=String(MAC_array[2],HEX);
  1140. response +="FFFF";
  1141. if (MAC_array[3]< 0x10) response +='0'; response +=String(MAC_array[3],HEX);
  1142. if (MAC_array[4]< 0x10) response +='0'; response +=String(MAC_array[4],HEX);
  1143. if (MAC_array[5]< 0x10) response +='0'; response +=String(MAC_array[5],HEX);
  1144. response+="</tr>";
  1145. response +="<tr><td class=\"cell\">Free heap</td><td class=\"cell\">"; response+=ESP.getFreeHeap(); response+="</tr>";
  1146. // XXX We Shoudl find an ESP32 alternative
  1147. #if !defined ESP32_ARCH
  1148. response +="<tr><td class=\"cell\">ESP speed</td><td class=\"cell\">"; response+=ESP.getCpuFreqMHz();
  1149. response +="<td style=\"border: 1px solid black; width:40px;\"><a href=\"SPEED=80\"><button>80</button></a></td>";
  1150. response +="<td style=\"border: 1px solid black; width:40px;\"><a href=\"SPEED=160\"><button>160</button></a></td>";
  1151. response+="</tr>";
  1152. response +="<tr><td class=\"cell\">ESP Chip ID</td><td class=\"cell\">"; response+=ESP.getChipId(); response+="</tr>";
  1153. #endif
  1154. response +="<tr><td class=\"cell\">OLED</td><td class=\"cell\">"; response+=OLED; response+="</tr>";
  1155. #if STATISTICS>=1
  1156. response +="<tr><td class=\"cell\">WiFi Setups</td><td class=\"cell\">"; response+=gwayConfig.wifis; response+="</tr>";
  1157. response +="<tr><td class=\"cell\">WWW Views</td><td class=\"cell\">"; response+=gwayConfig.views; response+="</tr>";
  1158. #endif
  1159. response +="</table>";
  1160. server.sendContent(response);
  1161. } // gwayConfig.expert
  1162. } // systemStatus
  1163. // --------------------------------------------------------------------------------
  1164. // H2 System State and Interrupt
  1165. // Display interrupt data, but only for debug >= 2
  1166. //
  1167. // --------------------------------------------------------------------------------
  1168. static void interruptData()
  1169. {
  1170. if (gwayConfig.expert) {
  1171. uint8_t flags = readRegister(REG_IRQ_FLAGS);
  1172. uint8_t mask = readRegister(REG_IRQ_FLAGS_MASK);
  1173. String response="";
  1174. response +="<h2>System State and Interrupt</h2>";
  1175. response +="<table class=\"config_table\">";
  1176. response +="<tr>";
  1177. response +="<th class=\"thead\">Parameter</th>";
  1178. response +="<th class=\"thead\">Value</th>";
  1179. response +="<th colspan=\"2\" class=\"thead\">Set</th>";
  1180. response +="</tr>";
  1181. response +="<tr><td class=\"cell\">_state</td>";
  1182. response +="<td class=\"cell\">";
  1183. switch (_state) { // See loraModem.h
  1184. case S_INIT: response +="INIT"; break;
  1185. case S_SCAN: response +="SCAN"; break;
  1186. case S_CAD: response +="CAD"; break;
  1187. case S_RX: response +="RX"; break;
  1188. case S_TX: response +="TX"; break;
  1189. default: response +="unknown"; break;
  1190. }
  1191. response +="</td></tr>";
  1192. response +="<tr><td class=\"cell\">_STRICT_1CH</td>";
  1193. response +="<td class=\"cell\">" ;
  1194. response += String() + _STRICT_1CH;
  1195. response +="</td></tr>";
  1196. response +="<tr><td class=\"cell\">flags (8 bits)</td>";
  1197. response +="<td class=\"cell\">0x";
  1198. if (flags <16) response += "0";
  1199. response +=String(flags,HEX); response+="</td></tr>";
  1200. response +="<tr><td class=\"cell\">mask (8 bits)</td>";
  1201. response +="<td class=\"cell\">0x";
  1202. if (mask <16) response += "0";
  1203. response +=String(mask,HEX); response+="</td></tr>";
  1204. response +="<tr><td class=\"cell\">Re-entrant cntr</td>";
  1205. response +="<td class=\"cell\">";
  1206. response += String() + gwayConfig.reents;
  1207. response +="</td></tr>";
  1208. response +="<tr><td class=\"cell\">ntp call cntr</td>";
  1209. response +="<td class=\"cell\">";
  1210. response += String() + gwayConfig.ntps;
  1211. response+="</td></tr>";
  1212. response +="<tr><td class=\"cell\">ntpErr cntr</td>";
  1213. response +="<td class=\"cell\">";
  1214. response += String() + gwayConfig.ntpErr;
  1215. response +="</td>";
  1216. response +="<td colspan=\"2\" style=\"border: 1px solid black;\">";
  1217. stringTime(gwayConfig.ntpErrTime, response);
  1218. response +="</td>";
  1219. response +="</tr>";
  1220. response +="<tr><td class=\"cell\">Time Correction (uSec)</td><td class=\"cell\">";
  1221. response += txDelay;
  1222. response +="</td>";
  1223. response +="<td class=\"cell\"><a href=\"DELAY=-1\"><button>-</button></a></td>";
  1224. response +="<td class=\"cell\"><a href=\"DELAY=1\"><button>+</button></a></td>";
  1225. response +="</tr>";
  1226. response +="</table>";
  1227. server.sendContent(response);
  1228. }// if gwayConfig.expert
  1229. } // interruptData
  1230. // --------------------------------------------------------------------------------
  1231. // websiteFooter
  1232. //
  1233. // Thi function displays the last messages without header on the webpage and then
  1234. // closes the webpage.
  1235. // --------------------------------------------------------------------------------
  1236. static void websiteFooter()
  1237. {
  1238. // Close the client connection to server
  1239. server.sendContent(String() + "<br><br /><p style='font-size:10px'>Click <a href=\"/HELP\">here</a> to explain Help and REST options</p><br>");
  1240. server.sendContent(String() + "</BODY></HTML>");
  1241. server.sendContent(""); yield();
  1242. }
  1243. #endif // A_SERVER==1