Last active
January 22, 2017 01:31
-
-
Save ttimbers/7ac7f5a53d541f3adfb7a84d0010ac69 to your computer and use it in GitHub Desktop.
Read data from arduino into R
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(grid) | |
f <- file("/dev/cu.usbserial-DA01HLQC", open="r") | |
data <- data.frame(scan(f, n=60, what = "double", allowEscapes = TRUE, sep = "\n")) | |
close(f) | |
colnames(data) <- "all_data" | |
parsed_data <- separate(data, all_data, | |
c("voltage_t", | |
"temp_c", | |
"voltage_h", | |
"humidity", | |
"humidiry_rel", | |
"dewpoint", | |
"clound_base"), | |
sep = " ") | |
# remove first row (sometimes has NAs and jumbled columns) | |
parsed_data <- parsed_data[-1,] | |
# change all columns to numeric | |
parsed_data <- map_df(parsed_data, as.numeric) | |
# add a time column for plotting | |
parsed_data$time <- seq_along(parsed_data$voltage_t) | |
# plot dewpoint and temp_c | |
dew_temp_plot <- ggplot(parsed_data, | |
aes(x = time, y = temp_c, colour = "Temperature (C)")) + | |
geom_line() + | |
geom_line(aes(x = time, y = dewpoint, colour = "Dewpoint")) + | |
ylim(10, 21) + | |
ylab("Temperature (C)") + | |
xlab("Time(s)") | |
dew_temp_plot | |
# plot humidity | |
hum_plot <- ggplot(parsed_data, | |
aes(x = time, y = humidiry_rel, colour = "Relative Humidity")) + | |
geom_line() + | |
ylab("Relative Humidity") + | |
xlab("Time(s)") | |
hum_plot | |
# overlay plots on same graph (with two-axis) | |
grid.newpage() | |
grid.draw(rbind(ggplotGrob(dew_temp_plot), ggplotGrob(hum_plot), size = "last")) |
Author
ttimbers
commented
Jan 22, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment