0% found this document useful (0 votes)
62 views16 pages

Blynkexamples

The document contains examples of Arduino code using Blynk and other libraries to control devices over WiFi. Example 1 shows a switch controlling an LED. Example 2 shows reading an analog sensor and controlling brightness of an LED. Example 3 shows reading temperature from a sensor. Other examples show using ultrasonic sensors, servos, and preferences to save state.

Uploaded by

UYeMin Htike
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views16 pages

Blynkexamples

The document contains examples of Arduino code using Blynk and other libraries to control devices over WiFi. Example 1 shows a switch controlling an LED. Example 2 shows reading an analog sensor and controlling brightness of an LED. Example 3 shows reading temperature from a sensor. Other examples show using ultrasonic sensors, servos, and preferences to save state.

Uploaded by

UYeMin Htike
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Example-1(Switch)

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

/* Fill-in information from Blynk Device Info here */


#define BLYNK_TEMPLATE_ID "TMPL6rgtkTfCz"
#define BLYNK_TEMPLATE_NAME "test"
#define BLYNK_AUTH_TOKEN "5na1A6kh1B-3UE3HctFcvaXG2vN6MNEp"

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

// Your WiFi credentials.


// Set password to "" for open networks.
char ssid[] = "PT-GTHS WS2C";
char pass[] = "GTHS@PT!23";

// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V0)
{
// Set incoming value from pin V0 to a variable
int value = param.asInt();
digitalWrite(2,value);

}
void setup()
{ Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// You can also specify server:
//Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 80);
//Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, IPAddress(192,168,1,100), 8080);
pinMode(2,OUTPUT);
}
void loop()
{
Blynk.run();
}
Example-2(Analog Read and Write)
/*************************************************************

/* Fill-in information from Blynk Device Info here */


#define BLYNK_TEMPLATE_ID "TMPL6oeLt4uO5"
#define BLYNK_TEMPLATE_NAME "Analogue"
#define BLYNK_AUTH_TOKEN "aZUOlHSQASa4glCBlcuOb_degaOlDHyD"

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "MYTEL-c9gJ";
char pass[] = "m4Xj7Z7P";
// Declaring a global variable for sensor data
int sensorVal, writevalue;;
const byte led_gpio = 2; // the PWM pin the LED is attached to
float Percentage;
void setup()
{ Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// You can also specify server:
//Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 80);
//Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, IPAddress(192,168,1,100), 8080);

ledcAttachPin(led_gpio, 0); // assign a led pins to a channel


// Initialize channels
// channels 0-15, resolution 1-16 bits, freq limits depend on resolution
// ledcSetup(uint8_t channel, uint32_t freq, uint8_t resolution_bits);
ledcSetup(0, 5000, 8); // 12 kHz PWM, 8-bit resolution

}
void loop()
{
// Reading sensor from hardware analog pin A0
sensorVal = analogRead(34);
Percentage = (float)sensorVal/4095*100;
writevalue = map(sensorVal,0,4095,0,255);
Blynk.virtualWrite(V1, Percentage);
ledcWrite(0, writevalue);
Blynk.virtualWrite(V0, sensorVal);
// Runs all Blynk stuff
Blynk.run();
// runs BlynkTimer
delay(30);
}
Example-3(Temperature Reading)
/*************************************************************

/* Fill-in information from Blynk Device Info here */


#define BLYNK_TEMPLATE_ID "TMPL6cCDLylFo"
#define BLYNK_TEMPLATE_NAME "Temperature Sensor with switch"
#define BLYNK_AUTH_TOKEN "0eVmRdzwMhi5M4UShaWli31CmGUYHc9u"

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

// Your WiFi credentials.


// Set password to "" for open networks.
char ssid[] = "PT-GTHS WS2C";
char pass[] = "GTHS@PT!23";

// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V0)
{
// Set incoming value from pin V0 to a variable
int value = param.asInt();
digitalWrite(2,value);

}
// Declaring a global variable for sensor data
int sensorVal;
float mV;
float Temp;

