Python客户端错误:[Errno 111]连接被拒绝

时间:2018-01-30 05:52:10

标签: python django sockets arduino

我真的不知道问题,但我收到一个错误:“连接被拒绝”。我不知道该怎么办请帮帮我。我正在尝试在Android应用程序上按下按钮时发送计数,并且我使用nanpy连接到arduino但是我收到了错误。

import socket
from nanpy import (ArduinoApi, SerialManager)


ledpin = 13
buttonpin = 12
buttonpin1 = 2
buttonpin2 = 3
buttonstate1 = 1
buttonstate2 = 1
buttonstate3 = 1

connection = SerialManager()
a = ArduinoApi(connection = connection)

a.pinMode(ledpin, a.OUTPUT)
a.pinMode(buttonpin, a.INPUT)
a.pinMode(buttonpin1, a.INPUT)
a.pinMode(buttonpin2, a.INPUT)


ADDR2 = ('192.168.1.7', 21568)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(ADDR2)

while True:

    try:
        buttonstate1 = a.digitalRead(buttonpin)
        buttonstate2 = a.digitalRead(buttonpin1)
        buttonstate3 = a.digitalRead(buttonpin2)             
        if buttonstate1:
            a.digitalWrite(ledpin, a.HIGH)
        else:
            a.digitalWrite(ledpin, a.LOW)
            add += 1
            s = str(add)
            sock.sendall(s)
            time.sleep (.5)

        if buttonstate2:                       
            a.digitalWrite(ledpin, a.HIGH)
        else:
            a.digitalWrite(ledpin, a.LOW)
            add += 1
            s = str(add)
            sock.sendall(s)
            time.sleep (.5)
        if buttonstate3:
            a.digitalWrite(ledpin, a.HIGH)
        else:
            a.digitalWrite(ledpin, a.LOW)
            add += 1
            s = str(add)
            sock.sendall(s)
            time.sleep (.5)                                             
    finally:

        sock.close

这是错误,有些人知道如何解决这个问题?

Traceback (most recent call last):
  File "/home/pi/Desktop/counter.py", line 24, in <module>
    sock.connect(ADDR2)
  File "/usr/lib/python2.7/socket.py", line 228, in meth
    return getattr(self._sock,name)(*args)
error: [Errno 111] Connection refused

0 个答案:

没有答案