git clone https://github.com/myDevicesIoT/Cayenne-MQTT-Python
cd Cayenne-MQTT-Python
sudo python setup.py install
then create a new device Bring your own thing on your cayenne dashboard and get the MQTT credentials and add them in below code.
#!/usr/bin/env python
import cayenne.client
import time
import logging
import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library
GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set pin 10 to be an input pin and set initial value to be pulled low (off)
# Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
MQTT_USERNAME = "MQTT_USERNAME"
MQTT_PASSWORD = "MQTT_PASSWORD"
MQTT_CLIENT_ID = "MQTT_CLIENT_ID"
TRIGGER_CHANNEL = 2 #Virtual channel for publishing the trigger value.
THRESHOLD = 0 #Threshold for the trigger.
i=0
timestamp = 0
send_below_threshold = False #Set to true if the trigger should happen when the data value is below the threshold,
#false if it should happen when the data value is above or equal to the threshold.
crossed_threshold = False
# 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.
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)
def send_trigger_value(trigger_channel, sensor_value, threshold, send_below_threshold):
global crossed_threshold
if (((sensor_value >= threshold) and not send_below_threshold) or ((sensor_value < threshold) and send_below_threshold)):
if not crossed_threshold:
client.virtualWrite(trigger_channel, 1, "digital_sensor", "d")
crossed_threshold = True
else:
client.virtualWrite(trigger_channel, 0, "digital_sensor", "d")
crossed_threshold = False
while True:
client.loop()
if (time.time() > timestamp + 10):
sensor_value = GPIO.input(10)
send_trigger_value(TRIGGER_CHANNEL, sensor_value, THRESHOLD, send_below_threshold)
timestamp = time.time()
Hi, thanks for your prompt reply, I have installed python, created a input12.py with this code and MQTT username, password and client_id, changed trigger channel for the channel where I will test this… nothing more changed.
I have chmod +x and executed input12.py and cayenne.mydevices detected the “new device”… now I think I need to create a new sensor on this new device… but only I can select the Raspberry Pi… I can´t select “new device” to create the sensor…
On other side, I think I need a python code and “new device” for all inputs, right? And I need to add to Raspbian startup, Am I right?
is your sensor a push button or a switch? if it is switch try this simpler code which sends data only on state change and will send trigger only once the state is changed.
#!/usr/bin/env python
import cayenne.client
import time
import logging
import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library
GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(40, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set pin 10 to be an input pin and set initial value to be pulled low (off)
# Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
MQTT_USERNAME = "xxx"
MQTT_PASSWORD = "xxx"
MQTT_CLIENT_ID = "xxx"
TRIGGER_CHANNEL = 2 #Virtual channel for publishing the trigger value.
i=0
timestamp = 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.
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)
previousState = -1
currentState = -1
while True:
client.loop()
if (time.time() > timestamp + 5):
currentState = GPIO.input(40)
print(currentState)
if (currentState != previousState):
client.virtualWrite(TRIGGER_CHANNEL, currentState, "digital_sensor", "d")
previousState = currentState
timestamp = time.time()
when you change the state of the switch, the data will be published only once on the dashboard.
you can edit the same python code and all the sensors code in it.