Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
GO-4-UR-GOAL-MQTT-Integrative-Project/sensor 5&6.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
97 lines (87 sloc)
3.34 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#join at line 120 | |
def update_status(client,status): | |
status=status_upper() | |
if status==states[0] or status==states[1]: #valid status | |
client.sensor_status=status #update_status | |
print("updating status",client.sensor_status) | |
def publish_status(client): | |
global start_flag #used to publish on start_flag | |
pubflag=False | |
if start_flag: | |
start_flag=False | |
pubflag=True | |
if time.time()-client.last_pub_time >=options["interval_pub"]: | |
pubflag=True | |
if time.time()-client.last_pub_time >=options["interval"] and chatty: | |
pubflag=True | |
logging.debug("old "+str(client.sensor_status_old)) | |
logging.debug("new "+ str(client.sensor_status)) | |
if client.sensor_status_old!=client.sensor_status or pubflag: | |
client.publish(sensor_status_topic,client.sensor_status,0,True) | |
print("publish on",sensor_status_topic,\ | |
" message ",client.sensor_status) | |
client.last_pub_time=time.time() | |
client.sensor_status_old=client.sensor_status | |
def Initialise_client_object(): | |
mqtt.Client.last_pub_time=time.time() | |
mqtt.Client.topic_ack=[] | |
mqtt.Client.run_flag=True | |
mqtt.Client.subscribe_flag=False | |
mqtt.Client.sensor_status=states[1] | |
mqtt.Client.sensor_status_old=None | |
mqtt.Client.bad_connection_flag=False | |
mqtt.Client.connected_flag=False | |
mqtt.Client.disconnect_flag=False | |
mqtt.Client.disconnect_time=0.0 | |
mqtt.Client.disconnect_flagset=False | |
mqtt.Client.pub_msg_count=0 | |
def Initialise_clients(cname): | |
#flags set | |
client= mqtt.Client(cname) | |
if mqttclient_log: #enable mqqt client logging | |
client.on_log=on_log | |
client.on_connect= on_connect #attach function to callback | |
client.on_message=on_message #attach function to callback | |
client.on_disconnect=on_disconnect | |
#client.on_subscribe=on_subscribe | |
#client.on_publish=on_publish | |
return client | |
def Connect(client,broker,port,keepalive,run_forever=False): | |
"""Attempts connection set delay to >1 to keep trying | |
but at longer intervals """ | |
connflag=False | |
delay=5 | |
#print("connecting ",client) | |
badcount=0 # counter for bad connection attempts | |
while not connflag: | |
logging.info("connecting to broker "+str(broker)) | |
print("connecting to broker "+str(broker)+":"+str(port)) | |
print("Attempts ",badcount) | |
try: | |
res=client.connect(broker,port,keepalive) #connect to broker | |
if res==0: | |
connflag=True | |
return 0 | |
else: | |
logging.debug("connection failed ",res) | |
badcount +=1 | |
if badcount>=3 and not run_forever: | |
return -1 | |
raise SystemExit #give up | |
elif run_forever and badcount<3: | |
delay=5 | |
else: | |
delay=30 | |
except: | |
client.badconnection_flag=True | |
logging.debug("connection failed") | |
badcount +=1 | |
if badcount>=3 and not run_forever: | |
return -1 | |
raise SystemExit #give up | |
elif run_forever and badcount<3: | |
delay=5*badcount | |
elif delay<300: | |
delay=30*badcount | |
time.sleep(delay) | |
return 0 | |