Here’s a quick one. I like using a pi as a server for things like mqtt, but with my experimentation I bring it back and forth between home and my local hackerspace. Now, sane people might use DNS to find the mqtt server, I had trouble with that and gave up. I can usually guess the ip address because the router holds on to the one it gave me last time, but that doesn’t always work. Here’s how I decided to have the headless pi tell me the ip it is connected at.
I tried doing this with an HD44780, I have seen people do it with a 10k pot and no other electronics between the pi and display. I tried for a day and couldn’t make that work. My next option was to but a display on a breakout board for the pi, then I can’t doubt my wiring and it either works or it doesn’t. It turns out that a nokia 5110 display is really cheap on ebay, so I looked around briefly and figured that I could get it working when it came in.
Here’s what I found:
the unit I got was wired to gpio that is not on the hardware spi bus, but the pins seemed the same as the one described in this article.
That means using the adafruit libraries described here.
The contrast sucks with the initial image test, but the text looks ok.
I needed to get my ip address in python, I used netifaces as described here.
That broke when there was no cable plugged in, so I caught the exception like this.
I wanted the hostname as well, so I used socket, like this.
I wanted it to start on boot so I used rc.local as found here.
my final code is located on my github here.