BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Get Datastream Value

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");}
 
}

Have you tried with a lower-case ‘v’ ?

Pete.

1 Like

Remove “&” before “token” from “?&token=” too.

2 Likes

Thank @BuildInnovation and @PeteKnight .
I tried with the 2 cases you gave. But the result is still the same as the original. I tried using the link with chrome and it returned the correct value. Do you have any other ideas?

The -1 error code you are getting means HTTPC_ERROR_CONNECTION_REFUSED

/// HTTP client errors
#define HTTPC_ERROR_CONNECTION_REFUSED (-1)
#define HTTPC_ERROR_SEND_HEADER_FAILED (-2)
#define HTTPC_ERROR_SEND_PAYLOAD_FAILED (-3)
#define HTTPC_ERROR_NOT_CONNECTED (-4)
#define HTTPC_ERROR_CONNECTION_LOST (-5)
#define HTTPC_ERROR_NO_STREAM (-6)
#define HTTPC_ERROR_NO_HTTP_SERVER (-7)
#define HTTPC_ERROR_TOO_LESS_RAM (-8)
#define HTTPC_ERROR_ENCODING (-9)
#define HTTPC_ERROR_STREAM_WRITE (-10)
#define HTTPC_ERROR_READ_TIMEOUT (-11)

I have a feeling that it may be to do with the variable type that you are using in your http.begin() command, but I’m a bit surprised that if this is the case you’re not getting a compilation error,

I thought the httpclient.begin() command expecyted a const.char* variable type rather than a String

you could achieve this with this conversion…

getBlynkHTTPclient.begin (fullRequest.c_str()); //Specify URL AND request as a null-terminated C-string pointer

Pete.

Why “sgp1.blynk.cloud” and not “https://blynk.cloud”?

“sgp1.blynk.cloud” , this follows the instructions in the document. You can find it in link below

https://docs.blynk.io/en/blynk.cloud/troubleshooting

I tried this command line but it doesn’t seem to be the solution. Its return is still the same at the beginning. :thinking: