Getting Started with the API

Need a boost on using the API?

So, I thought i would post what i’ve learned while using Blynk.
At the time of this writing, im a Blynk beginner and like figuring stuff out for myself, with a little help along the way.

If you’ve never used an api before, think of it as a way other applications can communicate to your application (Blynk in this case).
Usually there are so many ways to do it, using different library’s for different scripting languages, its hard for everyone to know how to use them all.

In this case, we have an http interface, so you’re browser can do most of the work.
In some cases, you’ll have to use another method to do post/puts, curl it generally supported rather well. Curl is generally a unix command , but i’ve had it on windows as well.

Start by having your project up and online and get one of the easily readable pin identifiers ready to go.
My simple project uses my raspberry pi and a relay to turn on my fireplace.
Im going to use Digital pin 4, or d4. A gpio pin should be converted to the digital number.

You should already know your auth code, Im gonna call mine XXXYYYZZZ from now on out.

Make sure you are using your local server if you have one, or the blynk-cloud if you’re using that. Note the port difference from my local server

http://blynk-cloud.com/
http://mylocal.blynk.server:8080/

The format for the calls are

http :// <server><port if not 80> / <authcode> / <query>

Once you figure out the server/port and auth code, the queries are the only things you’ll need to modify.

isHardwareConnected

Lets find out if your hardware is connected.

http://blynk-cloud.com/XXXYYYZZZ/isHardwareConnected

Its shows true in the browser window… that means my pi (or your hardware) is up and ready to go.
Note : false would mean you’re hardware is offline.

isAppConnected

Are there any application clients listening to the data? Is my phone running the application?
I share the app with my wife, the assumption is that if one or both of us were connected, it would show true.

http://blynk-cloud.com/XXXYYYZZZ/isAppConnected

false? No apps are listening.

project

Blynk keeps your project on the server in txt format under your user id. When you use the project query, it gives you all the info on your project build, buttons, boards, pins, colors, options, etc.

http://blynk-cloud.com/XXXYYYZZZ/project

It might be confusing to look at, but you will most likely recognize alot of your user interface in there.

qr

Generate your project QR code

http://blynk-cloud.com/XXXYYYZZZ/qr

get

Get the value of your pin, remember i was using d4, you’ll have to edit this part.
Note: remember that pins that are on or off will show a 1 for on, 0 for off.
You may encounter some “true / false” as you go along, so consider checking the value format before going too far.

http://blynk-cloud.com/XXXYYYZZZ/get/d4

using curl as another example. Notice the Virtual Pin as well.

unix# curl http://mylocal.blynk.server:8080/XXXYYYZZZ/get/v1

write (using get)

Change the value of a pin

Turn my fireplace ON
http://blynk-cloud.com/XXXYYYZZZ/update/d4?value=1

Turn my fireplace OFF
http://blynk-cloud.com/XXXYYYZZZ/update/d4?value=0

To update a table
Note: this is way beyond an API FAQ writeup, but hey, its out there.
Borrowed from Dmitriy’s (and @rockmanqh’s) post here

instead of Blynk.virtualWrite(V1, “add”, rowIndex, “My Event”, “My Value”);
you can use
http://blynk-cloud.com/XXXYYYZZZ/update/v1?value=add&value=rowIndex&value=My%20Event&value=My%20Value

write (using put)

I used curl to send the data, but you could find your own method

Turn On my fireplace
unix# curl -X PUT -H "Content-Type: application/json" -d '["1"]' http://blynk-cloud.com/XXXYYYZZZ/update/d4

Turn Off my fireplace
unix# curl -X PUT -H "Content-Type: application/json" -d '["0"]' http://blynk-cloud.com/XXXYYYZZZ/update/d4

Map updates
Note: Again, way beyond an API’s FAQ.
Map location borrowed from @eugene

curl -v -L --include \
 --request PUT `\
 --header "Content-Type: application/json"  \
 --data-binary "[\"1\", \"52.2949\", \"-4.8610\", \"Test\"]"  \
  http://blynk-cloud.com/XXXYYYZZZ/update/V30 

Widget Properties

Right now, might be a good time to go back to the /project command and see what options you could change on your widgets. I had poor luck changing anything except the label and the pin value.

Here is my personal example.
http://blynk:8080/XXXYYYZZZ/update/d4?label=test1

This updated the app right away and worked pretty good.
I tried others like the font size

d4?fontsize=LARGE
d4?fontsize=SMALL

They didnt seem to work

I tried changing it to a pushbutton instead of a switch

d4?pushMode=true

That didnt work either.
try your own from the items in /project, you might find more that work.
You might be able to tell im getting tired at this point as im giving up early.

Edit !!
In the end, i was able to make this work
http://blynk:8080/XXXYYYZZZ/update/v4?color=%23000000
the %23 is a character symbol for the pound(#) in the html color code. Still more experimenting needed.

notify

So, make sure you have a notify object in your app, then you can use curl to post data the notify api
Some browsers have posting abilities, i didnt mess around with it.
In the command below, the \ (slash) at the end of each line tells unix that the command is too long to be on one line and helps with human readability.

curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"body": "My fireplace is on Blynk Powered."}' \
   http://blynk-cloud.com/XXXYYYZZZ/notify

I havent tried, but the supposed limit is 255 chars.

email

YOU MUST ADD THE EMAIL WIDGET TO YOUR APP.

curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"to": "nobody@example.com","subj": "About My Fireplace","title": "Thats right !!! Its Blynk Powered!!"}' \
  http://blynk-cloud.com/XXXYYYZZZ/email

Summary

Hoping this helps, its not perfect, but hoping it gives you a start.

1 Like

It’s worth including a link to the official API documentation:

https://blynkapi.docs.apiary.io/#reference/0/get-project

Also, you can a better idea of what can be done using the “set widget property via GET” section by reading this:

http://docs.blynk.cc/#blynk-main-operations-change-widget-properties

Pete.