Guest User

Untitled

a guest
Jul 18th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. require("RPostgreSQL")
  2.  
  3. # create a connection
  4. pw <- {
  5. "{PASSWORD}"
  6. }
  7.  
  8. # loads the PostgreSQL driver
  9. drv <- dbDriver("PostgreSQL")
  10.  
  11. # creates a connection to the postgres database
  12. # note that "con" will be used later in each connection to the database
  13. con <- dbConnect(drv, dbname = "Field_Station2",
  14. host = "{IP_ADDRESS}", port = 5432,
  15. user = "postgres", password = pw)
  16.  
  17. #removes the password
  18. rm(pw)
  19.  
  20. date <- format(Sys.Date(), "%m-%d-%Y")
  21.  
  22. #Retrieve data from database according to provided SQL query
  23. getDataById <- function(device_id)
  24. {
  25. db_values <- dbGetQuery(con, sprintf("SELECT * FROM sensor_data2 WHERE device_id = '%s' AND day = '%s'", device_id, date))
  26. return(db_values)
  27. }
  28.  
  29. df_device_1 <- getDataById("ApplePi")
  30. df_device_2 <- getDataById("rpiX")
  31. df_device_3 <- getDataById("Raspi")
  32.  
  33. #Retrieve data from databse and convert to numeric values in order to plot
  34. time_1 <-as.POSIXlt(strptime(df_device_1$time, "%H:%M:%S"))
  35. time_2 <-as.POSIXlt(strptime(df_device_2$time, "%H:%M:%S"))
  36. time_3 <-as.POSIXlt(strptime(df_device_3$time, "%H:%M:%S"))
  37.  
  38. temp_1 <- as.numeric(as.character(df_device_1$temp))
  39. temp_2 <- as.numeric(as.character(df_device_2$temp))
  40. temp_3 <- as.numeric(as.character(df_device_3$temp))
  41.  
  42. humid_1 <- as.numeric(as.character(df_device_1$humid))
  43. humid_2 <- as.numeric(as.character(df_device_2$humid))
  44. humid_3 <- as.numeric(as.character(df_device_3$humid))
  45.  
  46. press_1 <- as.numeric(as.character(df_device_1$press))
  47. press_2 <- as.numeric(as.character(df_device_2$press))
  48. press_3 <- as.numeric(as.character(df_device_3$press))
  49.  
  50. require(ggplot2)
  51. #Plotdata using the dataframes and save to indivual files
  52. ggplot() + labs(title= paste("Temperature: ",date), x= "time", y= "temperature[F]") +
  53. geom_line(aes(x= time_1, y= temp_1), colour= "blue") +
  54. geom_line(aes(x= time_2, y= temp_2), colour= "red") +
  55. geom_line(aes(x= time_3, y= temp_3), colour= "green") +
  56. theme(axis.text.x = element_text(angle = 45, hjust = 1))
  57. ggsave("temperature_plot.jpeg", path= "~/Desktop/Sensor_Data_Graphs")
  58.  
  59. ggplot() + labs(title= paste("Humidity: ",date), x= "time", y= "humidity") +
  60. geom_line(aes(x= time_1, y= humid_1), colour= "blue") +
  61. geom_line(aes(x= time_2, y= humid_2), colour= "red") +
  62. geom_line(aes(x= time_3, y= humid_3), colour= "green") +
  63. theme(axis.text.x = element_text(angle = 45, hjust = 1))
  64. ggsave("humidity_plot.jpeg", path= "~/Desktop/Sensor_Data_Graphs")
  65.  
  66. ggplot() + labs(title= paste("Pressure: ",date), x= "time", y= "pressure[hPa] ") +
  67. geom_line(aes(x= time_1, y= press_1), colour= "blue") +
  68. geom_line(aes(x= time_2, y= press_2), colour= "red") +
  69. geom_line(aes(x= time_3, y= press_3), colour= "green") +
  70. theme(axis.text.x = element_text(angle = 45, hjust = 1))
  71. ggsave("pressure_plot.jpeg", path= "~/Desktop/Sensor_Data_Graphs")
  72.  
  73. #End database connection
  74. lapply(dbListConnections(drv = dbDriver("PostgreSQL")), function(x) {dbDisconnect(conn = x)})
Add Comment
Please, Sign In to add comment