Add config file. #39
@@ -1,2 +1,6 @@
 | 
				
			|||||||
[DEFAULT]
 | 
					[SERVER]
 | 
				
			||||||
PORT = 8080
 | 
					PORT = 8080
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[SENSOR]
 | 
				
			||||||
 | 
					# Set sensor type : Options are DHT11, DHT22 or AM2302
 | 
				
			||||||
 | 
					TYPE = DHT22
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -51,21 +51,22 @@ import arrow
 | 
				
			|||||||
import json
 | 
					import json
 | 
				
			||||||
import plotly
 | 
					import plotly
 | 
				
			||||||
import sqlite3
 | 
					import sqlite3
 | 
				
			||||||
 | 
					import sys
 | 
				
			||||||
 | 
					import Adafruit_DHT
 | 
				
			||||||
import ConfigParser
 | 
					import ConfigParser
 | 
				
			||||||
from plotly.graph_objs import *
 | 
					from plotly.graph_objs import *
 | 
				
			||||||
 | 
					
 | 
				
			||||||
config = ConfigParser.ConfigParser()
 | 
					config = ConfigParser.ConfigParser()
 | 
				
			||||||
config.read("config.ini")
 | 
					config.read("config.ini")
 | 
				
			||||||
port = config.getint('DEFAULT', 'PORT') 
 | 
					port = config.getint('SERVER', 'PORT') 
 | 
				
			||||||
 | 
					sensor = config.get('SENSOR','TYPE') 
 | 
				
			||||||
 | 
					
 | 
				
			||||||
app = Flask(__name__)
 | 
					app = Flask(__name__)
 | 
				
			||||||
app.debug = True # Make this False if you are no longer debugging
 | 
					app.debug = True # Make this False if you are no longer debugging
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@app.route("/")
 | 
					@app.route("/")
 | 
				
			||||||
def lab_temp():
 | 
					def lab_temp():
 | 
				
			||||||
    import sys
 | 
					    humidity, temperature = Adafruit_DHT.read_retry(getattr(Adafruit_DHT,sensor), 17) 
 | 
				
			||||||
    import Adafruit_DHT
 | 
					 | 
				
			||||||
    humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.AM2302, 17)
 | 
					 | 
				
			||||||
    temperature = temperature * 9/5.0 + 32
 | 
					    temperature = temperature * 9/5.0 + 32
 | 
				
			||||||
    if humidity is not None and temperature is not None:
 | 
					    if humidity is not None and temperature is not None:
 | 
				
			||||||
        return render_template("live.html",temp=temperature,hum=humidity)
 | 
					        return render_template("live.html",temp=temperature,hum=humidity)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -37,6 +37,11 @@ No text output. Two new records are inserted in the database when the script is
 | 
				
			|||||||
import sqlite3
 | 
					import sqlite3
 | 
				
			||||||
import sys
 | 
					import sys
 | 
				
			||||||
import Adafruit_DHT
 | 
					import Adafruit_DHT
 | 
				
			||||||
 | 
					import ConfigParser
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					config = ConfigParser.ConfigParser()
 | 
				
			||||||
 | 
					config.read("config.ini")
 | 
				
			||||||
 | 
					sensor = config.get('SENSOR','TYPE') 
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def log_values(sensor_id, temp, hum):
 | 
					def log_values(sensor_id, temp, hum):
 | 
				
			||||||
	conn=sqlite3.connect('/home/pi/Documents/pi-temp/pi_temp.db')  #It is important to provide an
 | 
						conn=sqlite3.connect('/home/pi/Documents/pi-temp/pi_temp.db')  #It is important to provide an
 | 
				
			||||||
@@ -51,7 +56,7 @@ def log_values(sensor_id, temp, hum):
 | 
				
			|||||||
	conn.commit()
 | 
						conn.commit()
 | 
				
			||||||
	conn.close()
 | 
						conn.close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.AM2302, 17)
 | 
					humidity, temperature = Adafruit_DHT.read_retry(getattr(Adafruit_DHT,sensor), 17)
 | 
				
			||||||
temperature = temperature * 9/5.0 + 32
 | 
					temperature = temperature * 9/5.0 + 32
 | 
				
			||||||
# If you don't have a sensor but still wish to run this program, comment out all the 
 | 
					# If you don't have a sensor but still wish to run this program, comment out all the 
 | 
				
			||||||
# sensor related lines, and uncomment the following lines (these will produce random
 | 
					# sensor related lines, and uncomment the following lines (these will produce random
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user