To distract myself from the anxiety of this interminable election season I decided to channel my angst into a project that displays the results from an aggregated election polling site in real-time. This IoT connected project retrieves the latest polling data from the polling aggegator site FiveThirtyEight.com (which correctly predicted the election outcome of 49/50 states in 2012), and displays the current net liklihood of winning for the two major political parties on a 4-digit 7-segment numerical display.
Step 1: Hardware
The hardware components for this project are:
- Adafruit Huzzah ESP8266 Breakout Board
- Logic Level Converter
- Sparkfun 20 mm 4-digit 7-segment Display
- (4) 5mm LEDs
- (4) 5mm LED mounting holders
- (4) 220 Ω resistors
- Slide switch
- (1) 10 K Ω resistorBreadboard and wires
- 6V 1A Wall Adapter (5V would probably work fine as well)
- USB to TTL Serial Cable (for programming the Huzzah)
I chose the Adafruit Huzzah board to control this project because it combines simple WiFi access using the ESP8266 with Arduino IDE compatibility and enough I/O pins to run the LEDs, switch and 7-segment display. Additionally, it's inexpensive and Adafruit provides some nice libraries and code examples to show how to use various protocols to access data over the Internet.
The logic level converter was necessary because the Huzzah operates at 3.3V and the 7-Segment display operates at 5V. Sometimes, you can trigger 5V logic with a lower voltage input, but I found the display wouldn't work without the converter.
The slide switch allows the user to toggle the view between the Democratic and Republican parties, as desired.
Step 2: Wiring
The wiring is shown on the diagram above. The 5mm LEDs connect to the Huzzah via 220 ohm resistors. In case you can't read the pin numbers on the diagram, the connections are: Power is connected to the board from the 6V wall adapter which plugs into a barrel-jack to breadboard adapter on the breadboard.
- Huzzah Pin 2 -> LED Up (indicates odds of winning have gone up)
- Huzzah Pin 15 -> LED Down (indicates odds of winning have gone down)
- Huzzah Pin 4 -> LED Dem (indicates display is showing odds of a Democratic win)
- Huzzah Pin 5 ->LED Rep (indicates display is showing odds of a Republican win)
- Huzzah Pin 13 -> Logic Converter -> 7-Segment LED RX (Displays the percent chance of winning)
Step 3: 3D Printed Case
The case doesn't fully enclose the electronics, but holds the LEDs in place and shields the electronics from the front view. I designed it in Sketchup and printed it without support. 3D printer design files for the case are at GitHub: https://github.com/geekmomprojects/apocalyptometer
To make the display more visually interesting, I glued on pictures of the symbols for the Democratic and Republican parties over their indicator LEDs (blue for Democrat and red for Republican, of course).
Step 4: Assembly
Assembly is very straightforward. Wire up the electronics on the breadboard as shown in step 2. The image above shows that actual wiring usually looks a lot messier than the diagram.
I soldered the 220 Ω resistors directly to the positive legs of the LEDs to keep wiring simpler. Then stick the breadboard to the back platform of the stand, and push the LED holders through the holes in the front panel. Then push the LEDs into the holders. The 7-segment LED display fits inside the rectangular hole in the front. I didn’t even have to glue it into place. A rubber band helped secure a barrel-jack connector to the breadboard to transmit power from the 6V wall adapter.
Step 5: Software - Raspberry Pi
Here are instructions to install mosquitto on a Raspberry Pi:
and here are instructions to install Node-RED on a Raspberry Pi:
If you want to use this flow directly, here is a link on how to import a flow from a file into Node-Red:
If you use it, you will have to make sure Lynx is installed on your system, and you will need to substitute your own values for the username/password combination to access the mosquitto server.
Step 6: Software - Huzzah
The Arduino IDE code that runs on the Adafruit Huzzah board is available at https://github.com/geekmomprojects/apocalyptometer and uses the Adafruit MQTT library to connect to the MQTT server, publish to the ‘polls/get’ topic to trigger a request for data and subscribe to the ‘polls/current’ topic to receive the latest polling data. I based this code off of one of the examples that comes with the Adafruit MQTT library.
I also used some code from the Sparkfun examples for setting values on the 4-digit 7-segment display available here: https://learn.sparkfun.com/tutorials/using-opensegment?_ga=1.92394845.1939965206.1463025221
The code also responds if the user moves the slide switch to toggle the display between the percentage chance of winning for the Republicans and Democrats. Additionally it lights up the appropriate LEDs to indicate whether the percentage chance of winning has most recently gone up or down.
Step 7: Future Improvements
I’ve contemplated adding an alarm (loud, klaxon-type) to indicate when my preferred party’s support drops below a certain level, or visual cues on how the race is going (e.g. panic/don’t panic display). I'm also joking about adding a Xanax dispenser.
Whatever the outcome of the election this November, I'll at least be able to look proudly at this project to say something positive came out of it!