-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpython_client.py
More file actions
59 lines (51 loc) · 1.56 KB
/
python_client.py
File metadata and controls
59 lines (51 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# !/usr/bin/env python
# -*- coding: utf-8 -*-
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
import time
from itertools import count
def reply():
global count
publish_mqtt('I heard you!')
count = 0
# 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("uicomm/j-p")
# The callback for when a message is received from the server.
def on_message(client, userdata, msg):
print("recieved: "+msg.topic+" "+str(msg.payload))
words = msg.payload.split()
if words[0] == 'run': # If 'run' is the first word of the message
globals()[words[1]]() # Run the function named by the second word in the message
def publish_mqtt(payload):
"""
Send an MQTT mesage to javaScript client
"""
topic = 'uicomm/p-j'
try:
publish.single(topic, payload, hostname='127.0.0.1', port=1883, retain=False, qos=0)
except Exception as err:
print "Couldn't publish :" + str(err)
pass
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("127.0.0.1")
#Start Loop
client.loop_start()
count = 0
try:
while True:
publish_mqtt("message number " + str(count) )
count += 1
time.sleep(2)
except Exception as err:
print 'Error: ', err
finally:
print ''
print 'Cleaning up'
client.loop_stop()
# GPIO.cleanup() anyone?