Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Defaults
nohup.out
*.pyc
7 changes: 0 additions & 7 deletions README

This file was deleted.

21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Raspberry Pi Temperature Logger

This repo contains code for a Raspberry Pi temperature logger which uses SQLite to store data read from a DS18B20 sensor.

There are several scripts that can be used. You'll need to trigger regular temperature logging from a cron job

## Scripts

`check_results.py` - Use to check the DB contents

`logger.py` - Used to log the temps into the DB, driven from a cron job

`webgui.py` - Drives a webpage to visualise the results

## Original info

This was all created originally from this website (which seems to be offline currently):

http://raspberrywebserver.com/cgiscripting/rpi-temperature-logger/building-an-sqlite-temperature-logger.html


40 changes: 40 additions & 0 deletions check_results.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python

# For checking the SQLlite DB

import sqlite3
import os
import time
import glob
import datetime

# global variables
dbname='./colchester.db'


# display the contents of the database
def display_data():

conn=sqlite3.connect(dbname)
curs=conn.cursor()

for row in curs.execute("SELECT * FROM temps"):
print str(row[0])+" "+str(row[1])

conn.close()


# main function
# This is where the program starts
def main():

# display the contents of the database
display_data()


if __name__=="__main__":
main()




Binary file added colchester.db
Binary file not shown.
37 changes: 13 additions & 24 deletions monitor.py → logger.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
#!/usr/bin/env python

import sqlite3
# Logs the temp to the SQLlite DB

import sqlite3
import os
import time
import glob
import datetime

# global variables
speriod=(15*60)-1
dbname='/var/www/templog.db'

dbname='/home/pi/rpi_temp_logger/colchester.db'

# Timestamp must include local timezone
def current_time():
ts = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
return ts

# store the temperature in the database
def log_temperature(temp):

conn=sqlite3.connect(dbname)
curs=conn.cursor()

curs.execute("INSERT INTO temps values(datetime('now'), (?))", (temp,))
curs.execute("INSERT INTO temps values( ?, ? )", (current_time(), temp))

# commit the changes
conn.commit()
Expand Down Expand Up @@ -55,10 +59,8 @@ def get_temp(devicefile):

# is the status is ok, get the temperature from line 2
if status=="YES":
print status
tempstr= lines[1][-6:-1]
tempvalue=float(tempstr)/1000
print tempvalue
tempstr=lines[1].split("=")
tempvalue=float(tempstr[1])/1000
return tempvalue
else:
print "There was an error."
Expand All @@ -83,30 +85,17 @@ def main():
w1devicefile = devicelist[0] + '/w1_slave'


# while True:

# get the temperature from the device file
temperature = get_temp(w1devicefile)
if temperature != None:
print "temperature="+str(temperature)
else:
if temperature is None:
# Sometimes reads fail on the first attempt
# so we need to retry
temperature = get_temp(w1devicefile)
print "temperature="+str(temperature)

# Store the temperature in the database
# Store the temperature in the database
log_temperature(temperature)

# display the contents of the database
# display_data()

# time.sleep(speriod)


if __name__=="__main__":
main()




Loading