We found this webpage, which gave us an introduction to using the XBees, and it didn't take long for the radio to start transmitting. The code for the outlets is as follows:
import socket import sys import time # read message to be sent message = raw_input("Enter your message:") # create a UDP socket with server's IP & port number server_address = ('172.18.0.10', 6001) while True: sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: # Send data print (sys.stderr, 'sending "%s"' % message) # Print the message to be sent. sent = sock.sendto(message.encode('utf-8'), server_address) #Send the message # Receive response print (sys.stderr, 'waiting to receive') #Listen for acknowlegement of file receival data, server = sock.recvfrom(4096) #Receive acknowledgement data print (sys.stderr, 'received "%s"' % data) #Print acknowledgement data finally: print (sys.stderr, 'closing socket') #Print prior to closure of socket sock.close() #Close the socket time.sleep(0.5)
And for the hub:
import socket import sys import csv import time localtime = time.localtime(time.time()) # Create a TCP/IP socket sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Bind the socket to the port server_address = ("172.18.0.10", 6001) #Server's IP Adress and Port Number for Communication. print (sys.stderr, 'starting up on %s port %s' % server_address) sock.bind(server_address) while True: print (sys.stderr, '\nwaiting to receive message') data, address = sock.recvfrom(4096) print (sys.stderr, 'received %s bytes from %s' % (len(data), address)) print (sys.stderr, data) if data: sent = sock.sendto("recieved data".encode('utf-8'), address) print(sys.stderr, 'sent %s bytes back to %s' % (sent, address)) with open('people.csv', 'r') as readFile: reader = csv.reader(readFile) lines = list(reader) readFile.close() with open('people.csv', 'w') as writeFile: writer = csv.writer(writeFile, delimiter =' ') writer.writerows(lines + [address, localtime] + [data]) writeFile.close() with open("people.csv", 'r') as f: lines = f.readlines() # remove spaces lines = [line.replace(' ', '') for line in lines] # finally, write lines in the file with open("people.csv", 'w') as f: f.writelines(lines)
We then focussed our attention on the Raspberry Pi, and, after doing some research, found out we could use a simple jumper cable from one GPIO pin to another in order to receive radio signals. This was also easy to implement - although we didn't have the perfect length of cable, so signal was a bit patchy to start with.
After finding the correct length wire & getting the Arduino to send data to the Pi, we encountered the problem of range. We realised that in a home, data may have to be transmitted over 50m, and through multiple walls - meaning that the current setup wouldn't be adequate. Therefore, we began looking for an alternative, as long-distance radio transmission was seemingly too complicated from such budget devices. We originally thought of Bluetooth, but, again, range was a problem (Bluetooth only allows ~10m). Eventually, we settled on WiFi - it is a widely available, easy to access resource that 90% of households in the UK have (source: ONS). Range will already be adequate, as it is likely that the signal covers the whole house already, and sending data via WiFi Direct is simple.
We then implemented them with the flow meter, allowing us to continue with other aspects of the project.
To preview how the site would look in your home, please see our demo site; all of our code is available open-source on GitHub.