DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • How To Become a 10x Dev: An Essential Guide
  • Does the OCP Exam Still Make Sense?
  • HashMap Performance Improvements in Java 8
  • Creating Scalable OpenAI GPT Applications in Java

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.

Igor Jarc user avatar by
Igor Jarc
·
Mar. 01, 17 · Tutorial
Like (2)
Save
Tweet
Share
18.45K Views

Join the DZone community and get the full member experience.

Join For Free

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

Light (web browser)

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

Opinions expressed by DZone contributors are their own.

Trending

  • How To Become a 10x Dev: An Essential Guide
  • Does the OCP Exam Still Make Sense?
  • HashMap Performance Improvements in Java 8
  • Creating Scalable OpenAI GPT Applications in Java

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: