Get notified of your Raspberry Pi’s IP address

I often find that if I connect my Raspberry Pi to a new network, it can be difficult to connect to it via SSH. When it connects, it should register its name on the network, so you should be able to just run ssh pi@raspberrypi, assuming your username is ‘pi’ and your Pi’s hostname is ‘raspberrypi’. But for me, for whatever reason, that name registration rarely works on my network, so I need to work out the device’s IP address in order to login to it.

Wouldn’t it be great if, as soon as your Pi is connected to the network, it could send you an email with its local IP address, so that you can instead connect by running a command like ssh pi@192.168.1.90? DNS doesn’t need to work, and this technique will send you an email whenever the Pi’s IP address changes.

This method requires knowledge of Amazon Web Services, and assumes you already have the AWS command line interface set up on the Pi. If you’re unfamiliar with AWS, you might still find this post somewhat useful, but you’ll need to substitute the part of my code that sends the notification.

AWS setup

  • Create an SNS topic to publish the notifications to
  • Subscribe your email address to the topic. You’ll receive an email with a link to click before the subscription takes effect
  • Make sure the Pi has permission to publish notifications to that topic

On the Pi

To set this up, you’ll already need access to the Pi’s terminal. A way to do this might be to plug in a monitor and keyboard.

Add some code

This script looks at your local IP addresses (v4 only), and gets your external IP address using Amazon’s checkip service. It compares those addresses against those stored in a file /tmp/ip.txt and, if there’s a difference between the previous time it was run, it will send a notification on Amazon SNS.

Put this wherever you want, but a good example might be ~/bin/ipnotify.sh

This file requires an environment variable to be set called TOPIC_ARN. We’ll get to that in the next step.

The handy thing about having ip.txt live in the /tmp directory is that the file gets deleted when the Pi turns off, so this will always trigger a notification the first time it is run.

Systemd timers

To make the code run once a minute,we’re going to create a systemd service and timer.

/etc/systemd/system/ipnotify.service

[Unit]
Description=Notify on SNS when IP address changes

[Service]
Type=oneshot
Environment="TOPIC_ARN=arn:aws:sns:eu-west-2:123456789:my-topic"
ExecStart=/bin/bash /home/pi/bin/ipnotify.sh
User=pi

Remember to change the Topic ARN to the one that matches your SNS topic, and the ExecStart script path to wherever you put that script from the above step.

You’ll want to set the User to ‘pi’ so that the script runs as the ‘pi’ user, otherwise you’ll need to configure the AWS CLI to run as root, and that’s probably not the right thing to do.

/etc/systemd/system/ipnotify.timer

[Unit]
Description=Notification timer

[Timer]
OnBootSec=1min
OnUnitActiveSec=1min

[Install]
WantedBy=timers.target

And this is the bit that makes it run every minute. OnBootSec starts the script 1 minute after booting up, and OnInitActiveSec repeats the script every minute.

To enable this timer so that it runs when you next boot up the Pi, run sudo systemctl daemon-reload followed by sudo systemctl enable ipnotify.timer. If you want to start that timer right now without restarting the Pi, also run sudo systemctl start ipnotify.timer.

Connect the Pi to a new network

If your Raspberry Pi has an Ethernet port, use an Ethernet cable to plug it into your router. Job done.

Setting up WiFi on your Pi is a little more complicated, but there are some handy instructions on the Raspberry Pi website.

A really handy thing I’ve done is configure the Pi to use my phone’s Internet connection using tethering. I edited the /etc/wpa_supplicant/wpa_supplicant.conf file to contain multiple networks like this:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
  ssid="my-home-network"
  psk="hunter2"
  key_mgmt=WPA-PSK
  priority=1
}

network={
  ssid="Dan's phone"
  psk="rosebud"
  key_mgmt=WPA-PSK
  priority=10
}

The priority field is a little confusing. If my home WiFi and phone’s tethering connection are both visible to the Pi, it will prefer the network with the largest value for priority, so in this case the Pi will prefer to connect to my phone.

Tethering

It turns out, yes you can SSH from your laptop to your Raspberry Pi using your phone’s tethering connection.

When you enable tethering on your phone then power on your Pi, the Pi should connect to your phone then send a notification to AWS. With your email address subscribed to that AWS topic, and with your laptop also connected to your phone, you should receive an email from the Pi.

Now that you know the Pi’s IP address, you can SSH into it using ssh pi@192.168.0.33.

Hope this helps someone!

Getting Festive with some Addressable LEDs: TfL Tree

This is a story of how a simple hardware hack got surprisingly out of hand.

Summer of Hacks 2019

It all started in the summer of 2019 during the Oxford Summer of Hacks. One of the events put on was a hardware hack day, where beginner tinkerers could learn about programming real things with the help of more knowledgeable people in the room. I was somewhere in the middle of the scale: I know how to program so I managed to teach a kid and his dad to write some code for a robot powered by a BBC micro:bit, but I needed help when it came to the most basic of hardware tasks.

I had a vague idea of what I wanted to work on during this hack day: after reading a blog post from the Raspberry Pi Foundation I ordered a small screen thinking I could get it to display the Tube status. However, that didn’t arrive in time, so I had to improvise with some LEDs instead.

Continue reading “Getting Festive with some Addressable LEDs: TfL Tree”

#Hackference 2018

Another year, my third Hackference. And what a fun weekend that was!

The Conference

Like last year, the conference was based in thestudio in Birmingham, right next to New Street station so it was really convenient to get there from London in the morning. It was an early start and I didn’t exactly have the earliest of nights either.

