Read data from the web with Webhook

Well, how far do you want to go? It may be enough to write a basic parser for “known” API’s or basic XML feeds.

I have very simple idea in case you need only 1 field. But I don’t know how to deal with many fields. For example in case you need only 1 field from above response you could specify in widget :

results.sunrise

and Blynk will return you only 1 string 5:43:55 AM

1 Like

It would be even more pretty if it returned a time variable … :wink:

But that would be a nice basic idea. It may be nice for the user if they can choose how to get the data back, e.g. int, string, time

@Dmitriy a return String as you have indicated would be pretty useless for Blynkers. Needs to reflect the type of variable e.g. time.

But this is just 1 field… You may need dozens.

Possibly yes, haha. Am I hurting your brain al ready too much? :wink:

1 Like

https://github.com/bblanchon/ArduinoJson this could be the answer …

2 Likes

@Lichtsignaal it’s an excellent library that I use on a regular basis and by far the most popular Json parser there is. I wouldn’t normally recommend “manual” parsing.

@naamah75 in case you missed latest update. Now this feature is implemented. However getting exactly what you need may be not so easy.

1 Like

I’ve been out for work! I will try it ASAP. :thumbsup:

1 Like

I made some test with webhook, in my project I have a webhook widget as follow…

… and this is my sketch …

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <WiFi.h>
#include <BlynkSimpleWifi.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "802d623481724fedaabf96aa31e3a174";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "FRITZ!Box Fon WLAN 7390";
char pass[] = "4506523768287213";
char server_ip[] = "192.168.1.110";
int port = 8442;

void setup() {
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass, server_ip, port);
}

void loop(){
  Blynk.run();
}

BLYNK_CONNECTED() {
  Serial.println("BLYNK_CONNECTED()");
  Blynk.virtualWrite(V0, "");
}

BLYNK_WRITE(V0) {   
  Serial.println("BLYNK_WRITE(V0)");
  String value = param.asStr(); 
  Serial.println(value);  
}

BLYNK_WRITE(V1) {
  Serial.println("BLYNK_WRITE(V1)");
  int value = param.asInt(); 
  Serial.println(value);
  if (value == 1) Blynk.virtualWrite(V0, "");
}

Unfortunately I’ve no data back from web… what’s wrong?

Please look in docs - http://docs.blynk.cc/#widgets-other-webhook it could be big content.

Also you need to send at least 1 char.

1 Like

YESSS!!! it works!
:hugging:

From UX point of view, I would like to be able to preview JSON in the app, choose the pairs that I would like to pass to the hardware.

Let’s say this is the JSON returned to V1:

{"results":{"sunrise":"5:43:55 AM","sunset":"6:55:05 PM","solar_noon":"12:19:30 PM","day_length":"13:11:10","civil_twilight_begin":"5:17:12 AM","civil_twilight_end":"7:21:47 PM","nautical_twilight_begin":"4:45:18 AM","nautical_twilight_end":"7:53:42 PM","astronomical_twilight_begin":"4:12:05 AM","astronomical_twilight_end":"8:26:55 PM"},"status":"OK"}

The interface can look like this quick draft:
Ideal interface should also parse substrings using (multiple) delimiters defined by user, for example
Space and “:” to simplify parsing further. (I’m not very good at JSONs, so it might be an unnecessary feature)

Then in the code:

hour = Blynk.parseJSON(V1, sunset[0]) // returns 6
min = Blynk.parseJSON(V1, sunset[1]) // returns 55
ampm = Blynk.parseJSON(V1, sunset[3]) // returns "PM"
3 Likes

It would be even more nice to assign a type to it. E.g. when I want a time, it could make me a time variable which I can use. In the above case it requires me to construct everything back to a time variable. It might not be needed for this kind of project, but when I’m working with multiple dates for scheduling, let’s say, parking time or walking my non-existing dog, it may come in handy to standardize everything to Unix time and have the user in the interface decide how to display it, e.g. AM, PM or 24-hour clock.

if i am using webhook to with this API: http://www.willyweather.com.au/api/docs/v2.html#forecasts-temperature

i just choose which bits of information i want?

or have i got this upside down? (I’m just checking before i get too deep into this!)

im going to try and write code so my house knows what the day might be like and configure the ventilation accordingly :slight_smile: (e.g. hot day predicted, get the cool air circulating early on, if cool day, no need to move any cool air…)

That should be basically it yes. You could write your own parser to proces the information, like I did, but the Blynk-guys obviously can improve on that and make life more easy for everyone. JSON is pretty much the standard.

cool cool! as long as i have the correct building blocks, i can usually get close (learn lots) and ask some dumb questions towards the end!

2 posts were split to a new topic: Requesting assistance with Webhook