1 | /*--------------------------------------------------
|
2 | HTTP 1.1 Webserver as AccessPoint for ESP8266
|
3 | for ESP8266 adapted Arduino IDE
|
4 |
|
5 | by Stefan Thesen 08/2015 - free for anyone
|
6 |
|
7 | Does HTTP 1.1 with defined connection closing.
|
8 | Handles empty requests in a defined manner.
|
9 | Handle requests for non-exisiting pages correctly.
|
10 |
|
11 | This demo allows to switch two functions:
|
12 | Function 1 creates serial output and toggels GPIO2
|
13 | Function 2 just creates serial output.
|
14 |
|
15 | Serial output can e.g. be used to steer an attached
|
16 | Arduino, Raspberry etc.
|
17 | --------------------------------------------------*/
|
18 |
|
19 | #include <ESP8266WiFi.h>
|
20 |
|
21 | const char* ssid = "ESP-Accesspoint";
|
22 | const char* password = "12345678"; // set to XX for open access point w/o passwortd
|
23 |
|
24 | unsigned long ulReqcount;
|
25 |
|
26 |
|
27 | // Create an instance of the server on Port 80
|
28 | WiFiServer server(80);
|
29 |
|
30 | void setup()
|
31 | {
|
32 | // setup globals
|
33 | ulReqcount=0;
|
34 |
|
35 | // prepare GPIO2
|
36 | pinMode(2, OUTPUT);
|
37 | digitalWrite(2, 0);
|
38 |
|
39 | // start serial
|
40 | Serial.begin(9600);
|
41 | delay(1);
|
42 |
|
43 | // AP mode
|
44 | WiFi.mode(WIFI_AP);
|
45 | WiFi.softAP(ssid, password);
|
46 | server.begin();
|
47 | }
|
48 |
|
49 | void loop()
|
50 | {
|
51 | // Check if a client has connected
|
52 | WiFiClient client = server.available();
|
53 | if (!client)
|
54 | {
|
55 | return;
|
56 | }
|
57 |
|
58 | // Wait until the client sends some data
|
59 | Serial.println("new client");
|
60 | unsigned long ultimeout = millis()+250;
|
61 | while(!client.available() && (millis()<ultimeout) )
|
62 | {
|
63 | delay(1);
|
64 | }
|
65 | if(millis()>ultimeout)
|
66 | {
|
67 | Serial.println("client connection time-out!");
|
68 | return;
|
69 | }
|
70 |
|
71 | // Read the first line of the request
|
72 | String sRequest = client.readStringUntil('\r');
|
73 | //Serial.println(sRequest);
|
74 | client.flush();
|
75 |
|
76 | // stop client, if request is empty
|
77 | if(sRequest==XX)
|
78 | {
|
79 | Serial.println("empty request! - stopping client");
|
80 | client.stop();
|
81 | return;
|
82 | }
|
83 |
|
84 | // get path; end of path is either space or ?
|
85 | // Syntax is e.g. GET /?pin=MOTOR1STOP HTTP/1.1
|
86 | String sPath=XX,sParam=XX, sCmd=XX;
|
87 | String sGetstart="GET ";
|
88 | int iStart,iEndSpace,iEndQuest;
|
89 | iStart = sRequest.indexOf(sGetstart);
|
90 | if (iStart>=0)
|
91 | {
|
92 | iStart+=+sGetstart.length();
|
93 | iEndSpace = sRequest.indexOf("X",iStart);
|
94 | iEndQuest = sRequest.indexOf("?",iStart);
|
95 |
|
96 | // are there parameters?
|
97 | if(iEndSpace>0)
|
98 | {
|
99 | if(iEndQuest>0)
|
100 | {
|
101 | // there are parameters
|
102 | sPath = sRequest.substring(iStart,iEndQuest);
|
103 | sParam = sRequest.substring(iEndQuest,iEndSpace);
|
104 | }
|
105 | else
|
106 | {
|
107 | // NO parameters
|
108 | sPath = sRequest.substring(iStart,iEndSpace);
|
109 | }
|
110 | }
|
111 | }
|
112 |
|
113 | ///////////////////////////////////////////////////////////////////////////////
|
114 | // output parameters to serial, you may connect e.g. an Arduino and react on it
|
115 | ///////////////////////////////////////////////////////////////////////////////
|
116 | if(sParam.length()>0)
|
117 | {
|
118 | int iEqu=sParam.indexOf("=");
|
119 | if(iEqu>=0)
|
120 | {
|
121 | sCmd = sParam.substring(iEqu+1,sParam.length());
|
122 | Serial.println(sCmd);
|
123 | }
|
124 | }
|
125 |
|
126 | .......
|
127 |
|
128 | //////////////////////
|
129 | // react on parameters
|
130 | //////////////////////
|
131 | if (sCmd.length()>0)
|
132 | {
|
133 | // write received command to html page
|
134 | sResponse += "Kommando:" + sCmd + "<BR>";
|
135 |
|
136 | // switch GPIO
|
137 | if(sCmd.indexOf("FUNCTION1ON")>=0)
|
138 | {
|
139 | digitalWrite(2, 1);
|
140 | }
|
141 | else if(sCmd.indexOf("FUNCTION1OFF")>=0)
|
142 | {
|
143 | digitalWrite(2, 0);
|
144 | }
|
145 | }
|
146 |
|
147 | ........
|