Cryptocurrencies and blockchain technologies have become extremely trending over the past few years. Many big tech companies invested in it. Some of them, like BitTorrent, had created their own crypto money to allow faster exchange and reward users.
Today we will learn how to record Binance data to Thingsboard. This can be useful to create a customized dashboard and set up email alerts.
Binance
Binance is a very well known cryptocurrency exhange platform. We will only use its testing API, so no need to create an account for this script.
Thingsboard
Thingsboard is a leading IOT platform written in Java and Angular, it is designed to handle large amount of requests from thousands devices. Thingsboard is compatible with most communication protocols, today we will use the light weight MQTT protocol. In order to setup Thingsboard, please refer to its documentation.
Python 3
Python is the programming language we will use in this tutorial. Both Thingsboard and Binance provide a complete python connector library. You do not need to be a Python expert as the following code will be pretty straight forward.
Installation
First of all, you should install Binance and Thingsboard Python connectors:
pip3 install tb-mqtt-client binance-connector
You can find more details and documentation on github repositories:
- https://github.com/thingsboard/thingsboard-python-client-sdk
- https://github.com/binance/binance-connector-python
Let's now write some code
Import necessary libraries:
#!/usr/bin/python3
from binance.spot import Spot as BinanceClient #Binance Client
from tb_device_mqtt import TBDeviceMqttClient, TBPublishInfo #Thingsboard Client
import time
Get latest price from Bitcoin/USDT
#Initialize Binance client
binance_client = BinanceClient(base_url="https://testnet.binance.vision")
#Get latest price
def getPrice(trade):
try:
response = binance_client.ticker_price(trade)
price = response.get('price')
print(price)
return price
except Exception as e:
print(e)
return None
Send data to Thingsboard
#thingsboard mqtt host.
#Ensure port 1883 is not blocked by your firewall.
TB_MQTT_HOST = "iot.example.com"
#thingsboard device access token
TB_DEVICE_KEY = "YOUR-DEVICE-ACCESS-KEY"
device = TBDeviceMqttClient(TB_MQTT_HOST, TB_DEVICE_KEY)
def sendTb(timeseries):
device.connect()
result = device.send_telemetry(timeseries)
success = result.get() == TBPublishInfo.TB_ERR_SUCCESS #wait to receive result
device.disconnect()
Main function
while True:
try:
#trade to follow Bitcoin / USDT
trade = "BTCUSDT"
#get price from Binance
price = getPrice(trade)
#prepare timeseries
timeseries = {
trade: price
}
#send to Thingsboard
sendTb(timeseries)
except Exception as e:
print(e)
time.sleep(15) #wait 15seconds
Timeseries and Dashboard
Run the script and you should now see data coming to Thingsboard device. You can then create a dashboard.
Rule Chain
In order to receive notifications when BTCUSDT reaches a certain level, we will create a Rule Chain in Thingsboard. You can download the configuration from github link at the end of article.
Conclusion
Within a very short time and little amount of code, we integrated Binance data into Thingsboard. You can duplicate or modify the code to add more Crypto and create your fully customized dashboard and alerts.
Complete code is here: https://github.com/acte-technology/binance-tb-tutorial