IoT Enabled Sensor Data Collection Centre With ESP8266 & PubNub




Introduction: IoT Enabled Sensor Data Collection Centre With ESP8266 & PubNub

About: Module143 - blog

Most of the tutorials on ESP8266 is either at the newbie level (remotely blinking an led) or too complex for somebody who is looking for something to improve and upgrade upon his led blinking skills.This instructable aims to bridge this gap to create a sensor hub using ESP8266 and post the collected data to PubNub. The main aim/objective is to minimize the time people spend on building the hardware and instead focus their time on data analytics and visualization with the collected data.

For people who are not familiar with ESP8266/NodeMCU we suggest you to get a basic understanding of how to flash and program through ESPLORER.There are a lot of tutorials/instructables which offers this like Get-Started-with-ESP8266-Using-AT-Commands-NodeMCU.

At the end of this instructable you will be able to build your own sensor data collection hub and a basic realtime graph visualization with help of PubNub

Welcome to ESP8266-NodeMCU Lesson - 102!!

Step 1: Components Used in the Project

The following components are required to complete the project

  • An ESP8266 board.The board which is used for this instructable is NodeMCU devKit v1.0 (insert module 143 link here)
  • Any sensor whose data needs to collected and logged.Here a simple potentiometer is used as a analog sensor
  • A slide switch
  • A micro to USB (type male) cable to upload the code to NodeMCU devKit v1.0 and power the device
  • 2 led's for mode indicator
  • Some male to male cables and breadboard
  • A PubNub account with publish key,subscribe key and a channel

Download the below files.If you don't want to change anything and just need it to work download the pre-compiled version(insert compiled version zip folder here).If you want to understand how it works and wants to get your hands dirty then you can also download the main source code(insert source code version here)

Step 2: Circuit Design

If you have bought (insert link of the final product here) then you can skip this step

Step 3: Uploading Code to NodeMCU Kit Using ESPlorer

Once you open the ESPlorer application the initial screen will look like the first image.Select the COM port from the dropdown at the top.If COM port is not displayed even when the device is connected just reopen the application.

Now there are two ways you can go ahead and complete this instructable

Finish the sensor hub hardware part as soon as possible and move on to play with the data

Understand the way NodeMCU and lua script works and customize according to your need


Upload all pre-compiled files(.lc files) and move on to next step

else if(option==2)

Just open the source code files (.lua) files in ESPlorer and start playing with the code.Jump to step 5 for description

Step 4: Configuring the Sensor Hub

Now slide the switch towards config mode and restart the module.Config mode indicator led should glow.

As shown in first image and wireless network with the name of "configMode" will be created and visble.Connect to that network with the password as "password1234" from any computer,lapotop or mobile .

Open any browse and enter the following url'wifi_network_name'&pwd='password'&apipubkey='publish_key'&apisubkey='subscribe_key'&channel='Channel_name'&sensorOneName='Sensor_1_name'&check=1

Replace the parameters in quotes with your own wifi(with internet access) username password values and PubNub keys.The final url should be something like below'&channel=channel1&sensorOneName=sensor1&check=1

If the sensor hub is properly configured you the config mode led will switch off and data mode led will switch on and the "configMode" wireless network will disappear. If you want to understand how this works or want to change some parameters check out the next step else jump to step 8

Step 5: Customizing the Configuration

So what happens is ESP8266 acts as a router and creates a wireless network with the given ssid,username and ip address to which you can connect.Configuration is done by entering the parameters in the format given below and hitting the below url from any browser of any device connected to the configured wireless network.'wifi_network_name'&pwd='password'&apipubkey='publish_key'&apisubkey='subscribe_key'&channel='Channel_name'&sensorOneName='Sensor_1_name'&check=1

Open ap.lua file in ESPlorer.This script is responsible for the initial configuration of the sensor hub.It generates two files based on the data that is given by the user

  • station.lua (contains the network ssid and password which has internet access to post the data)
  • api_file.lua (contains the PubNub keys,channel name with the sensor names)

To Customize ip address:

The ip can be set to any vaild address which should be changed in the url.Default address will be "".As seen in the first image the first 3 lines are responsible for setting the ip address and gateway.You can check if the address is proper by sending "=wifi.sta.getip()" command

To Customize ssid name

The next set of code in the same image is responsible for setting the ssid and password for the wireless network.

Remember if you have done any change in .lua file you have to compile them for the changes to get reflected after restart

  1. Upload the modified .lua file..for example ap.lua file
  2. Send the command "node.compile(ap.lua)" by clicking on the send button at the bottom of ESPlorer
  3. Now your lua files are compiled and new .lc files will be generated

Step 6: Addition of Sensors to the Hub and PubNub

To add more sensor

By default the code sends only one sensor data which is connected to analog pin 0.You can add more sensors to send data simultaneously.Follow the below steps

  1. Add the sensor name in the url as shown in bold.So now url will be like below'wifi_network_name'&pwd='password'&apipubkey='publish_key'&apisubkey='subscribe_key'&channel='Channel_name'&sensorOneName='Sensor_1_name'&sensorTwoName='Sensor_2_name'&check=1
  2. The corresponding name should be added to ap.lua file as shown in image 1
    print(sensorTwo) .(This is just to view and check the data)
  3. The final step is to add it to api_file generation part at the end as shown in image 2

    Repeat the same step for each addition of sensor. Remember to use for digital and for analog to digital signals

Remember if you have done any change in .lua file you have to compile them for the changes to get reflected after restart

  1. Upload the modified .lua file..for example ap.lua file
  2. Send the command "node.compile(ap.lua)" by clicking on the send button at the bottom of ESPlorer
  3. Now your lua files are compiled and new .lc files will be generated

Step 7: Initializing Data Collection and Sending to PubNub

Once the configuration is done properly the data mode led will get switched on.

Basically this means that the sensor data is being sent to PubNub based on the configuration settings that you had given in the previous steps.

By default the hub will send data to PubNub every 5 seconds.If you want to configure that follow the below steps

To Customize frequency of data collection:

  • Open main.lua in ESPlorer
  • Go to line highlighted in the image
  • The value mentioned there should be in milliseconds.It is advisable to atleast have an interval of 2 seconds to avoid any loss of data.
  • Upload main.lua file to esp and compile the file to generate .lc file
  • Restart the module and verify

Step 8: Simple Html Page for Realtime Display of Data From PubNub

Open the Sample.html file from the downloaded files.This is just a plain html page to view realtime graph of the collected data.

As seen in image 1, you just need to configure it with your own PubNub keys and channel name.

You can also add this to any website you develop and create even more stunning visualisation.Refer to PunNub EON for more information.

Step 9: Troubleshooting & FAQ

Will be updated shortly

Be the First to Share


    • Pocket-Sized Speed Challenge

      Pocket-Sized Speed Challenge
    • Audio Challenge 2020

      Audio Challenge 2020
    • Maps Challenge

      Maps Challenge


    DIY Hacks and How Tos

    Nice project. You should enter this into the Arduino contest that is running on the site. It is open to all microcontroller related projects.