3
votes

I'm working on an home automation app using python, but since migration this from my local setup to two physical machines(Server, Client) i am getting a connection refused error:

Traceback (most recent 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 File "/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, in f return action(*a, **b) File "/opt/web-apps/web2py/applications/Home_Plugs/controllers/default.py", line 32, in toggle GPIO.setup(light.OnPin,GPIO.OUTPUT) File "applications/Home_Plugs/modules/GPIOClient.py", line 23, in setup File "applications/Home_Plugs/modules/GPIOClient.py", line 18, in send host = '192.168.1.79' File "/usr/lib64/python2.7/socket.py", line 224, in meth return 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 ensure no blocking of connections. Telnet works

i also have a test client.py script whihc also works when ran from client commandline:

#!/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

Any more information require please let me know

Thanks in Advance

1
Are you sure that server is listening on port 21567?Prabhu
Thats was ones of the first things i check using netstat i can see the sevice listening on port 21567.Stephen Dean

1 Answers

0
votes

Turns out there was 2 incorrect carriage returns in my GPIOClient.py

Remove and now all is working