Dear all !
We hear so much about IoT and the Cloud so I thought, a practical example of how to combine one of the most popular IoT devices, the Raspberry PI, with Azure Automation is appreciated.
Whats the use case?
When building this, you will have a Raspberry PI with a push-button, and when this button is pressed, an Azure Automation runbook is triggered.
I actually used that for my wife´s recent birthday, so every time the button was pressed by a guest, she received an E-Mail with birthday wishes.
A more business oriented approach could be manual measurement or recurring events, or a help button in a meeting room, which raises an incident when, i.e. the coffee machine does not work, a feedback system with 2 buttons (like, dislike), where you could record time, date and store it in PowerBI and so on …
What do i need ?
- A Raspberry PI 3B or 3B+ (must have Wifi, even if this could be done with LAN connection as well …)
- A button, which closes the circuit when you press it (see picture above)
- An Azure Automation Account and a runbook which gets triggered by a webhook
- A wireless network where the Raspberry can connect to and send webhooks (HTTPS) to the internet.
- Little Linux Know How
- Little Azure and PowerShell know how
How does it work ?
The basic concept is pretty simple:
- A button is pressed on the Raspberry
- A Python script, running on the Raspberry, using the GPIO module catches this event and fires off the webhook.
- The webhook triggers the runbook !
Step by Step guide
Step 1: Prepare the Azure Automation Account and runbook
Before you start with the Raspberry, make sure the Azure Automation Account (AAA) is setup correctly and you have a runbook which has also a webkook it can be triggered.
In my case i have created a seperate AAA called „Buttonbuddy“.
This AAA has one runbook called „pressaction“.
The code of this runbook is not that relevant, but i thought if you want to build something for your birthday party, i´ll share it.
# Basic Mail-setup
$mailbox = "email@example.com"
$password = "SomePassword"
$SmtpServer = "yoursmtpserver.yourdomain.com"
$to = "firstname.lastname@example.org"
$encoding = [system.text.encoding]::UTF8
$cred = New-object pscredential -ArgumentList ($mailbox, (ConvertTo-SecureString $password -asplaintext -force))
# Constructing subject and body
# First we want to record the time when the button triggered the webhook
# I added 2 hours because of my timezone Addhours('2'), please adjust accordingly ...
$currenttime = (Get-Date).AddHours('2').Hour.ToString('00') + ":" + (Get-Date).Minute.tostring('00')
# Using a TextArray to select a subject line randomly. you can adapt or change to your needs.
$textArray = 'I like you!','You are cool !','Great Party!','Thanks for beeing part of your life!'
$Subjectwunsch = $textarray[(Get-Random -Maximum ($textarray).count)]
$subject = $currenttime + " - " + $Subjectwunsch
$Body = "Dear NAME - at $currenttime again, someone pressed the button and wishes you HAPPY BIRTHDAY"
Send-MailMessage -from $mailbox -to $to -Subject $subject -Body $Body -SmtpServer $SmtpServer -Credential $cred -Encoding $encoding
So when your runbook is finished, test it on the testing pane and if it works, publish it.
Now, create a webhook, so that you can trigger this runbook externally.
This works by:
- Clicking on the runbook, to see details in the Overview blade
- Click on „Webhook“
- Follow the blade config to create a new Webkhook, by giving it a name and a expiry date
- Save the webhook URL (this is important, because it will disappear. it must be something like this (https://s2events.azure-automation.net/webhooks?token=somesecrettoken%3d)
Thats it for the Azure Automation side, lets now take care of the Raspberry.
Step 2: Prepare the Raspberry PI Hardware
Before we bring an OS to the Raspberry, we need to wire and mount the button to the device. I simply followed the instructions of the below link (credits!) to make it work.
Mounting the button into the board, means you need to drill a hole into the case. Be cautious, i broke one during drilling… but finally the result should look like this:
Step 3: Install OS and configure the Raspberry
For ease of use i decided to use Raspbian (Debian based Linux) which you can find here: https://www.raspberrypi.org/downloads/raspbian/
Then install the OS on a SD card and boot your Raspberry. Configure LAN or WLAN as you wish, there must be internet connectivity available, otherwise the webhook target may never get reached. All the infos how to do that can be found here:
STEP 4: prepare the python script to trigger the webhook
Create a Python script in the home directory (or any other of your choice) of the user you login to the Raspberry.
The script content is based on the link for the hardware, slightly modified to be able to trigger webhooks:
import RPi.GPIO as GPIO
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
input_state = GPIO.input(18)
if input_state == False:
When you run this script with the command:
and you press the button, you should see „Button Pressed“ on the console. and the webhook gets triggered.
Step 5: Autostart the python script
There are a lot ways to autorun something on Linux, i decided to use crontab. Edit the crontab file by typing:
sudo crontab -e
Add a line at the end of this file with this content:
@reboot python /home/pi/switch.py >/home/pi/cronlogs 2>&1
Now reboot your Raspberry (shutdown -r) and press the button.
Webhook should get triggered and yoiu have a buttonbuddy like me !
Hope you find that useful and fun like me, have fun building!
If you have any questions, look into this link to contact me.