-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRasPi_terra_dashboard_code.R
164 lines (147 loc) · 7.59 KB
/
RasPi_terra_dashboard_code.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
library(ggplot2)
library(dplyr)
library(lubridate)
library(shiny)
library(shinyWidgets)
library(stringr)
# Path to sensor readings data in .txt file
readings <- "D:/KMMA_documents/side_projects/RasPi_terra/read.data.txt"
# Names of the species in the terrarium relative to gpio pin reading (use your species names to replace the examples) ####
pin18 = "A. felinus"
pin20 = "U. phantasticus"
pin21 = "E. multicarinata"
pin24 = "E. poecilogyrus"
# Build shiny UI
ui <- tabsetPanel(type = "tabs",
tabPanel("Temperature and Humidity",
fluidPage(
fluidRow(plotOutput("humplot", width = "100%")),
fluidRow(plotOutput("tempplot", width = '100%')),
setBackgroundColor(color = "white"))),
tabPanel("Mean humidity",
fluidPage(
fluidRow(plotOutput("meanhumAfe")),
fluidRow(plotOutput("meanhumUph")),
fluidRow(plotOutput("meanhumEmu")),
fluidRow(plotOutput("meanhumEpo")),
setBackgroundColor(color = "white"))),
tabPanel("Mean temperature",
fluidPage(
fluidRow(plotOutput("meantemAfe")),
fluidRow(plotOutput("meantemUph")),
fluidRow(plotOutput("meantemEmu")),
fluidRow(plotOutput("meantemEpo")),
setBackgroundColor(color = "white")))
)
server <- function(input, output, session) {
data <- reactiveFileReader(intervalMillis = 60000,
session = NULL,
filePath = readings,
readFunc = read.csv)
pal <- c("azure2","gold","plum1","springgreen")
Tthresh <- 45
Hthresh <- 100
#dataframe containing every datapoint
df <- reactive({
df1 <- data.frame(data())
colnames(df1) <- c("hour","date","pin","temp","hum")
time <- parse_date_time(paste(df1$date, df1$hour), orders="dmy HMS")
df2 <- cbind(df1, time)
lastday <- tail(df2$date, 1)
df3 <- df2[which(df2$date %in% lastday),] # select only last day
df4 <- df3 %>% mutate(species =
case_when(pin == 18 ~ "A. felinus",
pin == 20 ~ "U. phantasticus",
pin == 21 ~ "E. multicarinata",
pin == 24 ~ "E. poecilogyrus")
)
df5 <- df4[!(df4$temp>Tthresh | df4$hum>Hthresh),]
df5
})
#dataframe with mean day/night T and humidity per species
df_means <- reactive({
d1 <- data.frame(data())
colnames(d1) <- c("hour","date","pin","temp","hum")
time <- parse_date_time(paste(d1$date, d1$hour), orders="dmy HMS")
d2 <- cbind(d1,time)
lastweek <- unique(d1$date) %>% tail(7)
d3 <- d2[which(d2$date %in% lastweek),]
daynight <- NULL
for (i in as.numeric(str_extract(d3$hour, "\\d{2}"))) {
if (i >= 7 & i < 19) {x<-"day"}
else {x<-"night"}
daynight <- rbind(daynight, x)
}
d4 <- cbind(d3,daynight)
d5 <- d4[!(d4$temp>Tthresh | d4$hum>Hthresh),]
d6 <- d5 %>% mutate(species =
case_when(pin == 18 ~ "A. felinus",
pin == 20 ~ "U. phantasticus",
pin == 21 ~ "E. multicarinata",
pin == 24 ~ "E. poecilogyrus")
)
daynight_temps <- group_by(d6, date, species, daynight) %>% summarise(mean_temp = mean(temp, na.rm = T), mean_hum = mean(hum, na.rm = T))
daynight_temps$date <- as.Date(daynight_temps$date, format='%d/%m/%y')
daynight_temps
})
#plotting
p1 <- reactive({ggplot(df(), aes(x=time, y=hum)) + geom_point(aes(colour = species)) +
labs(x=NULL, y="humidity %") + theme_minimal() + scale_fill_manual(values=pal) +
theme(
axis.title.x = element_text(size = 16, face = "bold"),
axis.title.y = element_text(size = 16, face = "bold"),
axis.text.x = element_text(size = 16),
axis.text.y = element_text(size = 16))})
p2 <- reactive({ggplot(df(), aes(x=time, y=temp)) + geom_point(aes(colour = species)) +
labs(x="\ntime", y="Temperature °C\n") + theme_minimal() + scale_fill_manual(values=pal) +
theme(
axis.title.x = element_text(size = 16, face = "bold"),
axis.title.y = element_text(size = 16, face = "bold"),
axis.text.x = element_text(size = 16),
axis.text.y = element_text(size = 16))})
output$humplot <- renderPlot(p1(), res = 96)
output$tempplot <- renderPlot(p2(), res = 96)
#mean humidity
m1 <- reactive({ggplot(subset(df_means(), species=="A. felinus"), aes(date, mean_hum)) +
geom_point(aes(colour = daynight)) + geom_smooth(aes(colour = daynight), se = FALSE) +
theme_minimal() + scale_fill_manual(values=c("azure2","gold")) + labs(y="\nA. felinus\n", x="")
})
m2 <- reactive({ggplot(subset(df_means(), species=="U. phantasticus"), aes(date, mean_hum)) +
geom_point(aes(colour = daynight)) + geom_smooth(aes(colour = daynight), se = FALSE) +
theme_minimal() + scale_fill_manual(values=c("azure2","gold")) + labs(y="\nU. phantasticus\n", x="")
})
m3 <- reactive({ggplot(subset(df_means(), species=="E. multicarinata"), aes(date, mean_hum)) +
geom_point(aes(colour = daynight)) + geom_smooth(aes(colour = daynight), se = FALSE) +
theme_minimal() + scale_fill_manual(values=c("azure2","gold")) + labs(y="\nE. multicarinata\n", x="")
})
m4 <- reactive({ggplot(subset(df_means(), species=="E. poecilogyrus"), aes(date, mean_hum)) +
geom_point(aes(colour = daynight)) + geom_smooth(aes(colour = daynight), se = FALSE) +
theme_minimal() + scale_fill_manual(values=c("azure2","gold") + labs(y="\nE.poecilogyrus\n", x="")
)})
output$meanhumAfe <- renderPlot(m1(), res = 96)
output$meanhumUph <- renderPlot(m2(), res = 96)
output$meanhumEmu <- renderPlot(m3(), res = 96)
output$meanhumEpo <- renderPlot(m4(), res = 96)
#mean temperature
t1 <- reactive({ggplot(subset(df_means(), species=="A. felinus"), aes(date, mean_temp)) +
geom_point(aes(colour = daynight)) + geom_smooth(aes(colour = daynight), se = FALSE) +
theme_minimal() + scale_fill_manual(values=c("azure2","gold")) + labs(y="\nA. felinus\n", x="")
})
t2 <- reactive({ggplot(subset(df_means(), species=="U. phantasticus"), aes(date, mean_temp)) +
geom_point(aes(colour = daynight)) + geom_smooth(aes(colour = daynight), se = FALSE) +
theme_minimal() + scale_fill_manual(values=c("azure2","gold")) + labs(y="\nU. phantasticus\n", x="")
})
t3 <- reactive({ggplot(subset(df_means(), species=="E. multicarinata"), aes(date, mean_temp)) +
geom_point(aes(colour = daynight)) + geom_smooth(aes(colour = daynight), se = FALSE) +
theme_minimal() + scale_fill_manual(values=c("azure2","gold")) + labs(y="\nE. multicarinata\n", x="")
})
t4 <- reactive({ggplot(subset(df_means(), species=="E. poecilogyrus"), aes(date, mean_temp)) +
geom_point(aes(colour = daynight)) + geom_smooth(aes(colour = daynight), se = FALSE) +
theme_minimal() + scale_fill_manual(values=c("azure2","gold")) + labs(y="\nE. poecilogyrus\n", x="")
})
output$meantemAfe <- renderPlot(t1(), res = 96)
output$meantemUph <- renderPlot(t2(), res = 96)
output$meantemEmu <- renderPlot(t3(), res = 96)
output$meantemEpo <- renderPlot(t4(), res = 96)
}
shinyApp(ui, server)