Over a million developers have joined DZone.

Building a Wi-Fi Light Sensor

DZone's Guide to

Building a Wi-Fi Light Sensor

Hooking up your own light sensor to your Wi-Fi is easy enough and can be incorporated into other projects. You just need some hardware and some C.

· IoT Zone ·
Free Resource

Download The Comparative Guide to Rules Engines for IoT to learn what are the most common automation technologies used in the IoT domain for application development

In this tutorial, we will cover how to build Wi-Fi light sensor. We will use an ESP8266 and an LDR (light dependent resistor) and connect to EasyIoT Cloud. The sensor is plug and play. The ESP8266 Arduino IDE will be used to upload the program to the ESP8266.

This project is suitable for beginners because it consists of only three components. As mentioned, the sensor is plug and play — it automatically registers to EasyIoT Cloud

For the light sensor, we will use an LDR — light dependent resistor. The LDR is a light-controlled variable resistor. The resistance of a photoresistor decreases with increasing light intensity; in other words, it exhibits photoconductivity. It will be connected in series with a resistor. One side of the LDR is connected to the ground, and other to the resistor. And on the other side of the resistor, we're connecting is connected to 3.3V. Voltage in the LDR and resistor connection is dependent on light on the LDR resistor. The ESP8266 will be used to measure this voltage and send it to EasyIoT Cloud.

ESP8266 light sensor

ESP8266 light sensor module

Finished ESP8266 Internet-controlled switch


  • ESP8266 WiFi module
  • Resistor 10K 
  • LDR photoresistor
  • 5V power supply

EasyIoT Cloud configuration 

No EasyIoT Cloud configuration is needed, just register to EasyIoT Cloud. After registration, go to Configuration->Account limits. Remember the Instance ID — you will need it to modify the program.

EasyIoT Cloud Instance Id



The program is written in the Arduino ESP8266 IDE. See the Arduino ESP8266 IDE tutorial to learn how to connect the ESP8266 module to your computer to upload the program. You will also need the ESP8266 EasyIoT Cloud REST API V1 library from GitHub.

In the program, change the AP name and password and Instance ID:

#define AP_USERNAME "xxx"
#define AP_PASSWORD "xxx"
#define INSTANCE_ID "xxx"

At the beginning, the program reads settings in EEPROM. If the module ID is 0, this means the module is not added to EasyIoT Cloud. It first generates a token, then generates the module, adds the parameters, and changes the module type. Then, it reads voltage on the analog pin and sends it to EasyIoT Cloud.   

#include <ESP8266WiFi.h>
#include "EIoTCloudRestApiV1.0.h"
#include <EEPROM.h>

#define DEBUG_PROG 

  #define DEBUG_PRINTLN(x)  Serial.println(x)
  #define DEBUG_PRINT(x)    Serial.print(x)
  #define DEBUG_PRINTLN(x) 
  #define DEBUG_PRINT(x)

EIoTCloudRestApi eiotcloud;

// change those lines
#define AP_USERNAME "xxx"
#define AP_PASSWORD "xxx"

#define INSTANCE_ID "xxx"


#define CONFIG_START 0
#define CONFIG_VERSION "v01"

#define MIN_VALUE 25
#define MAX_VALUE 800

#define SENSOR_PIN A0    // select the input pin for the potentiometer

