Posts

raspberry pi ad-hoc wifi with a password, no router needed

I wanted to set up a Pi so that the wifi would work standalone, with no router. You would connect to it like it was an access point, and then be able to access its web server but  no internet. It's rather easy to do this if you put it in ad-hoc mode, but then there's no security.

The alternative is to start setting it up like a full fledged router/access point but stop before the "routing and masquerading" is implemented in the this guide to setting up a pi as an access point. There were two hiccups in following the guide, however. First, if you are doing it over the network, some of the commands break the connection, stopping you mid-way thru setup. 2nd it didn't work until I rebooted. Here's my solution. skip all the commands shown in bold below as they appear in the guide, and make a shell script that you execute once all the configuration files have been nano'ed.

#!/bin/bash
systemctl daemon-reload
service dhcpcd restart
systemctl reload dnsmasq
systemctl unmask hostapd
systemctl enable hostapd
systemctl start hostapd
reboot

updated Pi NAS: automatic hard drive spin-down, and introducing virtual USB unplugging

After running for only a year my last Raspberry Pi Zero NAS died. The hard drive failed. It's not clear if it was the disk's fault, or if the 1A USB hub that powered the drive was insufficient and caused the heads to crash. Either way, the 500GB WD My Passport USB3 drive stopped being able to spin up, making a loud whir-click every 2 seconds or so. Time to build a new setup. The Pi itself seemed fine, thankfully.

I switched to a 2TB WD My Book with its own OEM 12v power supply, so I knew that the supplied power was sufficient. It's somewhat power hungry when spinning but idle: around 6.3w. If you can get it to go into standby mode that drops to 1.7w. Unplugging the USB drops it all the way to .7w. So we HAVE to get the drive to go into standby mode and it would be nice to get the drive unplugged somehow. Out of the box it appears to be stuck always spinning under linux, so there's no option but to dig in.

Unfortunately I was back to square one on making the drive spin down. So far no drive I've worked with (a "coolmax 500gb" and the "WD my passport 500gb") supported the same method for automatically going into standby under linux.

My failures:


hdparm -S 1 /dev/sda did nothing mounted or not.

 hdparm -B 1 /dev/sda  "HDIO_DRIVE_CMD failed: Input/output error APM_level      = not supported".

sdparm --flexible --command=stop /dev/sda just did nothing.

smartctl -d sat --set=standby,now /dev/sda did nothing while the drive was mounted. After unmounting it did.

There are ways to unmount drives that are not in use. While researching them I realized there might be a way to also unplug the usb, virtually, too... So I decided to roll my own solution.

My success: spin down and unplug USB too!


First, we need automount (autofs) which knows how to mount hard drives on demand and unmount after a timeout.

apt-get install autofs

we also need at for some of the scheduling.

apt-get install at

The key thing about autofs is that it executes a shell script before mounting, so we can do whatever magic we want as part of that.

Here's the script, named /etc/auto.disks


#!/bin/bash

# $1 is passed-over from automount
# key refers to the mount point we are looking for
key="$1"

if [ "$key" == "sda" ]; then
  echo 1 > /sys/devices/platform/soc/20980000.usb/buspower
  while ! [ -b /dev/$key ]; # wait until the file can be found
   do
    sleep .5
   done
   echo usbOffOnIdle | at now+11min
fi

# default mount options
opts="-fstype=ext4,rw"

# if a block device exists at /dev/[key]
# pass it back to automount
[ -b /dev/${key} ] && { echo "$opts :/dev/${key}"; }

