Skip to content

5. Software Guide

rirozizo edited this page Apr 13, 2020 · 6 revisions

If the below walls of text are a bit confusing, please use This Video Playlist from YouTube. I'll be explaining everything from there as well.

AdafruitIO

  • Create an AdaFruit IO Account HERE
  • Go to io.adafruit.com
  • Create a new Dashboard. I chose to call it "Home Automation", you can call it whatever you want because this won't be related to any code.
  • Create a new Block
  • Select Toggle
  • In the Choose feed screen, create a new feed and call it master. You'll use this switch to control the ability to change all other switches in your project.
  • Name your block anything you want and hit create at the bottom right. By default, the text to turn the toggle on would be ON, and OFF would turn the toggle off.
  • Create a new Block
  • Select Toggle
  • In the Choose feed screen, create a new feed and call it something you'll use in your code to indicate a switch to control the equipment. I chose ac because I want to control my Air Conditioner
  • Name your block anything you want and hit create at the bottom right. By default, the text to turn the toggle on would be ON, and OFF would turn the toggle off.
  • Create a new Block
  • Select Indicator
  • In the "Choose feed" screen, create a new feed and call it something you'll use in your code to indicate the status of the equipment. I chose ac-status
  • Name your block anything you want and hit create at the bottom right. Under conditions, set the parameter to "=" and set the text to turn the toggle on to ON.
  • Create a new Block
  • Select Indicator
  • In the Choose feed screen, create a new feed and call it something you'll use in your code to indicate the status of the Raspberry Pi service. I chose service-status
  • Name your block anything you want and hit create at the bottom right. Under conditions, set the parameter to = and set the text to turn the toggle on to ON.
  • Click on AIO Key and save the long randomly generated key in the file /autohome/aiokey.txt We will use this later on in the script to communicate with AdaFruitIO

Monitor RaspberryPi Temperature and CPU Clock Speed (Optional)

This section is still a WIP

IFTTT (Optional)

  • Create an IFTTT Account HERE
  • Click on your profile, then click Create
  • Click on the word This
  • Search for Google Assistant and choose that
  • Choose Say a simple phrase
  • Customize what you would want to say to trigger the toggle in AdaFruitIO. I chose Turn bedroom AC On and made it reply Bedroom AC has been turned on
  • Click on the word That
  • Choose Send data to Adafruit IO
  • Choose your feed name
  • Input the data you want to send to AdaFruit's feed. I chose ON as data

Now you can turn your device On via the Google Assistant

  • Click on your profile, then click Create
  • Click on the word This
  • Search for Google Assistant and choose that
  • Choose Say a simple phrase
  • Customize what you would want to say to trigger the toggle in AdaFruitIO. I chose Turn bedroom AC Off and made it reply Bedroom AC has been turned off
  • Click on the word That
  • Choose Send data to Adafruit IO
  • Choose your feed name
  • Input the data you want to send to AdaFruit's feed. I chose OFF as data

Now IFTTT communicates with AdaFruitIO via your commands sent to Google Assistant :)

Raspberry Pi

Using installer.sh

I created an installer.sh script that will put all the files where they belong and convert driver.py to a service that automatically runs at boot. To configure everything:

  • Make sure you put all the files in
/home/pi/autohome/
  • So the driver.py, aiokey.txt, autohome.service, and installer.sh are all under /home/pi/autohome/
  • Now open up aiokey.txt and put your AdaFuitIO key there, make sure there are no leading or trailing spaces. Save that file and exit it
  • Open up driver.py, edit ADAFRUIT_IO_USERNAME to match your username. Save and close the file.
  • Now run the installer.sh script as root:
sudo ./installer.sh

Now you should be able to control the autohome service using sudo service autohome start|stop|status.

If you want to edit autohome to suit your needs, you can simply edit the driver.py file in /home/pi/autohome

Monitor RaspberryPi Temperature and CPU Clock Speed (Optional)

This section is still a WIP

Manually configuring everything (Better to learn! :D)

This section contains the full guide to run this project, assuming you've already installed RaspbianOS on the Raspberry Pi and you have it online and updated to its latest version.

  • First we need to install the required python package for us to run AdaFruitIO on python:
pip3 install adafruit-io
  • This will allow us to run python code related to AdaFruitIO as a normal user. Since we're also automating this process and making it so that whenever the Pi is rebooted, it runs the Home Automation service, we also need to give root the ability to work with AdaFruitIO's python code
sudo pip3 install adafruit-io
  • Now once you have everything downloaded from this github project to the Pi, extract everything and put the contents of the "raspberry pi" folder in
/home/pi/autohome/
  • So the driver.py, aiokey.txt, autohome.service, and installer.sh are all under /home/pi/autohome/
  • Now open up aiokey.txt and put your AdaFuitIO key there, make sure there are no leading or trailing spaces. Save that file and exit it
  • Open up driver.py, edit ADAFRUIT_IO_USERNAME to match your username. Save and close the file
  • Now you should be good if you run the driver.py file
python3 /home/pi/autohome/driver.py
  • If you get the following messages, you're good to go:
Connected to Adafruit IO!
Connected to Adafruit IO!  Listening for ac changes...
  • If you get the following message, check your AIO key and username:
