ESP01 Webserver für Mikrocontroller Workshop

Dependencies:   LCD_i2c_GSOE ESP8266WebserverGSOE

Committer:
jack1930
Date:
Sat Aug 07 09:58:47 2021 +0000
Revision:
7:43007e757e83
Parent:
6:47d69b87b1ac
post

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jack1930 0:829bac853c96 1 /* ATCmdParser usage example
jack1930 0:829bac853c96 2 * Copyright (c) 2016 ARM Limited
jack1930 0:829bac853c96 3 *
jack1930 0:829bac853c96 4 * Licensed under the Apache License, Version 2.0 (the "License");
jack1930 0:829bac853c96 5 * you may not use this file except in compliance with the License.
jack1930 0:829bac853c96 6 * You may obtain a copy of the License at
jack1930 0:829bac853c96 7 *
jack1930 0:829bac853c96 8 * http://www.apache.org/licenses/LICENSE-2.0
jack1930 0:829bac853c96 9 *
jack1930 0:829bac853c96 10 * Unless required by applicable law or agreed to in writing, software
jack1930 0:829bac853c96 11 * distributed under the License is distributed on an "AS IS" BASIS,
jack1930 0:829bac853c96 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
jack1930 0:829bac853c96 13 * See the License for the specific language governing permissions and
jack1930 0:829bac853c96 14 * limitations under the License.
jack1930 0:829bac853c96 15 */
jack1930 0:829bac853c96 16
jack1930 0:829bac853c96 17 #include "mbed.h"
jack1930 0:829bac853c96 18 #include "ESP8266Webserver.h"
jack1930 0:829bac853c96 19 #include "LCD.h"
jack1930 3:8dba70d37b7d 20
jack1930 0:829bac853c96 21 PortOut diag(PortC,0xFF);
jack1930 0:829bac853c96 22 lcd myLCD;
jack1930 1:1f13f5f1fdbc 23 ESP8266Webserver myWebserver;
jack1930 5:b4ae03006c7d 24 AnalogIn poti(PA_0);
jack1930 5:b4ae03006c7d 25 DigitalIn taste(PA_1);
jack1930 0:829bac853c96 26
jack1930 6:47d69b87b1ac 27
jack1930 0:829bac853c96 28
jack1930 5:b4ae03006c7d 29 string getRootPage()
jack1930 0:829bac853c96 30 {
jack1930 6:47d69b87b1ac 31 string webpage;
jack1930 0:829bac853c96 32 webpage="<!DOCTYPE html>\n";
jack1930 3:8dba70d37b7d 33 //Javascript
jack1930 6:47d69b87b1ac 34 webpage+="<script type=\"text/javascript\">\n";
jack1930 3:8dba70d37b7d 35 webpage+="var x;";
jack1930 6:47d69b87b1ac 36 webpage+="function z(){location.assign(\"http://"+(string)(myWebserver.gibIP())+"\");}";
jack1930 4:d19ae06fc0bb 37 webpage+="function sT(){x=setInterval(z,5000);}";
jack1930 3:8dba70d37b7d 38 webpage+="function spT(){clearInterval(x);}";
jack1930 3:8dba70d37b7d 39 webpage+="onload=sT();";
jack1930 3:8dba70d37b7d 40 webpage+="</script>\n";
jack1930 3:8dba70d37b7d 41 //HTML
jack1930 0:829bac853c96 42 webpage+="<html>\n";
jack1930 0:829bac853c96 43 webpage+="<head>\n";
jack1930 0:829bac853c96 44 webpage+="<title>STM32 HTTP</title>\n";
jack1930 0:829bac853c96 45 webpage+="</head>\n";
jack1930 0:829bac853c96 46 webpage+="<body>\n";
jack1930 0:829bac853c96 47 webpage+="<h1>WIFI mit STM32 ESP01</h1>\n";
jack1930 6:47d69b87b1ac 48 webpage+="<p>Aufrufe:"+to_string(myWebserver.Aufrufe)+"</p>\n";
jack1930 6:47d69b87b1ac 49 webpage+="<p>Poti:"+to_string(poti)+"</p>\n";
jack1930 6:47d69b87b1ac 50 webpage+="<p>Taste PA_1:"+to_string(taste)+"</p>\n";
jack1930 6:47d69b87b1ac 51 //Formular
jack1930 0:829bac853c96 52 webpage+="<form>\n";
jack1930 0:829bac853c96 53 webpage+="<label for=\"Suchbegriff\">Suchbegriff</label>\n";
jack1930 0:829bac853c96 54 webpage+="<input id=\"Suchbegriff\" name=\"Suchbegriff\">\n";
jack1930 5:b4ae03006c7d 55 webpage+="<label for=\"checkmich\">check mich:</label>\n";
jack1930 5:b4ae03006c7d 56 webpage+="<input type=\"checkbox\" id=\"checkmich\" name=\"checkmich\">\n";
jack1930 0:829bac853c96 57 webpage+="<button>finden</button>\n";
jack1930 0:829bac853c96 58 webpage+="</form>\n";
jack1930 6:47d69b87b1ac 59 //Buttons
jack1930 0:829bac853c96 60 webpage+="<H2>LED On/Off </H2>\n";
jack1930 0:829bac853c96 61 webpage+="<a href=\"ledAn\"><button>ON</button></a>\n";
jack1930 0:829bac853c96 62 webpage+="<a href=\"ledaus\"><button>OFF</button></a>\n";
jack1930 0:829bac853c96 63 webpage+="</body>\n";
jack1930 0:829bac853c96 64 webpage+="</html>\n";
jack1930 3:8dba70d37b7d 65
jack1930 0:829bac853c96 66 return webpage;
jack1930 0:829bac853c96 67
jack1930 0:829bac853c96 68 }
jack1930 0:829bac853c96 69
jack1930 0:829bac853c96 70
jack1930 0:829bac853c96 71 void testfunc()
jack1930 0:829bac853c96 72 {
jack1930 0:829bac853c96 73 diag=diag|0x80;
jack1930 0:829bac853c96 74
jack1930 5:b4ae03006c7d 75 myWebserver.send(200,"text/html",getRootPage());
jack1930 0:829bac853c96 76 }
jack1930 0:829bac853c96 77
jack1930 0:829bac853c96 78 void testfunc2()
jack1930 0:829bac853c96 79 {
jack1930 0:829bac853c96 80 diag=0x40;
jack1930 5:b4ae03006c7d 81 myWebserver.send(200,"text/html",getRootPage());
jack1930 0:829bac853c96 82 }
jack1930 0:829bac853c96 83 void testfunc3()
jack1930 0:829bac853c96 84 {
jack1930 0:829bac853c96 85 diag=0x20;
jack1930 5:b4ae03006c7d 86 myWebserver.send(200,"text/html",getRootPage());
jack1930 0:829bac853c96 87 }
jack1930 0:829bac853c96 88
jack1930 0:829bac853c96 89 void testfunc4()
jack1930 0:829bac853c96 90 {
jack1930 0:829bac853c96 91 diag=diag|0x10;
jack1930 0:829bac853c96 92 myLCD.clear();
jack1930 0:829bac853c96 93 myLCD.cursorpos(0);
jack1930 3:8dba70d37b7d 94 myLCD.printf("%s",myWebserver.gibWert("Suchbegriff"));
jack1930 0:829bac853c96 95 myLCD.cursorpos(0x40);
jack1930 5:b4ae03006c7d 96 myLCD.printf("%s",myWebserver.gibWert("checkmich"));
jack1930 5:b4ae03006c7d 97 myWebserver.send(200,"text/html",getRootPage());
jack1930 0:829bac853c96 98 }
jack1930 0:829bac853c96 99
jack1930 0:829bac853c96 100 int main()
jack1930 3:8dba70d37b7d 101 {
jack1930 5:b4ae03006c7d 102 taste.mode(PullDown);
jack1930 5:b4ae03006c7d 103
jack1930 0:829bac853c96 104 myWebserver.on("Suchbegriff",&testfunc4);
jack1930 0:829bac853c96 105 myWebserver.on("ledaus",&testfunc2);
jack1930 0:829bac853c96 106 myWebserver.on("ledAn",&testfunc3);
jack1930 0:829bac853c96 107 myWebserver.on("/",&testfunc);
jack1930 0:829bac853c96 108
jack1930 0:829bac853c96 109 myWebserver.begin();
jack1930 0:829bac853c96 110
jack1930 2:c25aefe81534 111 myLCD.clear();
jack1930 2:c25aefe81534 112 myLCD.cursorpos(0);
jack1930 2:c25aefe81534 113 myLCD.printf("%s",myWebserver.gibIP());
jack1930 2:c25aefe81534 114
jack1930 0:829bac853c96 115
jack1930 0:829bac853c96 116
jack1930 0:829bac853c96 117 while(1)
jack1930 0:829bac853c96 118 {
jack1930 0:829bac853c96 119 myWebserver.handleClient();
jack1930 0:829bac853c96 120 }
jack1930 0:829bac853c96 121
jack1930 0:829bac853c96 122 }