Friday, 6 October 2017

Autumn Update

Some recent updates on the Github:

  • Interrupted is now compatible with the Arduino (1.8.4 and above) and Energia (18 and above) IDEs. However you have to cut-and-paste the examples, for now.
  • Tasks is also compatible with the Arduino and Energia IDEs, and (maybe) PlatformIO. Ditto caveat about examples.
  • uC-Makefile has been updated for the latest Arduino and Energia IDEs. Unfortunately Energia has discontinued support for the Stellarpad, so this option is now busted.
  • invaders now compiles under Energia 17, the last release to support Stellarpads.
  • WeatherGuy has had an ESP8266 refresh as WifiWeatherGuy. Stay tuned for more information on that.

Friday, 17 February 2017

ESP12-e Emergency Development Board

Mini-breadboards or DIY ESP Development Board?

What do you do when you can't wait for cheap breadboards to arrive from China? Build a development board of course!

The picture shows the work of an afternoon, to make a dev-board for the ESP-12 "IO Adapter Plate Expansion" (ebaysoldered up last time, which inconveniently didn't fit any breadboards around the house.

The circuit is based on this one except that it's powered from the FTDI cable.

The following week the mini-breadboards arrived. Haven't used them yet!

Thursday, 15 December 2016

Further adventures in ESP8266

ESP-12E with Adapter Board
This neat piece of bargain-priced kit arrived in the post from China recently and today I got around to soldering it together. You-Tube-r RandomHacks has a nice video describing how he did it; I'll just summarize my experience here:

  • You really want a magnifying glass!
  • Solder flux is nice to have but not essential.
  • Mount the adapter plate on the header strips and insert in a breadboard before soldering.
  • Tin two pins of the WiFi module at opposing corners and solder them first to hold it secure while soldering the others.
Despite all this I failed to solder a couple of the pins correctly at the first attempt; the magnifying glass to the rescue once again!

Frustratingly the adapter plate is exactly the wrong size for the breadboards I had at hand (too wide for a single one and too narrow for two side-by-side) so I had to connect it with male-female breadboard jumper wires. To program it, I found I had to connect the following pins:
  • Gnd, GPIO15 and GPIO0 to FTDI Gnd
  • Vcc, GPIO2 and CH_PD to FTDI Vcc
  • TxD to FTDI RxD
  • RxD to FTDI TxD
  • Rest to Gnd (momentary, to return to program mode)
Note that my FTDI connector supplied enough current to power the board at 3.3v; you may not be so lucky.

Tuesday, 13 September 2016

Sonoff Wireless Switch

All for less than a fiver!
This little baby is an ESP8266-powered wireless mains relay from ITead. Although it comes with firmware installed which will suit most people, that requires an internet connection as it needs to talk to the Amazon cloud. Luckily for us, some people have done a lot of hard work to provide several flavours of open replacement. I went with the Arduino one and version 1.6.11 of the IDE.

Preparation

  1. Get the ESP8266 core for Arduino by following the instructions. I used the git version.
  2. Get the Arduino version of the Sonoff firmware and read the prerequisite section of its wiki.
  3. I also had to specify version 3.1 of MQTT in PubSubClient.h for correct interoperation with mosquitto on Raspian.
  4. Edit the sketch's user_config.h for your LAN.
  5. Under the IDE tools menu, set Flash size: "1M (64K SPIFFS)"
  6. Solder a pin header onto the board and connect an FTDI cable to the headers.
  7. Holding the Sonoff button down, plug the FTDI cable into the host and upload the firmware.

Setup

  • The Arduino serial monitor comes in handy for troubleshooting, set the baud rate to 115200.
  • The Arduino IDE allows specification of debug level, under the Tools menu, to get firmware debugging messages on the serial port. (Set this prior to upload, obviously.)
  • I had to enable 802.11b networking on my router, before the Sonoff would connect.
  • I installed mosquitto on a Raspberry Pi, and enabled rsyslog on it. I also installed mosquitto-clients on my laptop to keep an eye on the MQTT stream.
  • On power-up the Sonoff creates a temporary access point, allowing direct configuration of networking parameters, if you've made a mistake, at IP address http://192.168.4.1. (This configuration page is also available during normal operation, at whatever address you've configured for it.)
  • Once this firmware has been flashed, it can be upgraded OTA. (I installed nginx on my laptop and had it serve files from my ~/www directory to facilitate this.) 

Friday, 26 August 2016

A Tinier Tiny

Now even tinier!
The photo shows an upgrade to last year's tiny ATtiny development board: now even smaller and with fewer components but with better breadboard performance!

The schematic and board layout (for Eagle) along with gerbers can be found here. The pcb (from Seeed, 1€) was cut down to size with a Dremel.

Thursday, 19 May 2016

