Useful Resources

Reference material or other items like datasheets, articles, or whitepapers. Useful for consideration in projects or general technical reference.

BACKGROUND

Raspberry Pi computers…   they are used for many DIY projects and are even used in production environments for various roles from IoT (Internet of Things) to network servers of all kinds.  They’re great for small industrial uses as well as IoT applications and the reason why is because they have a small power footprint and no moving parts, making them extremely robust.  The Achilles heel however, is the SD card that is used in them!  Seemingly, there are many accounts abound that tell tales of card corruption which can bring down a RPi based application.

Being a seasoned Linux user, I noticed one thing that may be to blame: growing logs & swap file usage in /var/swap & /var/log.   SD cards have a limited amount of write cycles and when they’re used up, the card will likely need to be binned, and a new one written and replaced.  It seems clear that most of the read/write activity seems to be done in the /var directory and so I figure that a better solution to this problem is to setup a USB drive (SSD or thumbdrive), format it for EXT4, and mount it permanently as /var in /etc/fstab.  This article will explain how to do just that, and by doing so, you will significantly prolong the life of your sd card!  Read on:

SOLUTION

The first thing you will notice when you plug a USB drive into your Pi is that it will most likely be formatted as FAT (we want to change this).  You can see the details of your USB drive by running fdisk -l and viewing the output below:

As you can see, in my example, the USB drive is an 8GB size and is already formatted as a “linux” filesystem, but in many cases you would see FAT or NTFS here.  Our goal is to delete any partitions on the USB and create a new one that looks like this example.   You can use any size drive you want, I recommend 8GB or larger – and larger is always better!

After you have created a single Linux partition on your USB device, you then want to write the changes to the USB and then format it.  In this example, the drive is /dev/sda.  We want to format the first partition so that partition will be /dev/sda1 and so we will issue the following command once we write the partition and close fdisk:

mkfs.ext4 -L var /dev/sda1  NOTE: be SURE you know which device you are about to format!!!   we assume your device will be /dev/sda but it could be different so check when you run fdisk -l.   The format can take a few minutes to a while depending on the size of your USB device, so be patient and wait until you are returned to a prompt.  Once back at the prompt, you will have a formatted EXT4 partition on your USB device, and you will be ready for the next step, copying your /var directory to the USB.

To copy the contents of /var to your USB, become root or use sudo and then mount the USB to a mountpoint.  I suggest we create /mnt/usb and mount to that to keep things simple.   execute:   sudo mount /dev/sda1 /mnt/usb then you will now be ready to move your /var contents to the USB.

