commit abe2d7e509a660bc8b6304012781dee88ffe547d
Author: David Sorber <dsorber@dm1z.(none)>
Date:   Mon Apr 29 16:45:47 2013 -0400

    Adding some charset info to make sure mod_wsgi doesn't puke on UTF-8 encoded strings. Also added a "-d" (debug enable) switch to the RPi client so that it only prints out debug information if you want it to. Finally, I added a try except block around the database insert code in the server daemon so that the whole daemone doesn't crash when there is an error.

diff --git a/525.743/code/rpi_client.py b/525.743/code/rpi_client.py
index 73a5ac4..60314bc 100644
--- a/525.743/code/rpi_client.py
+++ b/525.743/code/rpi_client.py
@@ -16,6 +16,8 @@ from bfcslib.status_led import LED
 from bfcslib.tmp512 import TMP512
 
 # Global variables
+DEBUG = False
+
 NUM_SENSORS = 4
 sensor_iterator = itertools.cycle(range(0, NUM_SENSORS))
 CURRENT_IDX = sensor_iterator.next()
@@ -23,7 +25,7 @@ SENSORS = None
 
 THRESHOLD_TEMP = 82
 
-HOST = '10.0.0.4' # phallanx for testing
+HOST = '192.168.1.2'
 PORT = 2243
 REMOTE_DATA_INTERVAL = 20 # in seconds
 
@@ -49,6 +51,11 @@ def main():
 	RPIO.setwarnings(False)
 
 	print 'Beer Fermentation Control System - RPI Client'
+	
+	# Check for the debug command line argument
+	if len(sys.argv) > 1:
+		if sys.argv[1] == '-d':
+			DEBUG = True
 
 	# Make sure the script is being run as root before continuing
 	if os.geteuid() != 0:
@@ -127,7 +134,7 @@ def main():
 
 		# Reset variables
 		temps = []
-		if interval_counter == REMOTE_DATA_INTERVAL + 1:
+		if interval_counter == REMOTE_DATA_INTERVAL:
 			interval_counter = 0
 
 		# Grab the current date/time
@@ -145,9 +152,10 @@ def main():
 				ss_display.display_number(temp)
 
 		# Bit 'o debug information
-		print now.strftime('%m/%d/%Y %H:%M:%S')
-		for idx in xrange(NUM_SENSORS):
-			print '\t Sensor %d: %d' % (idx + 1, temps[idx])
+		if DEBUG:
+			print now.strftime('%m/%d/%Y %H:%M:%S')
+			for idx in xrange(NUM_SENSORS):
+				print '\t Sensor %d: %d' % (idx + 1, temps[idx])
 
 		# Sort all the temperature readings for easier manipulation
 		temps.sort(reverse=True)
@@ -168,24 +176,26 @@ def main():
 
 		# Send a reading to the server once every minute
 		if interval_counter == REMOTE_DATA_INTERVAL:
-			print 'Sending reading to server...',
-			sys.stdout.flush()
+			if DEBUG:
+				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'
+				if DEBUG:
+					print 'DONE'
 			except:
-				print 'ERROR'
+				if DEBUG:
+					print 'ERROR'
 
 		interval_counter += 1
 
-		# Read once ever second
-		time.sleep(1)
+		# Read once every second (approximated to account for processing 
+		# latencies)
+		time.sleep(0.9)
 
 	s.close()
 
-
-
 if __name__ == '__main__':
-	sys.exit(main())
\ No newline at end of file
+	sys.exit(main())
diff --git a/525.743/code/server.py b/525.743/code/server.py
index 0a2f961..cb3c6cb 100644
--- a/525.743/code/server.py
+++ b/525.743/code/server.py
@@ -7,8 +7,8 @@ import sys
 
 from bfcslib.sensor_reading import SensorReading
 
-DB_PATH = '/Users/dsorber/Desktop/beer_temps.db'
-HOST = '10.0.0.4'
+DB_PATH = '/home/dsorber/bfcs/beer_temps.db'
+HOST = '192.168.1.1'
 PORT = 2243
 
 def main():
@@ -53,17 +53,22 @@ def main():
 
 			db_conn = sqlite3.connect(DB_PATH)
 			db = db_conn.cursor()
-			db.execute("INSERT INTO temp_data VALUES (%s, %s, %s, %s, %s, %s)" % 
-					   (calendar.timegm(obj.time.timetuple()), obj.temp1, 
-					   	obj.temp2, obj.temp3, obj.temp4, 
-					   	int(obj.relay_status and '1' or '0')))
-			db_conn.commit()
+			try:
+				db.execute("INSERT INTO temp_data VALUES (%s, %s, %s, %s, %s, %s)" % 
+						   (calendar.timegm(obj.time.timetuple()), obj.temp1, 
+							obj.temp2, obj.temp3, obj.temp4, 
+							int(obj.relay_status and '1' or '0')))
+				db_conn.commit()
+			except:
+				print 'FAILED'
+				db_conn.close()
+				continue
+				
 			db_conn.close()
-
 			print 'SUCCESS'
 
 	conn.close()
 
 
 if __name__ == '__main__':
-	sys.exit(main())
\ No newline at end of file
+	sys.exit(main())
diff --git a/525.743/code/web/app.wsgi b/525.743/code/web/app.wsgi
index 694de44..d0d8c81 100644
--- a/525.743/code/web/app.wsgi
+++ b/525.743/code/web/app.wsgi
@@ -41,10 +41,10 @@ def application(environ, start_response):
 
         # Generate the stream and then render it to HTML
         stream = template.generate(**response_args)
-        response_body = stream.render('html', doctype='html')
+        response_body = stream.render('html', doctype='html', encoding='utf-8')
 
         # Set the appropriate response headers
-        response_headers = [('Content-Type', 'text/html'),
+        response_headers = [('Content-Type', 'text/html; charset=utf-8'),
                             ('Content-Length', str(len(response_body)))]
 
     elif environ['PATH_INFO'].startswith('/day/'):
@@ -89,10 +89,10 @@ def application(environ, start_response):
 
         # Generate the stream and then render it to HTML
         stream = template.generate(**response_args)
-        response_body = stream.render('html', doctype='html')
+        response_body = stream.render('html', doctype='html', encoding='utf-8')
 
         # Set the appropriate response headers
-        response_headers = [('Content-Type', 'text/html'),
+        response_headers = [('Content-Type', 'text/html; charset=utf-8'),
                             ('Content-Length', str(len(response_body)))]
     else:
         # User supplied an invalid URL
@@ -108,4 +108,4 @@ def application(environ, start_response):
 
     # Return the response body. (Notice it is wrapped in a list although it
     # could be any iterable.)
-    return [response_body]
\ No newline at end of file
+    return [response_body]
