-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp.py
28 lines (21 loc) · 802 Bytes
/
temp.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
import paho.mqtt.client as mqtt
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM) # Set pin numbering mode to BCM
def toggle_pin(pin):
GPIO.setup(pin, GPIO.OUT) # Set the pin as an output
GPIO.output(pin, not GPIO.input(pin))
# 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))
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
if "TOGGLE" in msg.payload:
toggle_pin(18)
print("Toggleing pin 18")
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("localhost", 1883, 60)
client.subscribe("house/light")
client.loop_forever()