Digital Output Raspberry Python


#1

This is my code :

import cayenne.client #Cayenne MQTT Client from gpiozero import LED
led=LED(17) #Declaring button pin 17

#Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
MQTT_USERNAME = “8a58db60-”
MQTT_PASSWORD = “8aaf623135c1f2e6fe6de2d3b1a88”
MQTT_CLIENT_ID = “d5bfb780-ca3-552aaa” # The callback for when a message is received from Cayenne.
def on_message(message):
print("message received: " + str(message))
if message.channel==1: #Dashboard Led widget channel. They must be same.
if message.value==“1”: #If led command “1”, turn led on(message.value must be string)
led.on()
elif message.value==“0”: #If led command “0”, turn led off(message.value must be string)
led.off()

client = cayenne.client.CayenneMQTTClient()
client.on_message = on_message #When message recieved from Cayenne run on_message function
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID)

while True:
client.loop()

root@osmc:/home/osmc# sudo python guzik.py
Traceback (most recent call last):
File “guzik.py”, line 2, in
led=LED(17) #Declaring button pin 17
NameError: name ‘LED’ is not defined

Where is problem thx


#2

first try this https://raspi.tv/2013/rpi-gpio-basics-5-setting-up-and-using-outputs-with-rpi-gpio and get the output without the cayenne code.


#3

LED just about to switch off
LED just about to switch on
LED just about to switch off
LED just about to switch on
LED just about to switch off
:slight_smile:


#4

now looking at the LED code, add there the lines which are used to turn ON and OFF in the cayenne code when you receive data from the button:

def on_message(message):
    print("message received: " + str(message))
    if (message.channel == 4) and (message.value == "1"):
        #code to turn ON the led

#5

import cayenne.client
MQTT_USERNAME = “”
MQTT_PASSWORD = “”
MQTT_CLIENT_ID = “”

def on_message(message):
print("message received: " + str(message))
if (message.channel == 1) and (message.value == “1”):
print(“guzik on”)
elif (message.value==“0”):
print(“guzik off”)

client = cayenne.client.CayenneMQTTClient()
client.on_message = on_message
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID)

while True:
client.loop()

root@osmc:/home/osmc# sudo python guzik.py
File “guzik.py”, line 9
print(“guzik on”)
^
IndentationError: expected an indented block


#6

you need to have indented space left in python or you will get error. Have a look at this Have a look at this https://www.python-course.eu/python3_blocks.php