Plotting Air Quality Egg data using R
I recently got an Air Quality Egg. The Air Quality Egg is a open source hardware project to measure air quality hyper-locally. My egg is located on the balcony of my flat in Brixton, London. The Air Quality Egg device has sensors which read NO2, CO, humidity & temperature and posts the data to Cosm. You can view readings and simple graphs on the Cosm feed page.
Plotting sensor data using R
I wanted to see the trends in the data so I wrote a script in R to curl the data from the Cosm API and plot it using GGPlot.
Here is the script:
install.packages('RCurl') | |
install.packages("ggplot2") | |
library(ggplot2) | |
library(RCurl) | |
# grab an api key from cosm and put it here | |
api_key = 'YOUR_API_KEY' | |
# your feed id | |
feed_id = '106267' | |
csv_data = "" | |
start_date = as.POSIXct("2013/05/07","GMT") | |
end_date = as.POSIXct("2013/05/08","GMT") | |
while (start_date < end_date) { | |
start_date_as_str = format(start_date, format="%Y-%m-%dT%H:%M:00Z") | |
next_date = start_date + (3 * 60 * 60) | |
next_date_as_str = format(next_date, format="%Y-%m-%dT%H:%M:00Z") | |
cosm_url = paste("http://api.cosm.com/v2/feeds/", feed_id, "/datastreams/NO2_00-04-a3-37-cc-cb_0.csv?start=", start_date_as_str, "&end=", next_date_as_str,"&interval=0", sep="") | |
csv_data_for_period = getURL(cosm_url, httpheader = c('X-ApiKey' = api_key) ) | |
csv_data = paste(csv_data, csv_data_for_period, "\n") | |
start_date = next_date | |
} | |
data = read.table(textConnection(csv_data), sep = ",", col.names=c("at", "value")) | |
data$timestamp <- strptime(data$at, "%FT%T", tz = "UTC") | |
# strip out outliers | |
data = data[which(data$value < 1000),] | |
#summary(data) | |
ggplot(data, aes(timestamp,value)) + geom_point() + geom_smooth() + xlab("Time") + ylab("NO2 PPB") |
Here are some example graphs of NO2 over a single day and several days. (Note I don’t have the latest sensor updates so the readings may be a bit off)