Mission not accomplished yet You’ve posted code with lots of modifications. Attention to details is one of the things we are going to achieve with this “interactive tutorial”
I also don’t see the status in the Serial Monitor, saying: Connected to Blynk
or something similar
/**************************************************************
* 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[] = "34c49****************3";
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, "P*****e", "t********s");
}
void loop()
{
Blynk.run();
}
OK, connected board. flashed LED with the above code. No serial monitor enabled on example code so I cannot send what it says.
These lines should get Serial Monitor printing the connection status. Could you please double-check?
It was printing something using your previous code. So it should work the same
Great! So Blynk IS working.
Now you can blink LED connected to D2 (I assume) with the button in the app attached to D2.
Next: Mission 2: Control LED, connected to pin D2 on your ESP8266 using Button Widget attached to Virtual Pin V1. But before that, we need to understand how Virtual Pins work. If they do work…
Use your code as a foundation. Look at the GetData example. You’ll see that this code is very similar to yours.
Main difference is that it was written for a different connection type (Ethernet Shield)
Secondly, you’ll see this construction:
BLYNK_WRITE(V1)
{
BLYNK_LOG("Got a value: %s", param.asStr());
// You can also use: asInt() and asDouble()
}
Since Button widget is sending HIGH(1) or LOW(0), which is neither a String nor Double, you should use: param.asInt() Correct?
So your code should look like this:
BLYNK_WRITE(V1)
{
BLYNK_LOG("Got a value: %s", param.asInt());
}
Try incorporate this construction in your code and:
Post your code
Try pressing the button in the app
Post your Serial Monitor
Notice, that we are still using only examples, not your initial code.
This is where things fall apart. However, adding that BLYNK_LOG seemed to make a difference. First, here is the 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[] = "34c499cba10046a38045279e5ba3c973";
//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, "Paulie", "twofieros");
pinMode(2, 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)
{
BLYNK_LOG("Got a value: %s", param.asInt());
// You can also use: asInt() and asDouble()
//int widgetState = param.asInt(); // Here we create variable to store incoming values from V1.
if (param.asInt() == 1) { //if Button Widget value is 1 (HIGH)...
Blynk.virtualWrite(2, HIGH);//switch your LED ON
} else { //otherwise..
Blynk.virtualWrite(2, LOW); //switch your LED OFF
}
}
void loop()
{
Blynk.run();
//digitalWrite(LED_PIN, widgetState);
if (2 == HIGH) {
Serial.println("LED on");
}else{
Serial.println("LED off");
}
}
Now the serial monitor.
[29071] Connecting to Paulie
[33093] Connected to WiFi
[33094] Blynk v0.3.0
[33094] Connecting to cloud.blynk.cc:8442
LED off
LED off
LED off
LED off way down the line
LED off
[38142] Connecting to cloud.blynk.cc:8442
LED off
[38231] Ready (ping: 0ms).
LED off then further down the line
LED off
[40942] Got a value:
LED off
LED off as soon as I changed the widget state on the app it seemed to crash
LED off
[42042]
Exception (28):
epc1=0x4000bf64 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000001 depc=0x00000000
This is progress actually, before I was never able to get a change in the serial monitor with the app.
And now I just saw your notice, that we are still using only examples, not your initial code let me go back and try this again. I’ll still post this in case it shows something for you or anyone else.
#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";
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, "********", "*********");
}
BLYNK_WRITE(V1)
{
BLYNK_LOG("Got a value: %s", param.asInt());
// You can also use: asInt() and asDouble()
//int widgetState = param.asInt(); // Here we create variable to store incoming values from V1.
if (param.asInt() == 1){
Blynk.virtualWrite(2, HIGH);
}else{
Blynk.virtualWrite(2, LOW);
}
}
void loop()
{
Blynk.run();
}
here is serial monitor. It connects but then gibberish and the board locks up, drawing excess power. I didn’t put serial prints in to keep it simple for now. I’m not even sure I would get the Blynk prints right anyway.
I get that param is where the value is stored as a string. What confuses me is all the stuff in void setup which runs once. I’m imagining that what I setup in void setup is sent to Blynk server for processing. I just keep wanting to put all the function in void loop but the documentation says not to use any Blynk. command in void loop or I will get booted from the Blynk server. Do I have that logic correct? Can I call param in void loop and get the correct value? My guess is no and that is stored only on the server and out of reach from the programing on the local board.
OK, I’ve been on the phone with my parents sorting this out. I have the BLYNK_LOG working now. There was an issue with the suggested code. You had suggested:
BLYNK_LOG(“Got a value: %s”, param.asInt());
That doesn’t work. It only worked with param.asStr(). I had to change %s to %i for it to work. The correct code is below. This returns either 0 or 1. Perhaps the supporting documentation needs to explain that change. It’s apparently common in C++ but I was not aware of what was going on or why the change.
#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";
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, "Paulie", "t*********s");
}
BLYNK_WRITE(V1)
{
BLYNK_LOG("Got a value: %i", param.asInt());
}
void loop()
{
Blynk.run();
}
I’m now going to try to write param to a pin. I hope I have this finally figured out.
Sorry, I missed it, my bad. Glad you found it out.
Now, since you finally got the output from Virtual Pin, you can start using it for your needs. Change this part of the code:
BLYNK_WRITE(V1)
{
BLYNK_LOG("Got a value: %i", param.asInt());
}
to this (don’t change anything else):
BLYNK_WRITE(V1)
{
int pinValue = param.asInt(); //1 or 0 will be written to this variable
if (pinValue == 1){ // If Button Widget is pressed
digitalWrite(2, HIGH);
} else {
digitalWrite(2, LOW);
}
}
You might also need to set of your pin D2 pinMode to OUTPUT in void setup()
If you are talking about BLYNK_WRITE() – it’s not part of the void setup(). It’s a different entity. You can place it after void loop() if you want
This is very correct. Avoid putting stuff to void loop()
OK, I got it working. Boy, did it take baby steps. It sure is cool to see it working as planned. Thank you Pavel for your patience and guidance.
For anyone else who was following this thread or who wants to make what I did. Here is my final code.
//This program is a three-way switch for Blynk and a real switch.
//This was written for ESP8266. tested and confirmed to work.
#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[] = "YourAuthToken";
int button; //global value for blynk widget
void setup()
{
Serial.begin(9600);
pinMode (2, OUTPUT); //enable pin for lamp
pinMode (0, INPUT); //enable pin for switch
Blynk.begin(auth, "ssid", "password");
}
BLYNK_WRITE(V1)
{
button = param.asInt();
BLYNK_LOG("Got a value: %i", param.asInt());
if (button == 1)
{
BLYNK_LOG("HIGH");
}else{
BLYNK_LOG("LOW");
}
}
void loop()
{
Blynk.run();
if (button == digitalRead(0))
{
digitalWrite(2, HIGH);
Serial.println("lamp on");
}else{
digitalWrite(2, LOW);
Serial.println("lamp off");
}
}
I’m now going to construct the final circuit and install it into my 3d printed lamp. I learned a lot from this project. I hope to make many more using Blynk. I’m one happy kickstarter backer!
Hi Twofieros,
Thank you for your Code and I was also looking for a similar solution. I tried yours and it almost works. Now I can control the ESP’s led both from app and from the Switch connected with ESP
But I want to see the status of the ESP switch using the apps Display icon LED. i.e., The LED icon has to be ON or OFF reflecting Blynk_LOG’s Low and High status.
Please suggest the changes.
Thank You and Regards.
I plan to add that also. I’m waiting on some more parts to show up for my circuit. I’ll see if I can find some time soon to get that additional feature working soon. I’ll post the code here when I do. I’d love it if you could show what you made with it. I will show my final project when it’s all done.
I wrote the code to include an LED widget. It was pretty simple to add. It uses Virtual Pin 2 to save the status and push it to the dashboard on the phone. Here is the code:
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
//setup instructions: This runs on ESP8266-01. GPIO0 is mechanical switch. GPIO2 is output relay.
//Blynk setup: Make a button widget for Virtual Pin 1. Make an LED setting widget for virtual pin 2.
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "34c49*************************3";
int button; //global value for blynk widget
void setup()
{
Serial.begin(9600);
pinMode (2, OUTPUT); //enable pin for lamp
pinMode (0, INPUT); //enable pin for switch
Blynk.begin(auth, "P******e", "********");
}
BLYNK_WRITE(V1)
{
button = param.asInt(); //assign button to widget value
BLYNK_LOG("Got a value: %i", param.asInt());
if (button == 1)
{
// digitalWrite(2, HIGH);
BLYNK_LOG("HIGH");
}else{
// digitalWrite(2, LOW);
BLYNK_LOG("LOW");
}
}
void loop()
{
Blynk.run();
int lamp = 2;
int relay = 2; //pin number for output relay
if (button == digitalRead(0))
{
digitalWrite(lamp, HIGH);
Blynk.virtualWrite(V2, 1); //send value to virtual pin 2
Serial.println("lamp on");
}else{
digitalWrite(lamp, LOW);
Blynk.virtualWrite(V2, 0); //send value to virtual pin 2
Serial.println("lamp off");
}
}
Hello, I’m trying to follow this example to turn a relay on and off with an Uno + Ethernet Shield. I’ve tried quite a few variations, and I have inserted print statements so I see it’s going all the way through the code. But I can’t get reliable signals from Blynk to my relay to turn it on and off. It works - both ON and OFF work. But not every time. Maybe half the time, there is no response. Can you help?
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
int digitalPin=9;
int value;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxx";
void setup()
{
Serial.begin(9600);
pinMode(digitalPin, OUTPUT);
Blynk.begin(auth);
}
BLYNK_WRITE(V1){
value=param.asInt();
BLYNK_LOG("Got a value: %i", param.asInt());
if (value==1){
BLYNK_LOG("HIGH");
} else {
BLYNK_LOG("LOW");
}
}
void loop() {
Blynk.run();
if (value == 1)
{
digitalWrite(digitalPin, HIGH);
Serial.println("relay on");
} else{
digitalWrite(digitalPin, LOW);
Serial.println("relay off");
}
}