<jupyter_start><jupyter_text>Simulate Sensor Measurement Data and Publish it to an MQTT Broker.For testing pruposes of IoT systems and MQTT data transmission it is often convenient to simulate sensor data.The "sensor" in this case is just a sinosoidal function. Parameters are frequency (in Hz), phase, amplitude and amplidue offset. The variable is time (in secs).  The sensor has an ID and whenever a sensor measurement is taken the value is transmitted together with sensor_id and timestamp. The timestamp is the wallclock time (system time) of the computer the Python code is executed on.The message payload is a JSON string (Python dictionary string). It looks like this:```{"sensor_id": 22, "ts": "2020-12-04T14:21:56.550990CET", "val": 0.0}```  The Timestamp The timestamp represents the local time of the measurement. It is transmitted as an ISO time string.<jupyter_code>from datetime import datetime
 ts = datetime.now()
ts.timestamp()<jupyter_output><empty_output><jupyter_text>Time Zones Central European Time (UTC+1):https://www.timeanddate.com/time/zones/cet Central European Summer Time (UTC+2):https://www.timeanddate.com/time/zones/cest<jupyter_code>ts.isoformat()+"CET"<jupyter_output><empty_output><jupyter_text>MQTT BrokerThere are several possibilities to use an MQTT broker:1. Use eclipse publically accessible broker1. Install mosquitto MQTT broker locally, i.e. on your computer1. Use another dedicated MQTT broker, e.g. hsrw.space:1883  Python MQTT Client PackageIn case you have to install it first in Anadonda open an Anaconda prompt and execute```conda install -c conda-forge paho-mqtt```<jupyter_code>import paho.mqtt.client as mqtt<jupyter_output><empty_output><jupyter_text>MQTT Broker Connection Parameters<jupyter_code>broker = "hsrw.space"
port=1883

username = "user"
password = "<PASSWORD>"

myname = "john_doe"
#myname = "mickey_mouse"

# this should be a unique ID.
sensor_id = 22

topic = "/emrp2020/"+myname+"/sensor_sim_db"
print("Topic: ", topic)<jupyter_output>Topic:  /emrp2020/john_doe/sensor_sim_db<jupyter_text>The code to return a single sine wave<jupyter_code>#import math
def mysin(t=0,A0=1,f=1,k=0,phi=0):
    return A0*math.sin(2*math.pi*f*t)+k<jupyter_output><empty_output><jupyter_text>MQTT Publisher Code<jupyter_code>def on_publish(client,userdata,result):             #create function for callback
    print("data published")
    pass

#client1= mqtt.Client(client_id = "Prof. B.'s Publisher")           #create client object
client1= mqtt.Client()           #create client object
client1.on_publish = on_publish                          #assign function to callback

client1.username_pw_set(username = username, password = password)
client1.connect(broker,port) #establish connection

from time import sleep
import math
import json


dt = 1 # sample period (delay) in secs

A1 = 1
f1 = 1/20. # Hz

t = 0 # sec

while(True):
#for i in range(5):
    
    val = mysin(t,A1,f1)
    
    ts = datetime.now().isoformat()+"CET"
    #    ms = int(datetime.now().timestamp()*1000) # milliseconds!
    
    data = {"sensor_id": 22, "ts" : ts, "val" : val}
    
    msg = json.dumps(data)
    print("msg: ", msg)
    ret = client1.publish(topic,msg) 
    print("ret: ", ret)
    
    t += dt
    sleep(dt) # sleep 1 sec
    
ret = client1.disconnect()
print("ret: ", ret)<jupyter_output>msg:  {"sensor_id": 22, "ts": "2020-12-14T15:55:22.891736CET", "val": 0.0}
data published
ret:  (0, 1)
msg:  {"sensor_id": 22, "ts": "2020-12-14T15:55:23.895621CET", "val": 0.3090169943749474}
data published
ret:  (0, 2)
msg:  {"sensor_id": 22, "ts": "2020-12-14T15:55:24.896612CET", "val": 0.5877852522924731}
data published
ret:  (0, 3)
msg:  {"sensor_id": 22, "ts": "2020-12-14T15:55:25.898797CET", "val": 0.8090169943749475}
data published
ret:  (0, 4)
msg:  {"sensor_id": 22, "ts": "2020-12-14T15:55:26.900020CET", "val": 0.9510565162951535}
data published
ret:  (0, 5)
msg:  {"sensor_id": 22, "ts": "2020-12-14T15:55:27.901468CET", "val": 1.0}
data published
ret:  (0, 6)
msg:  {"sensor_id": 22, "ts": "2020-12-14T15:55:28.905690CET", "val": 0.9510565162951536}
data published
ret:  (0, 7)
msg:  {"sensor_id": 22, "ts": "2020-12-14T15:55:29.906803CET", "val": 0.8090169943749475}
data published
ret:  (0, 8)
msg:  {"sensor_id": 22, "ts": "2020-12-14T15:55:30.907594CET", "val": 0.58778525229247[...]
StarcoderdataJupyterStructuredCleanDedup