LAB 3



MUHAMMAD FAIRUZ BIN IMRAN (192020180)

DIPLOMA COMPUTER ENGINEERING (R2404)

NDJ 30003 - ELECTRONIC INSTRUMENTATION

LAB 3 - WIRELESS CONTROL AND LIGHT SENSOR



Blink Application Introduction


Blynk provides an interactive dashboard where users can build their own IoT projects by placing components. As a result, this will create a mobile app through which the user will be able to control microcontrollers connected via WiFi.

We will use the previous circuit configuration in this project to observe how Blynk interacts with those components. Watch Lab 2 to learn more about the components' functions.















How to Setup Blynk App

1) To begin, go to the Google Play Store and download the Blynk (legacy) app.

















2) You may now either register a new account or log in using your existing Facebook account.







3) To begin, go to the application's "New Project" button and create a new project.









4) Fill in the information correctly. Make sure you change the device to the ESP32 Dev Board. Then press the "Create" button.









5) It will now display a popup informing you that the Auth token has been sent to your email address.



Copy the authentication token into your inbox; it will come in handy later in the coding section.











6) The screen will now display a blank page or canvas. You may add some buttons and other fun objects by hitting the "+" symbol above.









7) Now, click the button icon, then you can configure the PIN in the output slot. Change the output pin to 16 and make sure it is a switch.











Connecting an ESP32 to a Blynk

1) We must first install the Blynk library before we can create the code to connect the Blynk. Go to your Arduino IDE to install it. Go to Sketch > Include Library > Manage Libraries at the top of the page. It should open a new window where you may type "Blynk" into the search field.







2) Now, paste the following code into your Arduino IDE.

#define BLYNK_PRINT Serial

#include <WiFi.h>

#include <WiFiClient.h>

#include <BlynkSimpleEsp32.h>



int led_gpio = 16;



#define Authorization_key "hXBwHPyX-kvDSQAxkEq_R_A21x******"

#define SSID "Write_your_SSID_here" // replace with your SSID

#define Password "********" //replace with your password



void setup() {

pinMode(led_gpio, OUTPUT);

Serial.begin(115200);

delay(10);

WiFi.begin(SSID, Password);

while (WiFi.status() != WL_CONNECTED) {

delay(500);

Serial.print(".");

}

Blynk.begin(Authorization_key,SSID,Password);

}

void loop(){

Blynk.run();

}



3) Did you recall receiving an Auth token in your email? Now, copy the Auth token and paste it into the following line:











4) Now, on the Blynk App, tap the "play" icon in the upper right corner.














5) When you click the button, the LED on pin 16 will light up.











Monitoring Temperature and Luminosity with Blynk App

Now we'll try to make a Blynk App that can monitor the temperature and brightness of the environment around us.

1) The Blynk App will have the following layout:











2) To have this layout, you can also use the scan tool on the top of the Blynk App to quickly duplicate the function from another:









3) To recreate the layout, scan the QR code below:











4) Now, paste the following code into your Arduino IDE.

/*************************************************************



This example shows how value can be pushed from Arduino to

the Blynk App.



NOTE:

BlynkTimer provides SimpleTimer functionality:

http://playground.arduino.cc/Code/SimpleTimer



App project setup:

Value Display widget attached to Virtual Pin V5

*************************************************************/



// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud

// See the Device Info tab, or Template settings

//#define BLYNK_TEMPLATE_ID "TMPLxxxxxx"

//#define BLYNK_DEVICE_NAME "Device"

//#define BLYNK_AUTH_TOKEN "YourAuthToken"





// Comment this out to disable prints and save space

#define BLYNK_PRINT Serial



#include <WiFi.h>

#include <WiFiClient.h>

#include <BlynkSimpleEsp32.h>



//Output Pin

#define LED_R 16

#define LED_Y 17

#define LED_G 5



//Input Pin

#define PB_Pin 4

#define VR_Pin 35

#define LDR_Pin 34

#define MCP_Pin 39

#define LM35_Pin 39



#define VIN 3.3 // V power voltage, 3.3v in case of NodeMCU

#define R 10000 // Voltage devider resistor value



// Your WiFi credentials.

// Set password to "" for open networks.

char ssid[] = "guest";

char pass[] = "***";

char auth[] = "b21adb19bbe145a2bbfe036c*****";



WidgetLED led1(V8);

BlynkTimer timer;



float get_VR(void)

{ float VR_Vout = (analogRead(VR_Pin)* VIN / float(4095));// Conversion analog to voltage

float VR = ((R * VR_Vout) / VIN); // Conversion voltage to resistance in Ohm

float VR_K = VR / 1000;

return VR_K;

}



int get_lux(void)

{ float LDR_Vout = (analogRead(LDR_Pin)* VIN / float(4095));// Conversion analog to voltage

float RLDR = (R * LDR_Vout)/(VIN - LDR_Vout); // Conversion voltage to resistance in Ohm

int lux = 170/(RLDR/1000); // Conversion resitance to lumen, RLDR/1000 - convert Ohm to KOhm

return lux;

}



float get_MCP9700(void)

{ float Temp_Vout = (analogRead(MCP_Pin) * VIN / float(4095)-0.5);// Conversion analog to voltage

float temp = Temp_Vout / 0.01; // Conversion resitance to lumen, RLDR/1000 - convert Ohm to KOhm

return temp;

}



float get_LM35(void)

