We set up a simple NodeMCU web server in our previous tutorial. There we controlled an LED from a web page hosted by the NodeMCU ESP8266 board! This time, we’ll be displaying data from a sensor connected to the NodeMCU’s analog pin.
Video Tutorial
In this tutorial, I’ll be displaying data coming from a light dependent resistor (LDR) to our NodeMCU web server. You can use any sensor available to you as long as its output voltage is not more than the recommended maximum of 3.3 V.
The ESP8266 itself accepts only up to 1 V on its analog pin. Thankfully, the NodeMCU board has a built-in voltage divider.
The ESP8266 (which is the main chip on the NodeMCU) uses a 10-bit ADC. This means the input voltage from 0 to 3.3 V will be equivalent to 0 to 1024.
Materials Needed
Wiring Diagram
Here’s our simple circuit for this tutorial:
I used a trimmer together with the LDR for voltage division so that I can adjust the “sensitivity” of my sensor.
Code
#include <ESP8266WiFi.h> #include <WiFiClient.h> #include <ESP8266WebServer.h> // Replace with your network credentials const char* ssid = "<Your WiFI SSID>"; const char* password = "<Your WiFI Password>"; ESP8266WebServer server(80); //instantiate server at port 80 (http port) String page = ""; double data; void setup(void){ pinMode(A0, INPUT); delay(1000); Serial.begin(115200); WiFi.begin(ssid, password); //begin WiFi connection Serial.println(""); // Wait for connection while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); server.on("/", [](){ page = "<h1>Sensor to Node MCU Web Server</h1><h3>Data:</h3> <h4>"+String(data)+"</h4>"; server.send(200, "text/html", page); }); server.begin(); Serial.println("Web server started!"); } void loop(void){ data = analogRead(A0); delay(1000); server.handleClient(); }
Basically, I used analogRead() to capture the voltage from the divider circuit setup by the trimmer and the LDR. The data is then embedded to the web server page.
Upload the code and open your browser to the address shown on the serial monitor (see previous tutorial for more on this).
Output
Here’s what the web page should look like:
Try to cover the LDR and refresh the page. It should show a different value.
The disadvantage with this sketch is that you need to refresh the page to update the sensor values. With ajax, you can avoid refreshing the page manually.
That’s it! Next up, we’ll log our sensor data to a spreadsheet program like Google Sheets.
Can I show this to my won website ??
Of course, but kindly rephrase please to avoid plagiarism 🙂
my web page not loaded
please give your libraries. my esp8266wifi library is not compleate
Hi! I suggest you add the ESP8266 core to your Arduino IDE as detailed in the first part of my NodeMCU series.
hlo can u tell me how can i automatically refresh the webpge,without doing it mannually. to read the sensor data continously
Hi,
I wrote a tutorial on how to use ajax with NodeMCU so that the page contents are automatically refreshed.
Thank you for the article.
As continuation of this proyect would be really interesting show how to display in a website accesible everywhere
Hi. Looking for a way to attach a DHT11 temperature/humidity sensor and 3 magnetic door switches. So i can see the temp/hum and if a door/window is open. could you please tell me how can i achieve that, please? Also how can i send the info to my server(in my LAN).
how can i send data from my webpage to nodemcu
Hello i have one error when i run the script i get ‘class WiFiServer’ has no member named ‘on’ i think its the version of the library or something else, can you plz help me ?
I run the 1.8.7 version of arduino.
How can i get live data from multiple analog sensors in nodemcu ??
Hi Roland, please help.
I wanna get data from my ADXL345 followed this great guide.
But when I connect ADXL345’s SDA to A0, there is only one number return.
How can I fix it?
Hi Duc Dao Minh!
ADXL345 communicates via I2C so you should not connect it to A0. Connect SCL to D1 and SDA to D2. Then use this library to easily interface with the ADXL345: https://github.com/sparkfun/SparkFun_ADXL345_Arduino_Library
Can’t wait to try now, I will reply with my result. Thank you for kind helping.
Hi Roland, thanks for help.
I connected as you guided but the data is unchanged.
Could I send you my circuit and my code, then you check it for me?
Best Regards.
Sure, just send it via email to roland[at]teachmemicro.com
Thank you Roland. I have sent an email as you recommended. Please check it.
how can i add another line of data for another sensor in this sketch?
You can concatenate another string to the data variable, just make sure you convert it to double first.
how to see the output on web page
can i use NodeMCU V3 in this project?