Publishing IoT IP Addresses to MQTT

I have been playing around with using ESP8266’s as endpoints around the house for collecting temperature, turning the sprinkler pump on, texting me when the doorbell is pressed, etc.  One of the challenges when doing setup/testing is getting the IP address of them when they first come online.

I am running dnsmasq on a raspberry pi, which provides both local DNS and DHCP services.  This is in no way novel, as it is a pretty vanilla configuration.  What I wanted to share is a small script that monitors for updates to the dnsmasq log, watches for new client releases/renewals and then posts those to the MQTT service.  There are lots of things you can do with it from there, but I usually just subscribe to the topic via terminal or mqtttool on my iPhone.

#!/usr/bin/python
from pygtail import Pygtail
import sys
import re
import time
import paho.mqtt.client as mqtt

client = mqtt.Client()
client.username_pw_set("xxxxxx","xxxxxx")

while (1):
 for line in Pygtail("/var/log/dnsmasq.log"):
   isItACK = re.search(r'DHCPACK',line)                #see if its an ACK
   if isItACK:
     newInput = re.sub(r'^.*DHCPACK\(eth0\).', "", line) #strip everything before mac address
     newInput = newInput.rstrip()                        #strip newlines
     client.connect("xxxxxx.widgetninja.net",1883)       #connect to mqtt
     client.publish("/network/DHCP",newInput)            #publish to mqtt
     client.disconnect                                   #hang up
     #sys.stdout.write(newInput)                         #debugging
 time.sleep(10)

Hope this helps save you some time trying to find the ip address of that new device, be it something you made or otherwise just added to your network.

mqtt

Monitor your Garage Doors with MQTT

Its a nighttime ritual for me.  Usher kids to bed, pick up kitchen, turn out lights, and peek into the garage to ensure the overhead doors are closed.  Odds are they aren’t, as someone always seems to have left them open through the normal bustle of the day.

I have been dabbling with home automation, building sensors and devices to automate the mundane.  So to that end, I set out to build something for the garage.  Design goals were:

  • Provide a status of open/closed for the two overhead doors
  • Ability to open/close doors from my phone

The hub of my home system today is Home Assistant (running on raspberry pi) which is very versatile, and allows many integration points.  As many internet of things devices are using MQTT, I am using that as the primary means of communication to/from the hub.

The micro controller used is an esp8266 which has an amazing story in itself; rising from simple beginnings – being initially sold as a uart->wifi adapter and becoming a stand along micro controller thanks to a dedicated group of enthusiasts.  This device can be had for about $8.00, has WiFi, and can run code compiled on the Arduino IDE.

Parts:

This project was fairly straightforward as it stands on the shoulders of projects done by others, as well as some starts and stops I had with recent ESP8266 projects.  The device is powered by a 5v wall wart, which supplies power for the ESP as well as the relay breakout board used.  These components combined with the hall sensors on the doors are all that was needed.  An overview of the hardware connections are here..

In Fritzing:

Fritzing Diagram

In reality:

ESP_WiringCloseUp

Note that the relay outputs that connect to the door ‘buttons’ can actually be connected right at the opener, where the button’s wire comes in.  Thats the blue/white wires on the right side above.

The door sensors are installed on the wall, and triggered by magnets on the doors.  This means any position other than fully closed will register as open (good in my opinion).  Installed they look something like this:

 

On the software side what the ESP8266 is doing is watching for the digital reads of pins D5 (door 1) or D6 (door 2) to change state (open / closed) and when it sees this it’ll post a message on the MQTT server.

The home automation hub takes it from there.  The other thing the ESP8266 is doing is watching for updates on MQTT for requests to open or close the door.  Once a message is notified the ESP will close the relay for a second (simulating a button press) and the door will start moving.  The messages look like this (going from open to closed):

/esp/garageDoorTwo/set 0       #this is the request to close
/esp/garageDoorTwo/state 0    #this is confirming state changed to closed

The actual code on the ESP8266 is linked below.  The highlights are the WiFi join, enabling of over-the-air updates, and the management of the MQTT messaging.

garageDoor.ino code

And if you happen to use HomeAssistant here is the relevant parts of the configuration.yaml file.

config.yaml code

And to wrap things up, here is what the results look like on my iPhone.

IOS_HA_Homescreen

Let me know if you have any questions or clarifications and I’ll try to answer them.

 

Calling this an amplifier build would be deceitful

Its more of a box build.

