Read data from the web with Webhook

Yr welcome. It all depends on what you want to achieve of course. I’ve noticed the calculations with time are easier with UNIX time, but if your input is limited, you could do without :slight_smile:

I figured Unix time was a pain, but it is really easy with the Time library if you take a little time (pun intended) to study it :wink:

@Costas @Lichtsignaal this request -

http://api.sunrise-sunset.org/json?lat=36.7201600&lng=-4.4203400&date=2016-08-25

returns

{"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"}

Do you use all this fields or just 1 specific? Like sunrise?

@Dmitriy for me it would just be sunrise and sunset at the moment as any “twilight” adjustments could be done with a slider. Ideally though all fields from all API’s should be available. Not an easy task I know.

Perhaps I should rephrase that. Say X number of fields to be available from each API. So all fields technically being available but a limitation on how many can be used within Blynk at any one time. As you will know some API’s have hundreds of fields and it wouldn’t be practical to be working on all of them within Blynk.

I parse all fields, so I can use anything which it returns. You can choose wether to use it or not, but everything is there, yes :slight_smile:

Well, I was thinking on how your code could be simplified with Blynk. Because when I look in @Costas parsing code I feel pain :slight_smile:.

1 Like

@Dmitriy yes painful to a real coder. Please note my parsing is for api.wunderground.com not api.sunrise-sunset.org and wunderground has lots more stuff in addition to the astronomy api.

This is not because of your code, this parsing in general is complex and I believe it is impossible task for most of Blynkers.

You would be in agony then if you saw what we do when we parse Blynk’s “project” api.

1 Like

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?