Skip to content

Instantly share code, notes, and snippets.

@AgustinParmisano
Last active December 3, 2018 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AgustinParmisano/1f70aac52c5d6b06711796ba5138f3ce to your computer and use it in GitHub Desktop.
Save AgustinParmisano/1f70aac52c5d6b06711796ba5138f3ce to your computer and use it in GitHub Desktop.
sensor simulation in mili time ranges
import time
import random
import paho.mqtt.client as mqtt
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("test/data")
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("localhost", 1883, 60)
delay_ciclo = 1000
while True:
start = int(round(time.time() * 1000))
start_time = time.time()
i = 0
o = 0
j = 0
interval = 1
prev = 0
mayor=[]
minor=[]
summax = 0
summin = 0
now = int(round(time.time() * 1000))
muestras = 25
#time.sleep(1)
for e in range(0,muestras):
maxr = 0
minr = 1000
now = int(round(time.time() * 1000))
while (int(round(time.time() * 1000) - now ) < 20):
j+=1
if (j > delay_ciclo):
r=random.randint(0,10000)
o+=1
j = 0
if maxr < r:
maxr = r
if minr > r:
minr = r
i+=1
summax += maxr
summin += minr
print("Cantidad de muestras: " + str())
print("Ciclos posibles en 20 milisegundos:" + str(i))
print("Ciclos en 20 milisegundos con espera de " + str(delay_ciclo) + " por ciclo:" + str(o))
resultmax = summax / 50
resultmin = summin / 50
print(resultmax)
print(resultmin)
result = (resultmax + resultmin) / 2
client.publish("test/data", result)
print("--- %s seconds ---" % (time.time() - start_time))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment