• nithya7rns

Smart Light System - IOT using ThingSpeak Cloud - Esp8266

Updated: Dec 31, 2020

Data collection and analysis plays an important role in the development of #smart devices. By analysis, the first thing is plotting a graph for the output versus time. Different online platforms aid the same and in this article, a similar device is going to be built which will not only display and give desirable output but will also send an analysis of the same.


Smart Control systems have been much necessary for better control and optimal usage of resources like electricity, water, etc. in our day-to-day life. Every system would start with the smallest automated device. A smart light System is one such part of a big #Automation system that gives an idea of how such a system works and how it is analyzed.



Hardware Requirements


The following hardware components are required for the circuit.


Components Required

  1. NodeMCU

NodeMCU

NodeMCU is a platform provided for #IoT. It basically includes firmware (computer software that contains instructions and controls the hardware) which runs on Esp8266 WiFi SoC (software on-chip). It is open-source firmware available for prototyping and developing kits. MCU means a Micro-Controller Unit. The device contains GPIO (General Purpose Input Output) pins which send and receives data on connection to other devices.

Esp12e module is also an SoC used for WiFi and is used in this project.

The specifications of the Esp12e module are:

  • 32-bit lx106 RISC Microprocessor.

  • 80MHz-160MHz, the adjustable clock frequency.

  • Supports Arctic OS(Operating System).

  • 1 KB Internal #RAM.

  • 4 MB External flash.

  • 2.5V to 3.6V, 3.3V onboard.

  • 600 milliAmp Regulator.

  • 80 milliAmp Operating Current.

  • 20 microAmp Current in sleeping Mode.

  • 17 GPIO pins (which include ADC channels, 2 UART Interfaces, 4 PWM outputs, LED, Reset button, etc.).

Buy NodeMCU from here.


. 2. Breadboard


Breadboard

The breadboard is the basic component of any circuit building process. All components, be it input sensors or output display devices are connected to the NodeMCU or any other microcontroller using wired connections through a breadboard. The holes in the breadboard are in series. There are various sizes like full-sized, half-sized, and mini breadboard.


Buy breadboard from here.



3. Jumper Wires


Jumper Wires

These are the main components that are used to establish the connections between different devices of the circuit.


Buy Jumper Wires from here.


4. LED


LED

Light Emitting Diode is a commonly used light source. It is a semiconductor that emits light when current flows through it.


Buy LED from here.



5.Resistors


10k Ohm Resistor
220 Ohm Resistor










Resistors are passive devices that restrict the flow of current or divide the voltage through the circuit. The resistors used for the proper functioning of this circuit are 10k Ohm and 220 Ohm Resistor.


For 10k Ohm Resistor, the 4-band color code is Brown, Black, Orange, Gold where Orange color is the multiplier (1000) and Gold color shows the tolerance level (±5%).


For the 220Ohm resistor, the 4-band color code is Red, Red, Brown, Gold where Brown color is the multiplier(10) and Gold color shows the tolerance level(±5%).


Buy Resistors from here.


6. LDR


LDR

Light Dependent Resistors (LDR) are photoresistors that allow to measure the intensity of light and gives the output in terms of Voltage. It converts the analog value of voltage into a #digital range of values (0-1023).


Buy LDR Sensor from here.


Software Requirements

1. Arduino IDE


Arduino IDE Logo

#Arduino IDE (Integrated Development Platform) which is used to interface the boards like NodeMCU to run the desired code designed to interface hardware devices.



2. ThingSpeak


#ThingSpeak cloud is an open-source IoT application that provides API to store and retrieve data from the data given through the device connected to it. It uses HTTP and MQTT protocols. ThingSpeak is a cross-platform which is used for data analysis( by plotting real-time graphs) and has a grown relation with Map-labs. It is also having a numerical computing software MatLab for map-works.



Circuit Connections

Circuit Diagram

The circuit connections are as follows.

  • A0 terminal of the NodeMCU is connected to one terminal of LDR and to the 10K Ohm resistor which is then connected to the Ground in the breadboard.

  • The other terminal of the LDR is connected to the 3V3 pin of the NodeMCU.

  • The LED is connected to the NodeMCU board through the 220 Ohm Resistor.

  • One terminal of the resistor is connected to the D1 pin of the NodeMCU board and the other terminal of the resistor is connected to the LED.

  • The negative terminal of the LED is connected to the Ground pin of the board through the beadboard.

  • Finally, the NodeMCU board is connected to the system with the IDE through the USB cable.



Code:


The code description is as follows.

#include <ThingSpeak.h>

The ThingSpeak header file must be included in the code for the interfacing action to take place. If the header file is not available, it must be installed and used.

#include <ESP8266WiFi.h>

As we are using the ESP8266 chip for WiFi, the corresponding header file must also be added to use the library functions associated with it.

const char* ssid     = "IAM IRONMAN";
const char* password = "12192302";

These constant character values are declared to store the Network name and the password used for WiFi connection.

const int ldrPin = A0; 

The pin which is connected to the LDR is A0. This must be declared and the value must be assigned to an integer to store the value.

int input_val = 0;

The input value from the LDR must be initialized to 0 so that no other Junk value is assigned.

const int ledPin = 5;

The LED is connected to pin 5 and the same is assigned to an integer.

WiFiClient  client;

Under the class WiFiClient obtained by including the #ESP8266WiFi header file, a variable client is declared. The above statement shows the same.

long myChannelNumber = 1101154;
const char myWriteAPIKey[] = "KSP74PSLSVCW3C1L";

The next step is to connect the ThingSpeak cloud to the device. The Channel ID and the API key is provided on the ThingSpeak page after the necessary steps are followed to set up the channel. The same must be entered in the IDE to interface the device and the Cloud storage.

