Beruflich Dokumente
Kultur Dokumente
ESP8266 have only one adc channel. Lets begin to read analog and make something
cool
3 Serial.println("");
7 delay(500);
8 Serial.print(".");
9 }
10
12 Serial.println("");
13 Serial.print("Connected to ");
14 Serial.println(ssid);
2 server.on("/",handleRoot);
4 server.onNotFound(handleWebRequests); //Set setver all paths are not found so we can handle as per URI
5 server.begin();
The web server main page is on root. The notFound Handler performs task such as
sending javascripts, jQuery and Css file to client. ESP redirect explained here
1 void handleWebRequests(){
2 if(loadFromSpiffs(server.uri())) return;
5 message += server.uri();
9 message += server.args();
10 message += "\n";
13 }
15 Serial.println(message);
16 }
The above code actually first decodes the URL which is not found then these arguments
are passed to spiffs loader. ESP8266 SPIFFS is explained here
20 }
21
22 dataFile.close();
23 return true;
24 }
Once you know above all programming techniques lets move to actual programming.
Final Complete Code
copy and paste this code in arduino. then upload it
1 /*
3 * https://circuits4you.com
4 */
6 #include <ESP8266WiFi.h>
7 #include <ESP8266WebServer.h>
11
15
16
17 ESP8266WebServer server(80);
18
19
20 void handleADC(){
21 int a = analogRead(A0);
22 a = map(a,0,1023,0,100);
25 server.send(200, "text/plane",adc);
26 }
27
28 void handleRoot(){
30 server.send(302, "text/plane","");
31 }
32
33 void handleWebRequests(){
34 if(loadFromSpiffs(server.uri())) return;
37 message += server.uri();
41 message += server.args();
42 message += "\n";
45 }
47 Serial.println(message);
48 }
49
50 void setup() {
51 delay(1000);
52 Serial.begin(115200);
53 Serial.println();
54
55 //Initialize File System
56 SPIFFS.begin();
58
59
62 Serial.println("");
63
66 delay(500);
67 Serial.print(".");
68 }
69
71 Serial.println("");
72 Serial.print("Connected to ");
73 Serial.println(ssid);
76
77 //Initialize Webserver
78 server.on("/",handleRoot);
80 server.onNotFound(handleWebRequests); //Set setver all paths are not found so we can handle as per URI
81 server.begin();
82 }
83
84 void loop() {
85 server.handleClient();
86 }
87
91
107 }
108
109 dataFile.close();
111 }
Folder structure is your .ino file with data folder. In data folder you have these
files index.html, style.css, jQuery.min.js, d3-gauge.js.
Not Upload these files to your ESP8266 NodeMCU Flash File System. How to Do this
? Read here It takes a time to load files.
Step 3: Testing
Assuming that you have uploaded your program and SPIFFS files. Open serial monitor
and reset the ESP. You will get IP address, open it in web browser. Make sure your ESP
and your laptop are in same network
and you will get nice looking interface. as we have shown at the beginning.