-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt_computer.py
60 lines (53 loc) · 1.82 KB
/
mqtt_computer.py
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
60
import sys, os
import paho.mqtt.client as mqtt
import time
# try:
# import RPi.GPIO as GPIO
# except RuntimeError:
# print("Error importing RPi.GPIO! This is probably because you need superuser privileges. You can achieve this by using 'sudo' to run your script")
TOPIC_PATH="/omnihacks/record/10"
OFF=0
ON=1
CHANNELS = [3,5,7,8,10,11,12,13,15,16,18,19,21,22,23,24,26,29,31,32,33,35,36,37]
DEBUG = False
def dbg(msg):
global DEBUG
if DEBUG:
print(msg)
sys.stdout.flush()
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
dbg("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
dbg("Subscribing to " + TOPIC_PATH)# +"#")
client.subscribe( TOPIC_PATH)# +"#")
# Function that abstracts on/off condition for GPIO state
# def gpioStateFor(on):
# if on:
# return GPIO.HIGH
# return GPIO.LOW
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print("received")
f = open('new_doctor.wav', 'wb')
f.write(msg.payload)
f.close()
# os.system("afplay new_doctor.wav")
if len(sys.argv) > 1 and sys.argv[1] == '-d':
DEBUG = True
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
print("connecting")
client.connect("mqtt.eclipse.org")
print("connected")
# GPIO.setmode(GPIO.BOARD)
# GPIO.setwarnings(False)
# GPIO.setup(CHANNELS, GPIO.OUT)
# GPIO.output(CHANNELS, gpioStateFor(OFF))
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()