Unexpected disconnection.

Automating it

Now that everything is checked and working manually, it's better for us to automate the process of running this script as a service on boot.

  • Copy the autohome.service file to where linux usually looks in to see what services to run
sudo cp /home/pi/autohome/autohome.service /etc/systemd/system/
  • Reload the systemctl daemon
systemctl daemon-reload
  • Run the service
sudo systemctl start autohome
  • Check its status
sudo systemctl status autohome
  • If you get an error somewhere, you should check the file's permissions, the /etc/systemd/system/autohome.service file should have a permission of 644 which translates to rw-r--r--
  • If everything is alright, enable the service for it to run at boot
sudo systemctl enable autohome
  • Now you can use even simpler commands to start and stop and check the service's status
sudo service autohome start
sudo service autohome status
  • Now on every reboot, you can confirm that the script is working by toggling the AC toggle on in AdaFruitIO's Dashboard (while Master is on of course) and checking that the ac-status indicator turns on/off accordingly

Monitor RaspberryPi Temperature, CPU Clock Speed, and Under-Voltage (Optional)

You can optionally monitor the Raspberry Pi's Temperature, Clock Speed, and detect Undervoltage by doing some small edits to your io.adafruit.com Dashboard and some small scripts on your Pi.

PiTemp!

PiTemp is not just a Temperature monitor for the Pi, it's also a CPU Clock Speed monitor!

AdaFruit-IO's Side

  • Go to your Dashboard
  • Hit Create a new block, choose Line Chart
  • Create a new feed called pi-temp
  • You can optionally name it Pi Temp as a block Title.
  • X-Axis Label is Time
  • Y-Axis Label is Temperature
  • Click Create Block
  • Hit Create a new block, choose Line Chart
  • Create a new feed called pi-clock
  • You can optionally name it Pi Clock Speed as a block Title.
  • X-Axis Label is Time
  • Y-Axis Label is Clock Speed
  • Click Create Block

Raspberry Pi's Side

  • There's a folder in this code called pitemp, make sure that folder resides in /home/pi/pitemp/ and driver.py, installer.sh, and pitemp.service are all under that directory.
  • The driver.py script relies on your AutoHome /home/pi/autohome/aiokey.txt file to authenticate with AdaFruit-IO, so keep in mind that you need that file in order for this script to function properly.
  • Edit driver.py and change ADAFRUIT_IO_USERNAME to your username
  • Ensure that installer.sh is executable by running the following:
sudo chmod +x installer.sh
  • Now you can install the script by running:
sudo ./installer.sh
  • You should see the Pi Temp graph in AdaFruit-IO update once every 10 seconds with the current Raspberry Pi CPU Temperature

PiVolt!

AdaFruit-IO's Side

  • Go to your Dashboard
  • Hit Create a new block, choose Line Chart
  • Create a new feed called pi-volt
  • You can optionally name it Voltage LED Brightness as a block Title.
  • X-Axis Label is Time
  • Y-Axis Label is Brightness
  • Click Create Block

Raspberry Pi's Side

  • There's a folder in this code called pivolt, make sure that folder resides in /home/pi/pivolt/ and driver.py, installer.sh, and pivolt.service are all under that directory.
  • The driver.py script relies on your AutoHome /home/pi/autohome/aiokey.txt file to authenticate with AdaFruit-IO, so keep in mind that you need that file in order for this script to function properly.
  • Edit driver.py and change ADAFRUIT_IO_USERNAME to your username
  • Ensure that installer.sh is executable by running the following:
sudo chmod +x installer.sh
  • Now you can install the script by running:
sudo ./installer.sh
  • You should see the Voltage LED Brightness graph in AdaFruit-IO update once every 10 seconds. 255 means that the RED LED on your Raspberry Pi board is on at full brightness, which indicates that everything is OK. 0 means that the LED has turned off, which might indicate a voltage drop.

Turning your doorbell into a smart one! (Optional)

This side project of mine involves wiring things up next to your apartment's doorbell. This makes it so we can do several things when the doorbell is pressed, a lot more than just ringing the actual doorbell.

When somebody rings your doorbell, the Raspberry Pi controls the chime, and sends a picture of whoever is at the door to you via Telegram.

This requires you to create a Telegram bot.

Telegram

Create a Telegram bot by (WIP)

Raspberry Pi's Side

  • There's a folder in this code called autodoorbell, make sure that folder resides in /home/pi/autodoorbell/ and driver.py, installer.sh, and autodoorbell.service are all under that directory.
  • The driver.py script relies on your /home/pi/autodoorbell/token.txt file to authenticate with Telegram, and on your /home/pi/autodoorbell/botdriver.py file to only communicate with one Telegram user, so keep in mind that you need that file in order for this script to function properly.
  • Edit token.txt and put your token in that file instead of whatever is there
  • Edit botdriver.py and replace <put id here> with your chat ID so that the bot only speaks to you
  • Ensure that installer.sh is executable by running the following:
sudo chmod +x installer.sh
  • Now you can install the script by running:
sudo ./installer.sh
  • Once everything is done, you should be able to receive a picture of whoever is at the door when they ring the doorbell.