R Waters My Garden

I’m at a party, and the topic of programming languages comes up. A quarter of the room politely leaves, another half rudely leaves, and the remaining three people banter about the proper language for a certain project. Bob, Marsha, and I have the room to ourselves.

Tonight we’re talking microcontrollers: Arduino or Raspberry Pi. We like to connect things and aren’t afraid to release the magic white smoke with an ill-advised application of excess voltage. Bob assumes programming is done with micropython. Marsha prefers C.

“I use R to water my garden,” I say. “Yep, I installed R on a Raspberry Pi and use it to turn the water valves on and off.”

Bob snorts and shakes his head. “Why would you do such a thing? R is for statistics – not for flipping relays.”

Marsha, being a C programmer, looks on with an air of superiority. C programmers are superior, but don’t let them know–especially at a party–because once they get started, the remaining 25% of attendees will finally leave the room.

“It’s what I know,” I reply to Bob. “Irrigation only happens once a day, so I don’t need speed. What I need is the most convenient way to express my logic to the RPi. For me, R is my language of choice.

Raspberry Pi Zero WH

Let me explain how I’m implementing the irrigation system. First, I’m using a Raspberry Pi Zero, the low power member of the Raspberry Pi family. Most applications calling for a RPi Zero are trying to embed a single board computer in a device with limited space. The trade off is slower processing speed. The upside (besides size) is low power requirements. I can run this with a 1 amp 5 volt phone charger.

As to processing speed, I have this running a cron job for once a day at 6pm.

0 18 * * * /home/pi/sprinklR

Use R to Pull Weather Data

Among other things, the code running on the RPi pulls weather data from Open-Meteo …

library(httr2)

# get rain forecast -------------------------------------------------------
  
# https://open-meteo.com/
  

meteo_response <-
 request("https://api.open-meteo.com/v1/forecast") |>
    req_url_query(latitude = "45.5234") |>
    req_url_query(longitude = "-122.6762") |>
    req_url_query(current = "precipitation") |>
    req_url_query(daily = "precipitation_sum,precipitation_probability_max,et0_fao_evapotranspiration") |>

    req_perform() |>
    resp_body_json()
 

A few notes on the above code:

  • I use httr2 to handle the internet call. It’s easy and returns JSON.
  • Instead of implementing the entire tidyverse for %>%, I use |> (pipe forward) which has been part of Base R since 4.1.0.

Meteo returns the following response:

> meteo_response
$latitude
 [1] 45.52874

$longitude
 [1] -122.6962

$generationtime_ms
 [1] 0.05698204

$utc_offset_seconds
 [1] 0

$timezone

 [1] "GMT"

$timezone_abbreviation
 [1] "GMT"

...and so on...

More to Come

Bob and Marsha can hardly contain themselves for wanting to tell me all the reasons I’m wasting my time. We refill our drinks, then continue…

But for that, you’ll need to wait for the next post. You could subscribe and I’ll tell you when that happens…

MNR

How about some Tips & Tricks on Programming R?

Sign up to receive content in your inbox every month.

We don’t spam! Read our privacy policy for more info.