{ % feed_meta %} Home | A Few Pinches of Salt

Automated Electric Fence Status Monitoring - Part 2

The last time I spoke about this topic, I spoke about the fact that I had some trouble with getting the LDR to stay attached to the front panel of the fence box…

Over time, due to the fluctuation of temperature, humidity, light, dust, etc. eventually the duct tape lost its stickiness, which resulted in the LDR slowly peeling off of the surface.

This resulted in situations where I was reading false positives from the sensor, which in turn meant that I would get constant emails about the fence being off… When in fact it was functioning perfectly fine.

So, to fix this problem, I have attempted to come up with a partial solution to solve this with the use of double sided tape and a flat piece of wood (or any thin strip of material that has the right shape for my purposes.)

So, knowing that the LDR is basically a thin cylinder, I think I have come up with a solution that uses these three objects.

See the figure below which illustrates how I cut a hole out of a strip of the double sided tape, place the tape into the piece of wood, place the LDR so it faces outwards, and then place this directly onto the face of the fence box.

</embed>

This way, hopefully, it will stay adhered to the surface of the electric fence box while also not allowing in any light from anywhere else except the box.

Let’s see how long this lasts for…


Configuring R Markdown

I really love markdown. The simplicity and clean documents that it can generate. However, lately I have come across RMarkdown. A beefier version of markdown that can do so much more. It can run code, generate plots, including languages other than R which is cool.

But really, I wanted to create a document that follows the style of Edward R. Tufte, and some awesome people have put in the time to make a package that can do exactly this!

First, have R installed, this can be found here.

Then, just because it is more user friendly, install RStudio, it’s a nice light weight IDE that sometimes comes in handy.

Then, install the RMarkdown package inside R. If you want to do this inside of RMarkdown (or R), then all you will need is the following:

install.packages("rmarkdown")

Now, RMarkdown actually makes use of a number of other tools in order to generate the document that you want. Put simply, your ‘.Rmd’ file that you create will be fed into knitr, which will run all of the R code that you have in the file, and it will create a standard markdown file, this has the code and the generated output from each of the code blocks that you may have used. Next, and you are going to have to have pandoc installed for this next part, the compilation process uses pandoc to process the markdown file. This supposedly will generate your file.

Part of the process, naturally uses TeX, which is why, I think, the process generates .tex files.

Some way along the process of generating this document, it got stuck, after it had generated the .tex file.

So, as I already have MiKTeX installed on my system, I ran it there. It apparently needed to install a number of packages to get this to work, and MiKTeX generated the file that I wanted.

So, hoping that this was the reason that R was complaining, I ran the original script again, and voila, it generated the document without an issue!!

Anyways, the following line allows you to generate the document from the command line:

Rscript --default-packages=rmarkdown,tufte GenerateDoc.r

Where ‘GenerateDoc.r’ has:

#!/usr/bin/env Rscript

library("rmarkdown")
library("tufte")
rmarkdown::render('ProtocolDocumentation.Rmd')

The ‘.Rmd’ file also needs to have the following in its header:

---
title: Switching Protocols 
author: Tristan Kuisis
date: '`r paste("First created on Feb 24, 2020. Updated on", Sys.Date())`'
output:
  tufte::tufte_handout: default
  tufte::tufte_html: default
---

Kinda weird, lots of setup to run a single ‘.Rmd’ Anyways, it works now.


How to use gitignore to not ignore

I was recently working on a problem where I needed a git repository to have a folder that is empty. The reason for this was that the folder structure needed to be maintained such that when the repository was loaded into a new system, then dependent applications can easily place files within those already created folders.

Now, the standard way that repository folder structures work is that folders are not tracked if there are not any files within those folders.

More specifically, I want the system to ignore all files within the folder, but to still make the folder when I clone the repository.

Now, this solution doesn’t really circumvent the problem of having to have files within the folders in order to keep the folder tracked, however, it can be seen as empty. This simple method is to place a .gitignore file in the folder, then put the following code in the file (code found here):

# Ignore everything in this directory
*
# Except this file
!.gitignore

I also found this handy command in another answer for adding a .gitignore file in each of the empty folders within the repository:

find . -name .git -prune -o -type d -empty -exec touch {}/.gitignore \;

It’s also obviously had much discussion and is often needed based on the fact that it is one of the FAQ’s on the git website.


Garmin Sleep Analytics

