Want to learn 3D Printing, hurry up and register for our upcoming Workshop that starts Dec.16th

IoT: light sensor data with NodeMCU chip

Posted by Momen 01/11/2017 0 Comment(s)

Welcome to Zeroohm Arduino Training series. In this series of Arduino Training, Zeroohm will focus its efforts on helping the community learn more about Arduino fundamentals and basics starting with basic control of LED's, motors, sensors and utilizing of wirless communication. We hope that you find this useful, Please, leave a comment for any questions or comments or reviews!

We provide all our trainings for groups from schools, universities, public sector and private sector in Abu Dhabi, Dubai, Sharjah, Fujirah, RAK or anywhere in UAE. Please,click here to contact us directly for any training inquires.

Displaying light sensor data by NodeMCU chip


We will Display light sensor data by NodeMCU chip, then we will explain programming commands.

Before we start this tutorial, we will take a look on NodeMCU chip before we start a these project of Internet of Things (IoT).

The NodeMCU Mini is an ESP8266 WiFi micro-controller that runs at 80MHz processing speed and at 3.3V logic voltage. This micro-controller contains a Tensilica chip core as well as a full WiFi stack. You can program the micro-controller using the Arduino IDE software for an easy-to-run Internet of Things (IoT) core or using the Lua script language.

Now we will start our project:

Hardware Components:

  • NodeMCU board.
  • ESPlorer program.
  • Bootstrap studio program.
  • Breadboard.
  • Plate NodeMCU board.
  • Light resistance (LDR).
  • Resistors (1K ohm).
  • LED.

Hardware Design:

Now let’s have a look at the hardware overview of the entire circuit shown in the image. Connect them as shown:


We also connect the ( LDR ) at pin (A0 ) from NodeMCU board.

Software Design:

The Code and explanation:

Copy this code to your ESPlorer software and save it with " ESPlorer_LDR" name or any other name (no problem at this point), then upload the code.


wifi.sta.config("your ssid","your pass")









function ldr()

ldr_value = adc.read(0)

percent_light = ldr_value*(100/1024)

percent_dark = 100 - percent_light



tmr.alarm(1,5000, 1, function()

ldr() bimb=bimb+1

if bimb==5 then bimb=0 wifi.sta.connect() print("Reconnect")




srv=net.createServer(net.TCP) srv:listen(80,function(conn)



conn:send('HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nCache-Control: private, no-store\r\n\r\n\

        <!DOCTYPE HTML>\

        <html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"></head>\

        <meta http-equiv="X-UA-Compatible" content="IE=edge">\

        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">\

        <meta http-equiv="refresh" content="6">\

        </head><div class="container">\

        <h1>Sensor Data</h1></br><div class="row">\

        <div class="col-md-4"><div class="panel panel-primary"><div class="panel-heading"><h3 class="panel-title">ldr value</h3>\

        </div><div class="panel-body">\

        <div class="form-group form-group-lg"><input type="text" class="form-control" value="'..ldr_value..'">\


        <div class="col-md-4"><div class="panel panel-success"><div class="panel-heading"><h3 class="panel-title">the percentage</h3>\

        </div><div class="panel-body">\

        <div class="form-group form-group-lg"><input type="text" class="form-control" value="light percent = '..percent_light..' %">\

        <input type="text" class="form-control" value="dark percent = '..percent_dark..' %">\


    conn:on("sent",function(conn) conn:close() end)




 code1-display- light-sensor data-NodeMCU

  • Set up the ESP8266 chip to receive Wi-Fi. That is mean we will runs the ESP8266 as a client.
  • In the second line we have the function that have tow arguments :

"ssid" We will write the name of the network we want to connect to.

"pass" We will write the secret password of the network we want to connect to.


  • In line 3 we will print the IP that we will use to connect to the server network page.
  • In Line 4 we have a delay function. The period of delaying here is by microsecond.


  • The Lines from 6 to 8, for definition the variables.
  • In the Line 9 we have a variable we will use it as a counter.
  • The command { adc.read() } at line 12, these use to read the values at the pin(A0), that LDR connected to.
  • Line 13 is to calculate the intensity of the light, then to save the value at the variable (percent_light ).
  • Line 14 is to calculate the intensity of darkness, then to save the value at the variable (percent_dark).
  • Line 15 is the end of the function ldr().


  • In the line 17 we have alarm function that execute a specific command every specific time period. Here is specifying by 5000 m/s (5 seconds).
  • Line 18: inside the alarm function will execute { ldr() } function and will increase the value of bimb variable to 1.
  • In the line 19: we will test the value of the bimb variable is it equal to 5 or not? If it is equal to 5 then the value bimb variable will equal to 0 and will connect to the Wi-Fi.
  • Line 20: is the end of the if statement rule.
  • Line 21: is the end of the alarm function.


  • Line 23: here is the command that will help us to be able to connect to the Wi-Fi network. Port 80 used to connect between the server and client.
  • Line 24: inside these function we will write the commands that enable us to design and format the sever page network.


  • The lines from 26 to 42 are the commands that used to create the web pages by bootstrap studio program or we can use any program where we have a good use.
  • Line 42: is the page server network.

The line 33 in the code.

The line 34 in the code.


The line 36 in the code.


The line 38 in the code.


The line 40 in the code.

The line 41 in the code.



Line 43: these function created to Display the contents of the networked page that we have created by using bootstrap studio program.

Line 44: is the end of the function{ conn:send('HTTP/1.1 200 OK\r\n….) }.

Line 45: is the end of the function { conn:on أﺍ)أﺍ "receive",function(conn,payload) }.

  • Now we will upload the program on the NodeMCU chip and starting our experiment:

After finishing the code we will connect the NodeMCU chip to the computer. Then we select the port that’s connected to the NodeMCU chip. Also we will chose 9600 as the speed of data transfer as in these image:


If we are do not find the port, we should press on the refresh button.

Then we click on the activate button connection  code9-display-light-sensor-data-NodeMCU   and the communication process will start as in these image:


  • Then we will upload the program by click on ( save to ESP ) button. Then we will save the code at (init.lua) name.
  • After saving the code at (init.lua) name, then the code will upload as in these image:


Now how to get the IP address:

After uploading the program on NodeMCU chip the IP will appear in the below like in these image:


But if not appear, then we will write these command { =wifi.sta.getip() } in the transmission box as in these image :


Then we press send, then the IP address will appear.


  • Few notes on the name of code file:

To run the program that was saved at ESP8266 properly, it is preferable to name all the files with (.init.lua) and save each code file at a new folder with same name of our project.

Let's have a look at Image below which explains the steps:



We hope that you have enjoyed and benefited from this tutorial. We say goodbye until the next tutorial.

Leave a Comment