diff --git a/README.md b/README.md index 5bc9ecfc968d30053d291d3d76d0efe69cfa3378..a1dc1311defec32375b55dce30ffb55a53e77b0a 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,10 @@ pip install --extra-index-url https://package-read:gkYP4xrm2PxicUbW1wra@git-ce.r ## Usage +### Running a mosquitto broker locally + +Extract the Mosquitto.zip folder and start the `start_mosquitto` script. + ### Publish messages ```python diff --git a/sample/publish.py b/sample/publish.py index 67d3b91262acee9af5561e98b337dfdd58c7a17d..68d87d4326c25f2438f8ea179561f4d63c8f5b81 100644 --- a/sample/publish.py +++ b/sample/publish.py @@ -1,13 +1,45 @@ import datetime import json import random +import numpy as np import time import uuid from wzl import mqtt +### Time series creator + +""" Constants altering the course of the exponential decay time series. Feel free to change them :) """ +T0 = 75 # Inital tem perature in degrees +tau = 180 # decay factor in s + +def exponential_decay(t, A0, tau, noise_factor=0.5): + """ + Virtual sensor simulating a measurement of an expoentially decaying quantity, such as the temperature in a coffee mug :) + t := time step, int + A0 := Initial amplitude at t=0 + tau := decay constant + noise_factor := noise amplitude control (0 means no noise!) + + """ + 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 = "" MQTT_PASSWORD = "" @@ -15,23 +47,40 @@ MQTT_BROKER = "127.0.0.1" MQTT_PORT = 1883 MQTT_VHOST = "/" -### to connect to the central MQTT-Broker of MQ-MS use the following settings: -# MQTT_BROKER = "wzl-mbroker01.wzl.rwth-aachen.de" -# MQTT_PORT = 1883 -# MQTT_VHOST = "metrology" +## TODO 1: Change the topic and observe what happens in the receiver output +## TODO 2: Change the topic to soil-conform format +MQTT_TOPIC = "IoP_Workshop" + +## TODO 3: Add a soil-conform status topic +MQTT_TOPIC_Status ="" + ### Ask for settings and individual credentials ### +# time steps +t = 0 # s if __name__ == "__main__": - client = mqtt.MQTTPublisher() + # The publisher and receiver classes may consider a prefix, that is put in front of each topic + ## TODO 1: Change the prefix and observe what happens + ## 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 = json.dumps({"value": [random.uniform(0, 5) for i in range(3)], "timestamp": datetime.datetime.utcnow().isoformat() + "Z", - "covariance": [[2, 0, 0], [0, 2, 0], [0, 0, 0]], "nonce": str(uuid.uuid4()), "hash": None, "unit": "MTR"}) - client.publish("/channel-001", message.encode("utf-8"), 0) + # 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 = "Hello IoP" + 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 + """ Fill in here """ + + # increase time step variable + t += 1 except KeyboardInterrupt: break