To move your /var contents to the USB execute:  mv /var/* /mnt/usb/. NOTE: you may get an error on moving the swapfile, this is OK, do not worry about it.  We need to then check /mnt/usb and verify that all our /var stuff is in there by running ls /mnt/usb  and you should see the same directories and files in there as you did in /var.   You are now ready to setup your USB device as the permanent home for the /var location.  To do this, you need to modify /etc/fstab and add an entry to the file showing the usb device mounted as /var as follows (use your favorite text editor – carefully!):

Once you have the fstab file looking similar to this example, you can then save the file, and reboot your Raspberry Pi.   When the Pi reboots, it will now mount /var to the USB drive and no longer use the SD card for swap or for logs and log rotation.   This will greatly save a huge number of write cycles to your SD card!  After you make this change you can also ensure swapping is turned off by running these commands:

sudo dphys-swapfile swapoff
sudo dphys-swapfile uninstall
sudo update-rc.d dphys-swapfile remove

I hope you find this info useful!

 

Because I am an active Ham Radio enthusiast, it is hard to pass up the chance to try new things that come out, especially new radios!  My first experience with digital Ham Radio was D-Star, and while D-Star is great and a lot of fun, I *had* to try DMR eventually.  Unlike D-star, in the beginning, it was hard to come by dual band DMR radios, but a lot has changed since the DMR mode has matured in the Ham Radio community.  DMR, has its roots in commercial land mobile communications, typically business, municipal, and public safety services.  The system was initially developed by Motorola and released as TRBO (MotoTurbo).  TRBO allowed the interlinking (via IP networks) of various repeaters and radio systems in the commercial world.  Each system was often referred to as a zone and a zone consisted of one or more channels and/or talk groups.

Enter 2017, and DMR (compatible with TRBO) is now available by many 3rd party OEMs and even in dual band flavors!  My first DMR radio was the TYT MD-390 (A great radio!) but when I first purchased mine, I paid $179 for a UHF only version without GPS!  Definitely cheaper than your average D-Star radio (most D-Star radios average $350).  It is no wonder a lot of Hams flocked to DMR, you could get TWO DMR radios for the price of ONE D-Star radio!  That simple economic fact allowed DMR to explode and now DMR radios are everywhere, and significantly less expensive.

Recently, I purchased a pair of Radioddity GD-77 Dual Band DMR radios from a trusted vendor on Ebay for $169/pair shipped.  The radios are a bit thinner than the ubiquitous MD-390, and upon unboxing them, I soon realized they were decently made and came with the following items in each box (though I ordered a pair, each radio ships individually):

  • Radio
  • 2200mAH battery
  • Programming Software (Windows)
  • Programming Cable (USB)
  • Belt Clip
  • Antenna
  • Manual
  • Drop-in charger
  • Power cord

Everything you need to operate is in the box!  You just need to install the software and figure out how to program the radio, which is not difficult if you are used to doing this sort of thing.  You basically create channels, then zones, add contacts, add channels to zones, and put your DMR ID in the proper field.  For advanced users, you can enable “expert mode” in the programming software by holding down CTRL ALT SHIFT + F11, and then entering this password: DMR961510.

STERN WARNING: It should be noted that the radio does ship with apparently random frequencies programmed by the factory and these frequencies (if used) would put you in serious trouble if used!!!   If you are a Ham, you are advised to program your radio with the proper frequencies before use.  If you are not a licensed Ham, I recommend you use the radio on the MURS frequencies, or (dare I say it) on the FRS frequencies (the ones often used by the popular bubble pack radios frequently available in stores).  DO NOT TRANSMIT ON THE RADIO UNTIL YOU PROGRAM IT TO PROPER FREQUENCIES, OR YOU COULD BE IN SERIOUS TROUBLE!!!!

The radio has a simple display, (I actually like it more than the MD-390 display, as you can read it without the backlight unlike the display on the MD-3X0.  I had my radios programmed in 5 minutes, and was on the air in no time, without reading the manual.  Who needs a manual anyway?  I have two uses for my GD-77 (I am buying 3 more handsets), 1) Ham Radio  2) FRS/MURS for family communications (keeping tabs on my kids as they play in the neighborhood).  In the later use (kiddie comms) one of the great feature of these radios is that:

  • They can use traditional FM mode – makes them compatible with other non-digital (analog) radios commonly available in stores.
  • TWO WAY TEXT MESSAGING! (across a talk group or 1-on-1) – digital mode only.
  • In digital mode, range is slightly increased and there is NO noise or static.
  • In digital mode, eavesdropping is largely eliminated since most MURS/FRS radios use analog FM – BONUS!
  • In digital mode, these radios can make use of optional encryption just by turning it on – eliminating any eavesdropping opportunity – just as private as a cellphone!
  • In digital mode, you can have one or several “Talk Groups” or privately message 1-to-1 – not possible with traditional FM radios.
  • These radios can enable “remote monitor” which lets you command another radio to start transmitting for a preset time interval so that you can listen to nearby sounds, or conversation that is happening near the far unit.  This is FANTASTIC for keeping tabs on your kids!  There’s no visible indication when you “open their mic” and listen in on what’s going on around them!  This allows parents to proactively check-in on their kids!
  • Programmable buttons for quick sending of alerts or activating functions.

Amazon has the GD-77 with prime shipping for $89/radio.  This is perfect if you are an experienced radio user and quite convenient.

PARENTS & SCHOOLS:  I am willing to offer parents a value added service:  you can buy the radios through me, and I can pre-program them to proper frequencies & with some “secure” channels if you wish to buy several as part of a group.  With this option, you can simply unbox and use them.  If interested in this offer, please call 617-651-1492 and ask for John & I’ll be happy to help.  This offer is available to non-commercial & family end users only.   For commercial users, please look up your nearest radio shop/dealer.

 

 

Sonoff: A Versatile WiFi Switch

I have been playing a lot with home automation technologies and the ESP82XX/arduino platform as hardware interfaces to my ever expanding control system. Most of the stuff I’m implemented has been Z-Wave, but though Z-Wave is an excellent technology, it is rather pricey to acquire. Since the ESP8266 & ESP8285 chips have started showing up in some finished electronics, it is now possible, now more than ever, to reprogram these devices with your own custom firmware!

One such device is made by iTead Studio and is called Sonoff. Sonoff allows you to switch an AC load using your mobile device or PC from anywhere via their own cloud. In my home automation implementation, I want as little cloud involvement as possible – to minimize attack risk, and to keep other firms from data mining my habits at home. The only way to do that is to use technologies that allow local control and without cloud reliance. Unmodified, the Sonoff is an excellent device and generally available for $7 per unit. If you don’t mind sharing your usage data with iTead Studio or relying on their cloud infrastructure to operate your device, then there’s no need to modify it – just use it as prescribed.

For those who want total control and privacy, you can easily use 3rd party firmware or write your own! In my case, I wrote my own firmware which enables a simple HTTP interface which can be called by my Domoticz controller or manually. Since I have a VPN connection to my home on a DDNS hostname, I can easily and securely operate any devices from anywhere in the world with no need for an outside 3rd party, right from my mobile. To flash a custom firmware, you will need a few things:

  • Sparkfun FTDI Basic 3.3v USB to Serial interface
  • 5 pin header (to solder into the Sonoff unit)
  • Arduino IDE software – to program the ESP8285 chip in the Sonoff
  • Screwdriver
  • Soldering Iron
  • Custom Firmware of your choosing or your own
  • Male to Female Dupont wires to wire the FTDI breakout to the header you’ll install (see diagrams)

*** WARNING *** WARNING *** WARNING ***

This project/information deals with MAINS CONNECTED equipment!  You should NOT attempt to undertake anything described herein unless you are familiar with and confident with working with electricity and electrical safety!  Risk of DEATH, FIRE, ELECTRIC SHOCK, and PROPERTY DAMAGE can result if you are attempt anything described here without being familiar with the concepts and safety herein.   I will not be liable for your use of any of this information should you or someone else be killed or injured.  I implore you to seek qualified and experienced help if you are unfamiliar with or unsure of anything described here.  PLEASE BE SAFE!

Let’s Get Started!

First, you will CAREFULLY disassemble your Sonoff unit and remove the curcuit board inside.  You will notice an unpopulated header where you will solder your 5 pin header to.  This is the programming interface.  (click for larger view)

Once you have soldered on the 5 pin header, you will now connect your FTDI serial breakout to your Sonoff:  (Click for larger view)

Once connected, now you will load your firmware into the IDE and choose the “Generic ESP8285” board and 1M size flash as shown: (click for larger view)

To flash: (NOTE: there is NO WAY to go back to stock firmware!  this is a final operation!!!)

  1. Remove 3.3v lead
  2. Hold down button & reconnect 3.3v lead (at the same time)
  3. Release the button
  4. Flash your firmware

That’s it!  Reassemble your Sonoff switch and your Sonoff should be ready to use!

Here’s my firmware.  You are free to use/modify it.   In my application I wanted the IP hardcoded, but you can easily change this to be DHCP.  I’m currently (at the time of this writing) trying to figure out how to serve the HTTP interface from SPIFFS but not quite there yet so for now, it is hardcoded and functional.

Here’s how mine looks:

Very simple!  You’ll notice that in my code, the switch responds to the URLs of /on and /off which makes it stupid simple to integrate with controllers like Domoticz, Wink, Smartthings, etc:  (click for larger view)

Once you configure this in your controller, you can then operate it as any other part of your system, and even with Amazon Echo (Alexa)!

Simple Download Manager for Linux/Mac

If you’re like me and you travel often, you know all too well how badly hotel wifi sucks.  Often, any attempt to download a large file is often met with frustration when the signal drops and you have to restart your download all over again.  Fortunately, there is a simple and blissful answer to this.  Enter, the persistent wget download script:

download-mgr2

As you can see here, only a minimum of code is needed to ensure you get that large file, no matter how many times your hotel connection craps out.  Simply create a password file (if downloading from a SSL site) and supply any needed user/password credentials.  This allows you to simply paste in a link and then walk away while the script does its magic.

Dependencies:

You need to install wget into your linux or mac machine.  Once you have that package installed, then this script will work just fine.

In this example, I’m downloading a large movie file from my NAS at home while I’m overseas.  You can see how this works here:

download-mgr

I’m on the east coast and can’t stay up late to listen live. So, I have setup a script on my Linux server that tunes in his show (I also set it up as a cron job so it is automatic) and the script saves the stream to a file on my NAS each night. From there, I can easily (and quickly) download it to my phone and listen ANYWHERE & ANYTIME without having to pay for access to the time travel archive. It’s like having a dedicated DVR for Art’s show!  It’s pure brilliance! Nice to have you back Art, and so far, the show is OUTSTANDING!

Here, I would like to help others who may be further east than I am and can’t be awake when his show airs.  If you are *somewhat* savvy with Linux, you can use this script to tune in and save his show (WITH BUMPER MUSIC INTACT) on your own system and listen as your schedule and life allow:stream-record

After setting up this script, you must call it in crontab to ensure it runs on Art’s posted schedule (note: please be sure to adjust the time/day values depending on what timezone your system adheres to).  here’s my crontab entry:

00 0    * * 2-6 root    /root/record.sh

This assumes you placed the script in /root/   but it can be any location you like.  Also, be sure you created any temporary and final directories as shown in the script or it will not work!

I wrote this script to only start the d-star daemons on a DV hotspot when there is a viable internet connection.  The reason for this is to simplify “in-the-field” use and only allow the system to start when there is an available internet connection.  Without this script, it was often a problem to ensure that the daemons always started AFTER the internet became available.  This script makes it easy to use a portable DV hotspot on D-Star in a simple “plug & play” way. Copy and paste this script to any text editor of your choice.

PLEASE NOTE: This code was modeled on an ODROID C1 (same price as the raspberry pi) but should be adaptable for the raspberry pi as well.

 

Global Functions File: (copy to /scripts/functions & set executable)

 

#!/bin/bash
#################################################################
#                                                               #
# Global functions for DV Hotspot automated management          #
# release v1    3-13-2015       John Rogers     K1WIZ           #
#                                                               #
#                                                               #
#################################################################

################# Check for internet reachability               
netcheck() {
N1=`ping -c 1 8.8.8.8 | grep '1 received' | wc -l`
	echo $N1 > /tmp/.netflag
}

################# start the gateway IF it is not already running
gwstart() {
        if [ "$G1" -eq 0 ]; then
                if [ "$N1" -eq 1 ]; then
                        /usr/local/bin/dstarrepeaterd -confdir:/etc &
                        	sleep 10
                        /usr/local/bin/timeserverd -confdir:/etc &
                        	sleep 10
                        /usr/local/bin/ircddbgatewayd -confdir:/etc &
                else
                        echo "$(date) NOT STARTED - NO INTERNET CONNECTION" >> /tmp/hs-log.log
                fi
        else
                echo "GATEWAY IS UP - RETURN TO LOOP" > /dev/null 2>&1
        fi
}

################# Check status of daemons
daemon() {
G1=`ps -ef | grep [i]rcddbgate | wc -l`
	echo $G1 > /tmp/.gwflag
R1=`ps -ef | grep [d]star | wc -l`
	echo $R1 > /tmp/.rptrflag
}

################# Misc Checks
safeshut() {
pwroff=`/usr/local/bin/gpio read 27`
	echo $pwroff > /tmp/.shutflag
        if [ "$pwroff" -eq 0 ]; then
                /sbin/poweroff
        else
                return 1
        fi
}
reset() {
restart=`/usr/local/bin/gpio read 26`
	echo $restart > /tmp/.restartflag
        if [ "$restart" -eq 0 ]; then
                /sbin/reboot
        else
                return 1
        fi
}

################# Initialize GPIO Channels
gpioinit() {
	/usr/local/bin/gpio mode 21 out
		/usr/local/bin/gpio write 21 0
	/usr/local/bin/gpio mode 22 out
		/usr/local/bin/gpio write 22 0
	/usr/local/bin/gpio mode 23 out
		/usr/local/bin/gpio write 23 0
	/usr/local/bin/gpio mode 24 out
		/usr/local/bin/gpio write 24 0
}

################# Drive GPIO & LEDS
leddriver() {
	gwflag=`cat /tmp/.gwflag`
		/usr/local/bin/gpio write 21 $gwflag
	rptrflag=`cat /tmp/.rptrflag`
		/usr/local/bin/gpio write 22 $rptrflag
	net=`cat /tmp/.netflag`
		/usr/local/bin/gpio write 23 $net
	shut=`cat /tmp/.shutflag`
		/usr/local/bin/gpio write 24 $shut
}

 

Management Loop Script calling functions above: (copy to /scripts/dvstart.sh & make executable)

(call this script from /etc/rc.local and background it with a “&”)

#!/bin/bash

# THIS SCRIPT WAITS FOR VIABLE INTERNET CONNECTION BEFORE STARTING HOTSPOT
# Written by: John Rogers, K1WIZ
# Permission is granted for free use as long as this notice is intact.

. /scripts/functions


for (( ; ; ))
        do
                safeshut
                reset
                netcheck
                daemon
                leddriver
                gwstart
                sleep 10
        done

So I recently decided to try a Banana Pro ARM microcomputer to evaluate for my next project.  I was disappointed that the unit output had overscan on my TV.  Fortunately there is a simple fix for this annoying problem which the folks at LeMaker do not mention in their forums.

 

1) Press Source on your remote
2) Move down to your HDMI source and press Tools
3) Select Edit name
4) Scroll down to PC and select that one
Your screen goes blank for a second – and mission accomplished.

banana-pro-interfaces

I wrote a short python script to read a GPIO pin and watch for a state change (from a momentary contact switch) in order to execute a safe, clean, & orderly shutdown of the raspberry pi computer, since no on-board provision exists out of the box. It is necessary to ensure a clean shutdown in order to avoid the chance that you might corrupt your SD card:


import os
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(4,GPIO.IN)

prev_input = 0
while True:
input = GPIO.input(4)
if ((not prev_input) and input):
os.system("/sbin/poweroff")
prev_input = input
#slight pause to debounce
time.sleep(0.05)

This script is used as an LED driver to show the operational status of the ircddbgateway & dstarrepeater daemons. If the daemons are up and running, an LED will be lit from the assigned GPIO to show simple operational status. Permission is granted to freely use and distribute for all purposes.


#!/bin/bash

gateway() {
G1=`ps -ef | grep [i]rcddbgate | wc -l`
echo $G1 > /sys/class/gpio/gpio17/value
}

repeater() {
R1=`ps -ef | grep [d]star | wc -l`
echo $R1 > /sys/class/gpio/gpio25/value
}

for (( ; ; ))
do
gateway && repeater
sleep 10
done

For added fun, you might also want to light an LED to indicate viable connectivity to the internet so that you know if you can connect to a reflector or not:


#!/bin/bash

netcheck() {
N1=`ping -c 1 8.8.8.8 | grep '1 received' | wc -l`
echo $N1 > /sys/class/gpio/gpio27/value
}

for (( ; ; ))
do
netcheck
sleep 20
done