add example code generator
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
# 1
|
||||
import paho.mqtt.client as mqtt
|
||||
# 2
|
||||
mqttBroker = "test.mosquitto.org"
|
||||
|
||||
class TemperatureServiceClient:
|
||||
def __init__(self):
|
||||
# 3
|
||||
self.client = mqtt.Client()
|
||||
# 4
|
||||
self.client.connect(mqttBroker)
|
||||
|
||||
|
||||
def sendTemperatureChange(self, id):
|
||||
# 5
|
||||
topic = "temperature/changed"
|
||||
# 6
|
||||
self.client.publish(topic, id)
|
||||
@@ -0,0 +1,16 @@
|
||||
from client import TemperatureServiceClient
|
||||
from random import randrange
|
||||
import time
|
||||
|
||||
client = TemperatureServiceClient()
|
||||
|
||||
id_length = 8
|
||||
min_value = 10**(id_length-1) # Minimum value with 8 digits (e.g., 10000000)
|
||||
max_value = 10**id_length - 1 # Maximum value with 8 digits (e.g., 99999999)
|
||||
|
||||
while True:
|
||||
randomId = randrange(min_value, max_value + 1)
|
||||
client.sendTemperatureChange(randomId)
|
||||
print("New temperature detected " + str(randomId) + " sent to temperature/changed")
|
||||
time.sleep(1)
|
||||
|
||||
Reference in New Issue
Block a user