2017-09-14 151 views
1

我正在嘗試使用API​​獲取當前本地時間。我正在使用WEMOS D1 Mini,並使用blynk的get方法從API返回JSON並將其存儲。在Arduino中獲取API的時間

我使用此代碼

#define BLYNK_PRINT Serial 

#include <ESP8266WiFi.h> 
#include <ArduinoJson.h> 
#include <BlynkSimpleEsp8266.h> 

String json; 
char auth[] = ""; 
char ssid[] = "YourNetworkName"; 
char pass[] = "YourPassword"; 

BLYNK_WRITE(V0) { 
    json = param.asStr(); 
} 

void setup() { 
    Serial.begin(9600); 
    Blynk.begin(auth, ssid, pass); 
} 

void loop() { 
    Blynk.run(); 
    Blynk.virtualWrite(V0, "https://api.bot-dev.org/time/"); 
    JsonObject& root = jsonBuffer.parseObject(json); 
    long time = root[String("ENtime")]; 
} 

,但我不能接受在很長一段時間變量的時間。

+2

你會得到什麼,你期望什麼? – eyllanesc

+0

「*不好用*」不是一個好問題描述。 –

回答

0

你可以用更簡單的方法做到這一點。 您需要將WebHook小部件添加到您的應用程序。在webhoook小部件中,您需要將https://api.bot-dev.org/time/網址。然後將這個小部件分配給虛擬引腳,比如說V0。 Webhook小部件在觸發後會返回硬件響應。所以,你的代碼應該看起來像:

BLYNK_WRITE(V0) { 
    //here you'll get response from the webhook 
    json = param.asStr(); 
} 

void setup() { 
    Serial.begin(9600); 
    Blynk.begin(auth, ssid, pass); 
} 

void loop() { 
    Blynk.run(); 

    //trigger the webhook 
    Blynk.virtualWrite(V0, 1); //you can send any value to trigger webhook 
} 

有想法,你也需要從主循環,以避免flooding搬出Blynk.virtualWrite

Here is有關webhook小部件的更多詳細信息。