Documenting My Code … For Me

There are two signs of old age:

Forgetfulness

and ….

…I forget the second.

Here’s some R code…

cnvmmdrtn <- function(mmfWtr) {
    return(mmfWtr * 271325622)
}

That was ugly

If you are reading this, you’re a coder and use functions. We write them for ourselves. If someone else writes a function, you can hope it works. If it doesn’t, you can hope to fix it. Hopefully, the return value is obviously correct. But maybe it’s subtly wrong?

If things are amiss, read the name of the function and hope it’s descriptive. I worked with a programmer who omitted all vowels from his function names. So the above code would expand to this…

conv_mm_to_duration <- function(mmOfWater) {
    return(mmOfWater * 271325622)
}
Continue reading Documenting My Code … For Me

Varnish Uber Epoxy

Epoxy scares me. Once you brush it on, you’ll never be able to get it off. Epoxy is like breaking an egg; once you start, you can’t put it back. There is no command-Z undo with epoxy.

Even if you were to remove the epoxy-saturated fiberglass, epoxy would have already seeped into the wood to a depth that cannot be sanded away. We traveled to Port Townsend to get help to do it correctly the first time..

Epoxy and fiberglass are great compared to aluminum, canvas, primer and paint, but leave epoxy exposed to the ultra-violet rays in sunlight and it turns yellow and breaks down into dust. Epoxy needs to be protected from sunlight.

Varnish is the ticket…

Continue reading Varnish Uber Epoxy

R Meets Hardware

R is a programming language for statistical computing and data visualization. It has been adopted in the fields of data mining, bioinformatics, and data analysis.

https://en.wikipedia.org/wiki/R_%28programming_language%29

Data mining, bioinformatics, data analysis…

…But not hardware.

We assume R isn’t useful for flipping switches and reading sensors. But that’s an assumption, not a fact.

Let’s be honest. Computer languages are just an abstraction layer on top of the metal. Languages provide constructs to make the expression of logic easier, tailored for the different ways people approach logic and data. But all languages eventually drive the behavior of transistors, and the logic gates and microcircuits built on top of those transistors.

We assume R doesn’t care about transistors. Possibly true, but I don’t believe that’s set in stone. In fact, I’ve proved it’s not true.

Continue reading R Meets Hardware

Party Buzz Kill: modifying data

So Steve (SQL), Marsha (C), Bob (Python), and I (R) are at this party. We have TOTALLY cleared the room, especially now that Steve and I are deep into a debate about saving native data objects to disk versus storing data in a database.

Monica is a real person! She does consulting in Health Data Science. I don’t know if she serves punch.

I see Monica enter from the kitchen, carrying a bowl full of punch. It’s an awkward task and the fruity, sticky liquid is sloshing on the floor. Monica does data science, so I’m hoping she’ll come to my assist. Sure enough, she places the punch bowl on the table and joins us. She’s about to say something when the front door swings open.

Guenter walks in; he just got off a plane from Germany, so he looks a bit jet-lagged. Since the room is filled with a bunch of people talking SQL, he assumes database debates are the theme of the party.

Guenter is a real person too.

“I think I have already written an article in this context,” Guenter begins.

Look – It’s Helen Wall. She’s real too!

Before he can say anything more, Helen speaks up. “Perhaps talking about programming is an attempt to get everyone to leave the house at the end of the party so you can go to bed?” Where Helen appeared from is a mystery.

Monica listens for a minute, then interrupts the pointless debate between Steve and I. “People who are math aficionados” she says, “are a lot more comfortable generating datasets on-the-fly. People like me enjoy relying on the safety and reliability of importing a structured dataset we checked earlier!”

Steve is happy to hear someone is on his side. Steve thinks I’m a knucklehead. There are many people who agree.

“Sure, but there are advantages to not messing around with unnecessary overhead,” I say. “Let’s play with an example.”

I get out a new napkin and sketch out some R code…

Continue reading Party Buzz Kill: modifying data

Irrigation with a Raspberry Pi

My urban garden needs irrigation during the summer months. The simple solution is to use commercially available irrigation timers; they turn on, then turn off. Simple enough. They cost about thirty dollars.

If you forget to bring them in during the winter, they freeze and break. After a few years, the plastic valves wear out and they jam closed (bad for plants) or jam open (bad for water bills). They don’t adjust to rain or hot weather.

You can buy more robust timers. Some of them have Internet connectivity and look up the weather forecast. They cost about $100. If you leave them outside, they freeze and break.

Or… You can use your knowledge of raspberry pi and the assortment of parts in the electronics box to build your own.

Raspberry Pi Zero

I chose a Raspberry Pi for three reasons:

  • I need internet connectivity if I am going to look up weather reports.
  • My preferred language is R, which I can run from a linux operating system, but not from micropython or C
  • I have several Raspberry Pi’s gathering dust in my office. I should use them.

I am using a Raspberry Pi Zero WH. The wireless makes it easy to connect to the internet and the headers provide a convenient way to connect relays and buttons.

The Bits

This project requires a mix of a lot of parts:

  • The Raspberry Pi controller
  • Designing the logic to convert the desired amount of water balanced against rainfall (measured in inches) into seconds to hold the valves open to deliver irrigation water (measured in gallons per hour)
  • Programming the logic in R
  • Controlling relays to control the voltage required by the irrigation valves
  • Plumbing the flow of water from house lines through irrigation valves and out to the drip irrigation system
  • Raspberry Pi Zero WH: $10?
  • Relays: $7 x 2 = $14
  • Irrigation Valves: $15 apiece
  • Random PVC: $25?
  • Power box, transformer, assorted parts: $25

