Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Created June 1, 2012 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tenderlove/2855029 to your computer and use it in GitHub Desktop.
Save tenderlove/2855029 to your computer and use it in GitHub Desktop.
require(graphics)
library("DBI")
library(RSQLite)
library(TTR)
drv <- dbDriver("SQLite")
conn <- dbConnect(drv, dbname = "sausage.sqlite3")
query <- function(con, query) {
rs <- dbSendQuery(con, query)
data <- fetch(rs, n = -1)
dbClearResult(rs)
data
}
box <- query(conn, "
SELECT id,
humidity / 10.0 as humidity,
(((temp / 10.0) * 9) / 5) + 32 as temp,
(((ambient_temp / 10.0) * 9) / 5) + 32 as ambient_temp,
ambient_humidity / 10.0 as ambient_humidity,
created_at
FROM measurements
WHERE
datetime(created_at) > datetime('now', '-2 hour')
")
x_range <- as.POSIXct(box$created_at, tz = "UTC")
png(filename = "out.png", height = 750, width = 1000, bg = "white")
plot(x_range, smooth(box$temp), type="l", ylim=c(50, 80), col="red", ylab="Degrees F", xlab="Time")
lines(x_range, smooth(box$humidity), type="l")
lines(x_range, EMA(box$ambient_temp, n = 10), type="l", col="green")
lines(x_range, EMA(box$ambient_humidity, n = 10), type="l", col="brown")
abline(h = 70, col="lightblue")
abline(h = 60, col="blue")
title("Sausage Box")
mtext("Relative Humidity", side=4)
legend("topleft", c("Box Temp", "Ambient Temp", "Humidity", "Ambient Humidity", "Humidity Setting", "Temp Setting"), lty = c(1, 1, 1, 1), col = c("red", "green", "black", "brown", "lightblue", "blue"))
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment