Revision 856d156b
Added by dsorber about 13 years ago
| 525.743/code/rpi_client.py | ||
|---|---|---|
|
import cPickle
|
||
|
import datetime
|
||
|
import itertools
|
||
|
import os
|
||
|
import socket
|
||
|
import sys
|
||
|
import time
|
||
|
|
||
| ... | ... | |
|
|
||
|
from bfcslib.relay import Relay
|
||
|
from bfcslib.sensor import Sensor
|
||
|
from bfcslib.sensor_reading import SensorReading
|
||
|
from bfcslib.seven_seg import SevenSegmentDisplay
|
||
|
from bfcslib.status_led import LED
|
||
|
from bfcslib.tmp512 import TMP512
|
||
| ... | ... | |
|
|
||
|
THRESHOLD_TEMP = 82
|
||
|
|
||
|
HOST = '10.0.0.4' # phallanx for testing
|
||
|
PORT = 2243
|
||
|
REMOTE_DATA_INTERVAL = 20 # in seconds
|
||
|
|
||
|
def button_isr(gpio_id, val):
|
||
|
global CURRENT_IDX
|
||
|
global SENSORS
|
||
| ... | ... | |
|
global CURRENT_IDX
|
||
|
global SENSORS
|
||
|
|
||
|
# Disable "channel already in use" warnings
|
||
|
RPIO.setwarnings(False)
|
||
|
|
||
|
print 'Beer Fermentation Control System - RPI Client'
|
||
|
|
||
|
# Make sure the script is being run as root before continuing
|
||
|
if os.geteuid() != 0:
|
||
|
print 'ERROR: You must run the client as root!'
|
||
|
return
|
||
|
|
||
|
# Attempt to connect to the server, make 5 attempts
|
||
|
print '\n\nAttempting to connect to server...',
|
||
|
sys.stdout.flush()
|
||
|
|
||
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||
|
tries = 5
|
||
|
while tries > 0:
|
||
|
try:
|
||
|
s.connect((HOST, PORT))
|
||
|
break
|
||
|
except socket.error:
|
||
|
tries -= 1
|
||
|
if tries == 0:
|
||
|
print 'FAILED'
|
||
|
print 'Unable to connect to %s:%s check your connection and ' \
|
||
|
'try again.' % (HOST, PORT)
|
||
|
return
|
||
|
time.sleep(1)
|
||
|
|
||
|
# Connection was successful
|
||
|
print 'SUCCESS'
|
||
|
print 'Connected to %s:%s' % (HOST, PORT)
|
||
|
|
||
|
# Setup the sensor status LEDs
|
||
|
sensor1_led = LED(24)
|
||
|
sensor2_led = LED(25)
|
||
| ... | ... | |
|
RPIO.wait_for_interrupts(threaded=True)
|
||
|
|
||
|
# Main loop
|
||
|
interval_counter = 0
|
||
|
while True:
|
||
|
|
||
|
# Reset variables
|
||
|
temps = []
|
||
|
if interval_counter == REMOTE_DATA_INTERVAL + 1:
|
||
|
interval_counter = 0
|
||
|
|
||
|
# Grab the current date/time
|
||
|
now = datetime.datetime.now()
|
||
|
|
||
|
# Iterate over all temperature sensors and read their temp
|
||
| ... | ... | |
|
relay_state = True
|
||
|
relay.on()
|
||
|
|
||
|
# Send a reading to the server once every minute
|
||
|
if interval_counter == REMOTE_DATA_INTERVAL:
|
||
|
print 'Sending reading to server...',
|
||
|
sys.stdout.flush()
|
||
|
reading = SensorReading(now, temps[0], temps[1], temps[2],
|
||
|
temps[3], relay_state)
|
||
|
try:
|
||
|
s.sendall(cPickle.dumps(reading))
|
||
|
print 'DONE'
|
||
|
except:
|
||
|
print 'ERROR'
|
||
|
|
||
|
interval_counter += 1
|
||
|
|
||
|
# Read once ever second
|
||
|
time.sleep(1)
|
||
|
|
||
|
s.close()
|
||
|
|
||
|
|
||
|
|
||
Made good progress tonight with the client and server apps. Added the socket connection and SensorReading object to hold the sensor readings (surprise).