This document outlines the process for pulling and analyzing data from Zentra devices, including data manipulation and visualization steps added by Lexi Firth to Oliver Hoffman’s initial API pull script. The script has evolved to include pulling multiple datasets and merging them for comprehensive analysis.
Loading necessary libraries and setting up the environment for accessing Zentra’s API.
# Required libraries
library(zentracloud)
library(dplyr)
library(tidyr)
library(purrr)
library(ggplot2)
library(lubridate)
# Getting the zentracloud package
url <- "https://gitlab.com/meter-group-inc/pubpackages/zentracloud"
remotes::install_git(url = url, build_vignettes = TRUE)
Configuring API access by setting the token and defining the start and end dates for the data pull.
# API token and date range setup
token <- "c0ac11cf6e3ee5e18fbe0f23f2c855efcd2d79d7"
start_date <- "2024-01-01 00:00:00"
end_date <- "2024-02-11 23:59:00"
serial_numbers <- c("z6-22281", "z6-22359", "z6-22401")
# Setting Zentracloud options
setZentracloudOptions(token = token, domain = "default")
getZentracloudOptions()
## <zentracloudOptions>
## ZENTRACLOUD_CACHE_DIR : /Users/lexifirth/Library/Caches/org.R-project.R/R/zentracloud
## ZENTRACLOUD_CACHE_MAX_AGE : 7
## ZENTRACLOUD_CACHE_MAX_SIZE: 500
## ZENTRACLOUD_DOMAIN : zentracloud.com
## ZENTRACLOUD_TOKEN : <-- hidden -->
Looping through each sensor to retrieve its readings within the specified time frame.
# Initialize an empty list for storing data
all_readings <- list()
# Retrieve readings for each sensor
for (sn in serial_numbers) {
readings <- getReadings(device_sn = sn, start_time = start_date, end_time = end_date)
all_readings[[sn]] <- readings
}
Combining sensor data, renaming columns, and reshaping the dataset into a long format for analysis.
# Combine and flatten sensor data
combined_data <- imap(all_readings, ~map_df(.x, mutate, sensor_id = .y, .id = "port_id")) %>%
bind_rows()
# Rename and select relevant columns
combined_data <- combined_data %>%
select(Device_id = sensor_id, port_id, timestamp_utc, datetime,
water_content_value = water_content.value,
soil_temperature_value = soil_temperature.value)
# Convert to long format
long_format_data <- combined_data %>%
pivot_longer(cols = c(contains("water_content"), contains("soil_temperature")),
names_to = "measurement_type", values_to = "measurement_value")
# Reformat port id and match to sensor
long_format_data <- long_format_data %>%
mutate(sensor_id = gsub("_port[12]$", "", Device_id),
port_id = gsub("^(.*)(_port[12])$", "\\2", port_id),
datetime = with_tz(datetime, tzone = "America/Denver"))
Visualizing soil moisture and temperature trends across different devices and ports.
ggplot(long_format_data, aes(x = datetime, y = measurement_value, color = Device_id,
group = interaction(Device_id, port_id, measurement_type))) +
geom_line() +
facet_wrap(~measurement_type + port_id, scales = "free_y") +
labs(title = "Soil Moisture and Temperature Trends",
x = "Time", y = "Measurement Value", color = "Device ID") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
## Merging with Metadata Merging the sensor data with additional
metadata for comprehensive analysis. Uncomment to merge SMdata=
SM_Install_LatLong_sb_for_GIS_10-5-23.csv
# Ensure Device_id is character for merging
long_format_data$Device_id <- as.character(long_format_data$Device_id)
# Assuming 'SMdata' is another dataset loaded previously
# SMdata$Device_ID <- as.character(SMdata$Device_ID)
# Merge datasets
# combined_df <- merge(long_format_data, SMdata, by.x = "Device_id", by.y = "Device_ID")
# Preview combined dataset
# head(combined_df)
Creating a CSV file of the combined dataset for further use.
# Uncomment the line below to export the dataset
# write.csv(combined_df, file = "zentraSMdata_combined.csv", row.names = FALSE)