ConnectionRefusedError:[Errno 111] Raspberry Pi连接被拒绝

时间:2018-04-19 18:35:22

标签: python raspberry-pi mqtt

我正在尝试使用覆盆子pi工作的pi相机,我得到了这个连接拒绝错误(见标题)。我尝试了traceroute测试,以确保它成功连接到服务器,但由于某种原因它没有连接,即使traceroute成功地从我的本地计算机进入iot服务器。这是代码。

camera.py:11: RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.
  GPIO.setup(LED, GPIO.OUT)
Traceback (most recent call last):
  File "camera.py", line 42, in <module>
    client.connect(BROKER, PORT, 60)
  File "/usr/local/lib/python3.5/dist-packages/paho/mqtt/client.py", line 768, in connect
    return self.reconnect()
  File "/usr/local/lib/python3.5/dist-packages/paho/mqtt/client.py", line 895, in reconnect
    sock = socket.create_connection((self._host, self._port), source_address=(sf._bind_address, 0))
  File "/usr/lib/python3.5/socket.py", line 712, in create_connection
    raise err
  File "/usr/lib/python3.5/socket.py", line 703, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused"

python代码:

import picamera
import time
import RPi.GPIO as GPIO
import paho.mqtt.client as mqtt
import hashlib

LED =  16
SWITCH = 20

GPIO.setmode(GPIO.BCM)
GPIO.setup(LED, GPIO.OUT)
GPIO.setwarnings(False)

BROKER = "some broker, taken out for security "
PORT = 1883
QOS = 1
data_block_size = 2000

STATE1 = 0
STATE2 = 1
STATE3 = 2
STATE4 = 3
state = STATE1
GPIO.setup(SWITCH, GPIO.IN, pull_up_down=GPIO.PUD_UP)
camera = picamera.PiCamera()

def my_callback(channel):
    global state
    if(state== STATE1):
        state = STATE2

def on_publish(client, userdata, result):
    print("image published")

GPIO.add_event_detect(SWITCH, GPIO.FALLING, callback=my_callback,bouncetime=200)

client = mqtt.Client()
client.connect(BROKER, PORT, 60)
client.on_publish = on_publish
client.subscribe("xxxxxphotobooth")
client.loop_start
try:
    while True:
        if(state == STATE2):
            blink_time = 0.5
            for count in range(10):
                GPIO.output(LED, True)
                time.sleep(blink_time)
                GPIO.output(LED, False)
                time.sleep(blink_time)
                blink_time += 0.05
                state = STATE3
        elif(state == STATE3):
            camera.start_preview()
            time.sleep(3)
            camera.capture('image.jpg')
            camera.stop_preview()
            GPIO.output(LED, True)
            time.sleep(1)
            GPIO.output(LED, False)
            state = STATE4
        elif(state == STATE4):
            #upload to broker
            f=open("image.jpg", "rb")
            fileContent = f.read()
            byteArr = bytearray(fileContent)
            client.publish("xxxxx/photobooth", byteArr, 0)
            state = STATE1

except KeyboardInterrupt:
    GPIO.cleanup()
    client.disconnect()

0 个答案:

没有答案
相关问题