Welcome to the end of the week! This morning: The reality of women in the video-game industry, creepy-realistic real-time CGI and the Instagram update you've been waiting for.
Engadget RSS Feed
Ever since mentioning my latest little coding project on the IoT Podcast, I’ve heard from a number of folks asking me how I did it.
In case you missed it, I have a small Python app that checks the percentage gain or loss of a cryptocoin every two minutes and sends that data to a connected bulb in my office. The bulb is lit either green to show a gain or red to show a loss. The bulb’s brightness is set to the percentage number gained or lost. A bright green light is a good thing is this case. A brightness percentage change of just one or two percent isn’t much but these coins can fluctuate wildly in price; I’ve seen 50 percent changes in just a few hours at times. In those cases, the brightness indicator is very noticable.
Here’s a short video look during the early stages of the project:
While the use case for my project is very specific, the general concept can be tailored to whatever information you want. For example, if you wanted to know the number of degrees above or below 70 it is outside, you could program a light to be a color representing warmth, such as yellow or orange. So consider this code a basic framework for your own project.
All you need is a both a data source and smart bulb that has a public API available. In my case, I’m getting my data through the Binance API because Binance is the coin exchange I trade on. For the bulb, I’m using the LiFX API that supports my LiFX bulb. The latter is great for basic testing: There’s a web interface to programmatically control your LiFX bulb without any coding involved.
Below is an early, simple version of my Python code as I started: At this point it didn’t light the bulb red to represent a percentage loss. I’ve since added that and other functionality. I’ve also made the code more modular since then, but since it gets pretty specific to my use case, I’m sharing the basic early version as an example that you can build upon for other purposes. Note that I’m a coding n00b so this first version isn’t the most efficient method by any means; I simply wanted to get the base functionality working before splitting the code into reusable functions and such.
To easily send http requests via the APIs, I’m importing the Requests package. I also import the Threading package to allow the code to run repeatedly; in this case, it’s every 120 seconds as shown on line 8.
Using code to programmatically control the LiFX bulb requires an ID token for security purposes. I’ve masked mine in the above image. You can generate a token in your LifX account settings as noted in the API. Lines 16 and 17 are the base URLs for both APIs, which are needed to generate http commands for gathering (or getting) Binance data and changing (or putting) the state of a LiFX bulb.
Next, I’ve set some parameters needed for the http requests the code sends. On line 20, I’ve set a variable for the bulb color in the case of a gain. Remember, this is an early version of the code, so I later set one to represent red for a “loss color”. Line 21 is the symbol for the coin I want to track: In this case, it’s XRP for Ripple coins, but I can set it for any coin on the Binance exchange.
Line 27 generates the http request URL to query the Binance site and it returns coin data in JSON format, which is set to the variable named “data” in line 30. From there, the code looks for the “priceChangePercent” returned in the JSON data and stores it in the variable “percentage”. I now have the percentage gain or loss for my coin.
Next the program uses that data to light the bulb. Here’s a look at the parameters you attach to the base LiFX API URL for doing this with an http request.
Since the LifX API says brightness is a number between 0 and 1, line 35 formats the percentage appropriately and saves that number to a “brightness” variable. The next line creates some parameters to pass over to LiFX, such as the power state, the brightness and the color. And line 38 adds those parameters to the base LiFX URL to change a bulb’s state; once that request is sent to LiFX, the bulb reacts appropriately. That’s it! And every two minutes the process repeats the coin data request followed by any adjustment to the bulb.
Again, this code is specific to my purposes, but you could tweak it by pulling in different data from other sites or use different smart bulbs, provided they have a public API. Now that I have some base functionality, I plan to expand it by illuminating the light to represent my total portfolio gain or loss; that’s trickier because it requires an encrypted account parameter in the http request and I’m still working on that in my code.
I’d also like to rework this project with a set of Nanoleaf light panels instead of a single bulb. That way, I can show more granular data because I have more lights to work with. Regardless, it’s not that difficult to get devices in your smart home telling you information at a glance, so have fun!
As we shared on the Internet of Things Podcast, we now have a dedicated line for listeners to call in and leave a voicemail. In case you need the number, it’s 512-623-7424; be sure to drop us a comment or question!
Instead of turning to Google Voice, Skype or some other readily available voicemail solution, we decided to take an IoT approach and build our own. It wasn’t that difficult and I learned quite a bit from trying to put the pieces together.
We bought a Raspberry Pi 3 Model B with clear case and 2.5A power supply for $ 45 to host the service. Once that arrived, I attached the Pi to a monitor, keyboard and mouse that I already had. Using another computer, I downloaded the Raspian operating system to a microSD card which is need to boot up the Pi. You can purchase a bundle that includes a pre-loaded microSD card, but if you don’t do that, the Pi won’t boot, so you can’t use it to get the operating system.
This Raspberry Pi model supports both wired and wireless networks, so upon the first boot up, I connected to my wireless home network without any problems. Python is pre-installed in the Raspbian build, so I didn’t have to install that. Python is the scripting language I chose to use for the project, however, it can be done with alternatives as well.
Python alone isn’t quite enough though. For phone calls, SMS and voicemail support you need additional tools. Enter Twilio.
Twilio is a low-cost cloud service that provides APIs that add communication services to applications. We bought our phone number, which costs $ 1.00 per month, even if we don’t use for any calls or messages. Twilio charges $ 0.085 a minute for incoming calls and $ 0.013 a minute for outgoing calls. Sending or reciving a text message from our Twilio number costs $ 0.0075, while recording a voicemail is only $ 0.0025. Even if we receive 100 messages a month, the cost of those, plus the phone number, is only two or three dollars.
Before creating and running the Python app that powers the voicemail, the Twilio libraries need to be installed on the Pi through one of these three commands in the Terminal app. Additionally, the Flask micro web development framework needs to be installed in the Terminal. Flask sets up a light web server for the Twilio APIs to interact with while our Python scripts provide instructions for how to handle incoming calls.
Although I’ve spent some time tinkering with and learning Python over the last few months, I don’t have much experience with building apps that call third-party APIs, such as those from Twilio. Luckily, Twilio offers several Quickstart examples for a number of programming languages. And one of those examples shows to how to create a Python app that accepts incoming calls, plays a voicemail greeting and records a caller message. This is the Quickstart code that I tweaked for our project.
Some of the Quickstart code wasn’t necessary for our IoT Podcast voicemail. The first section of code highlighted in the above image, for example, is used to identify callers for a customized greeting, but only if you know the caller’s number in advance. So I removed that. Additionally, there’s a section of code that reads aloud text for the voicemail greeting. We decided to record our own MP3 file for the greeting, so I modified that line as well. You can tweak the sample code however you’d like if you decide to create a similar project.
With all of the libraries installed and the Python code modified, our app is ready to accept calls and record voicemails. But it can’t quite do that just yet. There’s one more step: Twilio actually needs to know where on the internet the Raspberry Pi is running. Since this is a local server, that’s a bit tricky, but there’s a simple solution called Ngrok.
According to its website, Ngrok is a way to “expose local servers behind NATs and firewalls to the public internet over secure tunnels” which is how we connect the Pi server to Twilio services in two steps after downloading and installing Ngrok on the Pi.
First, we run Ngrok with the following command in another Terminal instance: ./ngrok http 5000. Once Ngrok is running, the app provides a unique URL for our local web server, such as https://27195c65.ngrok.io. Now that we have a URL, we simply need to configure Twilio to use it. In the Twilio Console for our phone number there’s a place to add the custom URL and then save the configuration.
Once Twilio and Ngrok are configured, it’s time to launch the Python app in its directory with a simple command line in the Terminal: python vm.py. Now, when a call comes in, Twilio pings our web server which tells it what to do via the Python script.
We decided to beef up our system with a few extra features: server monitoring and voicemail notifications. Both of these were super easy to enable.
For monitoring the online status of the Raspberry Pi, I turned to Uptime Robot, which offers both free and paid plans. Since we’re not an enterprise, the free plan is fine for our needs: Every five minutes, It pings the URL of the webserver on our Raspberry Pi. If there’s a problem, Uptime Robot sends me an email to research the issue.
Note in the above screenshot, our system ran perfectly fine for nearly three weeks. On September 12, however, we must have lost internet connectivity in my house as shown by the 9 minutes of downtime. The Pi and our scripts were running fine, but the webserver didn’t have web connection in the wee hours of the morning. Regardless, that problem thankfully sorted itself out!
Of course, we want to know as soon as an incoming voicemail is saved, so again, it’s Twilio to the rescue. By adding a few lines of code to our Python script, Stacey and I each get an SMS notification telling us there’s a new message.
Note that Twilio’s API requires a separate call for each message to be sent, which is why the similar code repeats once for Stacey’s phone number and then again for mine. If you plan to send bulk messages from Twilio, I expect that you can build a list of phone numbers and loop through them with the API call. Since we only have two contact numbers and don’t expect to add any more, I decided not to loop through a list of two elements.
Once we’re notified of a message, it’s just a matter of logging in to Twilio and downloading the voicemail for an upcoming episode.
There you have it! With an inexpensive Raspberry Pi, a little coding knowledge and effort, Twilio’s excellent APIs, documentation and Quickstart code, you can make your own voicemail server. For that matter, you can do a number of other things related to voice or message communications as well!
If you’d like to elbow your way into the ranks of professional web development, you may want to seriously consider mastering Python as one of your go-to coding tools. It’s easy to learn, easy to use, works well with other coding disciplines and is generally a coder’s best friend when it comes to completing a build on time and under budget. To get Python in your programming utility belt, pick up lifetime access to the Perfect Python Programming Bundle, on sale right now for only $ 24.99 from TNW Deals. Over these three courses, you’ll see why Python is such a…
Web pros are certain about one thing: Python is near the top of the list of essential programming skills, since it’s easy to learn and trusted by a host of big-name companies like Google, Facebook and Dropbox. By picking up the Complete Python Programming Bundle, you’ll have access to an array of training material you’ll need to not only understand Python top to bottom, but also to get a job as a qualified Python practitioner. The seven-course package is on sale right now at over 90 percent off its regular price, only $ 79 from TNW Deals. If you’ve never tackled…