Posted on January 9, 2018 at 5:00 PM
Keep your Pets Safe with Temperature Monitoring Provided by Raspberry Pi, Twilio, Ting
Some notes on this project:
As it may have become apparent, a lot of my projects are born out of necessity. Someone approaches me with a "wouldn't it be cool if...", I figure out a way to do it with the Pi, and I run with it. In this case, my parents own an RV and take it on trips with our dog. While on those trips, they will sometimes go out for the day and leave the dog in the RV with A/C on, etc. Any RV owner will tell you that there is always a chance of the A/C either freezing over and stopping working (causing the RV to heat up A LOT) or power being lost to the RV (again causing A/C to stop and the R/V to heat up). Both of these options can, and will lead to a very hot pet.
So the problem is simple on the surface, but in the background it's a bit more complicated. We want to either A) Monitor the A/C to see if it's running, B) Monitor electricity and tell if the power is out, or C) Monitor ambient temperature in the RV and notify the end user of dramatic increases. The system I decided on was C, for a few reasons. It would be easiest to build, it would be applicable to more than just an RV (any room that needs to maintain a temperature within a certain range), and I could build it all to run off a battery system so that if electricity is lost, the system still functions.
The system I ended up designing had to be easy to operate (my end goal was to consider making/selling these. If anyone is interested in purchasing one, please contact me), portable, battery powered (or at least battery backup) and above all else, foolproof. The end product is a Pi Zero W, with an Enviro pHat from pimoroni, running off an Anker power bank. The Pi obviously needed some way to contact the end user, so I decided on using Twilio for texting again, much like I had before for a guide. The last step was figuring out how to get the Pi connected to the internet. There are a few ways to do this, I settled on a WIFI hotspot from Huawei powered by a Ting sim card. Theoretically if your RV has some sort of WIFI hookup you could just use that, but if you were considering putting one in your car or some other location without WIFI, the Ting data plan is reasonably cheap and perfect for a small maker like me.
Starting out the project:
Begin by soldering male headers onto the Pi Zero W. Male headers allow for connectivity of the GPIO ports, and are one of the more popular ways to interface hardware with the Raspberry Pi. Begin by taking a 2x20 male header, and inserting the short side onto the Pi Zero W from the top. Once all 40 pins are sitting in the appropriate slots, carefully solder one pin on opposite corners. What this does is hold the header firmly in place as you solder the rest of the pins. If it is your first-time soldering take it slow. Apply heat to both the pin and the circular pad on the Pi, and let solder flow between them. If both the pin and the pad have not been heated, then the solder will only stick to the part that is hot. Once the 2 corner ones are done, go ahead and solder the remaining 38 pins carefully, and be sure that none of the pins are bridged together with solder.
Once the male headers are on the pi, the 2x20 female header needs soldered onto the PiMoronoi Enviro pHAT (a 2x20 female header SHOULD have come in the packaging with the pHAT). Remember that the pHAT goes on top of the Pi, so the female headers need to be inserted from the bottom of the pHAT. On the Pi we put the male headers on the top, and soldered on the bottom. For the pHAT we will insert the female headers from the bottom, and solder on the top. Like the Pi Zero, begin by soldering 2 opposite corners to secure the header, then do the remaining 38.
Once both the male header and female header are soldered together, the hard part is done! Create a bootable SD card image (I used Raspbian Jessie, follow my guide here. Boot the pi while connected to a monitor, connect to your home WIFI, and run updates by typing the following commands into a terminal window:
sudo apt-get update
sudo apt-get upgrade
After all of the updates are done, all of the hardware on the Pi's side is done, and ready to be programmed etc.
Now we will need to make a few accounts, this project uses Twilio for texting, Ngrok for receiving/responding to incoming texts(Kind of, I'll explain more later), and Ting for mobile WIFI. If the location you are putting the system at has WIFI, skip the part about Ting and the mobile hotspot. Minimum you need just the first 2 accounts, for true mobility Ting will be required, let me explain:
Twilio is a texting service (more on that here) that interfaces pretty easily with the python programming language, and can send and receive texts. One of the drawbacks to using it is when it comes to receiving texts. Basically it hosts a local webserver on the Pi that receives and responds to texts. The problem is that the webserver is local, and needs assistance to become truly online.
Ngrok.com provides this assistance. It is a system that takes a local webserver, and 'pushes' it live to a xxxxxx.Ngrok.io web address. Between Twilio and Ngrok, you will be able to send and receive/respond to texts on your Pi, provided you have internet.
Ting is a mobile carrier with VERY reasonable rates for small amounts of data usage (this project uses VERY little data). You can get a SIM card from ting for $9 a month, and ~$1 for 100MB (which is more than enough). Pair this sim card with any GSM hotspot, and you have yourself a mobile hotspot on the cheap!
Making a Twilio account I went over in this guide, so I won't talk too much on that.
To make an Ngrok account, begin by going to Ngrok.com and sign up! You SHOULD pay to get yourself a permanent 'reserved' url by going to the billing tab, and signing up for the basic plan ($60 yearly). The reason this is important: Ngrok will host your 'local' server I talked about above to a webserver, however Ngrok will pick a random url every time you reboot the pi. So there is no way to feasibly tell Twilio every time where to look for your public URL. Purchasing the basic plan from Ngrok will allow you to have a reserved URL instead of a random one, and Twilio will always be able to look in the same place for the outgoing text message info.
After you have generated your reserved URL from Ngrok, go back to your Twilio account. Click on 'phone numbers', then on your phone number issued by Twilio. On the page that loads, towards the bottom, you will see 2 fields: 'A Message Comes In' and 'Primary Handler Fails'. Put your http Ngrok URL in Both of these fields. Now Twilio knows where to look for incoming messages.
Now all of the subscription and web API services are configured on the providers side, it's time to begin setting up the Pi to do what we want it to do!
Boot your Pi, and bring up a terminal window. From here, create and navigate to a directory called scripts in the root of the device with the following command:
sudo mkdir /scripts
sudo cd /scripts
Once inside the directory, we will need to install the necessary python dependencies to run the Enviro pHAT, with the following command:
curl https://get.pimoroni.com/envirophat | bash
Once the command is done, I recommend rebooting the Pi to make sure the changes take.
At this point python has the needed packages installed to handle communication to/from the Enviro pHAT, however it still will need the packages installed to handle Twilio commands, for sending and receiving text messages. Type the following commands in a terminal window to get these packages
sudo pip install twilio
After that is done, reboot the Pi again, and python is all set. There are just a few more things you need to do to get everything ready to go!
Go to the Ngrok web page, and follow the commands to install Ngrok within your /scripts folder. Then, on the Ngrok web page, there is a section that talks about installing your private key. There was a command listed that looks sort of like this:
./ngrok authtoken "your_key_here"
Go ahead and copy that command and run it in a terminal window. This command installs your 'private key' to your Pi, so Ngrok knows who you are, and that you have rights to use their services. Once the key is installed, we SHOULD be ready to get some code up and running. I may upload my source code later, but this is a good starting point to get a lot of the project up and running until I have my code in a more user-friendly format.
Once you have downloaded and updated the code to include your specific API keys etc, a few more steps are needed. In order for this all to work, a local Ngrok server must be running, and the run.py file must be running. To configure both of these to run at startup, first run the following command:
sudo nano /etc/rc.local
and add the following 2 lines (make modifications if you saved the files in different locations):
sudo python /scripts/run.py
(your line of code to initialize your ngrok server)
Assuming both of the services started properly, you should see a few lines on your terminal screen upon reboot, then nothing else. Go ahead and attempt to text your Pi (its phone number is the receive number that Twilio assigned it/that you set as the 'fromNumber'). You should see a line pop up in the terminal, that shows it is receiving and sending a response! If not, check to make sure that you have the Ngrok server setup properly using the private key, that Twilio is set to look for the Ngrok URL on your Twilio page, and that the Ngrok/run.py services are running.
If the Pi is functioning as it should, the last thing you need to do is set it up to work with your Mobile WIFI solution. For me, that means turning on my WIFI hotspot, and connecting the Pi to it. If yours isn’t going to be mobile, then you don’t need to do this step!
Go to Ting.com and create yourself an account. Ting is a mobile phone/data provider who offers VERY reasonable pay-as-you-go plans, with very low costs for small amounts of data usage (this projects uses a few kilobytes). There are other alternatives, however Ting worked well for me. You need a SIM card, and a mobile wifi hotspot (Ting uses the GSM network, same as AT&T). The Mobile Wifi hotspot I purchased is solely to allow for the Pi to be able to send/receive texts anywhere, whether it has WiFi around it or not.
From here, I modified the default run.py that Ngrok gives you to handle incoming texts, and only allow commands sent from my, and my family's numbers. The run.py then calls tempMonitor.py(another python function I wrote) which begins monitoring. As of right now I am not releasing the source code for this project, but there is enough information here for you to begin toying with your own, if you'd like.
Congratulate yourself though, you now have a functional temperature monitoring station that will notify you if things get hot!
Below is a screenshot for what the texting interface looks like:
Raspberry Pi Zero W (Wireless) (new 2017 model)
Pimoroni Enviro pHAT
Anker 20100mAh Portable Charger PowerCore 20100 - Ultra High Capacity Power Bank with 4.8A Output, PowerIQ Technology for iPhone, iPad & Samsung Galaxy & More – Black
Huawei E5330Bs-2 21 Mbps 3G Mobile WiFi Hotspot (3G in Europe, Asia, Middle East & Africa) (white)
Ting Sim Card
Enter your Name and Email to receive notifications about new posts
Feel free to email me at firstname.lastname@example.org with any questions!