I'm having a lot of fun playing with Python, but I needed a quick project to learn all about APIs. This one is a lot like my son's night light, but uses a Raspberry Pi instead of an Arduino. Raspberry Pis allow me to talk to the outside world since the they're essentially a fully functional computer.
The LEDs are RGB addressable and allow me to change individual lights to any color at any time. Using this concept, I split the lamp into three individual sections:
The bottom is time of day. For example, the color is yellow if it's early, but it'll be purple for twilight hours. The middle is weather (temperature). White or light blue is cold, blue is nice, warm is orange, hot is red. The top is the stock market. When my stocks are doing well, they'll be green.
Thingiverse has this design for a honeycomb lampshade. I scaled it up and printed it in a natural (clear) PLA on the 3D printer.
It took 92 hours and surprisingly came out perfect. However, it needed to a base to house the Raspberry Pi, wires, shade, and tube that holds the LED array. This was designed in Fusion 360 and was surprisingly perfect on the first attempt.
Wiring up the LEDs was easy. NeoPixels need only 5V in, ground, and a data in signal from some type of controller. In this case, a Raspberry Pi 3 is used.
Unfortunately during the virus-related supply chain issues we're current facing, dark green is the only colored filament that looked decent. I'd prefer a black, but this was all there was in my stash.
Individually addressing NeoPixel LEDs can be done using something like:
pixels = (255,0,255) pixels.show()
We can address multiple LEDs by tossing that in a for loop:
for i in range(0,15): pixels[i] = (0,120,56) pixels.show()
For example code that's not covered here, check out my Kid Wake Up Light post.
Accessing APIs is fairly straightforward too. Below is an example of what I used to grab weather data:
import requests import json api_key = 'abc123' url = 'http://api.openweathermap.org/data/2.5/weather?q=my_city&appid=' + api_key r = requests.get(url) obj = r.json() temp = int(obj['main']['temp'])
Essentially you'll need to find a service that has API access, sign up, get a unique key, send a query, and receive results in a JSON format. Parsing JSON is easy as long as you select the right index names. Temperature was pulled in the example above, but the amount of data this API allowed access to wasn't restricted to a simple degree value. I'll eventually have the pixels twinkle if it's raining, or make them slowly move if it's windy.
This project was a lot of fun and certainly passed the time during the 'virus lockdown of March 2020.' This is one of many projects I've completed these last few weeks, but this one in particular was a great learning exercise for Python concepts. I'll need to venture away from doing LEDs for a while and focus more on woodworking and cooking now that it's getting warmer.