{ float Temp_Vout = (analogRead(LM35_Pin) * VIN / float(4095));// Conversion analog to voltage

float temp = Temp_Vout / 0.01; // Conversion resitance to lumen, RLDR/1000 - convert Ohm to KOhm

return temp;

}



// This function sends Arduino's up time every second to Virtual Pin (5).

// In the app, Widget's reading frequency should be set to PUSH. This means

// that you define how often to send data to Blynk App.

void myTimerEvent()

{

int PB_state = !digitalRead(PB_Pin);

float VR_vlaue = get_VR();

int lux_value = get_lux();

float MCP_value = get_MCP9700();

//float LM35_value = get_LM35();



if(PB_state){led1.on();} else{led1.off();}

//Blynk.virtualWrite(V8, PB_state);

Blynk.virtualWrite(V4, VR_vlaue);

Blynk.virtualWrite(V5, lux_value);

Blynk.virtualWrite(V6, MCP_value);

//Blynk.virtualWrite(V7, LM35_value);



Serial.println("PB state: "+String(PB_state));

Serial.println(String(VR_vlaue)+" KOhm");

Serial.println(String(lux_value)+" LUX");

Serial.println(String(MCP_value)+" 'C");

//Serial.println(String(LM35_value)+" 'C");



Serial.println();

}



// This function will be called every time Slider Widget

// in Blynk app writes values to the Virtual Pin 1

BLYNK_WRITE(V0)

{

int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable

digitalWrite(LED_R, pinValue);

Serial.print("V0 LED_R value is: ");

Serial.println(pinValue);

}



BLYNK_WRITE(V1)

{

int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable

digitalWrite(LED_Y, pinValue);

Serial.print("V1 LED_Y value is: ");

Serial.println(pinValue);

}



BLYNK_WRITE(V2)

{

int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable

digitalWrite(LED_G, pinValue);

Serial.print("V2 LED_G value is: ");

Serial.println(pinValue);

}





void setup()

{

// Debug console

Serial.begin(115200);



pinMode(LED_R, OUTPUT);

pinMode(LED_Y, OUTPUT);

pinMode(LED_G, OUTPUT);

//pinMode(PB_Pin, INPUT_PULLUP);



Blynk.begin(auth, ssid, pass);

// Setup a function to be called every second

timer.setInterval(1000L, myTimerEvent);

}



void loop()

{

Blynk.run();

timer.run(); // Initiates BlynkTimer

}



5) Remember to copy the Auth Token for the Blynk Project and paste it in the code:

// Your WiFi credentials.

// Set password to "" for open networks.

char ssid[] = "Redmi Note 9S";

char pass[] = "23456789";

char auth[] = "I4_37A29SiitA377FO3jkkGu-lqLiiDx";




6) Upload the sketch to the ESP32 board.

7) When you press one of the buttons, the LED should come on in response. The temperature and luminosity will be updated in real-time on the Super Chart.

8) The serial monitor in the Arduino IDE can also display temperature and luminosity data from the surrounding environment.











9) View the real-time footage of this project.






Lamp Indicator for the Luminosity of Surrounding
    
To build a lamp indication, do the following:

When the LUX value is high, just the green LED will illuminate.

When the LUX value is medium, just the yellow LED will illuminate.

When the LUX value is low, just the red LED will illuminate.



1) Now, paste the following code into your Arduino IDE.

if(lux_value<=29){

digitalWrite(LED_G,LOW);

digitalWrite(LED_Y,LOW);

digitalWrite(LED_R,HIGH);

}else if(lux_value>29 && lux_value<=50){

digitalWrite(LED_G,LOW);

digitalWrite(LED_Y,HIGH);

digitalWrite(LED_R,LOW);

}else{

digitalWrite(LED_G,HIGH);

digitalWrite(LED_Y,LOW);

digitalWrite(LED_R,LOW);

}



2) Paste these codes inside the myTimerEvent function.

void myTimerEvent()

{

int PB_state = !digitalRead(PB_Pin);

float VR_vlaue = get_VR();

int lux_value = get_lux();

float MCP_value = get_MCP9700();

//float LM35_value = get_LM35();



if(PB_state){led1.on();} else{led1.off();}

//Blynk.virtualWrite(V8, PB_state);

Blynk.virtualWrite(V4, VR_vlaue);

Blynk.virtualWrite(V5, lux_value);

Blynk.virtualWrite(V6, MCP_value);

//Blynk.virtualWrite(V7, LM35_value);



Serial.println("PB state: "+String(PB_state));

Serial.println(String(VR_vlaue)+" KOhm");

Serial.println(String(lux_value)+" LUX");

Serial.println(String(MCP_value)+" 'C");

//Serial.println(String(LM35_value)+" 'C");



Serial.println();



if(lux_value<=29){

digitalWrite(LED_G,LOW);

digitalWrite(LED_Y,LOW);

digitalWrite(LED_R,HIGH);

}else if(lux_value>29 && lux_value<=50){

digitalWrite(LED_G,LOW);

digitalWrite(LED_Y,HIGH);

digitalWrite(LED_R,LOW);

}else{

digitalWrite(LED_G,HIGH);

digitalWrite(LED_Y,LOW);

digitalWrite(LED_R,LOW);

}











3) Upload the sketch to the ESP32 board.

4) You can adjust the lux value if you want to. When the sensor is situated in a darker area, the red LED should turn on. In normal conditions, the yellow LED should turn on, while when the surroundings are brighter, the green LED should turn on. The lux value could be a monitor on the Blynk app.

5) View the real-time footage for this project.













Comments