BlynkTimer timer;
void myTimer()
{
// This function describes what will happen with each timer tick
// e.g. writing sensor value to datastream V5
Blynk.virtualWrite(V3, Temp);
}
void setup()
{ Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// You can also specify server:
//Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 80);
//Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, IPAddress(192,168,1,100), 8080);

// Setting interval to send data to Blynk Cloud to 1000ms.


// It means that data will be sent every second
timer.setInterval(1000L, myTimer);
pinMode(2,OUTPUT);

void loop()
{
// Reading sensor from hardware analog pin A0
sensorVal = analogRead(34);
mV=(float)sensorVal*3300/4095;
Temp=mV/10;
// Runs all Blynk stuff
Blynk.run();
// runs BlynkTimer
timer.run();

Arduino TimmerInterrupt
https://deepbluembedded.com/arduino-timer-interrupts/
Example-4(Distance/WaterHeight)
/*************************************************************
/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID "TMPL6dVBRi34F"
#define BLYNK_TEMPLATE_NAME "Distance"
#define BLYNK_AUTH_TOKEN "HhBAhUidUgJCVsnHrp7NcTaiv8OaAzSQ"

const int Echo=2;


const int Trigger=4;

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

// Your WiFi credentials.


// Set password to "" for open networks.
char ssid[] = "PT-GTHS WS2C";
char pass[] = "GTHS@PT!23";
// Declaring a global variable for sensor data
long Time;
double WaterHeight;
double distance;
double Percentage;
void setup()
{ Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
pinMode(Echo,INPUT);
pinMode(Trigger,OUTPUT);
}
void loop()
{
digitalWrite(Trigger,LOW);
delayMicroseconds(2);
digitalWrite(Trigger,HIGH);
delayMicroseconds(10);
digitalWrite(Trigger,LOW);
Time=pulseIn(Echo,HIGH);
distance=(double)Time*0.0343/2;
WaterHeight=45.00-distance;
Percentage=map(WaterHeight,0,40,0,100);
// Runs all Blynk stuff
Blynk.run();
Blynk.virtualWrite(V0, distance);
Blynk.virtualWrite(V1, Percentage);
Blynk.virtualWrite(V2, WaterHeight);
}
Example-5(Prefences)
/*************************************************************

#include <Preferences.h>
Preferences prefs;
int counter;
int flag=1;

void setup()
{ Serial.begin(115200);
prefs.begin("my-app");
counter = prefs.getInt("counter", 1); // default to 1
counter++;
prefs.putInt("counter", counter);
}

void loop()
{ delay(2000);
while(flag==1)
{Serial.print("Reboot count: ");
Serial.println(counter-1);
flag=0;
}
delay(2000);}

The data saved using preferences is structured like this:


namespace {
key:value
}
You can save different keys on the same namespace, for example:
namespace {
key1: value1
key2: value2
}

Example-6(Clean NVS-nonvola�le memory)


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

#include <nvs_flash.h>

void setup()
{
nvs_flash_erase(); // erase the NVS partition and...
nvs_flash_init(); // initialize the NVS partition.
while(true);
}

void loop() {}

Example-7(Auto/Manual Switch with Prefences)


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

/* Fill-in your Template ID Device Name and Auth Token */


#define BLYNK_TEMPLATE_ID "TMPL6UyUbuWRC"
#define BLYNK_TEMPLATE_NAME "LED"
#define BLYNK_AUTH_TOKEN "MJqzL0SOTmOBUJDalot4H48Ed0wLSa6V"
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "PT-GTHS WS2C";
char pass[] = "GTHS@PT!23";

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <Preferences.h>

Preferences pref;

#define LED 2 //D2


#define SwitchPin 13 //D13
#define wifiLed 4 //D4
#define VPIN_BUTTON V0

bool toggleState = LOW; //Define integer to remember the toggle state for
led

BlynkTimer timer;

BLYNK_WRITE(VPIN_BUTTON) {
toggleState = param.asInt();
digitalWrite(LED, toggleState);
pref.putBool("LED", toggleState);
}

void checkBlynkStatus() { // called every 2 seconds by SimpleTimer


bool isconnected = Blynk.connected();
if (isconnected == false) {
digitalWrite(wifiLed, LOW);
}
if (isconnected == true) {
digitalWrite(wifiLed, HIGH);
}
}
void manual_control()
{
if (digitalRead(SwitchPin) == LOW && toggleState==LOW)
{delay(200);
digitalWrite(LED,HIGH);
toggleState=HIGH;
pref.putBool("LED",toggleState);
Blynk.virtualWrite(VPIN_BUTTON, toggleState);
}
if(digitalRead(SwitchPin) == LOW && toggleState==HIGH)
{delay(200);
digitalWrite(LED,LOW);
toggleState=LOW;
pref.putBool("LED",toggleState);
Blynk.virtualWrite(VPIN_BUTTON, toggleState);
}
}

void getLedState()
{ toggleState = pref.getBool("LED");
digitalWrite(LED, toggleState);
Blynk.virtualWrite(VPIN_BUTTON, toggleState);
delay(200);
}

void setup()
{
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
//Open namespace in read-write mode
pref.begin("LED State", false);
pinMode(LED, OUTPUT);
pinMode(wifiLed, OUTPUT);
pinMode(SwitchPin, INPUT_PULLUP);
//During Starting all Relays should TURN OFF
digitalWrite(LED, toggleState);
digitalWrite(wifiLed, LOW);
timer.setInterval(2000L, checkBlynkStatus); // check if Blynk server is
connected every 2 seconds
delay(1000);
getLedState(); //fetch data from NVS Flash Memory
delay(1000);
}

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

Example-8(Servomotor)
/*************************************************************
#include <ESP32Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos=0, Angle;


void setup() {
myservo.attach(13); // attaches the servo on pin 13 to the servo object
}

void loop() {
pos=analogRead(34);
Angle=map(pos,0,4095,0,180);
myservo.write(Angle);
delay(100);
}

Example-9(Servomotor door)
/*************************************************************

#include <ESP32Servo.h>
#include <Preferences.h>

Preferences pref;
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
const int IR = 18;
int Angle, Pos=0;
bool flag;
void door()
{ pref.putBool("Open",HIGH);
for(Angle=Pos; Angle<=90; Angle+=1)
{myservo.write(Angle);
pref.putInt("position",Angle);
delay(35);}
delay(200);
pref.putBool("Open",LOW);
for(Angle=90;Angle>=0;Angle-=1)
{myservo.write(Angle);
delay(35);Pos=Angle;
pref.putInt("position",Angle);
if(digitalRead(IR)==LOW) {delay(100); door();}
}
Pos=0;
pref.putInt("position",0);
}

void InitialState()
{ Pos = pref.getInt("position");
flag = pref.getBool("Open");
if(flag==HIGH)
door();
else
{pref.putBool("Open",LOW);
for(Angle=Pos;Angle>=0;Angle-=1)
{myservo.write(Angle);delay(20);Pos=Angle; pref.putInt("position",Angle);
if(digitalRead(IR)==LOW) {delay(200); door();}}
Pos=0;
pref.putInt("position",0);
}
}

void setup() {
myservo.attach(13); // attaches the servo on pin 13 to the servo object
pinMode(IR, INPUT);
//Open namespace in read-write mode
pref.begin("mydoor",false);
InitialState();
}

void loop() {
if(digitalRead(IR)==LOW) door();
}

Example-10(DC motor forward/reverse)


/*************************************************************
int forward_flag=0,backward_flag=0,flag=0;
const int forward = 12;
const int backward = 14;
const int forward_Switch = 4;
const int backward_Switch = 5;
const int stop_Switch = 21;
void setup() {
pinMode(forward,OUTPUT);
pinMode(backward,OUTPUT);
pinMode(forward_Switch,INPUT_PULLUP);
pinMode(backward_Switch,INPUT_PULLUP);
pinMode(stop_Switch,INPUT_PULLDOWN);
digitalWrite(13,LOW);
digitalWrite(14,LOW);
}

void loop() {
if(digitalRead(forward_Switch)==LOW)
forward_flag=1;
if(forward_flag==1 && backward_flag==0)
{digitalWrite(forward,HIGH);digitalWrite(backward,LOW);}
if(digitalRead(backward_Switch)==LOW)
backward_flag=1;
if(backward_flag==1 && forward_flag==0)
{digitalWrite(forward,LOW);digitalWrite(backward,HIGH);}
if(digitalRead(stop_Switch)==HIGH) flag=1;
if(flag==1)
{digitalWrite(forward,LOW);
digitalWrite(backward,LOW);
flag=0;
forward_flag=0;
backward_flag=0;
}}

Example-11(DC motor direc�on and speed)


/*************************************************************
int Pot=0,forward=0,backward=0,flag=0;
const int frd = 4;
const int bkd = 5;
const int stop =21;
void setup()
{ pinMode(13,OUTPUT);
ledcAttachPin(14,0);
ledcSetup(0,5000,12);
pinMode(frd,INPUT);
pinMode(bkd,INPUT);
pinMode(stop,INPUT);
digitalWrite(13,LOW);
ledcWrite(0,0);
delay(500);}
void loop()
{Pot=analogRead(34);
if(digitalRead(frd)==HIGH && backward==0) forward=1;
if(forward==1)
{digitalWrite(13,LOW);
ledcWrite(0,Pot);}
if(digitalRead(bkd)==HIGH && forward==0) backward=1;
if(backward==1)
{digitalWrite(13,HIGH);
ledcWrite(0,Pot);}
if(digitalRead(21)==HIGH) flag=1;
if(flag==1)
{digitalWrite(13,LOW);
ledcWrite(0,0); flag=0;backward=0;forward=0;}}

Example-12(DC motor door)


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

#include<Preferences.h>
Preferences pref;
int forward_flag=0,backward_flag=0;
const int PIR=18;
const int forward=12;
const int backward=14;
const int LS_1=4;
const int LS_2=5;
int flag=0;

void door()
{ pref.putInt("Open",1);
forward_flag=1;
while(forward_flag==1 )
{digitalWrite(forward,LOW);
ledcWrite(0,4095/4);
if(digitalRead(LS_2)==LOW)
{forward_flag=0;
digitalWrite(forward,LOW);
ledcWrite(0,0);}
}
delay(500);
pref.putInt("Open",2);
backward_flag=1;
while(backward_flag==1)
{digitalWrite(forward,HIGH);
ledcWrite(0,4095*3/4);
if (digitalRead(PIR)==HIGH)
{backward_flag=0;delay(500); door();}
if (digitalRead(LS_1)==LOW)
{backward_flag=0;
digitalWrite(forward,LOW);
ledcWrite(0,0);}
}
pref.putInt("Open",3);
}
void InitialState()
{
flag=pref.getInt("Open");
if(flag==1)
door();
else if (flag==2)
{pref.putInt("Open",2);
backward_flag=1;
while(backward_flag==1)
{digitalWrite(forward,HIGH);
ledcWrite(0,4095*3/4);
if(digitalRead(PIR)==HIGH)
{backward_flag=0;delay(500); door();}
if(digitalRead(LS_1)==LOW)
{backward_flag=0;
digitalWrite(forward,LOW);
ledcWrite(0,0);}
}
pref.putInt("Open",3);
}
else if(flag==3) {}
}

void setup()
{
delay(200);
pinMode(PIR,INPUT);
pinMode(forward,OUTPUT);
ledcAttachPin(backward,0);
ledcSetup(0,5000,12);
ledcWrite(0,0);
pinMode(LS_1,INPUT_PULLUP);
pinMode(LS_2,INPUT_PULLUP);
digitalWrite(forward,LOW);
pref.begin("mydoor,false");
InitialState();
}
void loop()
{
if(digitalRead(PIR)==HIGH)
door();
}

Example-13(ACS712 DC Current Measurement)


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

float Estimate=0.00, offset = 0.00,mV=0.00, Mean=0.00, Current=0.00;


void get_offset()
{ for(int i=0; i<100; i++)
{
float Reading=0.00, Samples=0.00, Average=0.00;
for(int x=0;x<150; x++)
{
Reading=analogRead(34);
Samples+=Reading;
delayMicroseconds(10);
}
Average = Samples/150;
Estimate+=Average;
}
Mean = Estimate/100;
offset = Mean;
}

void setup() {
Serial.begin(115200); //Start Serial Monitor to display current read value
on Serial monitor
pinMode(2,OUTPUT);
delay(200);
get_offset();
}

void loop()
{for(int i=0; i<100; i++)
{
float Reading=0.00, Samples=0.00, Average=0.00;
for(int x=0;x<150; x++)
{
Reading=analogRead(34);
Samples+=Reading;
delayMicroseconds(10);
}
Average = Samples/150;
Estimate+=Average;
}
Mean = Estimate/100;
Estimate = 0.00;
Mean = Mean - offset;
mV = map(Mean,0,4095,0,3300);
Current = mV/100;
Serial.print(Current); Serial.println("\t Amp");
}

Example-14(ACS712 AC Current Measurement)


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

#include<stdlib.h>
const int sensorIn = 34;
int mVperAmp = 100;
double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;
double offset = 0;

void get_offset()
{while(1)
{Voltage = getVPP();
VRMS = (Voltage/2.0) *0.707; //root 2 is 0.707
AmpsRMS = (VRMS/mVperAmp) - offset;
Serial.println(AmpsRMS);Serial.println(offset);Serial.println("***********
**********");
if(abs(AmpsRMS)<0.005) break;
else if(AmpsRMS>0.00) offset = offset + AmpsRMS;
else if(AmpsRMS<0.00) offset = offset - abs(AmpsRMS);
}
}

void setup() {
Serial.begin (115200);
delay(1000);
get_offset();
}

void loop() {
Voltage = getVPP();
VRMS = (Voltage/2.0) *0.707;
AmpsRMS = (VRMS/mVperAmp)-offset;
Serial.print (AmpsRMS);
Serial.println ("\t Amps ");
delay (100);
}

// ***** function calls ******


float getVPP()
{
float result;
int readValue; // value read from the sensor
int maxValue = 0; // store max value here
int minValue = 4095; // store min value here ESP32 ADC resolution

uint32_t start_time = millis();


while((millis()-start_time) < 1000) //sample for 1 Sec
{
readValue = analogRead(sensorIn);
// see if you have a new maxValue
if (readValue > maxValue)
{
/*record the maximum sensor value*/
maxValue = readValue;
}
if (readValue < minValue)
{
/*record the minimum sensor value*/
minValue = readValue;
}
}

// Subtract min from max


result = ((maxValue - minValue) * 3300)/4095.0; //ESP32 ADC resolution
4096

return result;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy