I am trying to write a piece of code to get data from another blynk device via HTTPS API. The sketch is attached below. Results returned with code: -1. Has anyone tried doing this?
Details :
• Hardware : ESP8266
• Blynk 2.0 platform
• Blynk Library version : 1.0.1
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#define HTTP_CODE_OK 200
// Token device target
const char auth[] = "**********************************";
// Network settings
const char ssid[] = "Lan4";
const char pass[] = "123456789";
// Start the WiFi connection
void connectNetwork()
{
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
}
void setup()
{
Serial.begin(9600);
delay(10);
Serial.println();
Serial.println();
connectNetwork();
}
float getBlynkDatastreamInfo ( const String& BlynkAuthcode, int datastream )
{
// String BlynkAuthcode = Blynk authcode of device we are getting data FROM
// int datastream = Blynk Datastream we are getting data FROM
// example float value = getBlynkDatastreamInfo ( "authcode", V6 )
// if ( value == -123.45 ) Serial.println ( "Error" );
float returnedValue = -123.45; // default error value
Serial.print ( "\ngetBlynkDatastreamInfo called on datastream V" ); Serial.print ( datastream );
Serial.print ( " on device with authcode " ); Serial.println ( BlynkAuthcode );
// Set up an HTTP client object for the Blynk get
HTTPClient getBlynkHTTPclient;
// Server name URL
String BlynkGetServerName = "https://sgp1.blynk.cloud/external/api/get";
// Blynk's GET payload for getting data is "?token={token}&V8"
String BlynkRequest = "?&token=" + String ( BlynkAuthcode ) + "&V" + String ( datastream );
// Blynk datastream get uses GET and a single request string
String fullRequest = BlynkGetServerName + BlynkRequest;
Serial.print ( " fullRequest is <" ); Serial.print ( fullRequest ); Serial.println ( ">" );
getBlynkHTTPclient.begin (fullRequest); //Specify URL AND request
getBlynkHTTPclient.addHeader ( "Content-Type", "text/plain" );
//getBlynkHTTPclient.addHeader("Content-Type", "application/json");
//getBlynkHTTPclient.addHeader ( "Content-Type", "application/x-www-form-urlencoded" );
long elapsed = millis();
int BlynkReturnCode = getBlynkHTTPclient.GET();
Serial.print ( " elapsed time in ms = " ); Serial.println ( millis() - elapsed );
Serial.print ( " return code: " ); Serial.println ( BlynkReturnCode );
if ( BlynkReturnCode > 0 ) //Check the returning code (200 is AOK)
{
String payload = getBlynkHTTPclient.getString(); //Get the request response payload
Serial.print ( " response string: " ); Serial.println ( payload );
if ( BlynkReturnCode == HTTP_CODE_OK )
{
returnedValue = payload.toFloat();
Serial.print ( " Returning value: " ); Serial.println ( returnedValue ); Serial.println();
}
}
getBlynkHTTPclient.end(); //Close HTTP connection
return returnedValue;
} // end getBlynkDatastreamInfo
void loop() {
getBlynkDatastreamInfo ( auth, 1 ) ;
delay(5000);
if(WiFi.status())
{Serial.println("connect ok");}
else {Serial.println("connect not ok");}
}