@twofieros please check our draft documentation here and tell me if that helps you.
I got that link from you on Facebook. I gave it a go but Iām still stuck. It got me a little further reading the topic about virtual pins.
Iām getting compiling errors or param.asInt() not declared. I must not understand what is going on. perhaps a simple overview of what is happening would help or better sample code so I can see what is really happening.
I do not understand your problem. You have compilation error or your sketch doesnāt work?
Please start from empty simple sketch. Read carefully instruction in sketch. Try to run. Is it working?
This line is incorrect
Here is the correct usage:
BLYNK_WRITE(V1) // there is a widget in the app WRITING to V1. you can also use BLYNK_WRITE(1)
{
int virtualPinValue = param.asInt(); // here you assign Virtual Pin 1 value to a variable
if ( virtualPinValue == 1 ){ //here you check if the V1 value is 1 (HIGH)
do something;
} else {
do something else;
}
}
If you need to use virtual pin reading somewhere else, just define and use a global variable.
Pavel,
Thank you for the help. I got the code to compile. When I went to test I ran into wifi issues. For some reason my esp8266 board is booting everything else off my network and it wonāt connect. Iāll have to trouble shoot that before I can move on. Here is my final code:
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "34c*******************3";
const int LED_PIN = 2; // the pin that the pushbutton is attached to
const int SWITCH_PIN = 0; // the pin that the LED is attached to
int Blynk_Pin;
void setup()
{
Blynk.begin(auth, "P*****e", "t********s");
pinMode(SWITCH_PIN, INPUT);
// initialize the LED as an output:
pinMode(LED_PIN, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
// This function will be called every time
// when App writes value to Virtual Pin 1
BLYNK_WRITE(V1)
{
int virtualPinValue = param.asInt(); //Create virtualPinValue
if (virtualPinValue == 1) {
digitalWrite(Blynk_Pin, HIGH);
}else{
digitalWrite(Blynk_Pin, LOW);
}
BLYNK_LOG("Got a value: %s", param.asInt());
// You can also use: asInt() and asDouble()
}
void loop() {
Blynk.run();
//compare both states:
if (digitalRead(SWITCH_PIN) == Blynk_Pin) {
digitalWrite(LED_PIN, LOW);
Serial.println("LED off");
}
else
{
digitalWrite(LED_PIN, HIGH);
Serial.println("LED on");
}
}
I got the wifi issue resolved. Iām not getting the response Iām expecting from my board with my code. I think Iām not using the example code correctly. The code above is what Iām using.
Can I get an explanation of this line?
BLYNK_LOG("Got a value: %s", param.asStr());
// You can also use: asInt() and asDouble()
Iām guessing param.asStr() provides a value like an integer but I canāt call for the value like an integer. Is āGot a value: %sā something Iām supposed to rename and use somehow? My code complies but Iām not getting the responses Iām expecting. I think Iām using this line wrong. Thanks.
asStr should give you a hint to the data type - String, the āGot a Value: %Sā is the text that will be added to the Log file on the sever:
Something like (Assuming its timestamped):
[12/08/2015 8:55] Got a Value: Hello world
Not sure what you are trying to achieve, but in the example below on Virtual Pin 26 I am receiving a float value from a different device with a DHT22 connected, this is recieved by the param.asDouble() portion and stored in the Humidity var that is declared as a float. I then use this elsewhere to display it on an OLED screen.
BLYNK_WRITE(26) {
Humidity = param.asDouble();
}
Just to add a little more and happy for Dmitiry or Pavel to correct me if Iām wrong:
asStr = String
asInt = Integer
asDouble = Double precision floating point number
Do me a favor and run this on your board and in the application create a button assigned to V1 (You could eliminate almost everything in this code and just assign Digital Pin 2 to the button - but at least this gives you something to work from for other functions)
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "34c*******************3";
const int LED_PIN = 2; // the pin that the pushbutton is attached to
const int SWITCH_PIN = 0; // the pin that the LED is attached to
void setup()
{
Blynk.begin(auth, "P*****e", "t********s");
pinMode(SWITCH_PIN, INPUT);
// initialize the LED as an output:
pinMode(LED_PIN, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
// This function will be called every time
// when App writes value to Virtual Pin 1
BLYNK_WRITE(1)
{
int virtualPinValue = param.asInt(); //Read virtualPinValue from pin 1
if (virtualPinValue == 1) {
digitalWrite(LED_PIN, HIGH); //Please check your pins - its called LED but your comment states Switch
}else{
digitalWrite(LED_PIN, LOW);
}
}
void loop() {
Blynk.run();
}
Edit: Scrap that, it was right the first time, have not done it this way before to Digital pins.
Just for fun, try this, in the application, add a button and assign it Digital PIN 2:
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "34c*******************3";
const int LED_PIN = 2; // the pin that the pushbutton is attached to
const int SWITCH_PIN = 0; // the pin that the LED is attached to
void setup()
{
Blynk.begin(auth, "P*****e", "t********s");
pinMode(SWITCH_PIN, INPUT);
// initialize the LED as an output:
pinMode(LED_PIN, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
Blynk.run();
}
Iāve done that last one already. That is just the regular Blynk standalone esp8266 example. That works fine for me. I go the LED to blink from the app. The problem I have is figuring out how to use the data from the app. Iām trying to read the state the switch in the app is in and to compare that state to the physical switch on the outside of my lamp. I want either switch to turn the lamp on or off depending on the state of the other switch. So, if both switches are the same, lamp is off, if different, lamp on. Iām trying to do that with:
if (digitalRead (SWITCH_PIN) == Blynk_Pin) {
In Void setup I establish Blynk_Pin as virtual pin 1. The value of V1 should be HIGH or LOW. I donāt think Iām getting the understanding of how to read the values of the virtual pins. I hate going back to my parents and asking them because Iām a grown man now and long since moved out of the house but they do have 60+ years of programing between the two of them.
Bobbo, thank you for your help. I really appreciate it!
Pavel,
Iāve been reviewing Blynk with my parents again. We have pretty much had to reverse engineer the entire thing to figure out what is going on. Granted, itās been at least 15 years since they used C++ so some of it is still a mystery to us. What we cannot figure out is why your example seems to be a write to the virtual pin in order to call the value. I would think I would need a blynk_read in there to call for the value of the virtual pin. The other thing that has us concerned is not using the delay while not being booted from the blynk server from excess calls.
I started out writing code to do what I want with just physical switches. I got that done and it works. I then tried to add Blynk to that sketch and that is where everything went wrong. I am now trying to just write a sketch that will call a value of a virtual pin and use that to blink an LED. Basically, instead of creating a switch widget in the app that controls a physical pin on the ESP8266 I want the widget switch to write to a virtual pin and have my sketch read that virtual pin and cause the LED to blink accordingly. This is the basic read Iām trying to accomplish with my sketch but Iāve been unable to execute. The supporting documentation doesnāt explain that. I think itās a very basic thing that needs to be explained. Once the user can retrieve data from a virtual pin they can use that to do whatever they need. Can you give an example to do this? Hopefully, that will be my last question for a long time. Thanks!!!
Well, Iām a bit confused now. Getting a value from a Virtual Pin is super easy just by using the given example.
So letās repeat again step by step.
- Add button widget in the Blynk App. Choose V1 as an output. So now, when you click the button, it will WRITE
1 (HIGH)
or0 (LOW)
through the server to your ESP8266 - Now back to hardware part. I assume youāve managed establish connection with Blynk server. Right?
- This code should be used in order to obtain values coming to Virtual Pin V1:
//This constructions is used to get a value from a Widget in the app
//which WRITES values to Virtual Pin V1:
BLYNK_WRITE(V1)
{
int widgetValue = param.asInt()); // Here we create variable to store incoming values from V1.
if (widgetValue == 1) { //if Button Widget value is 1 (HIGH)...
digitalWrite(yourPin, HIGH); //switch your LED ON
} else { //otherwise..
digitalWrite(yourPin, LOW); //switch your LED OFF
}
}
Honestly, I donāt know how to explain it betterā¦ May be you should post your code and it will be more clear
This might be confusing, I see. But try to look at it differently:
BLYNK_WRITE(V1)
- is a declaration that there is a Widget in the app which WRITEs
to V1, and you are telling Blynk library that you are going to read values from V1
param.asInt());
- this construction is used to obtain parameters from the Widget which is WRITING
to V1
OK, I think I got it now. Iāll test it out tonight. Thank you again! When I have the completed code Iāll post it here for others to see.
OK, Itās still not working. Using serial monitor I can see Iām connecting to my network just fine. I get a scrolling āLED offā but there is no response from changing the widget. It shows connected on the app and the app seems to behave has it has when Iāve got it to work before. Here is my code. I hope you can see what Iām doing wrong.
All Iām trying to do is to get the LED to turn on by monitoring the virtual pin. I canāt help but think in void setup under BLYNK_WRITE() I should have BLYNK_WRITE or Blynk.virtualWrite instead of digitalWrite.
/**************************************************************
* Blynk is a platform with iOS and Android apps to control
* Arduino, Raspberry Pi and the likes over the Internet.
* You can easily build graphic interfaces for all your
* projects by simply dragging and dropping widgets.
*
* Downloads, docs, tutorials: http://www.blynk.cc
* Blynk community: http://community.blynk.cc
* Social networks: http://www.fb.com/blynkapp
* http://twitter.com/blynk_app
*
* Blynk library is licensed under MIT license
* This example code is in public domain.
*
**************************************************************
* This example runs directly on ESP8266 chip.
*
* You need to install this for ESP8266 development:
* https://github.com/esp8266/Arduino
*
* Change WiFi ssid, pass, and Blynk auth token to run :)
*
**************************************************************/
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "34c499cba**********3";
const int LED_PIN = 2;
int widgetState;
//#define BLYNK_PIN V1 // sets virtual pin used
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, "P****e", "t******s");
pinMode(LED_PIN, OUTPUT); //define LED pin as output
}
//This constructions is used to get a value from a Widget in the app
//which WRITES values to Virtual Pin BLYNK_PIN:
BLYNK_WRITE(V1)
{
int widgetState = param.asInt(); // Here we create variable to store incoming values from V1.
if (widgetState == 1) { //if Button Widget value is 1 (HIGH)...
digitalWrite(LED_PIN, HIGH); //switch your LED ON
} else { //otherwise..
digitalWrite(LED_PIN, LOW); //switch your LED OFF
}
}
void loop()
{
Blynk.run();
digitalWrite(LED_PIN, widgetState);
if (LED_PIN == HIGH) {
Serial.println("LED on");
}else{
Serial.println("LED off");
}
}
Can we please get an example code for reading the value of a virtual pin and using that to turn an LED on and off? I am still unable to get this simple task to even work. It would help if we could also have an example of BLYNK_PRINT so that I can see what the Blynk side is doing or not doing. The documentation doesnāt have enough information for me to get either of these to work.
Also, If I use BLYNK_LOG how to do I see what prints there?
you have defined this twice in your codeā¦ I donāt think this is something you meant toā¦
We tried to write a code for you, but even this isnāt workingā¦So we will learn how to make things Blynk step by step.
- Letās start without using Virtual Pins. Connect LED to Digital Pin on your ESP. Follow instructions in our ESP8266 example sketch and upload it to your board . Add Button widget in the app and attach it to the same Digital Pin. Press PLAY
Is it working? Does your LED react to the button in the app? Post your code here. Post your Serial Monitor output here as well.
After you accomplish this mission we will continue.
Pavel,
Yes, ESP8266 standalone sketch works just fine. I can blink an LED with it.
When I load up my code serial monitor says this:
[25897] Connecting to P****e
[30072] Connected to WiFi
[30072] Blynk v0.3.0
[30073] Connecting to cloud.blynk.cc:8442
LED off
LED off
LED off
LED off
LED off
LED off
LED off
LED off
Here is my code:
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "34c499****************************3";
const int LED_PIN = 2; //sets GPIO pin number
int widgetState; //int used to relay widget state
//#define BLYNK_PIN V1 // sets virtual pin used
//#define BLYNK_DEBUG // Optional, this enables lots of prints
//#define BLYNK_PRINT Serial
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, "P*******e", "t*********s");
pinMode(LED_PIN, OUTPUT); //define LED pin as output
}
//This constructions is used to get a value from a Widget in the app
//which WRITES values to Virtual Pin BLYNK_PIN:
BLYNK_WRITE(V1)
{
int widgetState = param.asInt(); // Here we create variable to store incoming values from V1.
if (widgetState == 1) { //if Button Widget value is 1 (HIGH)...
Blynk.virtualWrite(LED_PIN, HIGH);//switch your LED ON
} else { //otherwise..
Blynk.virtualWrite(LED_PIN, LOW); //switch your LED OFF
}
}
void loop()
{
Blynk.run();
//digitalWrite(LED_PIN, widgetState);
if (LED_PIN == HIGH) {
Serial.println("LED on");
}else{
Serial.println("LED off");
}
}