LAB 3
LAB 3: Blynk Application
In this laboratory session, we will learn how to use the Blynk application which enable inventor to to quickly build interfaces for controlling and monitoring their hardware projects from their iOS and Android device.
Setting up the Blynk
1. Search for Blynk on Google Play Store and install the application.
2. You will have to create an account to proceed further. You can also log in with your Facebook account if you possess that.
3. After you have successfully signed in the following window will appear. Click ‘New Project.’
4. Now specify the name of your project, device and connection type. After that press the ‘Create’ button.
5. You will receive a notification regarding your Authorization token. This can be accessed from the email account you signed in with and also through Project Settings.
6. Now the Blynk canvas will open as shown below:
7. Press on the screen. The widget box will appear. Now we will build our project. Firstly, we will press the ‘Button’ widget. You can view information regarding it by pressing the icon present in the far right as highlighted below.
8. Press the button widget again on the canvas to change its parameters. In the output section, choose the GPIO pin through which your LED will be connected to the ESP32 module. We have used GPIO25. You can use any other suitable output GPIO pin. Also, change the mode of the button to a ‘SWITCH’.
9. After you alter the settings of the button the canvas will look something like this:
10. At the top, you will find three icons. These are for Project settings, editing and play button.
Inside the play button, you can also find the stop button.
Task of the day:
Create simple application that integrate between sensors, input, output, and Blynk app.
1. Convert raw ADC value to LUX
2. Based on the LUX value:
a. If high – only on the green is on
b. If medium - only on the yellow is on
c. Else - only on the red is on
3. The LUX value and LEDs can be monitored at Blynk app
4. In Blynk app also has a button that can manually turn on/off LED on ESP32 (pin 2)
Circuit Diagram
Demonstration
LEDs color based on LUX value:
Manually turn on/off LED using Blynk's virtual push button:
Code
#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
char ssid[] = "";
char pass[] = "";
char auth[] = "";
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;
}
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(V6, 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();
}
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()
{
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);
timer.setInterval(1000L, myTimerEvent);
}
void loop()
{
Blynk.run();
timer.run(); // Initiates BlynkTimer
}
Comments
Post a Comment