Unlike last year, I managed to get to Euston station on time for my 6:43 train 🙌

There were plenty of great talks. Hackference is a 2-track conference, meaning there are usually 2 talks happening at the same time. It was a shame that the talks weren’t recorded this year, because there were a couple of occasions where I would have wanted to be in both talks at the same time. If only I had a time-turner…

Speaking of Harry Potter references, in Marta Bondyra’s talk I got to live the dream of being a real wizard in an awesome WebVR + Speech API demo:

Continue reading “#Hackference 2018”

Spot me an EC2 instance

If you’re looking to save money on your AWS bill, consider switching to a Spot instance.

I’ve been running most of my websites on a single Amazon EC2 instance for about 2 years now. It’s been fine: there’s a bit of setup involved but I’ve become more familiar with Linux servers as a result; and if ever I encounter a problem the solution is never more than a single Google away.

The first year on AWS was great: by creating a new account you get some things in your first year for free. A t2.micro instance (1 CPU, 1GB RAM) was part of this free tier, and it comfortably ran 5 of my little Node.js hacks so long as they don’t get too much traffic.

The following year meant that that same server was no longer free. An ‘on-demand’ instance (where pay per hour that it’s in use) costs just over $9 per month, but if you know you’re going to keep it running for a year, you can pay some of the money up front and bring the effective cost down to $6.50 per month. I thought that was a reasonable price, so I paid that for the second year. Continue reading “Spot me an EC2 instance”

Hackference 2017

Well, it has been almost a year since my previous post here, very much living up to the “occasional blogger” title on my Twitter profile.

I didn’t think there was going to be a Hackference 2017, what with it being ‘the last Hackference’ last year, but here we are.

Annoyingly, I made it to Euston station at 6:44am on the Friday. The train I booked was scheduled for 6:43, and annoyingly left on time, so I had to buy another ticket. £58 poorer than I was hoping to be that day, I got on the next train and still made it to The Studio (the venue for the day) on time.

I’m not going to say much about the talks: they were all really good, inspiring everyone to think differently about programming, design, and to try out some of the new Web technologies.

IMG_1686

Here’s Oxford’s very own Ben Foxall demoing a combination of the Web Audio API, WebGL and Nexmo’s voice API. In short, a visualisation of a phone call between Ben and his mum, happening in real time!

The next day was the hackathon. 24 hours of working on whatever you want, with whomever you want, from midday on Saturday to midday on Sunday. There were a few sponsors who ran challenges to help focus the direction of the hacks, including Microsoft. Microsoft were encouraging the use of their Cognitive Services APIs – a collection of machine learning features making it easy for developers to add image recognition, OCR, speech-to-text etc. to their own applications; and they would award a prize to the team with the best use of their APIs.

What follow are the slides from my presentation at the end. Continue reading “Hackference 2017”

Hackference 2016, Part 2 – The Hackathon

<- Part 1: the conference
On Saturday, once everyone had got a good night’s ‘sleep’ (our hotel was just next to a loud club which only got quiet after 3am), we walked over to the Impact Hub.

Before I get started, I’m using the word ‘hack’ in the non-malicious form. Tabloids use the word ‘hacker’ to describe someone with malicious intent who steals data or takes down websites (for example, this article in the Mirror). We use it to describe someone who thinks the best way of learning is by doing. A hackathon is just a group of sleep-deprived developers playing with something new.

The event itself was free – paid for completely by the wonderful sponsors.

Before the hackathon started, not many people knew what they wanted to make (including me). Luckily the sponsors got a chance to inspire us with their products and announce their prizes. There was a variety of companies attending, each with some cracking prizes for the teams making best use of their services.

Continue reading “Hackference 2016, Part 2 – The Hackathon”

Hackference 2016, Episode IV: A New Hackference

Last weekend saw me heading to Birmingham to attend Hackference, a 3-day event for all sorts of programmers. There was a conference on Friday at the Electric Cinema, followed by a 24-hour hackathon at the weekend.

I’d never been to Hackference before, so I didn’t know what to expect. It’s a shame it’s probably going to be the last one though.

What follows is my account of the weekend, aided by plenty of tweets. The weekend was so jam-packed with stuff to blog about, so I’m going to split it up in two.

Part 1 – the conference

The Electric Cinema was a really cool venue. There were two screens, so two talks could happen at the same time. This was good because we could choose the talks that interested us most, but bad because most of the time I wanted to see both! Continue reading “Hackference 2016, Episode IV: A New Hackference”

Realtime hacking with JSOxford

Yesterday I went to a hack day run by JSOxford. The theme was ‘realtime’, i.e. using Web technologies to update a site automatically from a data source.

Since I had no experience with realtime technologies before I came, I didn’t want to make anything too ambitious! I just wanted to learn the basics so that I could make something useful in the future.

Continue reading “Realtime hacking with JSOxford”

Code Golf at Bath Ruby User Group

Golf: normally, unless it’s of the ‘Crazy’ or ‘Adventure’ variety (is there a difference?), I will stay well away from it. That’s mainly because my arms don’t swing and it involves quite a lot of standing and waiting around outside. However, yesterday evening I played a game of Code Golf.

The game goes like this – there are 9 problems (or ‘holes’ if you will), and you need to write a method to solve each one. The problems are fairly simple (I was reminded of first year programming), but the aim was to solve the problems in the fewest number of characters possible. This meant if you stood any chance of winning, you would have to squash your code into something compact and unreadable.

Continue reading “Code Golf at Bath Ruby User Group”