SDA is my hard drive. We manage it as a special case, turning on the USB port when we want to mount it, and waiting until the device is detected before continuing. We also fire off an at command which will be responsible for shutting off the device when not being used, see below. It  will first start checking for idle at 11 minutes after mounting. 
echo 1 > sys/devices/platform/soc/20980000.usb/ buspower
is raspberry pi zero specific, and turns power on to the USB port (we'll turn it off later, and at boot)

We need to make autofs use this script by editing auto.master and adding 
/mnt /etc/auto.disks --timeout=600
which makes it call our script when trying to mount any device under /mnt, as in ls /mnt/sda . The timeout is in seconds.

So now we have a system that will turn on USB power whenever the drive is mounted, and will unmount 10 minutes after it is last used. But there's no unmount script triggered by autofs, which is why I need at, a handy scheduler that we can run in a loop until it detects the device is unmounted. 

Here's my usbOffOnIdle script, which I put in /usr/local/bin:
#!/bin/bash

if `ls /mnt | grep -q sda`; then  
 echo usbOffOnIdle | at now+10min  
else
 echo unmounted `date` >> /root/log
 smartctl -d sat --set=standby,now /dev/sda
 sleep 10
 echo 0 > /sys/devices/platform/soc/20980000.usb/buspower
fi

I use if `ls /mnt | grep -q sda`; to check for the mount because the regular  bash -e option will actually force the device to mount- oops!

If the drive is still mounted I just call myself again 10 minutes in the future and wait for autofs to do the umount-ing. If it's not mounted, I put the drive in standby (probably not needed) sleep a bit to be sure it happened, and then turn off power to the USB. 

In order to make the drive spin down first, you do need smartctl:

 apt-get install smartctl


Finally, we can use my script to unplug the drive on bootup. Using crontab -e add this line:


@reboot sleep 600; /usr/local/bin/usbOffOnIdle

Wait, that's it?

There's more than that to setting up a NAS, of course. All that was just to keep the power demands low, and to (probably) reduce wear on the drive. My previous post explains how to set up the rest of the Pi Zero NAS using Unison. You can ignore the parts about hard drive idling and fstab.

Some sources:

https://unix.stackexchange.com/questions/101680/automount-post-unmount-script

https://www.raspberrypi.org/forums/viewtopic.php?t=134351

Postscript

It's been running one year with 99.9% uptime. The only downtime seems to be network related but I'm not sure. I have it reboot once a week and that's reliably brought it back up. 


Raspberry Pi Automation

Since we have no network access on the road:

timedatectl set-ntp false

timedatectl set-time 16:00 --adjust-system-clock

map of the gpio pins

control the gpio pins from the shell just by echoing numbers to the file system

Here's a handy bash script for turning on pin(s). Name gpon, in /usr/local/bin, and chmod u+x gpon

#!/bin/bash
if [ ! -e /sys/class/gpio/gpio$1 ]; then
  echo "$1" > /sys/class/gpio/export
  sleep .25
fi

if [ `cat /sys/class/gpio/gpio$1/direction` != "out" ]; then
 echo "out" > /sys/class/gpio/gpio$1/direction
fi

echo "1" > /sys/class/gpio/gpio$1/value

if ! [ -z "$2" ]; then
 shift
 gpon $@
fi


likewise, gpoff:

#!/bin/bash
if [ ! -e /sys/class/gpio/gpio$1 ]; then
  echo "$1" > /sys/class/gpio/export
  sleep .25
fi

if [ `cat /sys/class/gpio/gpio$1/direction` != "out" ]; then
 echo "out" > /sys/class/gpio/gpio$1/direction
fi

echo "0" > /sys/class/gpio/gpio$1/value

if ! [ -z "$2" ]; then
 shift
 gpoff $@
fi


If timing is important, you can do the exporting at boot. 

#!/bin/bash
echo "14" > /sys/class/gpio/export
echo "15" > /sys/class/gpio/export
echo "18" > /sys/class/gpio/export
(etc.)

place in /user/local/bin/autoexec and then crontab -e, adding @reboot autoexec on a line by itself.

Setting up a webserver and PHP

Adding a clock for keeping time while the pi is off. Note the website is junk, no schematics anywhere for the plug-in version we bought. But fyi it plugs in flush with the 5v and 3.3v pins. The plug is passthru so you could connect more to the 5v and 3.3v pins. The guide is also wrong on how to enable i2c - it's under boot. Them the first time I query the clock I get the error "hwclock: ioctl(RTC_RD_TIME) to /dev/rtc to read the time failed: Invalid argument"Running sudo hwclock --show twice seems to solve the problem, or running hwclock --systohc (caution: resets the hw clock to the current system clock) 




starter backpacking trip in san diego alpine region: 4mi out and back on the pacific crest trail

I dreamed up this little trip to introduce my 5 year old to backpacking. It was 4 miles out and back in the laguna mountains along the Pacific Crest Trail weaving in and out of the tree line, with moderate ascent/descent (~1000 feet). Only an hour from central San Diego along the 8 freeway takes you to this entirely different "alpine" biome in the cleveland national forest. No reservations required, but the ranger station does want you to file a permit (easy to do online or over the phone).

We did this mid october. The daytime temps were low 70s, and the night time was 50 or maybe 45. The low was a bit chilly but it's nice to hike in pleasant daytime temps!

We parked at the Red-tailed roost trailhead (free!) around noon. We walked back over sunrise highway and walked s/sw along desert view / thing valley road for about a half-mile to reach the PCT. This single lane dirt road sees minimal use, probably only other fellow dispersed campers. For a road, it's pretty pleasant, with nice tree cover.

Turn left off Thing Valley Road when you reach this sign to join PCT
Note that you cannot camp in the Laguna Mountain Recreational area. You should be safe after you pass the sign above and join up with the southbound PCT but do check the official maps for boundaries.


Once on the PCT we were surrounded by oaks and pines and dappled shade for about 1.5 miles. We could have ended our backpacking trip anywhere along here, there were lots of flat places. At about going about 2+ miles from the red-tailed roost parking lot we reached the first patch of open vistas surrounded low manzanita. This was a short preview of things to come, and then we were back in the forest for another half mile, and our last opportunity to camp unless we wanted to do the full 4 mile trip.

Extra nice campsite ~2.5 miles from start

After that we were back in the manzanita. Little shade and no real options to get off the trail. Nice views though. We were past the midday heat by now which was nice.

Wide open vistas. Not much variety, and all down hill.
The descent was pretty much constant for this part of the trail, maybe 1000 feet over 1-1.5mi.

Then we came into a valley with an actual running stream! With good water filtration I'd feel very comfortable drinking it, but with such a short trip it was easy to carry 4L of water and have enough for the inbound and outbound trip.

The narrow valley was moderately tree-lined (and poison oak, and stickery plants). Thus, while in theory there were lots of places you could have set up camp, in practice we found only 1 area that was clear of offensive plants and nearly flat enough. This was exactly 4 miles from our parking spot.

Enough space for 3 tents, probably no more and you'd better be good friends!

I scouted another 1/2 mile down the canyon and did not see any more likely spots. There was this neat dead tree though.


Supposedly there's another camping option if you were to go a full mile or so down the PCT to around the point that the trail climbs back out of the canyon. You didn't hear it from me, though.

We had plenty of time and light to set up camp, eat, and then it was dark, cold, and the night sky was filled with stars like a san diego city boy wouldn't believe. But, since I grew up in the hills of VA, it was just a nice reminder of a previous life/home.  At nightfall it was 50 degrees out, and I didn't check again but I'm sure it got lower than that, maybe 45. Our tents were warm enough with insulated sleeping pads and bags rated to 35 degrees, but only because we wore our down jackets and warm underwear. I suppose that might be an argument for doing this earlier in the year, but at the cost of a less pleasant trip though the scrub.

Next day we ate and broke camp slowly, leaving just as the sun finally crested our little valley and filled our campsite with its cancer rays. But with the temperatures still low, the climb up and out was very pleasant and we were back in the tree cover before it got too warm.

We had a pleasant walk out the rest of the way and were back at the car by 12:30, exactly 24 hours from when we left. 

The google assistant (aka OK google): can't do attitude

I'm continually disappointed by the google assistant. Before having one I would have guessed the problem would be poor voice recognition - but that's not the case. It seems to convert my speak to text great. It just doesn't want to help.

On my phone:

OK Google...
... turn off my screen (I can't do that yet)
... reboot my phone (I can't do that yet)
... start driving me to mainstreet (just sits there with the overview without actually starting)
... drive me to mainstreet [now] (same)
... start navigation (still doesn't start driving me to mainstreeet)
... search for blah blah blah (responds on my google home mini instead of my phone)

Part of the problem I have is that sometimes I'd rather my phone respond than my google mini. I don't enjoy having webpages read to me for instance. But there's no keyword that means: do this on my phone, not my home assistant. In a similar vein, "hilarity" ensues when a friend comes over and OK googles their phone in earshot of my mini. Both respond, talking over each other like toddlers. Not that the assistant is as smart as a toddler.

What about problems specific to the mini/home speaker? Probably some of these are issues with the phone assistant, too, but I experience them with the mini for sure.

OK google...
...reset timer to 1 minute (sorry I can't change timers on this device)
...set timer for 1 minute (long cutesy response that I've heard way too many times)
...don't be cute (has no effect, but should disable the asinine attempt at humor above)

And then there's reminders. Which used to be tollerable and now are just useless. Here's what I used to do: "...Remind me to buy freezer bags at walmart". And it worked. Sometimes the phone forgot to remind me when I got to a walmart, but OK. Now: "which walmart do you mean (long list follows). And often when I tell it which one: "I'm sorry I didn't catch that". Totally nerfs a otherwise useful feature. Like I really care which walmart I buy frreezer bags.

Another way reminders suck, and always have: they are impossible to hear, and even if you do, the mini won't tell you what the reminder is for. "... remind me to empty dryer in 1 hour". one hour later: "I have a reminder for alan". Why won't you tell me what the reminder is, google? Most of the time "I have a reminder for Alan" takes longer than just saying what the reminder is, and contains WAY less information to boot. I realize that sometimes you might not want the mini to broadcast what it is that you are being reminded of. Well say some keyword that will help you remember like "rosebud" for that extremely rare use-case. Don't ruin the whole feature for a rare use case, google. So maybe the way reminders work is great for some people, I don't know. But here's the easy solution: give us some control, as in "... Change the reminder sound" (can't do it), "...speak full reminders" (huh?) "... reminders sound until dismissed" (why would I want to help you, dave?).

If it sounds like I want reminders to act like alarms, that's partially true. You can name alarms, but just like reminders the name is never spoken, so what's the point?!. I'd be fine if reminders were always wilting violets, and alarms were loud and pushy.  But if they have been named, say the name for goodness sake. Do you think I gave you a name because of my deep and unending love for you, entity that disappears forever by design after just a few minutes?

I could go on and on. Google assistant is such a disappointment. If I didn't already have hardware invested (phone and speaker) I'd try the others in a heartbeat.


How the 3dfx voodoo worked

Lots of interesting technical details. All it really did was draw nicely filtered textures but on a Pentium that was a hard task:

upgrading a HP 2170p to an IPS screen, some assembly required!

original TN LCD
A couple years ago I purchased a "killer" netbook: the HP 2170p. This machine sold for about $1000 new, but these days it's much cheaper because of how old it is. Cheaper than anything remotely in the modern equivalent of the netbook class, anyway. Mine has a i5-3427U CPU which is very capable.

Though it's been a nice machine one aspect of it really disappoints: the LCD. It's a regular TN matrix with poor saturation, brightness, and atrocious viewing angles. I searched to see if HP ever made a variant with an IPS display, but they did not. Then I wondered if maybe there was an IPS display made for a different machine that would fit in the same chassy. Bingo! This Chinese forum post listed a compatible IPS LCD that could be squired cheaply. Thank you google translate!

The LP116WH6 SLA1 is used in the HP chromebook 11 (and perhaps elsewhere).  Since that model was once relatively cheap, and is quite old, replacement parts for it are super cheap. I purchased a grade A used LCD for about $20, shipping included! 

All that remained was removing the old display and inserting the new one. The official disassembly instruction includes a lot of extra steps, but gave me the confidence to unsnap the protective bezel, which was the only part that was at all tricky. Ignore the guide otherwise, it's just necessary to remove a few screws from the screen; the main body of the laptop is untouched.

IPS replacement, before replacing bezel 
The only catch is the IPS screen is not a perfect match for the 2170p: it's just a hair wider. This really only manifests in one issue: the mounting holes for the screws are also a hair too wide. I had to use a drill to make the holes closer to the LCD body. Otherwise it would not have been possible to screw the screen down and it could have fallen out at some point :-( 

I also had to remove some mounting tabs in the computer's lid - easy enough with some snipers & a vacuum up to clean up the magnesium scraps.

Preserving dark detail without washout
The result: fantastic viewing angles, so  much better than the original shown above. The screen is much more readable at lower illumination levels, too (better contrast, I guess).

After I reinstalled the bezel it covered up maybe 2/3 of a pixel on the right side of the screen. This means that a single pixel wide white line actually looks red in isolation. Easy to ignore! The other issue is that it's too saturated and there's a lot of black crush (the darkest 16 or so pixels are mapped to black). The Intel drivers offer enough controls to mitigate this, though if you completely fix the black crush it makes everything look washed out. Check the photo for the setting that are a good compromise, IMHO. I didn't check for black crush on the old LCD but I don't recall any. I wonder if it's an issue of one expecting so called "video" level LUT (16-255), and the other using computer levels (0-255).






Email me

Name

Email *

Message *