R Waters My Garden

[This article was first published on R Programming Archives - Mark Niemann-Ross, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 …


# 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() |>

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
 [1] 45.52874

 [1] -122.6962

 [1] 0.05698204

 [1] 0


 [1] "GMT"

 [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…


The post R Waters My Garden appeared first on Mark Niemann-Ross.

To leave a comment for the author, please follow the link and comment on their blog: R Programming Archives - Mark Niemann-Ross.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)