httpServer.ino 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //extern ESP8266WebServer httpServer;
  2. static const char httpRoot[] PROGMEM =
  3. R"(<html><body>
  4. <h1><span id='devname'></span></h1>
  5. <h3>WiFi Switch</h3>
  6. <form id='BtnFrm1'><input type='hidden' name='Btn1' value='1'></form>
  7. <form id='BtnFrm2'><input type='hidden' name='Btn2' value='1'></form>
  8. <form id='BtnFrm3'><input type='hidden' name='Btn3' value='1'></form>
  9. <div style='font-size:xx-large'>
  10. <div id='sw1div'>1: <input type='button' id='tbtn1' onclick='return sendBtn(1)'/><br></div>
  11. <div id='sw2div' style='display:none;'>2: <input type='button' id='tbtn2' onclick='return sendBtn(2)'/><br></div>
  12. <div id='sw3div' style='display:none'>3: <input type='button' id='tbtn3' onclick='return sendBtn(3)'/><br></div>
  13. </div>
  14. <br>
  15. <br>
  16. WiFi verbunden mit <i><span id='ssid'></span></i>.<br>
  17. <h6>Letztes Update vor
  18. <span id='ut'></span> Sekunden.
  19. <span id='status'></span>
  20. </h6>
  21. <br>
  22. <a href='/wifi.htm'>WiFi-Einstellungen</a><br>
  23. <a href='/config'>Einstellungen</a><br>
  24. <a href='/update'>Firmware Update</a>
  25. <script>
  26. function g(i) { return document.getElementById(i) };
  27. var xhttp, updateTime;
  28. var textA = 'AUS';
  29. var textE = 'EIN';
  30. function sendBtn(btn) {
  31. var frmn='BtnFrm'+btn;
  32. var form = g(frmn);
  33. return transmit(form);
  34. }
  35. function transmit(f) {
  36. if (!xhttp) {
  37. g('status').innerHTML = 'l&auml;dt...';
  38. xhttp = new XMLHttpRequest();
  39. xhttp.timeout=2000;
  40. xhttp.open('POST', 'api');
  41. xhttp.send(f ? (new FormData(f)) : '');
  42. xhttp.onreadystatechange = function () {
  43. if (xhttp.readyState === XMLHttpRequest.DONE && xhttp.status === 200) {
  44. var data = JSON.parse(xhttp.responseText);
  45. g('ssid').innerHTML = data.ssid;
  46. if(data.devname != undefined) g('devname').innerHTML = data.devname;
  47. if(data.swState2 != undefined) g('sw2div').style.display='inline';
  48. else g('sw2div').style.display='none';
  49. if(data.swState3 != undefined) g('sw3div').style.display='inline';
  50. else g('sw3div').style.display='none';
  51. if(data.swState1 == '1') g('tbtn1').value = textE;
  52. else g('tbtn1').value = textA;
  53. if(data.swState2 == '1') g('tbtn2').value = textE;
  54. else g('tbtn2').value = textA;
  55. if(data.swState3 == '1') g('tbtn3').value = textE;
  56. else g('tbtn3').value = textA;
  57. xhttp = null;
  58. updateTime = 0;
  59. }
  60. }
  61. }
  62. return false;
  63. }
  64. transmit();
  65. setInterval(function () { g('ut').innerHTML = ++updateTime; }, 1000);
  66. setInterval(transmit, 5000);
  67. </script>
  68. </body></html>)";
  69. static const char httpConfPage[] PROGMEM =
  70. R"(<html><body>
  71. <h1>WiFi Thermostat</h1>
  72. <h2>Configuration</h2>
  73. <a href='/'>Home</a>
  74. </body></html>)";
  75. void httpServerHandleRoot() {
  76. httpServer.send_P(200, "text/html", httpRoot);
  77. }
  78. void httpServerHandleConfPage() {
  79. httpServer.send_P(200, "text/html", httpConfPage);
  80. }
  81. void httpServerHandleNotFound() {
  82. String message = "File Not Found\n\n";
  83. message += "URI: ";
  84. message += httpServer.uri();
  85. message += "\nMethod: ";
  86. message += (httpServer.method() == HTTP_GET) ? "GET" : "POST";
  87. message += "\nArguments: ";
  88. message += httpServer.args();
  89. message += "\n";
  90. for (uint8_t i = 0; i < httpServer.args(); i++) {
  91. message += " " + httpServer.argName(i) + ": " + httpServer.arg(i) + "\n";
  92. }
  93. httpServer.send(404, "text/plain", message);
  94. }
  95. void httpServerInit() {
  96. //handles commands from webpage, sends live data in JSON format
  97. httpServer.on("/api", []() {
  98. //Serial.print("httpServer.on /api");
  99. if (httpServer.hasArg("Btn1")) {
  100. lastSwitchSource[0] = 2;
  101. relaisToggle(0);
  102. Serial.println("web Btn1");
  103. } //if
  104. if (httpServer.hasArg("Btn2")) {
  105. lastSwitchSource[1] = 2;
  106. relaisToggle(1);
  107. Serial.println("web Btn2");
  108. } //if
  109. if (httpServer.hasArg("Btn3")) {
  110. lastSwitchSource[2] = 2;
  111. relaisToggle(2);
  112. Serial.println("web Btn3");
  113. } //if
  114. //build json object of program data
  115. StaticJsonBuffer<200> jsonBuffer;
  116. JsonObject &json = jsonBuffer.createObject();
  117. json["ssid"] = WiFi.SSID();
  118. json["swState1"] = relais_state[0];
  119. if(RELAIS_COUNT > 1) json["swState2"] = relais_state[1];
  120. if(RELAIS_COUNT > 2) json["swState3"] = relais_state[2];
  121. json["devname"] = DEVICE_NAME;
  122. char jsonchar[200];
  123. json.printTo(jsonchar); //print to char array, takes more memory but sends in one piece
  124. httpServer.send(200, "application/json", jsonchar);
  125. }); //httpServer.on /api
  126. //get heap status, analog input value and all GPIO statuses in one json call
  127. httpServer.on("/info", HTTP_GET, []() {
  128. String json = "{";
  129. json += "\"wifissid\":\"" + WiFi.SSID() + "\"";
  130. json += "\"heap\":" + String(ESP.getFreeHeap());
  131. //json += ", \"analog\":" + String(analogRead(A0));
  132. //json += ", \"gpio\":" + String((uint32_t)(((GPI | GPO) & 0xFFFF) | ((GP16I & 0x01) << 16)));
  133. json += "}";
  134. httpServer.send(200, "text/json", json);
  135. json = String();
  136. }); //httpServer.on /info
  137. httpServer.on("/", []() {
  138. httpServerHandleRoot();
  139. });
  140. httpServer.on("/config", []() {
  141. httpServerHandleConfPage();
  142. });
  143. httpServer.onNotFound([]() {
  144. httpServerHandleNotFound();
  145. }); //httpServer.onNotFound
  146. // HTTP Updater at /update
  147. httpUpdater.setup(&httpServer);
  148. httpServer.begin();
  149. }