Smart wifi Bulb

Annotation 2020-05-11 201614

Hi, Today we are going to build a system which can control bulb or another home appliance with our mobile. This system will control the bulb using wifi, thats why it is also called IoT system.
First we need to describe what target do we want  to achieve,

Goal:

We want to make a smart bulb that can automatically turn on and off during night and day respectively and can also detect motion can turn it on.
Along with all of that, we also want to control it using our smart phones, and also monitor the light intensity and relay state. We also want to set different mods in apps to suit our taste.
There will be four modes.

  1. OFF
    This mode will turn off the light.
  2. ON
    This mode will turn on the light.
  3. NIGHT SWITCH
    This mode will turn on the light only when it’s dark.
  4. MOTION
    This mode will turn on the light only when it’s dark and it detects motion.

So with our goal in mind lets make a list of components what we need,

Component list:

  1. Wemos D1 mini
  2. Relay
  3. PIR 
  4. LDR
  5. Project Box
  6. 5v adapter
  7. Jumpers wires and protoype board

Note : We will also need other things such as headers, solder and other tools.

Hardware:

Noe once we have all the component we need we will build our hardware.
For that first we have to solder and connect every thing together like shown in the circuit below on piece of prototype board.


After connecting everything close the circuit in project box.


Note: Make sure nothing is covering LDR and PIR sensors. Wemos is 3.3v device so you either have to use relay that works with 3.3v or else you have to use level shifte

Software:

Now the most interesting part starts, building app and programming wemos mcu.

App:

As we are smart so we are not going to build our app from scratch else we will blynk app tu build custom GUI and use it to control our IoT system.
the steps for app development are as follow ;

  1. Download and install blynk app from playstore.
  2. Make account and log into it. Make a new project, your project ID will sent to your e-mail. Save it as we will need it later.
  3. Make GUI of your aap using Drag & Drop feature of app.
  4. Give the proper name and set proper parameters of your added widgets.

It will look something like this;

Now lets move to arduino programming. 

Arduino:

Follow the following steps to code wemos di mini

Download, install and open arduino IDE on your computer.

Add esp8266 boards to your board managers, if not already added.

Copy paste the following code;

Code:

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = “gwmCOtJXzPe7oug76dMjSlBPE2J2hUoo”;

// Your WiFi credentials.
// Set password to “” for open networks.
char ssid[] = “Searching, Please wait…”;
char pass[] = “njmsa500”;

int pirSensor=D0;
int ldr=A0;
int relay=D5;
bool motion=false;
unsigned long light_intensity=0;
int pinValue =3;
bool state = LOW;
bool night = false;
BlynkTimer timer;

BLYNK_WRITE(V0) // V0 is the number of Virtual Pin
{
pinValue = param.asInt();
//Serial.println(pinValue);
}

void myTimerEvent()
{
// You can send any value at any time.
// Please don’t send more that 10 values per second.
if(Blynk.connected()==false){
pinValue =3;
}

motion = digitalRead(pirSensor);
light_intensity =0;
for(int i=0;i<1000;i++){
light_intensity = light_intensity + analogRead(ldr);
}
light_intensity = light_intensity/1000;
Serial.println(light_intensity);

if (light_intensity > 800){
night = true;
}
else if (light_intensity < 700){
night = false;
}

if(pinValue == 1){
state = HIGH;
}

else if(pinValue == 2){
if (night)
{
state = HIGH;
}
else{
state=LOW;

}
}

else if(pinValue == 3){
if (motion && night)
{
state = HIGH;
}
else{
state=LOW;
}
}

else if(pinValue == 4){
state = LOW;
}

else {
pinValue =3;
}

Blynk.virtualWrite(V1, light_intensity);

if(state == HIGH){
digitalWrite(relay,HIGH);
Blynk.virtualWrite(V2, “ON”);
}
else {
digitalWrite(relay,LOW);
Blynk.virtualWrite(V2, “OFF “);
}

}

void setup(){
// Debug console
Serial.begin(9600);
pinMode(pirSensor,INPUT);
pinMode(ldr,INPUT);
pinMode(relay,OUTPUT);

for(int i=0;i<100;i++){
light_intensity = light_intensity + analogRead(ldr);
}
light_intensity = light_intensity/100;
//Serial.println(light_intensity);
if(light_intensity > 800){
digitalWrite(relay,HIGH);
}

timer.setInterval(1000L, myTimerEvent);
WiFi.begin(ssid, pass); // Non-blocking if no WiFi available
Blynk.config(auth);
Blynk.connect();
// You can also specify server:
//Blynk.begin(auth, ssid, pass, “blynk-cloud.com”, 80);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
}

void loop(){
// Initiates BlynkTimer
Blynk.run();
timer.run();
}

change the wifi name, password and auth key with your’s.

Connect the wemos di mini to computer using usb cable, select port on arduino IDE and upload the code.

Finish:

Congratulations…! you have successfully finished the Iot project, now you can control your home appliance from any where in the world.
I hope you enjoyed along the way.
See you in an other post.
Have a good day… 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *