Blynk Cloud to Website (solved)

I am new to blynk,.but have used Particle.io cloud with success.
I have a website where I get Particle cloud data into a webpage. Very simple, lots of examples.

Now am trying to get blynk.cloud data into the website page to display variables.
The website is coded in PHP or javascript but I can learn other methods if required.
I must be missing something fundamental?

There is already data in the Blynk cloud and if I place this url in my Browser it works (the return value is shown in the browser page:
https://blynk.cloud/external/api/get?token=**********&dataStreamId=3

So, how do I put code (PHP, javascript, etc) in a website page to get that return into a PHP or Javascript variable?

This cannot be that difficult. A 20 line example would probably get me started

Or a link to examples of getting blynk.cloud data into a website page.

Any suggestions or help would be much appreciated.

Solved I found this html / javascript code to work with or without the ny3. in the url.

<html>
   head>
       <title>Weather Station</title>
   </head>
   <body>
    <script>
     var request = new XMLHttpRequest();
      request.open('GET', 'https://blynk.cloud/external/api/get?token=*********&dataStreamId=3');
      // request.open('GET', 'https://ny3.blynk.cloud/external/api/get?token=*********&dataStreamId=3');
     request.onreadystatechange = function () {
      if (this.readyState === 4) {
         //  console.log('Status:', this.status);   // not needed
         //  console.log('Headers:', this.getAllResponseHeaders());  // not needed
         //  console.log('Body:', this.responseText);     //  This is dataStreamid 3 value
         //  alert(this.responseText);      // Just show the value in an alert
         // document write puts text onto the webpage.  
         document.write(" <h4>Weather Station<br>Development </h4>Wind Gusts: " + this.responseText + "  <br> "); 
      }
     };
     request.send();
    </script>
   </body>
</html>

I know nothing about PHP and web design, but the API documentation for the Legacy API had the option to show methods in various languages.
The link is here:

https://blynkapi.docs.apiary.io/#reference/0/get-pin-value/get-pin-value
By default is shows the RAW request. However, you can change that to PHP and it shows a code snippet…

Obviously, you’ll need to change the syntax of the actual URL to the blynk.cloud one, but maybe this will get you started?

Also, you need to read this:
https://docs.blynk.io/en/blynk.cloud/troubleshooting

image

You need to put the full URL of the Blynk server that hosts your project into the API call, otherwise the machine hosting your website may resolve to the wrong geographic cloud server and you will get an Invalid Auth Token error.

Pete.

1 Like

The javascript example is not recommended because it shows the token in the webpage source

Here is a php Curl example which runs on the server and will not display the token in the webpage
NOTE: This did not run in my local server. returned this message:
‘SSL certificate problem: unable to get local issuer certificate’
This seem to suggest the website you are creating a page in must have SSL
The code works in my actual website - It uses SSL .

For the code to work with a webpage NOT using SSL just use the url with http:// …
instead of https://

<$php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, “https://blynk.cloud/external/api/get?token=***********&v0&v1&v2&v3&v4&v5&v6&v7&v8&v9&v10”);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
$response = curl_exec($ch);
echo “
All variables from Weather Station
”;
var_dump($response);
echo “
Error Return
”;
var_dump(curl_error($ch));
curl_close($ch);
?>

Hope this helps someone else

1 Like

Thanks for input. I was trying the Curl solution on my local server which did not like the https:// in the url and I was not looking at the Curl errors. Duh! Thanks again!

See my solutions in original post and in my reply to original post

Glad you managed to get it working, and thanks for the follow-up.

Pete.