So browsing adafruit.com a while back I came across a class-d amplifier board they recently introduced. What is interesting about class-d amps is they are very efficient, thus generally needing no heatsinks. Seeing boards like this I am frequently reminded how far things have progressed since my electronics classes in 92/93. In that class I tried to build an amplifier for my car, and really didn’t get much further than etching the board. The traces didn’t etch cleanly, even using the advanced (at the time) sharpie+acid method. After getting board patched up with jumper wires to fix screwed up traces I then realized how expensive power mosfets were for a high school student. End of that project.

So I of course bought this little amplifier board immediately. There has been some conversations around the house that we should have music upstairs. So this board + an old ebay purchased airport express should do the trick nicely. So board in hand, it needed a box. I always skimp on the box, but in this case the board was pre-manufactured so I could afford to put some energy into its home. I haven’t done anything with any hardwoods on the CNC yet, and I had some walnut I got from a friend when he moved. So I set about making a box.

I have no jointer, so surfacing with the CNC, using a 1″ bit.

WP_20140427_002

Flipped the board over and routed a box + lid.

WP_20140427_011

WP_20140427_013

WP_20140519_002

The lid didn’t fit perfectly, and required some chisel work on the inside lip to seat properly. The outside also needed some time on the disc sander to be flush all the way around. I really need to upgrade my y/z axis with some linear bearings. Maybe this winter.

With the overall box built i used the drill press to put some holes in the back to mount the 1/8″ input, the 12v barrel jack, and the 4 speaker posts. Of course one of those holes is a square, so I had to spend some time with the files.

WP_20140519_003

Before mounting anything I needed to finish the back, as it would have been hard to do with all the posts etc in place. I was just going to do a basic coat of poly as this wood was very nice. Got out the rattle-can and gave it the first light coat – which did not work out well. I was not paying attention and grabbed black spray paint instead of my clear lacquer. How could that happen?

WP_20140519_004

Quick wipe down with mineral spirits, but black was deep in the pores. Decided to just blast the whole thing with black, wipe with spirits, and sand again. Turned out ok, gave the wood a darker appearance – although this wont be my ‘go to’ finishing technique.

WP_20140519_005

Mounted the amplifier inside with double-sided foam tape, and soldered up all the connections. Pretty basic.

WP_20140601_004

Functionality test with a 12v 1.5A switching power supply I had in the junk box, and it wouldn’t power up. I kinda forgot that 20w per channel is 40w. At 12v you need 3.5A at full power. I tested instead with a 11.1v Lipo battery from one of my RC airplanes, using alligator wires to the board and all was fine. (fatal mistake)

Double checked everything, glued the lid on, and finalized the finish.

WP_20140605_003

WP_20140605_004

I sourced an old pc power supply, as they can handle crazy current, and modified it to work w/o a pc. Grounding green wire is all it takes. I pulled out all the un-needed connections and put a barrel connector on it.

WP_20140606_003

Brought everything inside, connected up the speakers, power, airport express… and it doesn’t work. Much testing ensues, still does not work. Shamefully bring it out to the garage, and bust it open with a chisel.

WP_20140607_002

Of all things it turns out the barrel connector plug was bad. That was why the initial power supply didn’t work. That is why new power supply didn’t work. The test battery worked because I alligator jumped the leads to the back-side of the connector. Dang.

So now the power supply is hard wired in and everything works fine. This amp does a great job driving bookshelf speakers, and i would use it again. Things I would do differently if i were to do this again (hint : i won’t).

    Not use black spray paint by accident
    Put an indicator LED in the box so I know if there is power
    Turn the box ‘upside down’ and inset a plexiglass bottom that is screwed in (for access)

Overall a fun project, and I learned how nice walnut is to work with. I also finished an amplifier project, that i started over 20 years ago!

Pinball Machine Build

Been pretty lax about updating this blog recently. I have undertaken a fairly time consuming build of a virtual pinball machine. I have been making posts in a forum on that topic as I have required input from that community throughout the build. I am almost(ish) done and will make a video highlighting the process when complete. In the interim, if you want to see some of the gory details you can check out my build log.

WP_20131217_020

Pinball Build

Update : Build complete!

Word Clock Build

This project was one of those long-running ones which took almost 7 months from when I saw the first one out there and wanted to make one, to when I actually got started.  One of the big catalysts was Joe’s blog post on his build – so hats off to him!  Joe did perf board wiring, and I wanted to create a circuit board for the task.  The reason for this was twofold: (1) I hate perf board and (2) I wanted to build the atmega circuit and bootstrap it myself.