I’ve documented the existing tool at https://github.com/mnr/sprinklR/wiki . This includes lots of photos

There is a dashboard showing performance at https://niemannross.com/sprinklR

This photo shows the development board. It’s just a piece of scrap wood with components temporarily screwed in place. At the top is the power for Raspberry Pi and irrigation valves. In the middle is the Raspberry Pi Zero, below that is the two servos used to control the irrigation voltage, and below that is the irrigation valves.

A jumble of electronics on a wood board

There isn’t any water connected to the system at this point – I’m only trying to test the electronics and develop and test the code. In particular, I’m trying to see if the software & hardware are providing realistic results. I don’t want to give it access to my water bill until I’ve reality checked its performance.

Here is an overall picture of the three parts on the finished board. Left: Irrigation valves and plumbing, Center: Raspberry Pi and relays, Right: power.

Notice I’ve moved the project to a larger and more formal board. This shape was more suited to the layout of the parts. I wanted the plumbing to be physically below power and the Raspberry Pi in case there was a leak.

This is a closeup of the raspberry pi. I’ve housed this in a mason craft food preservation container. Costs about $3 and is waterproof – hopefully something I don’t need.

The board chassis attaches the base of the container and includes a hole for feeding wires through a notch in the base and up to the RPi. Access is by pulling off the jar – it’s held on by a gasket. Because the RPi Zero uses so little energy, cooling is not a problem.

Software

I run this on linux – my programming language of choice is R. The code constantly changes – thank god for git and github. I do a lot of development on a Macintosh, then push to github. I ssh into the Raspberry Pi and pull from github to update the running code. This gives me a slight buffer from the online system, and github provides me with a backup should the memory chip in the RPi fail.

Links

    After Matthew. Before Port Townsend

    Janell and I first saw the canoe on July 4th, 2023. We were in town for a few rushed days between paddling the Apostle Islands in Wisconsin and paddling the Sunshine Coast in Canada.

    Robin, Matthew’s mom, told us about the canoe Matthew started in high school and left in the garage. It was time for this boat to transition. Either finish it, or burn it. Just get it out of the garage.

    We visited the boat between other getting-ready-to-go tasks and could see the shape of an emerging canoe. We noticed it was attached to a substantial strong back. It was not something we could easily move.

    We told Paul, Matthews’ dad, that we were interested, couldn’t take it right now, and would have to figure out some logistical issues. We walked back to the car, excited but puzzled.

    Continue reading After Matthew. Before Port Townsend

    Rain – Evapotranspiration = mm Water

    “Eeee-VAP-oooo-TRANS-PURR-ation,” I savor the word as I release it into our conversation. I’m still at the party with Marsha and Bob. We’re trying to determine why anyone (such as me) would want to use R on their Raspberry Pi.

    Photo by Tima Miroshnichenko

    “Big word,” says Bob. “What’s it mean?”

    “Water evaporation from the earth and transpiration from plants,” I respond. “It’s a sum of the water escaping from my irrigation system. Look it up on Wikipedia.”

    Marsha interrupts grumpy Bob; “So – That means, um…desired amount of water – rainfall + evapotranspiration equals the amount of water your irrigation system needs to supply.”

    “Precisely,” I agree. “Until I found out about evapotranspiration, I was unsure how to account for temperature. I knew hot days would require more water because of increased evaporation; but was stumped how to translate temperature into increased inches of necessary water.”

    “Never heard of it,” says Bob.

    “Me neither,” I agree. “Evapotranspiration is handy, but doesn’t show up in all weather forecasts. Open-Meteo makes it available.”

    “Say you’ve got seven days worth of this miracle number,” says Bob. “What does the R code look like?”

    Continue reading Rain – Evapotranspiration = mm Water

    Boat Lines

    If you’ve spent significant time around boats, you develop an appreciation for the lines of a boat. I’ve shown the canoe to a lot of people, and at some point, people who know boats will walk to the bow, squat, close one eye, and sight along the keel. They assess the line from bow to stern—is it straight?

    Wood strip canoes start with a strongback, a sturdy spine and stations to form the shape. If the strong back isn’t straight, then the lines of the boat will be off. 

    Matthew built the strongback and laid the first strips at the Benson high school wood shop in 2013. He took pictures of his work, and it’s easy to see why the canoe looks like it does.

    Continue reading Boat Lines

    Party Buzz Kill: Data Storage

    I’m at this party where Bob and Marsha and I are discussing the best languages for programming a Raspberry Pi. Bob advocates for Python, Marsha is a devout student of C. I’m defending my use of R. After all, Raspberry Pi starts with R. We have chased all the other guests out of the room with our conversation.

    “With R, I have all sorts of built-in data management,” I say. “Manipulating matrices is in R’s basic DNA.”

    Steve wanders in from the other room and joins our conversation. “Matrices aren’t a proper data strategy. You should be using a database. You can run SQLite on a Raspberry Pi with hardly any effort.”

    Bob and Marsha simultaneously turn to stare me down. They are curious about how I’m going to get around this supposition.

    “Sure. SQL with R–in particular SQLite, would have been easy to implement,” I pontificate. “Just call up RSQLite, push a few buttons, and Bob’s Your Uncle.”

    “And that’s not what you did?” Steve is incredulous.

    “I store the R object on disk and pull it into memory when I need it.”

    “What kind of knucklehead stores data as a file on disk?”

    Continue reading Party Buzz Kill: Data Storage

    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.”

    Continue reading R Waters My Garden