RaspberryPi Web Accessible Temperature Logger
[initial version 04/07/2014]
[update 08/06/2014 - added 5 sensor files and instructions for probe adjustment]
[update 08/14/2014 - added probe read value filter to eliminate invalid spikes]
[update 09/04/2014 - added 4 sensor files]
[update 09/12/2014 - added 3 sensor files, corrected typos]

This kit will install support to read from one to five DS18B20 "OneWire" temperature sensors, log the reading(s) to a database with a timestamp, and fetch entries from that database to drive a web page with a temperature chart and statistics.

Assumptions: 
- Raspbian (Debian "Wheezy") OS, Apache2 web server, Chromium or compatible web browser already installed.
- user (default = pi) with superuser access on demand

Components needed:
- 1 to 5 wired DS18B20 sensors
- 4.7K ohm axial lead resistor, 1/8W works well
- hookup wires

Overview: Connect sensors, unpack archive files and move to require folders, set ownership & permissions for files, enable OneWire GPIO support, test the sensor(s), install SQLITE3, create the database, move database to require folder, set ownership & permission for database, create a CRON job to run the sensor polling code, and test the result.

NOTE: File names 'monitor_Nsensors.py' and 'webguiN.py' are used below rather than enumerating all five pairs of possible file names.
      Use the appropriate pair of these files for the number of probes to be used.

1. Shut down, remove power, video cable, network cable, SD card, USB devices, etc, and open case (if used).

2. Wire up DS18B20 sensor(s):

Sensor red wire(s) to +3.3V (GPIO header pin 1 or 17)
Sensor black wire(s) to GND (GPIO header pin 6,9,14,20 or 25)
Sensor yellow (or white) wire(s) to GPIO4 (GPIO header pin 7)
Add a 4.7K ohm 1/8w resistor across GPIO header pins 1 and 7 (ie: between red and yellow/white wires).

NOTE: there is a wide variety of wiring colors used with these sensors. red/white/black and red/yellow/black seem to consistently represent PWR/signal/GND, however there are red/green/yellow wires used which variously have represented either PWR/signal/GND or PWR/GND/signal.


3. Reconnect all cables and devices. Power up, then download and unpack the archive into /home/pi, then:

Move files to /usr/lib/cgi-bin : 	monitor_Nsensors.py, pint.ico, webguiN.py 

Move files to /var/www/img:				thermometer.png, thermometer4k.png

Leave files in /home/pi :					test_sensors.py, 2x13_header.jpg, ds18b20.jpg, GPIO_header.jpg, temp_logger.txt

NOTE: You only need the files for the number of sensors installed. You can delete the extras.


4. Change ownership and permissions on monitor scripts:

$ sudo chown www-data:www-data /usr/lib/cgi-bin/monitor_Nsensors.py
$ sudo chmod +x /usr/lib/cgi-bin/monitor_Nsensors.py


5. Change ownership and permissions on web server scripts:

$ sudo chown www-data:www-data /usr/lib/cgi-bin/webguiN.py
$ sudo chmod +x /usr/lib/cgi-bin/webguiN.py


6. Enable w1 bus support: Edit file /etc/modules

$ sudo nano /etc/modules
w1-gpio					<- add this line
w1-therm				<- and this line

^o to save the file
^x to exit nano


7. Reboot the RPi


8. Test temperature sensors: (do this in /home/pi)

$ python test_sensors.py

Up to five sensors can be detected and interrogated by this program.
If no sensors are found, shut down and check wiring.
If sensors are detected, they will be listed in order of their device addresses (each device has a unique hard-coded address).
The logging program will always use the sensors in numeric order by address, low to high (first = channel 0, second = channel 1, etc).
If using multiple sensors, use this program to determine which is which by observing which sensor changes temperature while being warmed in the hand.
Label each sensor with its channel number.


9. Install SQLITE3:

$ sudo apt-get update
$ sudo apt-get install sqlite3


10. Create SQLITE3 temperature database: (do this in /home/pi)

For one channel:

$ sqlite3 temp_data.db
	BEGIN;
	CREATE TABLE temps (timestamp DATETIME, temp0 NUMERIC);
	COMMIT;
	.quit


For two channels:

$ sqlite3 temp_data2.db
	BEGIN;
	CREATE TABLE temps (timestamp DATETIME, temp0 NUMERIC, temp1 NUMERIC);
	COMMIT;
	.quit


For three channels:

$ sqlite3 temp_data3.db
	BEGIN;
	CREATE TABLE temps (timestamp DATETIME, temp0 NUMERIC, temp1 NUMERIC, temp2 NUMERIC);
	COMMIT;
	.quit


For four channels:

$ sqlite3 temp_data4.db
	BEGIN;
	CREATE TABLE temps (timestamp DATETIME, temp0 NUMERIC, temp1 NUMERIC, temp2 NUMERIC, temp3 NUMERIC);
	COMMIT;
	.quit


For five channels:

$ sqlite3 temp_data5.db
	BEGIN;
	CREATE TABLE temps (timestamp DATETIME, temp0 NUMERIC, temp1 NUMERIC, temp2 NUMERIC, temp3 NUMERIC, temp4 NUMERIC);
	COMMIT;
	.quit



11. Move database to /var/www, change ownership to www-data, and set permissions:

For one sensor:

