Running Arduino Uno + Wifi Shield here.
I have used Blynk for quite some time, no problems getting my hardware to connect and run multiple types of projects. But then there’s webhook …
Here is my simple sketch:
#define BLYNK_PRINT Serial
#define BLYNK_MAX_READBYTES 1024
#include <BlynkSimpleWifi.h>
BlynkTimer timer;
char auth[] = "myBlynkKey";
char ssid[] = "myNetwork";
char pass[] = "myNetworkKey";
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
timer.setInterval(10000L, getWeather);
}
void loop()
{
Blynk.run();
timer.run();
// Up to this point everything connects as expected and I can make multiple projects from here on that work well.
}
void getWeather()
{
Blynk.virtualWrite(V0,1);
Serial.println("TestPoint1"); // This prints OK every 10 seconds as expected
}
BLYNK_WRITE(V0)
{
Serial.println("TestPoint2"); // This does not print
String WebHook = param.asStr();
Serial.println(WebHook);
}
The Blynk documentation here says that “every time there is a “write” command to V0 pin (e.g. with Blynk.virtualWrite(V0, 1) from hardware or from app widget assigned to V0 ), BLYNK_WRITE(V0) construction will be triggered and processed.”
As you can see in the sketch, I am running a timer every 10 seconds to write the value 1 to port V0, but I do not experience the BLYNK_WRITE(V0) trigger the way I understood that Blynk describes it.
The function Serial.println(“TestPoint2”) never runs, which means the rest of it does not run either and everything I have configured on the webhook widget is irrelevant at this point.
Did I understand the instructions correctly?
Many thanks in advance!
@murilopicanco please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly. Triple backticks look like this:
```
I’m not sure where the Webhook widget comes in to this.
The behaviour you’re experiencing is intentional. It prevents your code going in to a continuous loop.
If you attached a widget (a switch for example) to V0 then changes to the switch in the app will trigger the BLYNK_WRITE(V0) callback function in your code.
A Blynk.VirtualWrite(V0) from your code won’t trigger the callback.
Maybe if you explained more about what it is that you’re trying to achieve then we’d be able to point you in the right direction.
The Webhook widget is connected to V0 on the the app side, and it contains a URL that I can successfully use on a browser and get a perfect response about the weather in my location.
The Blynk documentation states what I said about the use of virtualWrite and its relationship to BLYNK_WRITE, but I agree with you that it does not work hence I made this post.
In my code, instead of using a button that I press to send a value of 1 to the Webhook on V0, I built a timer that sends 1 to V0 every 10 seconds as you can see. But of course, since this does not trigger BLYNK_WRITE, even if the Webhook is sending the command to the URL correctly, I won’t be able to read the response.
Okay, now that you’ve explained m,ore about where the webhook widget come in to the equation you can ignore my previous answer.
The BLYNK_WRITE(V0) callback in your code won’t be triggered by a Blynlk.virtualWrite(V0) from your code, but it will be triggered by a response from the weather service API.
My guess is that the app setup is incorrect, so you aren’t getting a response from the server.
I assume that the ** characters at the beginning and end of this line, and other lines in your code, are because you didn’t format your code correctly first time around?
URL returns the expected values when I use a browser to test it
I do seem to be able to read the returned values and print them to serial as you see me trying in the code
I don’t understand how to trigger BLYNK_WRITE(V0) and cant find documentation that tells me to try anything else. All that blynk says is what I mentioned in my original post.
I believe my code is correct and this is the issue:
"#define BLYNK_MAX_READBYTES 1024. Where 1024` - is maximum allowed message size."
The response I am getting from my weather service is 1307 bytes, which exceeds the webhook limit. I tested the same code with another weather service that returns a smaller response and it works as expected.
This means that Blynk.virtualWrite(V0,1) does trigger the function BLYNK_WRITE(V0) as described in the Blynk documentation. And in my case, using a timer to run Blynk.virtualWrite(V0,1) every 10 seconds also works since it’s no different from pressing a button every 10 seconds for example.
No it does not. It seems that 1024 is a hard coded maximum limit defined by Blynk for the webhook. If the response exceeds it, nothing comes through regardless of your sketch setup.
I need to find a free API based weather forecast service that returns up to 1024 bytes, if I am to avoid bringing all the code to the board itself similar to what @Blynk_Coeur posted.
The whole point of the webhook is to move most of that code from your microcontroller to the Blynk servers and keep your sketch small, but with a 1024 bytes limit it rules out several potential use cases.
@murilopicanco Hi may i know how to solve this issue. My serial monitor shows buffer overflow after I include the BLYNK_MAX_READBYTE. My sketch no issue just the openweathermap info doesnt print out.
@Kyle_Gee please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```