diff --git a/sample/publish_sample.py b/sample/publish_sample.py new file mode 100644 index 0000000000000000000000000000000000000000..9f763a33c097f26fa135851ec90344816412b3f0 --- /dev/null +++ b/sample/publish_sample.py @@ -0,0 +1,80 @@ +import datetime +import json +import random +import numpy as np +import time +import uuid +import os + +from wzl import mqtt + +### Time series creator + +T0 = 75 # Inital tem perature in degrees +tau = 180 # decay factor in s + +def exponential_decay(t, A0, tau, noise_factor=0.5): + value = (A0-21) * np.exp(-t / tau) + 21 + value += np.random.normal(0, noise_factor * np.sqrt(value)) + return value + +### Function to embed sensor value into MQTT2Influx bridge format + +def soil_msg(value, unit="MTR", nonce = dict(uuid=str(uuid.uuid4()))): + """ + value := sensor value, list + unit := unit of the value, str + nonce := arbitrary dictionary of metadata, dict + """ + return json.dumps({"value": [value], "timestamp": datetime.datetime.utcnow().isoformat() + "Z", + "nonce": nonce, "hash": None, "unit": unit}) + +### Ask for settings and individual credentials ### + +## TODO 2: Use your credentials and the correct config for connecting to the MQTT broker at WZL +MQTT_USER = os.environ["MQTT_USER"] # to be changed to your individual credentials or save your credentials in an environment variable called MQTT_USER, too +MQTT_PASSWORD = os.environ["MQTT_PASSWORD"] # to be changed to your individual credentials or save your credentials in an environment variable called MQTT_PASSWORD, too + +# MQTT_BROKER = "127.0.0.1" +MQTT_PORT = 1883 + +## TODO 1: Change the topic and observe what happens in the receiver output +## TODO 2: Change the topic to soil-conform format +MQTT_TOPIC = "OBJ-Env/VAR-Temperature" + +## TODO 3: Add a soil-conform status topic +MQTT_TOPIC_Status ="OBJ-Env/VAR-Status" + +### to connect to the central MQTT-Broker of MQ-MS use the following settings: +MQTT_BROKER = "134.130.175.204" +MQTT_VHOST = "metrology" + +### Ask for settings and individual credentials ### + +# time steps +t = 0 # s + +if __name__ == "__main__": + # The publisher and receiver classes may consider a prefix, that is put in front of each topic + ## TODO 2: Add the WS b24 prefix to the Publisher + client = mqtt.MQTTPublisher(prefix="b24") + client.connect(MQTT_BROKER, MQTT_PORT, MQTT_VHOST + ":" + MQTT_USER, MQTT_PASSWORD) + + while True: + try: + # Message field + ## TODO Task 1: Change the message string and observe what happens with the transmitted message + ## TODO Task 2: a) Use the soil_msg function defined above to bring your message into the soil-conform format. + ## b) Use the exponential decay function from above and and pass a meaningful unit and a dictionary that includes a keyword "name" and as value your name + message = soil_msg(exponential_decay(t, T0, tau), "K", nonce=dict(name="Dominik")) + client.publish(MQTT_TOPIC, message.encode("utf-8"), 0) + + time.sleep(1) + ## TODO Task 3: Create a second soil message that transmits the status of your demonstrator (0 = running, 1 = Off, 2=Maintenance) to the broker and publish it under a dedicated topic + message = soil_msg(1, "", nonce=dict(name="Dominik")) + client.publish(MQTT_TOPIC_Status, message.encode("utf-8"), 0) + + # increase time step variable + t += 1 + except KeyboardInterrupt: + break