So if you watch the video below, you will see I failed at getting my atmega to run.  Could not figure it out!  Next time I will surely include additional LED’s (pin 13 anyone?) to aid troubleshooting.  So, that said in the zip file attached you will find my eagle schematics.  If you use them, be aware the microcontroller portion requires some additional scrutiny.  Maybe its fine, and I just messed up the wiring.  Who knows.  If you want to take a peek at it and give me feedback I would appreciate it!

Other big change/addition from Joe’s build was I added a Real Time Clock (RTC).  In my case I used the ChronoDot.  It is much more accurate, and tracking time in code was MUCH easier.  Here is my code, eagle schematic, and .DXF file for the led array.

WP_20130217_001

 

Lighting Controller :: Part 3

I got the components mounted on the main and remote boards. After much deliberation we dropped $12 per remote node to pick up some waterproof plastic ammo boxes to put the electronics in. These boxes will be out in the yard exposed to snow/rain. The boxes worked out very well, as the 3-gang outlet box fit nicely inside. Just drilled a hole in the bottom so the cords can go in/out.

Wiring up the outlets was pretty easy. Common wire was shared across them all, and the hot (black) wire was sourced from the circuit board (triac). I did not use any ground wire as the Christmas light strings don’t have ground. I also used a pretty light gauge wire as I will only be driving a single string of LED lights per outlet.

WP_20121202_003
WP_20121201_004
WP_20121201_002

Got everything wired up at the neighbors and everything worked well. Even with 100′ cat5 runs to some of the remote boxes everything worked well. We did have some flakeyness where it would lock up occasionally after a few hours. I upgraded the wallwart power supply to an old computer supply, and at the same time fixed a solder joint for the ground wire that looked ‘cold’. Not sure which of the two upgrades fixed the issue, but all is good now.

Below is a picture of the main controller board wired up on the workbench. Will post a video of the light show in the next update!

xmas controller

One thing you may notice is the FTDI controller connected via USB. This is due to the fact the Arduino Mega changed the way they emulate usb/serial. This new chip on the arduino is not identified by the software running the lighting show (Vixen). Using an older-style FTDI board remedied that situation…

2013 Christmas light controller :: Part 2

The controller portion of this project will be based off an Arduino Mega 2650.  Of that the intent is to use 30 of the available digital channels.  The target for 30 is arrived that for each remote board I can get 6 channels pretty easily.  That is driven by 8 wires in an rj-45, with 2 being used for power – leaving 6.  Based off how many lights we actually wanted to hang, we decided 5 groups of 6 channels should fit the bill.  Getting to the design on the Arduino shield I quickly discovered I was limited by the 4×3 inch area provided by the freeware version of Eagle PCB design.  In hindsight I should have just ponied up the credit card and purchased the hobbyist version.  I will probably end up getting that for next years’ inevitable growth.

With the working limitations I had, I needed to break the shield up into two pieces, as I could not fit everything in the 4×3 working area.  I ended up making one board for 18 channels, and another for 12.  Here is a zip with the eagle files.  I warn you in advance the .sch files are not pretty… 🙂  They are free to use for non-commercial purposes.

Overall its a pretty simple design.  Arduino pin goes high, opens the gate on an NPN transistor.  This grounds the led on board lighting it, as well as proving a ground leg for the remote board which turns on the remote LED and Optocoupler which in turn flips the triac on.  Simple daisy chain effect.

master board

Turning that into a working board was a bit trickier, as I couldn’t get it designed as a single sided board.  That mean I had to get the board aligned perfectly after milling one side and flipping it over.  After the first board which I got close but not quite (pictures below) I figured it out on the second board.  It turned out to be much easier than I was making it..

Of importance is the get the board aligned straight along the x axis.  I did this by routing a .1″ deep square the board fit in.  The square was oversized, I just wanted a straight x-axis line.  I then mounted the board against that axis with double-sided tape.  make sure there is 100% coverage on the board.. don’t leave tape gaps as board will flex down on z axis in those spots.  I then aligned the x/y corner and etched the bottom of the board, and drilled the bottom.  The gcode for these operations were created through pcbgcode which I wrote about in another article.

To get the top of the board, flip, re-tape, and align to the x axis.  Then jog the CNC to line up on a few selected holes from the previous drilling operation.  In eagle, hover your  mouse over these holes and you will see the x,y location.  Enter these in your machine, and validate a few other holes.  You should be good to go for etching on the top!

eagle xy

First attempt with trying to measure offsets for the front/back operations.  This created some offset holes, which had to be managed a bit with a dremel grinder.  Still a usable board.

offset2offset1

Second attempt using the eagle offsets described above, which came out very nice.

master1master2

Next update will show assembled boards, and hopefully a video of everything operational.