Some nodeMCU connection troubles

For a work project, I’m using a nodeMCU, which is an ESP8266 module flashed with a Lua interpreter (and put on a nice PCB with USB serial and stuff). By the way, ESPlorer is a really nice interface to the nodeMCU.

I started by following an Adafruit tutorial to get WiFi working, which was easy enough. Then I hit an issue where it would silently fail to complete HTTP requests. No obvious errors – it just wouldn’t work. The callback function to print the response wasn’t being called.

Here’s Adafruit’s code:

sk=net.createConnection(net.TCP, 0)
sk:on("receive", function(sck, c) print(c) end )
sk:send("GET /testwifi/index.html HTTP/1.1\r\nHost:\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")

Pretty simple – create a connection, set a receive callback, connect to an IP address and set a GET request.

The Adafruit tutorial also say that you can just use a domain name and let the module do a DNS lookup:

sk=net.createConnection(net.TCP, 0)
sk:on("receive", function(sck, c) print(c) end )
sk:send("GET /testwifi/index.html HTTP/1.1\r\nHost:\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")

This worked for me with the Adafruit website, but when I tried to do the same thing with a different domain name, nothing came back.

After tearing my hair out for a while, the following concept finally struck a chord:

API calls are asynchronous. There is no waiting for them to complete. Application programming must wait for callbacks, not assume completion.

The connect() function is an API call. The Adafruit code shouldn’t just call send() straight afterwards – it has no gurantee that a connection has been made.

I added a seperate callback for the “connection” event and called send inside that:

to_send ="GET /testwifi/index.html HTTP/1.1\r\nHost:\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n"

