Skip to content

Micropython libraries for access to Blynk, Adafruit.io, Thingspeak, Accuweather, Imgw, Pushsafer via REST API

License

Notifications You must be signed in to change notification settings

stonatm/REST-API

Repository files navigation

REST API

Short & simple micropython scripts

blynk.py blynk.png

Access to Blynk service via REST API

function init( token )

Store authorization token to use with others functions.

parameters:

token - authorization token.


function write_pin( pin, value )

Write a value to pin.

parameters:

pin - pin name (must be string like "V0", "V1").

value - a value to be writen to pin.


function read_pin( pin )

Read a pin value.

parameters:

pin - pin name to read

Function return pin value or string with error description if any occur.


function notify( message )

Sends a notification to android blynk app with given message.

parameters:

message - message text to send


function is_app_connected()

Function check if android app is connected with blynk service.

Function returns strings "true" or "false" (they are not boolean logical values)


Example:

from blynk import blynk
blynk.init("YOUR_AUTH_TOKEN")

blynk.write_pin("V0", 12.5)
blynk.notify("test message")

if blynk.is_app_connected() == "true":
  print("app online")

adafruit.py adafruit.png

Access to adafruit.io service via REST API

function init( username, aio )

Initialize library with username and adafruit AIO key.

parameters:

username - your adafruit.io username.

aio - your adafruit.io AIO access key.


function write_feed( feed_name, value )

Write a value to feed

parameters:

feed_name - your adafruit feed name.

value - value to sent to given feed


function read_feed(feed_name)

Read last stored value from given feed.

parameters:

feed_name - your adafruit feed name.


Example:

from adafruit import adafruit
adafruit.init('ADAFRUIT_USERNAME', 'YOUR_AIO_KEY')

adafruit.write_feed('FEED_NAME', 23.5)
print( adafruit.read_feed('FEED_NAME') )

tspeak.py tspeak.png

Access to thingspeak service via REST API

function init( channel_id, read_key, write_key )

Initialize library with your channel id number and access keys.

parameters:

channel_id - your thingspeak channel id number

read_key - your thingspeak read access key

write_key - your thingspeak write access key


function write_channel( channel_number, value )

Write a value to thingspeak given channel number.

parameters:

channel_number - channel number (1-8)

value - a value to write to given channel number

Function returns a three type of value:

  • -1 when errors occur.

  • 0 when data was rejected due to sending more than once every 15 seconds.

  • integer number when data was stored. This is the next entry id of the message sent to channel.


function read_channel( channel_number )

Read last stored value from given channel number.

parameters:

channel_number - channel number (1-8)

If any error occurs function return a -1 value.


Example:

from tspeak import tspeak
tspeak.init( 'CHANNEL_ID_NUMBER', 'READ_KEY', 'WRITE_KEY' )

tspeak.write_channel( 1, 123.5)
# wait 15 seconds before sending next data to the next channels
print( tspeak.read_channel(1) )

accuweat.py accuweather.png

Access to accuweather.com synoptic data via REST API

How to get your Api Key:

How to find city ID for your city:

function init(apikey, cityid)

Initialize the library with Api key and city id number. You must run this function before use other functions from this library.

parameters:

apikey - your accuweather.com account apikey

cityid - your city id number


function refreshData()

Get actual synoptic data from accuweather.com and store them to later use by read... functions. In free trial account there is a 50 requests per day limit.


readTemperature()

Return temperature for your city


function readWeatherText()

Return text description of weather for your city like "Sunny", "Cloudy" etc.


function readWeatherIcon()

Return only icon number (not icon) according to actual weather in your city. Icon list: https://developer.accuweather.com/weather-icons


function readIsDayTime()

Return True if in your city is a day time otherwise return False


function readLocalObserwationDate()

Return a local date of obserwation in your city. Return string in format YYYY-MM-DD


function readLocalObserwationTime()

Return a local time of obserwation in your city. Return string in format HH:MM:SS


All of read functions return proper data or text description of error if any occur.

example

from accuweat import accuweat
accuweat.init( 'ACCUWEATHER_API_KEY', 265168)

accuweat.refreshData()

print( accuweat.readTemperature() )
print( accuweat.readWeatherText() )
print( accuweat.readWeatherIcon() )
print( accuweat.readIsDayTime() )
print( accuweat.readLocalObserwationTime() )
print( accuweat.readLocalObserwationDate() )

imgw.py imgw.png

Access to Polish synoptic data service imgw.pl via REST API

acquireData(station_id)

Downloading synoptic data from the service for a given station id and store it in memory for later use.

parameters:

station_id - your station (city) id number


List of station id's and city names - imgw.csv

All the functions described below return the stored data retrieved by the function acquireData.

function getStationID()

Return station id number.


function getStationName()

Return station name (city).


function getMeasurementDate()

Return measurement date (string formated as YYYY-MM-DD).


function getMeasurementTime()

Return hour of measurement time (string formated as HH).


function getTemperature()

Return temperature (°C).


function getWindSpeed()

Return wind speed (m/s).


function getWindDirection()

Return wind direction (azimuth/angle i'm not sure ).


function getRelativeHumidity()

Return relative humidity (%RH).


function getTotalRainfall()

Return total rainfall (mm).


function getPressure()

Return pressure (hPa).


example

Text descriptions in example are in Polish due to the website offering only data for Polish cities.

import imgw

imgw.acquireData(12360)
print('id stacji: ' + imgw.getStationID())
print('nazwa stacji: ' + imgw.getStationName())
print('data pomiaru: ' + imgw.getMeasurementDate())
print('godzina pomiaru: ' + imgw.getMeasurementTime() + ':00')
print('temperatura: ' + imgw.getTemperature() + ' °C')
print('prędkość wiatru: ' + imgw.getWindSpeed() + ' m/s')
print('kierunek wiatru: ' + imgw.getWindDirection() + ' °')
print('wilgotność: ' + imgw.getRelativeHumidity() + ' %')
print('suma opadów: ' + imgw.getTotalRainfall() + ' mm')
print('ciśnienie: ' + imgw.getPressure() + ' hPa')

pushsafer.py pushsafer.jpg

Sending push message thru pushsafer.com service via REST API. Only a message push was implemented.

send_notification(private_key, device, title, message, sound, vibration, icon )

Send notification to device with message.

Returns 1 when message is sent or 0 when any error occured.

parameters:

private_key - your private key

device - target device message id

title - title of message

message - message to sent

sound - (1-62) which played sound number (0 = silent)

vibration - how ofyen may vibrate (0-3)

icon - icon number (1-177)


example

import pushsafer
pushsafer.send_notification( 'KEY', 'DEVICEID', 'Message title', 'Message content', 1, 1, 1 )