When I got my Garmin watch near the middle of last year, I thought it would help me to better track my day and to optimize how I spend my time. Unfortunately, I eventually realised that the smart watch is only so smart as the feedback that it has programmed into it and how you change your habits based on what it is telling you. The watch has a number of sensors built into it, in general, it measures your steps and heart rate. The use of these sensors allows for a number of inferences to be made such as flights of stairs climbed, stress, and a number of other activities/analyses that are created in order to allow for greater insights. This guy has some useful information on how reliable each of these pieces of information are. This is purely based on the fact that some of these features are inferred from multiple levels down the data collection system.

Anyways, what I really wanted to do was to download my data in order to see what I can do with the actual data. In doing this, I saw that the bulk data download provides some really convenient JSON files that hold a few pieces of data on each day and how you slept. I created a GitHub repository to keep track of this process, it can be found here, it runs you through the process of downloading your own data and extracting it.

I just wanted to get an idea of how my sleep is compared to averages, or how much I should be sleeping.

Sleep Analysis and Research

There are numerous articles on the internet which speak about the best ways in which to sleep, how to organize your day to allow for better quality sleep, foods that you should/shouldn’t eat. I won’t cover these, instead leave this to the user and allow them to interpret their sleep data as they wish.

In order to provide some metrics or abilities to analyse sleep, I have looked at some journals that look into the science of sleep.

This article (and this one) has recommendations on the number of hours in a day that an individual should sleep based on their age. This is purely a recommendation and depends on the individual.

The SleepFoundation.org has some recommendations to attempt to get better sleep:

  • Stick to a sleep schedule, even on weekends.
  • Practice relaxing bedtime ritual.
  • Exercise daily.
  • Evaluate your bedroom to ensure ideal temperature, sound, and light.
  • Sleep on a comfortable mattress and pillows.
  • Beware of hidden sleep stealers, like alcohol and caffeine.
  • Turn off electronics before bed.

Some of these are not particularly suited to analysing with the data, however, I am taking note of some of these that may be easy to implement and test out.

This article, though not from a journal has some notes on what may be done to improve sleep. Take these with a pinch of salt as always.

Sleep Stages

In general, according to Wikipedia (and obviously whoever Wikipedia is referencing) sleep occurs two main stages, Rapid Eye Movement (REM) and Non-REM.

Each of these stages lasts for approximately ninety minutes, this means that in a normal sleep cycle, you will have 4 - 6 of these cycles.

The NREM cycle is broken up into three separate stages: N1, N2, and N3 where the last stage is often referred to as delta sleep or deep sleep.

The normal order of this is:

N1 –> N2 –> N3 –> N2 –> REM

The proportion of REM sleep is higher the closer to waking time. [1^]Interesting, I wonder if this is why I feel like I do most of my dreaming just before I wake up.


Logging Cron Output

Over the last few months I have really spent a lot of time working with Linux. Part of this work means that I have to set up automated systems that are able to log information, run scripts, gather data from sites, etc.

Luckily Linux comes with some really useful tools that allow for automated programs to be run. There are numerous configurations that one can use with crontabs in Linux which I am currently using.

However, when setting up these systems, I often ran into issues with how I configured the scripts, where they were stored, and how they were run.

In addition to this, cron doesn’t natively provide the ability to tell the user what happened when a cron job did (or didn’t) run.

The following template allows the user to set up cron tabs such that their outputs are placed in log files, such that debugging during the cron creation process and future failures can be captured for later debugging.

Users

The first issue that I came across was how the script is run and which user runs the script.

To access the crontab of the user currently logged in can be run with:

crontab -e

This opens the crontab file that has the list of cron jobs that are used in the machine and user 1 2.

Logging Output

Normally, you configure cron to email you when cron jobs fail, it also stores its logs in a distro specific folder.

I want to configure the cron tab so that it will send its output directly to a log file of my choosing, that way debugging can be made simpler and the logs can be directed into specific log files for each job. This cleans up the logging process as I can narrow down which process is causing issues without having to scroll through many entries.

The most simple way that I have found to log the output of cron jobs is the following:

*/1 * * * * /home/user/script.sh >> /home/user/script.log 2>&1

This will place the output from script.sh to script.log.

  1. Note: The cron job will be run as if it were being run by the user that the cron tab file is in. This means that it will run with the permissions given to that user, in addition to the path variables available to that user. 

  2. Also take care about where the cron job is being run from, this should guide you in how to format the relative paths that you use to run the different programs.