struct StoreStruct {
  // This is for mere detection if they are your settings
  char version[4];
  // The variables of your settings
  char token[41];
  uint moduleId;
  //bool tokenOk; // valid token  
} storage = {
  // token
  // The default module 0 - invalid module
  //0 // not valid

String moduleId = "";
String parameterId = "";

int sensorValue = 0; 

void setup() {


    eiotcloud.begin(AP_USERNAME, AP_PASSWORD);

    // if first time get new token and register new module
    // here hapend Plug and play logic to add module to Cloud
    if (storage.moduleId == 0)
      // get new token - alternarive is to manually create token and store it in EEPROM
      String token = eiotcloud.TokenNew(INSTANCE_ID);
      DEBUG_PRINT("Token: ");

      // remember token
      token.toCharArray(storage.token, 41);

      // add new module and configure it
      moduleId = eiotcloud.ModuleNew();
      DEBUG_PRINT("ModuleId: ");
      storage.moduleId = moduleId.toInt();

      // stop if module ID is not valid
      if (storage.moduleId == 0)
        DEBUG_PRINTLN("Check Account limits -> module limit.");
        while(true) delay(1);
      // set module type
      bool modtyperet = eiotcloud.SetModulType(moduleId, "MT_GENERIC");
      DEBUG_PRINT("SetModulType: ");

      // set module name
      bool modname = eiotcloud.SetModulName(moduleId, "LUX meter");
      DEBUG_PRINT("SetModulName: ");

      // add image settings parameter
      String parameterImgId = eiotcloud.NewModuleParameter(moduleId, "Settings.Icon1");
      DEBUG_PRINT("parameterImgId: ");

      // set module image
      bool valueRet1 = eiotcloud.SetParameterValue(parameterImgId, "radiation.png");
      DEBUG_PRINT("SetParameterValue: ");

      // now add parameter to value
      parameterId = eiotcloud.NewModuleParameter(moduleId, "Sensor.Parameter1");
      DEBUG_PRINT("ParameterId: ");

      //set parameter description
      bool valueRet2 = eiotcloud.SetParameterDescription(parameterId, "LUX");
      DEBUG_PRINT("SetParameterDescription: ");

      //set unit
      bool valueRet3 = eiotcloud.SetParameterUnit(parameterId, "%");
      DEBUG_PRINT("SetParameterUnit: ");

      //Set parameter LogToDatabase
      bool valueRet4 = eiotcloud.SetParameterLogToDatabase(parameterId, true);
      DEBUG_PRINT("SetLogToDatabase: ");

      bool valueRet5 = eiotcloud.SetParameterAverageInterval(parameterId, "10");
      DEBUG_PRINT("SetAvreageInterval: ");

      // save configuration

    // if something went wrong, wiat here
    if (storage.moduleId == 0)
      while(true) delay(1);

    // read module ID from storage
    moduleId = String(storage.moduleId);
    // read token ID from storage
    // read Sensor.Parameter1 ID from cloud
    parameterId = eiotcloud.GetModuleParameterByName(moduleId, "Sensor.Parameter1");
    DEBUG_PRINT("parameterId: ");

    //pinMode(A0, sensorPin);

void loop() {
  sensorValue = analogRead(SENSOR_PIN);
  float level = 100 - ((sensorValue - MIN_VALUE) * 100 / (MAX_VALUE - MIN_VALUE));

  sensorValue = level;

  bool valueRet = eiotcloud.SetParameterValue(parameterId, String(sensorValue));
  DEBUG_PRINT("SetParameterValue: ");

  delay(1000 * 10 * REPORT_INTERVAL);

void loadConfig() {
  // To make sure there are settings, and they are YOURS!
  // If nothing is found it will use the default settings.
    for (unsigned int t=0; t<sizeof(storage); t++)
      *((char*)&storage + t) = EEPROM.read(CONFIG_START + t);

void saveConfig() {
  for (unsigned int t=0; t<sizeof(storage); t++)
    EEPROM.write(CONFIG_START + t, *((char*)&storage + t));



In our case, we use the ESP8266 NodeMCU, but you can use any other ESP8266 with AI. Just connect a 10K resistor and the LDR to 3.3V and GND. The Resistor and LDR connection connect to the ESP8266 analog input.

ESP8266 light sensor schematic

See how CEP engines, stream processing engines, flow based programming engines and other popular rule-based technologies perform against seven IoT-specific criteria.

esp8266 ,iot ,tutorial ,light sensor ,wi-fi

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}