Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Build an open IoT platform with Red Hat—keep it flexible with open source software.

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

Materials

  • 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

 

Program

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 

#ifdef DEBUG_PROG
  #define DEBUG_PRINTLN(x)  Serial.println(x)
  #define DEBUG_PRINT(x)    Serial.print(x)
#else
  #define DEBUG_PRINTLN(x) 
  #define DEBUG_PRINT(x)
#endif

EIoTCloudRestApi eiotcloud;

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

#define INSTANCE_ID "xxx"

#define REPORT_INTERVAL 1

#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 = {
  CONFIG_VERSION,
  // token
  "1234567890123456789012345678901234567890",
  // The default module 0 - invalid module
  0,
  //0 // not valid
};

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

int sensorValue = 0; 


void setup() {
    Serial.begin(9600);
    DEBUG_PRINTLN("Start...");

    EEPROM.begin(512);
    loadConfig();

    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: ");
      DEBUG_PRINTLN(token);
      eiotcloud.SetToken(token);

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

      // add new module and configure it
      moduleId = eiotcloud.ModuleNew();
      DEBUG_PRINT("ModuleId: ");
      DEBUG_PRINTLN(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: ");
      DEBUG_PRINTLN(modtyperet);

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

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

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

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

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

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

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

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

      // save configuration
      saveConfig();
    }

    // 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
    eiotcloud.SetToken(storage.token);    
    // read Sensor.Parameter1 ID from cloud
    parameterId = eiotcloud.GetModuleParameterByName(moduleId, "Sensor.Parameter1");
    DEBUG_PRINT("parameterId: ");
    DEBUG_PRINTLN(parameterId);

    //pinMode(A0, sensorPin);
}

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

  sensorValue = level;
  DEBUG_PRINT("LUX = ");
  DEBUG_PRINTLN(sensorValue);

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

  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.
  if (EEPROM.read(CONFIG_START + 0) == CONFIG_VERSION[0] &&
      EEPROM.read(CONFIG_START + 1) == CONFIG_VERSION[1] &&
      EEPROM.read(CONFIG_START + 2) == CONFIG_VERSION[2])
    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));

  EEPROM.commit();
}


Hardware

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

Download Red Hat’s blueprint for building an open IoT platform—open source from cloud to gateways to devices.

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

Published at DZone with permission of Igor Jarc, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}