Library für ESP01 Webserver STM32F103RB Nucleo
Dependents: Nucleo_dht11_Webserver WebServerGSOEWorkshopSlider WebServerGSOEWorkshopDynamisch WebServerGSOEWorkshopStatisch ... more
ESP8266Webserver.h@32:edfb58f0aa3a, 2021-08-04 (annotated)
- Committer:
- jack1930
- Date:
- Wed Aug 04 16:35:06 2021 +0000
- Revision:
- 32:edfb58f0aa3a
- Parent:
- 30:17dd2e085453
- Child:
- 33:c3010ec1f537
Doku
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jack1930 | 32:edfb58f0aa3a | 1 | /** @file Einstellungen.h */ |
jack1930 | 26:b045cb598879 | 2 | /** My ESP8266Webserver class. |
jack1930 | 26:b045cb598879 | 3 | * Used for Webserver on Microcontroller. |
jack1930 | 29:a907422c0eae | 4 | * Grundeinstellungen und Definitionen: Siehe Einstellungen.h |
jack1930 | 26:b045cb598879 | 5 | * |
jack1930 | 26:b045cb598879 | 6 | * Example: |
jack1930 | 26:b045cb598879 | 7 | * @code |
jack1930 | 26:b045cb598879 | 8 | * #include "mbed.h" |
jack1930 | 27:31c8b9939601 | 9 | * #include "ESP8266Webserver.h" |
jack1930 | 27:31c8b9939601 | 10 | * #include "LCD.h" |
jack1930 | 27:31c8b9939601 | 11 | |
jack1930 | 27:31c8b9939601 | 12 | * lcd myLCD; |
jack1930 | 27:31c8b9939601 | 13 | * ESP8266Webserver myWebserver; |
jack1930 | 27:31c8b9939601 | 14 | * AnalogIn poti(PA_0); |
jack1930 | 27:31c8b9939601 | 15 | * DigitalOut myled(PC_0); |
jack1930 | 27:31c8b9939601 | 16 | * string getRootPage() |
jack1930 | 27:31c8b9939601 | 17 | * { |
jack1930 | 27:31c8b9939601 | 18 | * string webpage; |
jack1930 | 27:31c8b9939601 | 19 | * webpage="<!DOCTYPE html>"; |
jack1930 | 27:31c8b9939601 | 20 | * //HTML |
jack1930 | 27:31c8b9939601 | 21 | * webpage+="<html>"; |
jack1930 | 27:31c8b9939601 | 22 | * webpage+="<head>"; |
jack1930 | 27:31c8b9939601 | 23 | * webpage+="<title>STM32 HTTP</title>"; |
jack1930 | 27:31c8b9939601 | 24 | * webpage+="</head>"; |
jack1930 | 27:31c8b9939601 | 25 | * webpage+="<body>"; |
jack1930 | 27:31c8b9939601 | 26 | * webpage+="<h1>WIFI mit STM32 ESP01</h1>\n"; |
jack1930 | 27:31c8b9939601 | 27 | * webpage+="<p>Poti:"+to_string(poti)+"</p>\n"; |
jack1930 | 27:31c8b9939601 | 28 | * webpage+="<a href=\"ledAn\"><button>ON</button></a>\n"; |
jack1930 | 27:31c8b9939601 | 29 | * webpage+="<a href=\"ledAus\"><button>OFF</button></a>\n"; |
jack1930 | 27:31c8b9939601 | 30 | * webpage+="</body>"; |
jack1930 | 27:31c8b9939601 | 31 | * webpage+="</html>"; |
jack1930 | 27:31c8b9939601 | 32 | * return webpage; |
jack1930 | 27:31c8b9939601 | 33 | * } |
jack1930 | 27:31c8b9939601 | 34 | * void testfunc() |
jack1930 | 27:31c8b9939601 | 35 | * { |
jack1930 | 27:31c8b9939601 | 36 | * myWebserver.send(200,"text/html",getRootPage()); |
jack1930 | 27:31c8b9939601 | 37 | * } |
jack1930 | 27:31c8b9939601 | 38 | * void testfunc2() |
jack1930 | 27:31c8b9939601 | 39 | * { |
jack1930 | 27:31c8b9939601 | 40 | * myled=0; |
jack1930 | 27:31c8b9939601 | 41 | * myWebserver.send(200,"text/html",getRootPage()); |
jack1930 | 27:31c8b9939601 | 42 | * } |
jack1930 | 27:31c8b9939601 | 43 | * void testfunc3() |
jack1930 | 27:31c8b9939601 | 44 | * { |
jack1930 | 27:31c8b9939601 | 45 | * myled=1; |
jack1930 | 27:31c8b9939601 | 46 | * myWebserver.send(200,"text/html",getRootPage()); |
jack1930 | 27:31c8b9939601 | 47 | * } |
jack1930 | 27:31c8b9939601 | 48 | * int main() { |
jack1930 | 27:31c8b9939601 | 49 | * myWebserver.on("ledAus",&testfunc2); |
jack1930 | 27:31c8b9939601 | 50 | * myWebserver.on("ledAn",&testfunc3); |
jack1930 | 27:31c8b9939601 | 51 | * myWebserver.on("/",&testfunc); |
jack1930 | 27:31c8b9939601 | 52 | * myWebserver.begin(); |
jack1930 | 27:31c8b9939601 | 53 | * myLCD.clear(); |
jack1930 | 27:31c8b9939601 | 54 | * myLCD.cursorpos(0); |
jack1930 | 27:31c8b9939601 | 55 | * myLCD.printf("%s",myWebserver.gibIP()); |
jack1930 | 27:31c8b9939601 | 56 | * while(1) { |
jack1930 | 27:31c8b9939601 | 57 | * myWebserver.handleClient(); |
jack1930 | 27:31c8b9939601 | 58 | * } |
jack1930 | 26:b045cb598879 | 59 | * } |
jack1930 | 26:b045cb598879 | 60 | * @endcode |
jack1930 | 26:b045cb598879 | 61 | */ |
jack1930 | 13:ed5c59ed4ae5 | 62 | #include "mbed.h" |
jack1930 | 16:b09391fb73b4 | 63 | //#include "string" |
jack1930 | 14:d17cfd8131b1 | 64 | #include "Einstellungen.h" |
jack1930 | 0:92e4253d430a | 65 | |
jack1930 | 14:d17cfd8131b1 | 66 | |
jack1930 | 13:ed5c59ed4ae5 | 67 | class ESP8266Webserver |
jack1930 | 0:92e4253d430a | 68 | { |
jack1930 | 19:e0bcbb2a3480 | 69 | private: |
jack1930 | 19:e0bcbb2a3480 | 70 | |
jack1930 | 13:ed5c59ed4ae5 | 71 | int hs_count=0; |
jack1930 | 13:ed5c59ed4ae5 | 72 | Callback< void()> cbs[10]; |
jack1930 | 13:ed5c59ed4ae5 | 73 | char hs[10][20]; |
jack1930 | 13:ed5c59ed4ae5 | 74 | char recbuf[1000]; |
jack1930 | 13:ed5c59ed4ae5 | 75 | bool beinhaltet(char* suchstring); |
jack1930 | 13:ed5c59ed4ae5 | 76 | int port=80; |
jack1930 | 13:ed5c59ed4ae5 | 77 | bool dbg; |
jack1930 | 13:ed5c59ed4ae5 | 78 | int clientID[10]={0,0,0,0,0,0,0,0,0,0}; |
jack1930 | 13:ed5c59ed4ae5 | 79 | int clientIdx=0; |
jack1930 | 13:ed5c59ed4ae5 | 80 | char sendstring[1000]; |
jack1930 | 13:ed5c59ed4ae5 | 81 | BufferedSerial *_serial; |
jack1930 | 15:e710f0431920 | 82 | char ipadr[100]; |
jack1930 | 13:ed5c59ed4ae5 | 83 | ATCmdParser *_parser; |
jack1930 | 16:b09391fb73b4 | 84 | |
jack1930 | 13:ed5c59ed4ae5 | 85 | bool gefunden; |
jack1930 | 14:d17cfd8131b1 | 86 | string ipad=ip; |
jack1930 | 13:ed5c59ed4ae5 | 87 | |
jack1930 | 15:e710f0431920 | 88 | |
jack1930 | 13:ed5c59ed4ae5 | 89 | |
jack1930 | 0:92e4253d430a | 90 | public: |
jack1930 | 13:ed5c59ed4ae5 | 91 | char suchergebnis[20]; |
jack1930 | 27:31c8b9939601 | 92 | /** Aufrufzähler (öffentliches Attribut) |
jack1930 | 27:31c8b9939601 | 93 | */ |
jack1930 | 16:b09391fb73b4 | 94 | int Aufrufe=0; |
jack1930 | 27:31c8b9939601 | 95 | |
jack1930 | 30:17dd2e085453 | 96 | /** Create Webserver instance |
jack1930 | 27:31c8b9939601 | 97 | */ |
jack1930 | 14:d17cfd8131b1 | 98 | ESP8266Webserver(); |
jack1930 | 27:31c8b9939601 | 99 | |
jack1930 | 27:31c8b9939601 | 100 | /** Legt die Callback-Operationen fest |
jack1930 | 27:31c8b9939601 | 101 | * |
jack1930 | 27:31c8b9939601 | 102 | * @param handlestring Suchtext im Request des Clients |
jack1930 | 27:31c8b9939601 | 103 | * @param func Callback-Operation falls der handlestring im Request gefunden wurde |
jack1930 | 27:31c8b9939601 | 104 | * @returns |
jack1930 | 27:31c8b9939601 | 105 | * 0 on success, |
jack1930 | 27:31c8b9939601 | 106 | * -1 on error |
jack1930 | 27:31c8b9939601 | 107 | */ |
jack1930 | 13:ed5c59ed4ae5 | 108 | int on(const char* handlestring,Callback< void()> func); |
jack1930 | 27:31c8b9939601 | 109 | |
jack1930 | 27:31c8b9939601 | 110 | /** Startet den Webserver |
jack1930 | 27:31c8b9939601 | 111 | * @returns |
jack1930 | 27:31c8b9939601 | 112 | * 0 on success, |
jack1930 | 27:31c8b9939601 | 113 | * -1 on error |
jack1930 | 27:31c8b9939601 | 114 | */ |
jack1930 | 13:ed5c59ed4ae5 | 115 | int begin(void); |
jack1930 | 27:31c8b9939601 | 116 | |
jack1930 | 27:31c8b9939601 | 117 | /** Prüft in der Endlosschleife zyklisch, ob neue Anfragen (Requests) vorliegen |
jack1930 | 27:31c8b9939601 | 118 | * |
jack1930 | 27:31c8b9939601 | 119 | * @returns |
jack1930 | 27:31c8b9939601 | 120 | * 0 on success, |
jack1930 | 27:31c8b9939601 | 121 | * -1 on error |
jack1930 | 27:31c8b9939601 | 122 | */ |
jack1930 | 13:ed5c59ed4ae5 | 123 | int handleClient(void); |
jack1930 | 27:31c8b9939601 | 124 | |
jack1930 | 27:31c8b9939601 | 125 | /** Schickt eine Antwort (response) zum Client |
jack1930 | 27:31c8b9939601 | 126 | * |
jack1930 | 27:31c8b9939601 | 127 | * @param HTTPStatus Statuscode (200 = alles in Ordnung) |
jack1930 | 27:31c8b9939601 | 128 | * @param Mimetype z.B. "text/html" |
jack1930 | 27:31c8b9939601 | 129 | * @param webseite String (const char*) der die Webseite enthält |
jack1930 | 27:31c8b9939601 | 130 | * @returns |
jack1930 | 27:31c8b9939601 | 131 | * 0 on success, |
jack1930 | 27:31c8b9939601 | 132 | * -1 on error |
jack1930 | 27:31c8b9939601 | 133 | */ |
jack1930 | 13:ed5c59ed4ae5 | 134 | int send(int HTTPStatus,const char* Mimetype, const char* webseite); |
jack1930 | 27:31c8b9939601 | 135 | |
jack1930 | 27:31c8b9939601 | 136 | /** Schickt eine Antwort (response) zum Client |
jack1930 | 27:31c8b9939601 | 137 | * |
jack1930 | 27:31c8b9939601 | 138 | * @param HTTPStatus Statuscode (200 = alles in Ordnung) |
jack1930 | 27:31c8b9939601 | 139 | * @param Mimetype z.B. "text/html" |
jack1930 | 27:31c8b9939601 | 140 | * @param webseite String der die Webseite enthält |
jack1930 | 27:31c8b9939601 | 141 | * @returns |
jack1930 | 27:31c8b9939601 | 142 | * 0 on success, |
jack1930 | 27:31c8b9939601 | 143 | * -1 on error |
jack1930 | 27:31c8b9939601 | 144 | */ |
jack1930 | 16:b09391fb73b4 | 145 | int send(int HTTPStatus,const char* Mimetype, std::string webseite); |
jack1930 | 27:31c8b9939601 | 146 | |
jack1930 | 27:31c8b9939601 | 147 | /** Findet einen Wert-String zu dem Suchstring in dem Request |
jack1930 | 27:31c8b9939601 | 148 | * Damit können Inputs in Formularen (Textfelder, Slider, Textboxen usw.) |
jack1930 | 27:31c8b9939601 | 149 | * ausgewertet werden |
jack1930 | 27:31c8b9939601 | 150 | * @param suchstring nach diesem String wird gesucht |
jack1930 | 27:31c8b9939601 | 151 | * @returns |
jack1930 | 27:31c8b9939601 | 152 | * Wertstring |
jack1930 | 27:31c8b9939601 | 153 | */ |
jack1930 | 13:ed5c59ed4ae5 | 154 | const char* gibWert(const char* suchstring); |
jack1930 | 27:31c8b9939601 | 155 | |
jack1930 | 27:31c8b9939601 | 156 | /** Findet einen Wert-String zu dem Suchstring in dem Request |
jack1930 | 27:31c8b9939601 | 157 | * Damit können Inputs in Formularen (Textfelder, Slider, Textboxen usw.) |
jack1930 | 27:31c8b9939601 | 158 | * ausgewertet werden |
jack1930 | 27:31c8b9939601 | 159 | * @param suchstring nach diesem String wird gesucht |
jack1930 | 27:31c8b9939601 | 160 | * @returns |
jack1930 | 27:31c8b9939601 | 161 | * Wertstring |
jack1930 | 27:31c8b9939601 | 162 | */ |
jack1930 | 16:b09391fb73b4 | 163 | const char* gibWert(std::string suchstring); |
jack1930 | 13:ed5c59ed4ae5 | 164 | void debugOn(bool pD); |
jack1930 | 15:e710f0431920 | 165 | void listAPs(); |
jack1930 | 27:31c8b9939601 | 166 | |
jack1930 | 27:31c8b9939601 | 167 | /** Gibt die IP-Adresse des Servers als String zurück |
jack1930 | 27:31c8b9939601 | 168 | * |
jack1930 | 27:31c8b9939601 | 169 | * @returns |
jack1930 | 27:31c8b9939601 | 170 | * Wertstring |
jack1930 | 27:31c8b9939601 | 171 | */ |
jack1930 | 15:e710f0431920 | 172 | char* gibIP(); |
jack1930 | 20:7924d3383786 | 173 | |
jack1930 | 14:d17cfd8131b1 | 174 | |
jack1930 | 0:92e4253d430a | 175 | }; |