#!/usr/bin/env python import cayenne.client import time import logging import RPi.GPIO as GPIO from time import sleep # Cayenne authentication info. This should be obtained from the Cayenne Dashboard. MQTT_USERNAME = "xxxx" MQTT_PASSWORD = "xxx" MQTT_CLIENT_ID = "1xxxx" def SetAngle(angle): duty = angle / 18 + 2 GPIO.output(13, True) pwm.ChangeDutyCycle(duty) sleep(1) GPIO.output(13, False) pwm.ChangeDutyCycle(0) # The callback for when a message is received from Cayenne. def on_message(message): print("message received: " + str(message)) # If there is an error processing the message return an error string, otherwise return nothing. if (message.channel == 5) and (message.value == "0"): SetAngle(0) if (message.channel == 5) and (message.value == "1"): SetAngle(90) client = cayenne.client.CayenneMQTTClient() client.on_message = on_message client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, loglevel=logging.INFO) # For a secure connection use port 8883 when calling client.begin: # client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, port=8883, loglevel=logging.INFO) i = 0 timestamp = 0 GPIO.setmode(GPIO.BOARD) GPIO.setup(13, GPIO.OUT) pwm = GPIO.PWM(13, 50) pwm.start(0) sleep(2) while True: client.loop() if (time.time() > timestamp + 10): client.celsiusWrite(1, i) client.luxWrite(2, i*10) client.hectoPascalWrite(3, i+800) timestamp = time.time() i = i+1