TZ-IOT releases Python SDK: tziota

TZ-IOT releases Python SDK: tziota

This article blog link: http://blog.csdn.net/jdh99 , Author: jdh, reprint please specify.

 

Introduction:

The internal test service of TZ-IOT has been launched in "The Internet of Things Platform TZ-IOT Releases Transparent Cloud Internal Test Service: V1.0 ", and the Python SDK is released in this article.

 

use:

You don't need to rent a public server, you can choose an embedded device like a networkable PC or Raspberry Pi to be a server. Communicate with nodes that are also based on TZ-IOT.

 

installation:

pip install tziota

If it is already installed, you need to update the latest version:

pip install tziota --upgrade

initialization:

First , apply for the IA address of the node according to the instructions in the " Internet of Things Platform TZ-IOT Releases Transparent Cloud Internal Test Service: V1.0 ".

Suppose you apply for the address: 2140:0000:0000:0101 (abbreviated 2140::101), and the password is "456".

The initialization code:

def main():
    # 初始化参数
    param = tziota.Param()
    # 本地UDP绑定的IP和端口
    param.local_ip = "10.58.4.52"
    param.local_port = 14131
    # 本节点IA地址和密码
    param.local_ia = 0x2140000000000101
    param.pwd = "456"
    # 服务器IP和端口
    param.server_ip = "115.28.86.171"
    param.server_port = 14129
    # 服务器IA地址
    param.server_ia = 0x2140000000000002
    tziota.init(param)
    
    # 注册接收回调函数
    tziota.register_callback_rx(deal_rx)

Receiving interface:

In the callback function:

def deal_rx(ia, data):
    print('IA:0x%016x' % ia)
    for x in data:
        print('%02x' % x, end=' ')
    print()

ia: IA address of the other party

data: data sent by the other party

 

Send interface:

frame = bytearray()
for i in range(5):
    frame.append(i + 10)
tziota.send(0x2140000000000100, frame)

The send interface has two parameters:

The first is the destination IA address, and the second is the data to be sent.

 

Online interface:

tziota.is_online():

Online refers to whether the node is successfully connected to the server.

 

Complete test code:

2140::100 Send byte data: 0x01020304 to 2140::101

import threading
import time

import tziota


def main():
    param = tziota.Param()
    param.local_ip = "10.58.4.52"
    param.local_port = 14130
    param.local_ia = 0x2140000000000100
    param.pwd = "123"
    param.server_ip = "115.28.86.171"
    param.server_port = 14129
    param.server_ia = 0x2140000000000002
    tziota.init(param)
    tziota.register_callback_rx(deal_rx)

    threading.Thread(target=cycle_task).start()


def deal_rx(ia, data):
    print('IA:0x%016x' % ia)
    for x in data:
        print('%02x' % x, end=' ')
    print()


def cycle_task():
    while True:
        if tziota.is_online():
            frame = bytearray()
            for i in range(5):
                frame.append(i)
            tziota.send(0x2140000000000101, frame)
        else:
            print('当前掉线')

        time.sleep(1)


if __name__ == '__main__':
    main()

2140::101 sends byte data: 0x0a0b0c0d0e to 2140::100

import threading
import time

import tziota


def main():
    # 初始化参数
    param = tziota.Param()
    # 本地UDP绑定的IP和端口
    param.local_ip = "10.58.4.52"
    param.local_port = 14131
    # 本节点IA地址和密码
    param.local_ia = 0x2140000000000101
    param.pwd = "456"
    # 服务器IP和端口
    param.server_ip = "115.28.86.171"
    param.server_port = 14129
    # 服务器IA地址
    param.server_ia = 0x2140000000000002
    tziota.init(param)

    # 注册接收回调函数
    tziota.register_callback_rx(deal_rx)

    threading.Thread(target=cycle_task).start()


def deal_rx(ia, data):
    print('IA:0x%016x' % ia)
    for x in data:
        print('%02x' % x, end=' ')
    print()


def cycle_task():
    while True:
        if tziota.is_online():
            frame = bytearray()
            for i in range(5):
                frame.append(i + 10)
            tziota.send(0x2140000000000100, frame)
        else:
            print('当前掉线')

        time.sleep(1)


if __name__ == '__main__':
    main()

operation result:

2140::100 Receive data:

IA:0x2140000000000101
0a 0b 0c 0d 0e 
IA:0x2140000000000101
0a 0b 0c 0d 0e 
IA:0x2140000000000101
0a 0b 0c 0d 0e 
IA:0x2140000000000101
0a 0b 0c 0d 0e 

2140::101 Receive data:

IA:0x2140000000000100
00 01 02 03 04 
IA:0x2140000000000100
00 01 02 03 04 
IA:0x2140000000000100
00 01 02 03 04 
IA:0x2140000000000100
00 01 02 03 04 

 

Guess you like

Origin blog.csdn.net/jdh99/article/details/90674083