Credentials and base url for cumulocity are stored in a user’s private .Renviron
file.
The credentials are defined as follows:
CUMULOCITY_base_url = "[tenant url]"
CUMULOCITY_usr = "[username]"
CUMULOCITY_pwd = "[password]"
The tenant url should be of the form “https://tenant_name.cumulocity.com”.
The file .Renviron
can be edited with usethis::edit_r_environ()
.
Get devices for the tenant and display a few columns. If no arguments are passed, get_devices()
returns all devices.
devices <- get_devices() print(devices[,c("type", "name", "id")]) #> type name id #> 1 c8y_MQTTDevice temp_001 #1 141 #> 2 c8y_MQTTDevice MQTT Device simA1000 1416318 #> 3 c8y_MQTTDevice MQTT Device simA1001 1416323 #> 4 c8y_MQTTDevice MQTT Device simA1002 1416328 #> 5 c8y_MQTTDevice MQTT Device simA1003 1416333 #> 6 c8y_MQTTDevice MQTT Device simA1004 1416338 #> 7 c8y_MQTTDevice MQTT Device simA1005 1416341 #> 8 c8y_MQTTDevice MQTT Device simA9999 1416348 #> 9 c8y_MQTTDevice pos_001 #1 147
Get measurements for device “temp_001 #1” and plot the result. Datetimes are returned as character strings; we first convert to POSIXct
before plotting.
device_id <- 141 meas <- get_measurements(device_id = device_id, num_rows = 100, date_from = "2019-09-30T19:59:00Z") kable(head(meas[c("time", "type", "c8y_Temperature.T.value")]))
time | type | c8y_Temperature.T.value |
---|---|---|
2020-02-06T00:00:02.447Z | c8y_Temperature | -6.691306 |
2020-02-06T00:00:07.453Z | c8y_Temperature | -5.000000 |
2020-02-06T00:00:12.455Z | c8y_Temperature | -3.090170 |
2020-02-06T00:00:17.448Z | c8y_Temperature | -1.045285 |
2020-02-06T00:00:22.456Z | c8y_Temperature | 1.045285 |
2020-02-06T00:00:27.448Z | c8y_Temperature | 3.090170 |
time_parsed <- as.POSIXct(meas$time, format = "%Y-%m-%dT%H:%M:%OSZ", tz = "Z") plot(time_parsed, meas$c8y_Temperature.T.value, type = "l", xlab = "Time", ylab = "Temperature (deg C)")
We can get events for the other device:
events <- get_events(device_id = 147, num_rows = 6, date_from = "2019-09-30T19:59:00Z") kable(events[c("type", "time", "c8y_Position.lng", "c8y_Position.alt")])
type | time | c8y_Position.lng | c8y_Position.alt |
---|---|---|---|
c8y_LocationUpdate | 2020-03-26T21:21:09.105Z | 6.758051 | 0 |
c8y_LocationUpdate | 2020-03-26T21:21:04.111Z | 6.756248 | 0 |
c8y_LocationUpdate | 2020-03-26T21:20:59.104Z | 6.753502 | 0 |
c8y_LocationUpdate | 2020-03-26T21:20:54.104Z | 6.749554 | 0 |
c8y_LocationUpdate | 2020-03-26T21:20:49.104Z | 6.745090 | 0 |
c8y_LocationUpdate | 2020-03-26T21:20:44.095Z | 6.742601 | 0 |
By default, the content is parsed, but it is possible to return a list of JSON objects.
meas_03 <- get_measurements(device_id = device_id, num_rows = 2, date_from = "2019-09-30T19:59:00Z", parse_json = FALSE) print(meas_03) #> [[1]] #> [1] "{\"next\":\"https://db.us.cumulocity.com/measurement/measurements?dateTo=2020-04-29T13:37:20Z&pageSize=2&source=141&dateFrom=2019-09-30T19:59:00Z¤tPage=2\",\"self\":\"https://db.us.cumulocity.com/measurement/measurements?dateTo=2020-04-29T13:37:20Z&pageSize=2&source=141&dateFrom=2019-09-30T19:59:00Z¤tPage=1\",\"statistics\":{\"totalPages\":null,\"currentPage\":1,\"pageSize\":2},\"measurements\":[{\"self\":\"https://db.us.cumulocity.com/measurement/measurements/413574\",\"time\":\"2020-02-06T00:00:02.447Z\",\"id\":\"413574\",\"source\":{\"self\":\"https://db.us.cumulocity.com/inventory/managedObjects/141\",\"id\":\"141\"},\"type\":\"c8y_Temperature\",\"c8y_Temperature\":{\"T\":{\"unit\":\"\\u00baC\",\"value\":-6.6913060636}}},{\"self\":\"https://db.us.cumulocity.com/measurement/measurements/413576\",\"time\":\"2020-02-06T00:00:07.453Z\",\"id\":\"413576\",\"source\":{\"self\":\"https://db.us.cumulocity.com/inventory/managedObjects/141\",\"id\":\"141\"},\"type\":\"c8y_Temperature\",\"c8y_Temperature\":{\"T\":{\"unit\":\"\\u00baC\",\"value\":-5}}}]}"