For this project I hacked an old rotary phone, so it will tell me if a HTTP server in my AWS account is down. To do this a Lambda function will check via Cloudwatch Events an EC2 instance by checking a HTML page. The Lambda function is tracked by a Cloudwatch Alert. If the check fails, an alert is triggered and a SNS message is sent to a topic. A second Lambda function is connected to this SNS topic and will forward this message to a MQTT topic. The Pi Zero in the red phone is subscribed to this topic. If a MQTT message is reaching the red phone, it will ring, the Pi than calls the AWS Polly API to transform the text message into an Audio File an plays it on the handset.
Its really hard to make a rotary phone ring these days. You would need a 60 to 105-volt RMS 20-Hertz sine wave to get the bells ringing. A ringing voltage generator is expensive and I don't have one. A simple hack to overcome this is, to remove the original ringing circuit from the phone and replacing it with two small solenoids. I got mine from Ebay, they are about 6 € each. In order to control them with the Pi Zero I created a small NPN transistor circuit, because its not possible to drive them directly over the GPIOs.
Grab your Pi zero W and a SD card and follow the instructions on dietpi.com to setup and access the Pi.
Install:
apt-get install python3.4
apt-get install python3.4-dev
apt-get install alsa-utils
apt-get install python3-pip
apt-get install python3-pygame
apt-get install python3-rpi.gpio
Redirect the PWM audio to the GPIO pins by adding to /DietPi/config.txt:
dtoverlay=pwm-2chan,pin=18,func=2,pin2=13,func2=4
modprobe snd_bcm2835
- reboot the pi
- use the alsa tools to test audio
change the python version to 3.4:
update-alternatives --list
update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives --install /usr/bin/python python /usr/bin/python3.4 2
python --version
AWS- user for this needs access to Lambda, IoT, Polly, SNS
- use a EC2 instance with a HTTP server running or create one
- create a new IoT device and download zip with credentials files
- create SNS topic "redphone"
- create a role which can execute lambda and IoT
- create Python 3.6 Lambda function for SNS to Iot (code in git)
- create ping Python 3.6 Lambda function, add the DNS of the HTTP server (code in git)
- create an alert in Cloudwatch on the ping Lambda with the hostname as description
- create an Cloudwatch Event Rule as a cron for the ping lambda
- clone the github repo on the Pi
- install requirements with pip install -r requirements.txt
- add AWS keys and IoT endpoint to config file in src folder
- unzip the credential files from the Iot device to the auth folder
- download root cert to the auth folder, rename to root-CA.crt
python mqtt.py
Comments