$ sudo mv temp_data.db /var/www/
$ sudo chown www-data:www-data /var/www/temp_data.db
$ sudo chmod +x /var/www/temp_data.db

For two sensors:

$ sudo mv temp_data2.db /var/www/
$ sudo chown www-data:www-data /var/www/temp_data2.db
$ sudo chmod +x /var/www/temp_data2.db

For three sensors:

$ sudo mv temp_data3.db /var/www/
$ sudo chown www-data:www-data /var/www/temp_data3.db
$ sudo chmod +x /var/www/temp_data3.db

For four sensors:

$ sudo mv temp_data4.db /var/www/
$ sudo chown www-data:www-data /var/www/temp_data4.db
$ sudo chmod +x /var/www/temp_data4.db

For five sensors:

$ sudo mv temp_data5.db /var/www/
$ sudo chown www-data:www-data /var/www/temp_data5.db
$ sudo chmod +x /var/www/temp_data5.db

12. Create cron job for temperature monitor python script

NOTE: the leading '*/5' sets the period to 5 minutes. Adjust as desired.
Only one of the entry lines is required to be added.

$ sudo crontab -u www-data -e
*/5 * * * * /usr/lib/cgi-bin/monitor_1sensor.py		  <- add this line near bottom for one sensor, OR
*/5 * * * * /usr/lib/cgi-bin/monitor_2sensors.py		<- add this line near bottom for two sensors, OR
*/5 * * * * /usr/lib/cgi-bin/monitor_3sensors.py		<- add this line near bottom for three sensors, OR
*/5 * * * * /usr/lib/cgi-bin/monitor_4sensors.py		<- add this line near bottom for four sensors, OR
*/5 * * * * /usr/lib/cgi-bin/monitor_5sensors.py		<- add this line near bottom for five sensors

^o to save the file
^x to exit


At this point the python monitor script should run every 5 minutes, read the sensor(s), and post the data to the database (temp_dataN.db)

All that remains is running the web server python script from a browser to access the database and format and display the resulting web page.


13. Launch Chromium on the RaspberryPi and enter the url for your RPi, followed by the file name for the webgui version used (ie: webguiN.py).
You can use the localhost address as a test.

Eg: http://127.0.0.1/cgi-bin/webgui1.py or http://127.0.0.1/cgi-bin/webgui2.py or http://127.0.0.1/cgi-bin/webgui3.py or http://127.0.0.1/cgi-bin/webgui4.py or http://127.0.0.1/cgi-bin/webgui5.py

Replace 127.0.0.1 with LAN IP address to access web page from a web browser on another system on your LAN.
To access the web page from a system external to your LAN you'll need to determine the external IP address for your network and establish port-forwarding rules through any routers between the public internet and your LAN.

================================================================================================================

Probe correction:

The python monitor scripts monitor_(n)sensors.py now have correction factors for each probe channel so all probes can be correlated against each other or a standard.
The values (adjch0 through adjch4) can be set positive or negative; they will be added to the raw probe readings before the result is stored in the database.

Examples:

adjch0=1.5			#+1.5F adjustment
adjch1=-0.8 		#-0.8F adjustment

================================================================================================================

Probe read value filter:

Occasionally (especially the first probe read after a system power-up) the DS18B20 probe may return a nonsensical temperature value (typically 0 or 185F). This will cause a "spike" in the temperature plot, which in turn causes the plot Y-axis to be scaled to fit the "spike".

To filter these events there are two values added to the probe monitoring scripts (monitor*.py).

# read bounds for probes, probe readings not within bounds will be retried
# set the desired limit values
hibound=100
lobound=20

When each probe is read the returned value is tested to be within the lobound/hibound limits. If the value passes the filter the reading is stored in the database, otherwise a retry of the probe read is performed.

The default values should work for virtually any environment but can be edited if desired.

================================================================================================================

Extra:

Integration with RaspberryPints:

You can replace the RaspberryPints icon with the thermometer icons provided with this kit, and change the http hotspot link to point to the temperature logger page.

Edit the file /var/www/index.php

$ sudo nano /var/www/index.php

Find the .png file tags:

^W (where is) then enter .png and hit Enter
The cursor will move to the first instance.
We will change the http url and the filename 

Replace "http://www.raspberrypints.com" with the url of your RPi server
Replace "raspberrypints-4k" with "thermometer4k"

Two lines below is the other instance.

Replace "http://www.raspberrypints.com" with the url of your RPi server
Replace "raspberrypints" with "thermometer".

^o to save the file
^x to exit nano

Examples: 

<a href="http://96.252.83.5:85/cgi-bin/webguiN.py"><img src="img/thermometer4k.png" height="200" alt=""></a>
<a href="http://96.252.83.5:85/cgi-bin/webguiN.py"><img src="img/thermometer.png" height="100" alt=""></a>


Now, when you load your RaspberryPints taplist, there should be a thermometer icon in the top right than when clicked will load your temperature logger page.

================================================================================================================
Credits!



Totally facilitated by Steve Breuning's work found here: http://raspberrywebserver.com/cgiscripting/rpi-temperature-logger/
I merely expanded the database for multiple probes and tweaked some database and html code. I'd be at Square One without his precedent.

And, of course, fellow HBTer Thadius856, mcangeli, skemp45 and the entire RaspberryPints crew for not only the Digital Tap List effort but for inspiration to get off my ass and build what I couldn't buy.

Cheers!

/Dave T. (aka day_trippr)