Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import RPi.GPIO as GPIO
- import time
- import paho.mqtt.client as mqtt
- import json
- username = "43807467-bbbf-4cd0-9d90-b13d454209d2"
- password ="Orh4r16u+dntVFyNRpjpuAyYGE+hZCug906oRVV1VM0="
- controllTopic = "xi/blue/v1/93336b3c-49f4-44a5-885f-5cb31504dea7/d/43807467-bbbf-4cd0-9d90-b13d454209d2/device-log"
- publishTopic = "xi/blue/v1/93336b3c-49f4-44a5-885f-5cb31504dea7/d/7d7afa69-bda6-4219-a9ae-633262a81cfb/device-log"
- publishPayload = "{\"default\": \"message message\"}"
- # The callback for when the client receives a CONNACK response from the server.
- def on_connect(client, userdata, 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("$SYS/#")
- client.subscribe(controllTopic)
- client.subscribe(publishTopic)
- # The callback for when a PUBLISH message is received from the server.
- def on_message(client, userdata, msg):
- global publishTopic
- global publishPayload
- print(msg.payload)
- parsedMessage = json.loads(msg.payload)
- if 'channel' in parsedMessage:
- publishTopic = json.dumps(parsedMessage['channel']).replace("\"", "")
- if 'message-by-device' in parsedMessage:
- publishPayload = json.dumps(parsedMessage['message-by-device'])
- if 'led_blink_count' in parsedMessage and msg.topic == publishTopic:
- print('here you go')
- for i in range(0, parsedMessage['led_blink_count']):
- GPIO.output(20, GPIO.HIGH)
- time.sleep(1)
- GPIO.output(20, GPIO.LOW)
- time.sleep(1)
- def on_publish(client, userdata, mid):
- print("message sent")
- client = mqtt.Client()
- client.on_connect = on_connect
- client.on_message = on_message
- client.on_publish = on_publish
- client.username_pw_set(username, password)
- client.tls_set("gs_root.cer")
- client.connect("broker.demo.xively.com", 8883, 60)
- client.loop_start()
- # Handling button press on Raspbery PI
- GPIO.setmode(GPIO.BCM)
- GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_UP)
- GPIO.setmode(GPIO.BCM)
- GPIO.setup(20, GPIO.OUT)
- GPIO.output(20, GPIO.LOW)
- while True:
- input_state = GPIO.input(21)
- if input_state == False:
- print('Button Pressed')
- client.publish(publishTopic, publishPayload, qos=0)
- time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement