This repository has been archived by the owner on Apr 6, 2022. It is now read-only.
forked from manasesjesus/c8y_hw_mqtt
-
Notifications
You must be signed in to change notification settings - Fork 3
/
hello_mqtt.py
63 lines (54 loc) · 1.92 KB
/
hello_mqtt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import paho.mqtt.client as mqtt
import time, threading, random
# client, user and device details
serverUrl = "mqtt.cumulocity.com"
clientId = "my_mqtt_python_client"
device_name = "My Python MQTT device"
tenant = "<<tenant_ID>>"
username = "<<username>>"
password = "<<password>>"
receivedMessages = []
# display all incoming messages
def on_message(client, userdata, message):
print("Received operation " + str(message.payload))
if (message.payload.startswith("510")):
print("Simulating device restart...")
publish("s/us", "501,c8y_Restart");
print("...restarting...")
time.sleep(1)
publish("s/us", "503,c8y_Restart");
print("...done...")
# send temperature measurement
def sendMeasurements():
try:
print("Sending temperature measurement...")
publish("s/us", "211," + str(random.randint(10, 20)))
thread = threading.Timer(7, sendMeasurements)
thread.daemon=True
thread.start()
while True: time.sleep(100)
except (KeyboardInterrupt, SystemExit):
print("Received keyboard interrupt, quitting ...")
# publish a message
def publish(topic, message, waitForAck = False):
mid = client.publish(topic, message, 2)[1]
if (waitForAck):
while mid not in receivedMessages:
time.sleep(0.25)
def on_publish(client, userdata, mid):
receivedMessages.append(mid)
# connect the client to Cumulocity and register a device
client = mqtt.Client(clientId)
client.username_pw_set(tenant + "/" + username, password)
client.on_message = on_message
client.on_publish = on_publish
client.connect(serverUrl)
client.loop_start()
publish("s/us", "100," + device_name + ",c8y_MQTTDevice", True)
publish("s/us", "110,S123456789,MQTT test model,Rev0.1")
publish("s/us", "114,c8y_Restart")
print("Device registered successfully!")
client.subscribe("s/ds")
sendMeasurements()