void setup() {
   Serial.begin(9600); 
   delay(100);

The setup( ) function includes the necessary values like baud rate and delays to be set before the execution of the code starts.

pinMode(ledPin, OUTPUT); 

The PIN to which the LED is connected is set to OUTPUT mode at the beginning as the action will happen on the LED-based on LDR input.

digitalWrite(ledPin, 0);

The value received from the LED pin is set to 0 after which the values will vary based on the condition and the LDR input.

pinMode(ldrPin, INPUT);

The LDR sensor is the one that gives input to the device in this circuit. Hence, the pin to which LDR is connected is declared as INPUT.

 Serial.println();
 Serial.println();
 Serial.print("Connecting to ");
 Serial.println(ssid);

A few blank lines are printed and after which the statement is printed which indicates the WiFi network (name) to which it is connecting.

WiFi.begin(ssid, password);

WiFi.begin( ) is a function that is executed to allow the device to connect to the WiFi network given in the code.

while (WiFi.status() != WL_CONNECTED) {
   delay(200);
   Serial.print("...");
   }

A while loop is given to check whether the device is connected to the network or not. If it is not connected, there is a delay given and a series of dots are printed on the screen to indicate that the device is still connecting to the #network.

   Serial.println("");
   Serial.println("WiFi connected");  
   Serial.println("IP address: ");

Once the circuit/device is connected to the WiFi network, the same is displayed on the serial monitor along with the IP address.

 Serial.println(WiFi.localIP());
   ThingSpeak.begin(client);
  }

The local IP address is then printed and the transmitting of the received data begins on executing the "ThingSpeak.begin(client);" line of code. The setup( ) function ends here.

void loop() {  
   input_val = analogRead(ldrPin);    

The loop( ) function starts and the first action is to read the input from the LDR. The analogRead( ) function does the same.

Serial.print(input_val);

The input value is then printed on the serial monitor.

if (input_val<=500){
    digitalWrite(ledPin, HIGH);
    Serial.println("LED is ON");
  }

An 'if' condition is written to check the input value from the LDR. If the input value exceeds the given checkpoint value, the LED pin is set to HIGH value and the LED glows. The status of the LED is printed on the screen.

else {
  digitalWrite(ledPin, LOW);
  Serial.println("LED is OFF");
  }

If the condition is not true, the execution goes to the else part, where the LED pin value is set to LOW and the LED is OFF. The status is printed on the screen.

ThingSpeak.writeField(myChannelNumber, 1,input_val, myWriteAPIKey);
}

Finally, the input values, are sent to ThingSpeak #cloud to the corresponding channel number and API key, and hence the code ends.



COMPLETE CODE


#include <ThingSpeak.h>
#include <ESP8266WiFi.h>

const char* ssid     = "IAM IRONMAN";
const char* password = "12192302";
const int ldrPin = A0; // Defining LDR PIN 
int input_val = 0;  // Varible to store Input values
const int ledPin = 5;


WiFiClient  client;
long myChannelNumber = 1101154;
const char myWriteAPIKey[] = "KSP74PSLSVCW3C1L";

void setup() {
   Serial.begin(9600); 
   delay(100);
   pinMode(ledPin, OUTPUT);
   digitalWrite(ledPin, 0);
   pinMode(ldrPin, INPUT);
   
   Serial.println();
   Serial.println();
   Serial.print("Connecting to ");
   Serial.println(ssid);
  
   WiFi.begin(ssid, password);
  
   while (WiFi.status() != WL_CONNECTED) {
   delay(200);
   Serial.print("...");
   }
  
   Serial.println("");
   Serial.println("WiFi connected");  
   Serial.println("IP address: ");
   Serial.println(WiFi.localIP());
   ThingSpeak.begin(client);
  }


void loop() {  
   input_val = analogRead(ldrPin);      // Reading Input
   Serial.print(input_val);
   if (input_val<=500){
    digitalWrite(ledPin, HIGH);
    Serial.println("LED is ON");

  }
  else {
  digitalWrite(ledPin, LOW);
  Serial.println("LED is OFF");
  }
  ThingSpeak.writeField(myChannelNumber, 1,input_val, myWriteAPIKey);
}

code
.txt
Download TXT • 1KB

Note: Please change the file format to .ino(Arduino IDE) format before you upload the code to the ESP8266 board.



Working:


The basic principle of working of the sensor is the principle of Photoconductivity. It states that the conductivity of the LDR(photocell) varies according to the absorption of the light. Also, the resistors in this circuit are provided to protect the sensor from the high electricity flow.


Before interfacing the circuit to the code, the ThingSpeak account must be created, the channel must be set and the configurations must be properly assigned. The #API keys must be obtained after registering the type of device we will be interfacing and the same must be provided to the IDE as input so that the device can connect to the Cloud and further can access the output values from the sensor.


Circuit connected

The input for the circuit is given from the LDR sensor which detected the intensity of light. The value, as mentioned above is given to the NodeMCU in terms of voltage. This value of the voltage is printed as an output on the serial monitor along with the status of the LED. Based on the input level and the condition set by the user, the LED turns ON and OFF accordingly.


The steps for correctly creating an account on ThingSpeak is explained in the video.


Video By - Abhishek Goud

#Cloud storage and analysis of the input and output from devices are very essential for further development of the device. The proper implementation of the circuit increases the application level of the device.



SEE ALSO:

69 views
  • Facebook
  • YouTube
  • Instagram
  • LinkedIn
LearnElectronics
Contact Us
© Copyright - LearnElectronics 2021