Skip to content

Blynk IoT library for Python and Micropython

License

Notifications You must be signed in to change notification settings

TefoAM/lib-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Blynk Python Library

Provides API for Blynk Server communication and messaging on IoT/desktop systems with Micropython/Python support.


Blynk is the most popular Internet of Things platform for connecting hardware to the cloud, designing apps to control them, and managing your deployed devices at scale.

  • With Blynk Library you can connect over 400 hardware models (including ESP8266, ESP32, NodeMCU, all Arduinos, Raspberry Pi, Particle, Texas Instruments, etc.)to the Blynk Cloud. Full list of supported hardware can be found here.

  • With Blynk apps for iOS and Android apps you can easily build graphic interfaces for all of your projects by simply dragging and dropping widgets on your smartphone. It's a purely WYSIWG experience: no coding on iOS or Android required.

  • Hardware can connect to Blynk Cloud (open-source server) over the Internet using hardware connectivity on board, or with the use of various shields (Ethernet, WiFi, GSM, LTE, etc). Blynk Cloud is available for every user of Blynk for free.

Blynk Banner

Installation of Blynk Python Library

Installation via python pip

  • Check python availability in your system.

    python --version
    

    To exclude compatibility issue preferable versions are Python 2.7.9 (or greater) or Python 3.4 (or greater) If python not present you can download and install it from here.

    Note! To run python in "sandbox" you can try virtualenv module. Examine this link how to do it.

  • If you’re using preferable versions of python mentioned above, then pip comes installed with Python by default. Check pip availability:

    pip --version
    
  • Install blynk library

    sudo pip install blynklib
    

Manual installation

Library can be installed locally from git sources:

git clone https://github.com/blynkkk/lib-python.git
cd lib-python
pip install --user -e .

# sudo pip install -e .  # if installation needed not for current but for all users

Testing

You can run unit tests on cPython systems using the command:

python setup.py test

Note! Unit Tests for Micropython ENV currently not implemented

Micropython installation

Some hardware platforms can use Micropython package. This is helpful for preliminary testing and debugging of your code outside of real hardware. Supported platforms and related installation docs can be found here.

Features

This library supports Python2, Python3, and Micropython.

  • Communication with public or local Blynk Server.
  • Exchange any data between your hardware and app
  • Tested to work with: Raspberry Pi (any), ESP32, ESP8266
List of available operations:
  • Subscribe to connect/disconnect events
  • Subscribe to read/write events of virtual pins
  • Virtual Pin write
  • Virtual Pin sync
  • Send mobile app push notifications
  • Send email notifications
  • Send twitter notifications
  • Change widget GUI parameters in Blynk app based on hardware input

Quickstart

Install Blynk python library

Install Blynk App: Google Play | App Store**

  • Create new account in Blynk app using your email address
  • Create a new Project in Blynk app
  • You will get Auth Token delivered to your email account.
  • Put this Auth Token within your python script to authenticate your device on public or local
BLYNK_AUTH = '<YourAuthToken>' #insert your Auth Token here

Usage example

import blynklib

BLYNK_AUTH = '<YourAuthToken>' #insert your Auth Token here
# base lib init
blynk = blynklib.Blynk(BLYNK_AUTH) 
# advanced options of lib init
# from __future__ import print_function
# blynk = blynklib.Blynk(BLYNK_AUTH, server='blynk-cloud.com', port=80, heartbeat=10, rcv_buffer=1024, log=print)

# register handler for Virtual Pin V22 reading.
# for example when a widget in Blynk App asks Virtual Pin data from server within given interval    
@blynk.handle_event('read V22')
def read_virtual_pin_handler(pin):
    
    # your code goes here
    # ...
    # Example: get sensor value, perform calculations, etc
    sensor_data = '<YourCalculatedSensorData>'
    critilcal_data_value = '<YourCriticalSensorValue>'
        
    # send value to Virtual Pin and store it in Blynk Cloud 
    blynk.virtual_write(pin, sensor_data) #example: blynk.virtual_write(24, sensor_data)
        
    # you can perform actions if value reaches a threshold (e.g. some critical value)
    if sensor_data >= critilcal_data_value
        
        blynk.set_property(pin, 'color', '#FF0000') # set red color for the widget UI element 
        blynk.notify('Warning critical value') # send push notification to Blynk App 
        blynk.email(<youremail@email.com>, 'Email Subject', 'Email Body') # send email to specified address
        
# main loop that starts program and handles registered events
while True:
    blynk.run()

Other Examples

Check more_examples to get familiar with some of Blynk features.

Examples List:

Core operations:
  • 01_write_virtual_pin.py (how to write to Virtual Pin )
  • 02_read_virtual_pin.py (how to read Virtual Pin )
  • 03_connect_disconnect.py (connection management)
  • 04_email.py(how to send send email and push notifications) // TODO: combine it into one example
  • 05_set_property_notify.py (how to change some of widget UI properties) // TODO: move notifications to 04 example
  • 06_terminal_widget.py (communicate between hardware and app through Terminal widget)
  • 07_tweet_and_logging.py (how to post to Twitter and log events from your hardware)
Raspberry Pi (any):
  • 01_weather_station_pi3b.py (Connect DHT22; BMP180 sensors and send data to Blynk app)
ESP32
  • 01_touch_button.py (Connect TTP223B touch sensor to ESP32 and react to touch)

License

This project is released under The MIT License (MIT)

About

Blynk IoT library for Python and Micropython

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%