I have set up a home automation using a raspberry pi b+ V3 as the brains main part and raspberry pi zero's located around the house at different points.

I have one pi zero beside my heating, this is connected to a relay via gpio which turns the boiler on or off.

I had used sockets for this, the zero has a server socket running 24/7 and the b+ runs the client socket.

Last week this suddenly stopped working, I rebooted the pi zero and it worked, left it for a while and a few days later it stopped again. I changed the port to see if that made a difference, rebooted and it worked.

Until last night, when it stopped again. From what I can see the server is still running and the client doesn't show up any errors so I can't figure out why this has changed.

I've done no changes other then some small php on the b+ and I haven't touched my router.

Here's the server.py code:

import socket
import sys
import time
import RPi.GPIO as GPIO


# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to the port
server_address = ('', 10000)
print >>sys.stderr, 'starting up on %s port %s' % server_address

# Listen for incoming connections

while True:
# Wait for a connection
print >>sys.stderr, 'waiting for a connection'
connection, client_address = sock.accept()

    print >>sys.stderr, 'connection from', client_address

    # Receive the data in small chunks and retransmit it
    while True:
        data = connection.recv(1024)
        if data == 'Heating on' :
        elif data == 'Heating off' :
            print >>sys.stderr, 'no more data from', client_address

    # Clean up the connection

And the client code heating_on.py Heating off is the same only has Heating off instead of on.

import socket
import sys
import time

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect the socket to the port where the server is listening
server_address = ('', 2222)
#print >>sys.stderr, 'connecting to %s port %s' % server_address

# Send data
message = 'Heating on'
#    print >>sys.stderr, 'sending "%s"' % message

# Look for the response
amount_received = 0
amount_expected = len(message)

while amount_received < amount_expected:
    data = sock.recv(1)
    amount_received += len(data)
    # print '%s' % data

I have the b+ running a cron job every minute to see if the heating should be on or off (based on temperature etc.) so the client always runs every minute with either "Heating on" or "Heating off"

I can't find any logs to show any errors either. Am I missing something ? Thanks in advance

  • You'll almost certainly need some reconnection logic in the face of errors. For example, DHCP leases will be renewed, occasionally wifi connections may drop, routers might reboot, etc. Generally, I wouldn't assume a TCP/IP socket would reliably last beyond a few hours. – Dave Jones Oct 5 '17 at 0:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.