Introduction: ESP8266 Light Sensor
In this tutorial we will show how to build WiFi light sensor. We will use ESP8266 and LDR (light dependent resistor) and connect to EasyIoT Cloud. Sensor is plug and play. ESP8266 Arduino IDE will be used to upload program to ESP8266.
Step 1: Materials
- ESP8266 WiFi module
- Resistor 10K
- LDR Photoresistor
- 5V Power supply (phone charger)
Step 2: Get Security Token
No EasyIoT Cloud configuration is needed, just register to EasyIoT Cloud service. After registration go to Configuration->Tokens And press button Add token. Remember Token - you will need it to modify program.
Step 3: Program
Program is written in Arduino ESP8266 IDE. See Arduino ESP8266 IDE tutorial how to connect ESP8266 module to computer to upload program. You will also need ESP8266 EasyIoT Cloud REST API V1 library.
Program is available at GitHub. In program change AP name and password and token:
#define AP_USERNAME "xxx"
#define AP_PASSWORD "xxx"
#define TOKEN "xxx"
Program at beginning reads setting in EEPROM. If module ID is 0 this means module is not added to EasyIoT Cloud. In this case module is added to EasyIoT Cloud, adds parameters and change module type. Then it reads voltage on analog pin and sends to EasyIoT Cloud.
Step 4: Hardware
In our case we use ESP8266 NodeMCU, but you can use any other ESP8266 with AI. Just connect 10K resistor and LDR to 3.3V and GND. Resistor and LDR connection connect to ESP8266 analog input A0.
After programming module is automatically added to EasyIoT Cloud. Just login to EasyIoT Cloud and you will see module in WEB interface.

Participated in the
Sensors Contest 2017
4 Comments
Tip 1 year ago
I wanted to create a light sensor that I could poll locally. No cloud. I took the simple authenticationless webserver example with the Arduino IDE ESP8266 extension and put the value of A0 in the root of the webserver. I used the exact hardware setup in the picture above. Here is the code
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
// Replace with your network credentials
const char* ssid = "MyFy";
const char* password = "farkomatic";
ESP8266WebServer server(80); //instantiate server at port 80 (http port)
String page = "";
double Lightdata;
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 = String(Lightdata); //publish the value in text
server.send(200, "text/html", page);
});
server.begin();
Serial.println("Web server started!");
}
// loop to read sensor
void loop(void){
Lightdata = analogRead(A0);
delay(1000);
server.handleClient();
}
Here is the simple way I poll the data from the nodemcu (10.1.10.8) using a linux box on the same network.
# value=`wget -q -O - http://10.1.10.8`
# echo $value
44.00
#
That is a daylight value. I can now get the light value on demand. Thanks for the help iotguy.
Question 3 years ago on Step 4
it is fine and easy to made it (copy paste), I also made the DS18B20(copy paste). But how to combine two senzors and send it to? Something tells me that the copy paste of two programs will not work together :))
3 years ago
Looks good :) You should enter in the Sensors Contest.
Reply 3 years ago
Than you for the suggestion. I will.