Raspberry Pi Media Centre

I have a love-hate relationship with our Raspberry Pi based Kodi Media Centre. On one hand it's cheap as chips and streams all the TV shows we could want (while not being a television) but on the other it's prone to regular heart-attacks when its SD card has been written too much.

I have been in the habit of backing it up religiously every month and have been rewarded by its dying every six months or so. Time to hack some Linux!

(It goes without saying that what follows is merely an optimization of the excellent work done by Sam Nazarko on Raspbmc, now OSMC.)

First thing is to disable Kodi logging entirely. Do this by creating a file /home/pi/.kodi/userdata/advancedsettings.xml:

<advancedsettings>
  <loglevel>-1</loglevel>
</advancedsettings>

Second of all: tmpfs. First we connect the various other temporary directories to /tmp:
$ sudo rm -rf /var/tmp
$ sudo ln -s /tmp /var/tmp
$ rm -rf /home/pi/.kodi/temp
$ ln -s /tmp /home/pi/.kodi/temp

Next we overlay a couple of tmpfs filesystems over /tmp and /var/log (where all of the system daemons write their logfiles). Add the following lines to the end of /etc/fstab:

tmpfs           /tmp            tmpfs   defaults,noatime,nosuid,size=50m    0   0
tmpfs           /var/log        tmpfs   defaults,noatime,nosuid,size=50m    0   0

This creates a couple of ram-disks each limited to 50MB in size. They won't use this RAM unless it's required and if it fills up (it hasn't for me yet) a reboot will start again from scratch.

Useful tools for monitoring disk usage are iostat and fuser (the latter is in the psmisc package). Another is the humble find, for example to see all files modifed within the last day:

$ find /home -mtime 0       
/home/pi
/home/pi/.kodi
/home/pi/.kodi/userdata/Thumbnails/b
/home/pi/.kodi/userdata/Thumbnails/b/b1726aa3.jpg
/home/pi/.kodi/userdata/Thumbnails/b/b8b95467.jpg
/home/pi/.kodi/userdata/Thumbnails/b/b34fab01.jpg
...

Hmmm, looks like there's still some work to do here! Let's move the thumbnails to /tmp too:

<advancedsettings>
  <loglevel>-1</loglevel>
  <pathsubstitution>
    <substitute>
      <from>special://profile/Thumbnails/</from>
      <to>/tmp/Thumbnails</to>
    </substitute>
  </pathsubstitution>
</advancedsettings>

Now that thumbnails are being stored in /tmp, we need to clear them out every so often to avoid filling it up. Add this line to your crontab, with "crontab -e":


0 5 * * mon find /tmp/Thumbnails -type f | xargs rm -f




This will remove all thumbnails every Monday at 5am.



Update: Hackaday just ran a story on Preventing Flash Memory Corruption containing many useful tips; the most relevant one not covered here is filesystem commit interval. By mounting the root filesystem as follows (/etc/fstab again), Linux will flush its buffers to the SD card every 5 minutes instead of every 5 seconds by default:



/dev/mmcblk0p2  /               ext4    defaults,noatime,commit=300 0       0


This is a quick win because frankly who cares if a write recording the fact you watched an episode of Futurama is lost for a potential 60x reduction in the number of writes? Not me!

Monday, 19 October 2015

The Dining Philosophers

It seems as if this blog has been veering towards software for a while now, and this post is about as far from hardware as it's ever been --- but hey, it has LEDs!

The problem of the Dining Philosophers was first described by Dijkstra in 1965 as an example of resource allocation in concurrent (operating) systems. Briefly, five philosophers sit around a table with a bowl of spaghetti in the middle, spending their time alternately thinking and eating. There are five forks at the table, one between each philosopher, and in order to eat, a philosopher requires those adjacent to him.

Two important properties to be addressed by a solution to the problem are: safety and liveness. The first means that the solution is free from deadlock while the second requires that every philosopher eventually gets to eat. There are many solutions but the one implemented here is that proposed by Dijkstra himself: freedom from deadlock is ensured by introducing an asymmetry: the lowest numbered philosopher reaches for his right-hand fork first while the others reach for the fork on their left.


In the solution, each Philosopher is implemented as a Task and each fork as a Semaphore.

Tasks are non-preemptive, light-weight threads of control, they run until they call a blocking function, for example waiting on a Semaphore or sleeping for some time.

There are two kinds of task, the implicit one which runs setup() and loop(), and explicit ones which inherit from class Task and must provide their own stacks, here, 50 words.

In the example, the main setup() initialises the Task system and starts the explicit tasks; it then implements the behaviour of Philosopher #4 in its loop().



In the video, red LEDs indicate a thinking philosopher and green ones a philosopher eating spaghetti.

As always, the Task library is available on Github for Arduino and Energia.