Skip to content

Instantly share code, notes, and snippets.

@phuctu1901
Last active November 7, 2019 01:30
Show Gist options
  • Save phuctu1901/f826c36c41b897fe41ae761069367506 to your computer and use it in GitHub Desktop.
Save phuctu1901/f826c36c41b897fe41ae761069367506 to your computer and use it in GitHub Desktop.
import wiotp.sdk.device
import datetime
import time
# Sự kiện khi nhận một request từ phía server
def myCommandCallback(cmd):
print("Command received: %s" % cmd.data)
print(cmd.data)
event = cmd.data.get("event")
# Sự kiện sau khi gởi dữ liệu thành công về server
def myOnPublishCallback():
current_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
print("Confirmed event %s received by IoTF\n" % current_time)
# Cấu hình cho thiết bị để kết nối với server từ các thông tin đăng ký trước đó
myConfig = {
"identity": {
"orgId": "mmj3og",
"typeId": "RaspberryPi",
"deviceId": "pi001"
},
"auth": {
"token": "*4wHj6xLBRc+CJJcD-"
}
}
# Khởi tạo thiết bị
client = wiotp.sdk.device.DeviceClient(config=myConfig, logHandlers=None)
# Cấu hình sự kiện
client.commandCallback = myCommandCallback
#Kết nối thiết bị
client.connect()
# Liên tục gởi tín hiệu về server
while True:
send_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
data2send = {"time": send_time}
# Sử dụng lệnh này để gởi dữ liệu về server
success = client.publishEvent("data2send", "json", data2send, qos=0, onPublish=myOnPublishCallback)
if not success:
print("Not connected to IoTF")
break
time.sleep(10)
# Ngắt kết nối thiêt bị
client.disconnect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment