【问题标题】:raspberry pi led not defined error awsiot mqtt树莓派导致未定义错误awsiot mqtt
【发布时间】:2018-03-01 19:08:40
【问题描述】:

我正在尝试通过 AWSIoT 点亮 raspberrypi 上的 LED,但我不断收到此错误

文件“mypub.py”,第 71 行,在
GPIO.output(led21,GPIO.HIGH)
连接返回结果:0
NameError: name 'led21' is not defined

谁能帮忙解决这个问题?

我正在玩 MQTT,试图点亮 LED 并最终与其他传感器交互。

我正在使用从here 获得的代码

# Publishes the Data to AWS IoT

import os
import socket
import ssl
import paho.mqtt.client as mqtt   # Import the Paho-MQTT Client
from time import sleep            # Import sleep from time for delays
import RPi.GPIO as GPIO           # Import GPIO Library for Raspberry Pi

connection_flag = False           # Initialize the Connection Flag as False    

GPIO.setmode(GPIO.BCM)            # Set Mode of RPi Pins i.e Broadcom or Chipset

# Define LED
led = 21                          # Put LED Numbers

# Set Pin as Output
GPIO.setup( led, GPIO.OUT )       # Pin 21
GPIO.output(led, GPIO.LOW )       # Initialize LED's

def on_connect(client, userdata, flags, rc): # on_connect()-Event handler
    global connection_flag        # global to check if the Connection to AWS Cloud has been Made.
    connection_flag = True
    print( "Connection returned result: " + str( rc ) )

def on_message(client, userdata, msg):       # on_message()-Event handler
    print( msg.topic + " " + str( msg.payload ) )

mqttc = mqtt.Client()             # Initiate Paho-MQTT Client
mqttc.on_connect = on_connect     # .set on_connect Event handler
mqttc.on_message = on_message     # .set on_message

# Define the AWS Host Key  ; Thing Name defined in AWS IoT; Root Certificate Path; Certificate Path; Private Key Certificate Path  
awshost   = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
awsport   =  8883                 # AWS Port( Default: 8883 )
clientId  = "raspberrypi"         # Client ID
thingName = "raspberrypi"         # Thing Name defined in AWS IoT
caPath    = "root-CA.crt"         # Root Certificate Path
certPath  = "raspberrypi.cert.pem"     # Certificate Path
keyPath   = "raspberrypi.private.key"  # Private Key Certificate Path

# Configure network encryption and authentication options
# Enable SSL/TLS support
mqttc.tls_set(caPath, certfile=certPath, keyfile=keyPath, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None)

mqttc.connect(awshost, awsport, keepalive=60) # Connect to AWS Host
mqttc.loop_start()

while True:
    if connection_flag == True:
        GPIO.output( led21, GPIO.HIGH )

        # Update LED Data on AWS IoT in Real Time
        # State tells the current and previous state of LED
        # Reported gives the timestamp
        jsonMessage = "{  \"state\": { \"reported\": { \"Led\": " + str(state) + "} } }"
        mqttc.publish( "$aws/things/raspberrypi/shadow/update",
                        jsonMessage,
                        qos = 1
                        )
        mqttc.publish( "LED: ",         # Publish the Data to AWS IOT and get it on Subscription
                        state,
                        qos = 1
                        )
        print( "LED: " + "%d" % state )
        sleep( 1.0 )

        GPIO.output( led21, GPIO.HIGH ) # Update LED Data on AWS IoT in Real Time
        jsonMessage = "{ \"state\": { \"reported\": { \"Led\": " + str(state) + "} } }"
        mqttc.publish( "$aws/things/raspberrypi/shadow/update",
                        jsonMessage,
                        qos = 1
                        )
        sleep( 1.0 )
        mqttc.publish( "LED: ",        # Publish the Data to AWS IOT and get it on Subscription
                        state,
                        qos = 1
                        )
        print( "LED: " + "%d" % state )
   else:
        print( "Waiting for Connection..." )

ser.close()
GPIO.cleanup()

【问题讨论】:

    标签: python mqtt led


    【解决方案1】:

    简单:

    您的初始设置关联了一个符号 led,而不是 led21

    ...
    # Define LED
    # Put LED Numbers
    led = 21
    

    而代码的后面部分使用了未定义的符号led21

    GPIO.output( led21, GPIO.HIGH )
    

    替换名称以使其变得“连贯”并且瞧... :o)

    【讨论】:

    • 干杯伙伴,我是个盲人,但在修复后我得到一个新错误 NameError: name 'state' is not defined
    • 简单:定义state = "not defined yet"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    • 2015-11-26
    • 2017-08-09
    • 2018-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多