<class 'socket.error'> ([Errno 111] Connection refused)
I am working on a home automation application using python, but since migrating from my local setup to two physical machines (Server, Client), I get a rejection error:
Traceback (last call last): File "/opt/web-apps/web2py/gluon/restricted.py", line 227, in restricted exec ccode in environment File "/ opt / web-apps / web2py / applications / Home_Plugs / controllers /default.py ", line 85, in" /opt/web-apps/web2py/gluon/globals.py ", line 393, in self._caller = lambda f: f () File" / opt / web-apps /web2py/gluon/tools.py ", line 3440, returning action (* a, ** b) File" /opt/web-apps/web2py/applications/Home_Plugs/controllers/default.py ", line 32, at toggle file GPIO.setup (light.OnPin, GPIO.OUTPUT) "application / Home_Plugs / modules / GPIOClient.py", line 23, in setting File "application / Home_Plugs / modules / GPIOClient.py", line 18, in host message = '192.168.1.79 "File" / usr / lib 64 / python2.7 / socket.py ", line 224,in return method getattr (self._sock, name) (* args) error: [Errno 111] Connection refused
Server code :
#!/usr/bin/env python
import socket
import RPi.GPIO as GPIO
import sys
import logging
SETUP = chr(0)
OUTPUT = chr(1)
GPIO.setmode(GPIO.BOARD)
def gpio_setup(data):
pin,dir = ord(data[0]),ord(data[1])
GPIO.setup(pin,dir)
logging.gpioServerLog("setup" + str(pin) + str(dir))
return 0
def gpio_output(data):
pin,val = ord(data[0]),ord(data[1])
GPIO.output(pin,val)
logging.gpioServerLog("out" + str(pin) + str(val))
return 0
if __name__=='__main__':
HOST = ''
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST, PORT)
serversock = socket.socket()
serversock.bind(ADDR)
serversock.listen(5)
while 1:
ret = None
logging.gpioServerLog('waiting for connection...')
clientsock, addr = serversock.accept()
logging.gpioServerLog('...connected from:' + str(addr))
data = clientsock.recv(BUFSIZ)
if data[0] == SETUP:
ret = gpio_setup(data[1:])
elif data[0] == OUTPUT:
ret = gpio_output(data[1:])
if ret:
clientsock.send('Thank you for conencting')
clientsock.send(ret)
clientsock.close()
Client code :
#!/usr/bin/env python
# coding: utf8
import socket
import struct
SETUP_CMD = chr(0)
OUTPUT_CMD = chr(1)
OUTPUT = chr(0)
INPUT = chr(1)
ON = chr(1)
OFF = chr(0)
def send(data):
sock = socket.socket()
host = '192.168.1.79'
port = 21567
sock.connect((host,port))
sock.send(data)
sock.close
def setup(pin,dir):
data = struct.pack("ccc",SETUP_CMD,chr(pin),dir)
send(data)
def output(pin,val):
data = struct.pack("ccc",OUTPUT_CMD,chr(pin),val)
send(data)
Machines : Server - Raspbian Wheezy / Python 2.7 Client - CentOS 7 / Python 2.7
I have disabled all firewalls to avoid blocking connections. Telnet works
I also have a test client.py script that also works when run from the client command line:
#!/usr/bin/python
# This is client.py file
import socket # Import socket module
import GPIOClient as GPIO
import time
GPIO.setup(11,GPIO.OUTPUT)
GPIO.output(11,GPIO.ON)
time.sleep(1)
GPIO.setup(11,GPIO.INPUT)
Any help would be much appreciated
More information required, please let me know
Thanks in Advance
source to share