sk=net.createConnection(net.TCP, 0)
sk:on("receive", function(sck, c) print(c) end )
sk:on("connection", function(sck, c)

This worked just fine!

The actual issue is because when given a domain name, the ESP8266 has to do a DNS lookup. This takes time. If the send() method is called before the connection is made, it just fails.

So, this is a lesson. Wait for the callback from every API call with one attached, no matter how trivial.

Posted in Computing, Electronics | Tagged | 2 Comments

Tsunami Signal Generator “Case”

I recently got a Tsunami signal generator from Arachnid Labs. I could have got a proper case for it, but instead I thought I’d make my own.

I really like making simple lasercut cases, so I threw together a “wave-like” design, cut entirely in 3mm clear acrylic.

Drawing of the acrylic case components

The acrylic case parts, designed in Inkscape

The top and bottom of the case are identically shaped. In order to raise the PCB off the bottom and allow for mounting nuts, I cut four standoffs to hold M4 nuts. The base has four etched areas to locate the standoffs. Holes in the top of the case allow M4 screws to pass through. The top has the tsunami logo etched into it, which I got from their Github page.

Here’s the base:

The base with standoffs and M4 nuts.

The base with standoffs and M4 nuts.

I chose to fill the logo with blue wax to match the PCB colour. Here’s the top after filling with wax, and after hot-air levelling and cleaning:

The top of the casing after wax fill.The top of the case after cleaning

Finally, here’s the case with the tsunami in place:

IMG_20150904_145552 IMG_20150904_134532

I chose to leave the wax slightly underfilled to allow the roughness of the arcylic etch to show through. This sort of goes well with the “turbulent” nature of the wave image.

Design files at my github.

Posted in Electronics | Tagged , , | Leave a comment

Adding graphics to KiCad PCBs

The wrong way to use Bitmap2Component

There are many, many blogposts and videos about how to convert a bitmap image into a KiCad module for putting on a PCB.

Here are a two:

None of them worked for me. I eventually got it working with a slightly altered workflow.

The first step is the same, using Bitmap2Component to create a KiCad module from a bitmap:


With MY version of KiCad, you get a .kicad_mod file. I put this file into my project folder, thinking this this was a fairly sensible thing to do:WrongLocation

Then I added the file itself as a library in PCBnew Preferences->Library Tables:


But when I tried to add this module onto the PCB, I got an error:


Since KiCad changed its library format some time in the past, I thought maybe that Bitmap2Component still produced legacy libraries.

So I changed the Plugin Type from KiCad to Legacy in Preferences->Library Tables.

I got a different error:


After much swearing, I finally figured out what I was doing wrong.

What I was doing wrong (and how to do it right)

Bitmap2Component DOES produce the current (not legacy) module files.

But Library Tables expects the path of the folder they’re in, not the path to the module itself. This is why KiCad has the .pretty folders. They’re full of .kicad_mod files.

So I moved the module file I created to a Graphics.pretty folder inside my project:


And then I changed the Library tables path to point at that folder (with the KiCad plugin type). After that, success!


Apart from image scaling, but that’s a ranty blog post for another day.

Posted in Electronics | Tagged , | 3 Comments

Breakin Board for Just Add Sharks

Those who take an interest in such things will know that Just Add Sharks have released a new breakin board for replacing “standard” Leetro controllers with your own.

I produced the board to a design given to me by Martin from Just Add Sharks. The board should have been easy to knock up, but for the following reasons:

  • My preferred PCB design software is Eagle. The free version of Eagle.
  • The free version of Eagle is not for commercial use.
  • The free version of Eagle only has a board area of 100×80.

While the first reason is probably not much of a concern personally (I doubt Cadsoft will come after me for the footling amount of money I would make), the second limitation is an actual limitation.

I have previously blogged about whether to go for KiCAD or Eagle, but this job made the decision for me. KiCAD it was.

It was my first project in KiCAD, so I had a lot to learn. The main sticking point was around how libraries work, but that seems to be a common thing for new KiCAD users.

The only thing I had to actually ask about was how to hide silkscreen (or objects in general) on the PCB. Each component has a name and value on the silkscreen, but I didn’t need these things. How do I hide them? Well, the answer’s here. You toggle the visibility of the layer in the “Render” tab.

The only thing I wish I’d done was ask if I could put “PCB layout by James Fowkes” or something on the PCB. That would have been an indulgence, but a nice indulgence.

Posted in Electronics | Tagged , , | Leave a comment

Hell yes feminism

Most people who know me will probably be vaguely aware that I consider myself a feminist.

I probably make loads of mistakes while I’m doing it. I am far from perfect.

But just to make it abundantly clear: hell yes feminism.

Posted in Uncategorized | Leave a comment

Renaming things quickly in Eagle

I use Eagle for my PCB design and I generally find it to be a nice piece of software. One annoyance that I came across recently was the seemingly hard task of doing a “bulk rename” of components on a schematic.

I tried searching for how to do this, but couldn’t find an answer that did what I want.

As an example, here’s what I wanted to do:

  • Create six red LEDs (RED1 through RED6) with cathodes connected to GND
  • Create six green LEDs (GRN1 through GRN6) with cathodes connected to GND
  • Create six blue LEDs (BLU1 through BLU6) with cathodes connected to GND

So I’m aiming for this, with LEDs ready for connecting to a driver:


The really slow way to do this is to:

  1. Create all 18 LEDs
  2. Rename them individually
  3. Connect up the cathodes

This is boring, slow and error-prone (about 5 minutes).

The slightly faster way is to:

  1. Create an LED, rename it to RED1
  2. Copy five times, which creates RED2 through 6
  3. Repeat 1 and 2 for GRN and BLU LEDs
  4. Connect up cathodes

This is slightly less boring and error prone (about 2 minutes 30 seconds).

Here’s what I want to do:

  • Create six LEDs, join up cathodes to GND
  • Copy this group twice
  • Rename each group of LEDs in one operation

I couldn’t find any way to perform the last bit (a bulk rename), so I looked into writing my own ULP (User Language Program) to rename a group. The ULP would take a prefix, number and optional suffix as input. Then with the currently selected group of components, rename each using the fixed prefix and suffix, with the number incrementing each time.

For example, the inputs “RED, 1” would rename the selected group “RED1, RED2, RED3, …, REDx”, where x is the last component in the group. The inputs “U, 10, A” would rename the selected group “U10A, U11A, U12A, …, UxA”, where x is the last component in the group.

The ULP for this only took 45 minutes to write by referencing (copying from) the examples and existing ULPs. It shows a confirmation box before committing (the individual rename operations go into the undo history as well).

This script reduced the time it took to create the schematic in the picture above to 1 minute 15 seconds.

I think this will save me a lot of time. The ULP is available my Eagle github repo.

Posted in Electronics, Uncategorized | Tagged , , , | Leave a comment

Depression Quest: First Thoughts

Trigger warning: this post discusses depression in some detail. It is not written by a mental health professional.  Sufferers of depression and similar conditions should think carefully about playing the game it discusses. It is intended to raise awareness and aid understanding for non-sufferers. If you think you may be negatively affected by the game, get a professional opinion or try asking the developer before playing it.

Recently, through the actions of some very thoughtless and blinkered people, I became aware of a person called Zoe Quinn and a game called Depression Quest.

I wonder how many other people have “found” this game through the reaction to the whole #gamergate thing. I hope it got a lot of traction from it. It might make up a tiny bit for the horrific abuse that the creator endured.

I’ve just finished a play-through of the game. Before I go back again, I thought I would share my first reactions.

It’s a deeply emotional game. I don’t have depression, but I know people who do. Before starting, I wondered how this might affect my choices in the game. As it turned out, I don’t think it did, much.

At almost every choice, I found myself thinking “Well, this is obviously the best way to go. This will obviously help.”. This says a lot about how difficult it is to put myself in the mindset of someone with depression. The obvious, rational choice can seem stupid, overwhelming and terribly scary. I know this, and yet still I found it hard to not pick the “obvious” (to me) choice.

Therefore, at the end of my first play through, my character got an “doing pretty well” ending. I’ll be playing through again (and again), in the hope of gaining a deeper understanding of how things can go. As the “ending” of the game says, there is no real end.

Posted in Uncategorized | 2 Comments



For a Nottingham Hackspace project, I was asked if I could measure the RPM of a motor. “Sure!”, I said.

What was really needed was quickly bodging together a beam-break circuit and connecting it to an oscilloscope. This would have taken a couple of hours at the most. But like a proper software engineer, I decided to make the “generic” solution to the problem, which took ages.

There are actually two ways to measure RPM electronically

  • Using a beam-break or reflective sensor and a counter, which provides a direct measurement.
  • Using a strobe light and adjusting the frequency until the rotating object appears stationary.

I went with the second option – building a stroboscope.

System Design

At its heart, a stroboscope is just a rectangular wave generator hooked up to a light source. I wanted a few extra features to make it nice to use:

  • Ability to set flash rate by either frequency or RPM
  • Set duty cycle of output without affecting flash rate
  • LCD display
    • 2×16 for displaying RPM, frequency and duty (one per line, so one will be hidden at any time)
  • Rotary encoder (with button) for main interface
    • When button is not pressed, knob will increase/decrease the value of the selected digit
    • When button is pressed, rotating knob will scroll through display digits
  • Buttons for quickly halfing/doubling thirding/trebling the flash rate
    • This is useful for checking that you haven’t hit on a multiple of the rotation rate
  • Nice beefy output stage for switch big sets of LEDs

The system was prototyped on an Arduino Uno, and then a Nano was used for the actual board. The basic hardware diagram looks like this:

A hand-drawn diagram of the stroboscope hardware. An encoder, four buttons, LCD, power output stage and LEDs connect to an Arduino Nano

The system hardware diagram


There were a few new software modules I had to write for this:

  • A driver for the 16-bit timer in the ATmega
    • I’d written a basic one of these before, but it needed a lot of fleshing out to support the fast PWM mode I was using
  • A generic rotary encoder library
    • This is the first project I’ve built using a rotary encoder – I took the code from the encoder library from PJRC and modified it for my own library.
  • An LCD library
    • Much the the encoder, this is the first project I’ve made using a 16×2 LCD – I took the code from the LCD library from Peter Fluery and modified it for my own library.

In addition, the application was split into UI, LCD and “strobe maths” modules. The application is takes responsibility for the encoder and passes encoder activity to either the strobe or UI modules (depending the state of the encoder button).
The strobe module keeps track of RPM, frequency and duty cycle, keeping them consistent and within limits. The application updates the timer and UI with the strobe data every 100ms.

Diagram showing software modules (timer, encoder, strobe, UI, buttons, LCD) and connections

The software system diagram

Testing Library

This probably deserves a blog post of its own, but it was for this project that I developed my own software test harness specifically for my AVR projects.

I use the unity test suite for testing application level software, but it doesn’t play well with any libraries targeted at the AVR microcontrollers.

It’s still a work in progress, but I can now write a test harness alongside an application and run it with my actual AVR libraries.

Loosely, the process is:

  • A “testing” makefile target calls a python script that parses the io.h file for the target microcontroller.
  • That python script produces a C file with uint8_t variables representing the microcontroller registers and a “fake” io.h with these variables declared as extern.
  • Test harness files for each peripheral, together with test functions in the library files, are able to perform limited “simulation” of external events by setting these “registers”.
  • A test suite (called on each timer tick) sets up these events and tests the result.

I’m pretty happy with this setup. I developed the entire application code and libraries without ever once touching any hardware, and it worked first time. Test-driven development is pretty awesome.


Hardware is my downfall. I’m pretty bad at making good-looking boxes for things. For this project, I took an old modem-router and stripped out the innards. All the resulting holes were capped with lasercut plastic. It looks OK. Not great, just OK.

The stroboscope unit

The stroboscope unit

Here’s the wiring inside. I couldn’t be bothered to do a PCB for this one, despite vowing never to hand-wire anything ever again after the UNIX Clock.

The internal wiring of the stroboscope

The internal wiring of the stroboscope

Finally, here’s the stroboscope in action. The strobe light is a high-power green LED glued into an old microphone boom, screwed into a bit of metal rail. Once you’ve hit the frequency and the motor arm appears stationary, it’s fascinating to see it move as the motor speed slowly changes or the strobe frequency is changed.

A picture of the stroboscope measuring the speed of a motor

The stroboscope in action!

We also tried measuring the speed of Rob’s electric skateboard wheels, with results that didn’t seem too stupid (7kph slow, 15kph fast).

All in all, this was a pretty nice project. Nothing went vastly wrong, although it took more time that I would have liked.

Posted in Electronics, Uncategorized | Tagged , , , | 5 Comments

Get Me To Town!

Ever since I saw this project ( by Gareth James at Brighton Hackspace, I’ve wanted to make my own display like it. It’s a display that shows when the next trains are from his local station to get into Brighton.

I need to get from Beeston to Nottingham, where I have more choices than just one train. There are two different bus stops (both with different routes) and a train station I could use. Working out which one I should use at a particular time requires looking at three different webpages, which is a bit of a faff.

The basic principle of the display is to scrape website(s) for the relevant information and display it in a easy-to-glance-at format. Gareth’s display used a RaspberryPI to perform the scraping and a TFT screen for the display.

Since I have an HP Touchpad that I barely used, I decide it was finally time to put Android on it (Cyanogenmod), and get into the world of app development. Android devices have a “Daydream” mode (a bit like a screensaver) that can come on when charging. To my (limited) knowledge, they’re just applications that declare themselves as daydreams. This was what I chose to write, since I could put the tablet on its wireless charger and automatically display the application, and then just pick it up when I need to use it normally.

The website I chose to scrape for the bus information is For trains I used The “Terms” page asks you kindly not to use it for apps, but I hope that my one-request-every-10-minutes-or-so-on-just-my-tablet application won’t cause an issue.

When the application level update manager decides it needs to perform an update, the data flow from website to the screen is:

GetMeToTown dataflow and components

GetMeToTown dataflow and components

  • An asynchronous URL thread is started that pulls the raw HTML.
  • The HTML data is passed to a class that uses TagSoup to scrape it and produce strings containing the pertinent information.
  • The strings are passed to a handler class that produces a list of individual journey objects.
  • The journey objects are passed to a manager that collates all the journeys from each website and sorts into time order.
  • The application requests journeys from the manager and decides which ones to display (based on user-configurable time and display settings).
  • The display is dynamically re-generated if the number of displayed rows needs changing.

Every 30 seconds, the screen is updated with how much time is remaining for each journey.
This 30-second tick also allows the journey manager to discard old journeys and to decide when to make a request for new website data.
A new request is made each time an item is removed from the list or when 15 minutes has elapsed since the last update. This means that most of the time a lot of data is pulled for journeys already in the list. The reason for doing it this way it means the data is “more live” – in case of delays/cancellations/breakdowns to services.

The application all runs off a state machine (I love state machines. They make things sane.) using stateless4j (, a Java port of the C# “stateless” project (

The 30-second tick is kicked off by a handler registered with the Android API. The tick itself just passes an “update display” request into the state machine, or starts an update if the update manager decides that one is required.

Here’s what the screen looks like on a typical weekday morning:

The GetMeToTown display

The GetMeToTown display

The screen displays eight items, each with an icon showing the transport type (the British Rail symbol for trains, the relevant bus route logo for the four different buses I can take) and the time the service departs. The time is in “minutes remaining” format for journeys in the next 20 minutes, and the departure time for journeys further in the future.

It takes me about 5 minutes to walk to the stops, so the time turns red with 10 minutes to go. The journey is removed from the screen with 4 minutes remaining, since I certainly won’t make it after that.

Most of the numbers (number of journeys to display, update times, journey discard and highlight times) are user-configurable with a Settings activity.

Posted in Computing | Tagged , | Leave a comment

SimCity: Reminiscence Edition

If you read this for electronics stuff, prepare for disappointment. Because this is a sleep-deprivation induced rant about a video game.

SimCity: What the hell happened to you?

Every now and then I feel like going back and playing SimCity. When I was the right age to really appreciate the game, SimCity 3000 was the latest one at the time. It had pleasant, pleasing graphics and soundtrack, lots of scope and didn’t take itself too seriously. The cities you built could grow to enormous size, enough to push the 900MHz Athlon I had at the time to its limits when drawing the whole thing.

Crucially, it wasn’t SimCity 4. I got it on Steam a while back. Argh, that was a mistake.

When I play SimCity, I want to build and run a city. I don’t want to drive around it (that’s what GTA and Driver are for). I don’t want to care about individual Sims in it (more than any others).

What I really want is a more realistic “local government simulator”. For me the best bit about SimCity, apart from the long-term building, was choosing all the progressive, green, lefty policies and having them work.

What I want from SimCity

Features I would love to see in a better SimCity would be:

More realistic building timescales

When you zone land for buildings in the game, it takes time for buildings to appear. If the economic conditions aren’t right, they may not appear at all. I want the same thing for other building. You want three miles of road? 8 months, and the cost is an estimate. You want a new water pipe under that industrial zone? That’s lots of digging. One year minimum, and the economic output of that area will fall. Oh, you need that pipeline now? Too bad, should have planned earlier. You’ve changed your mind about the construction of that new motorway? It’s going to cost time and money to remove what we’ve started.

Realistic limits to what you can build

I was always a bit dubious about building my own power plants. Isn’t that a national government/utilities thing? Since when does a town with a population of 5000 just decide to build a coal fired power station?

Towns aren’t (typically, at least)  owners and operators of their own utilities. However, the local government gets to decide who gets to build what, and where. I want the Sim equivalent of EDF asking to build a new nuclear plant, and the likely impacts presented. I want to be able to refuse, and then I want the chance that the national government forces me to have it anyway and my citizens and I have to cope with that.

The feeling that I’m part of a bigger country

On the subject of national government, I want much more interaction with them. A huge part of local government is balancing the needs of your citizens with the wider national (and indeed global) interest. For instance, the government is going to put a high speed railway through your countryside. Do you support it or not? What do your citizens think? Will it stop in your city, and if it does, will it be a boon or a drain?

On the subject of the countryside, I want something akin to greenbelt laws. Why do my Sims not kick up a fuss when I fill in that lake or raise a forest to the ground? Maybe I should only be allowed to zone a new commercial block if I agree to build a load of new parks – with penalties if I don’t. Maybe the government will decide to stop me zoning altogether in some places. Who knows, but I do know that local authorities can’t just decide that a bit of land will be for houses now – as the regular e-mails from my MP will attest.

The feeling that I’m part of a wider economy

You already get a fair few economic choices in the game, but it’s all fairly local. I want to see the impact of the wider economy. You got to trade water, power, waste with the surrounding towns and cities, but there was never a sense of a fully developed economy. I want the town up the road to get a new factory, pulling activity away from my town and causing massive traffic problems. I want my local economy to be affected by the national one. Employment and investment as part of a national and global whole.

In short…

I want the plans I make to be forced to take time and the wider world into account, not just – as was most often the case in SimCity – money.

That sounds simple enough. Over to you Maxis.

Aside | Posted on by | 2 Comments