Skip to content

Instantly share code, notes, and snippets.

@kosso
Last active June 25, 2023 15:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kosso/a513135384d5d78247f1af8464917a4c to your computer and use it in GitHub Desktop.
Save kosso/a513135384d5d78247f1af8464917a4c to your computer and use it in GitHub Desktop.
Instructions for setting up a Raspberry Pi Zero to send an iOS push notification when network interfaces go 'up'
###Sending Push notifications from Raspberry Pi when network interfaces go online.
- @kosso
- Apple : Set up app ID with Push Notifications cert.
- Download the .p12 CERT and KEY files
via: http://blog.raminrakhamimov.com/?p=81
Convert the p12 files to PEM format with:
# CERT
openssl pkcs12 -clcerts -nokeys -out apns-dev-cert.pem -in apns-dev-cert.p12
# KEY
openssl pkcs12 -nocerts -out apns-dev-key.pem -in apns-dev-key.p12
# STRIP PASSWORD FROM KEY
openssl rsa -in apns-dev-key.pem -out apns-dev-key-noenc.pem
# COMBINE CERT AND KEY as single .pem
cat apns-dev-cert.pem apns-dev-key-noenc.pem > apns-dev.pem
# Install pyapns Twistd etc.
sudo apt-get install libffi-dev
sudo apt-get install libssl-dev
sudo easy_install cryptography
sudo easy_install pyopenssl
sudo easy_install service_identity
sudo easy_install python-epoll
sudo easy_install pyapns
# The latest (17.x) Twisted version does work with pyapns! https://github.com/samuraisam/pyapns/issues/83
sudo pip install Twisted==16.1.1
# To start Twistd :
sudo twistd -r epoll web --class=pyapns.server.APNSServer --port=7077
# (creates .pid file in current directory)
# To stop Twistd :
sudo kill -9 `sudo cat twistd.pid`
# send_push.py - usage: ./send_push.py "hello world!"
----------------------------------------------------------------
#!/usr/bin/python
import sys
from pyapns import configure, provision, notify, feedback
print 'Send message :', sys.argv[1]
alert_text = sys.argv[1]
# Get Device token from an iOS app registration for notifications.
device = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# Same port as twistd
configure({'HOST': 'http://localhost:7077/'})
def provision_callback(e):
print 'called provision_callback '+str(e)
def provision_errback(e):
print 'called provision_errback : '+str(e)
def notify_callback(e):
print 'called notify_callback '+str(e)
def notify_errback(e):
print 'called notify_errback : '+str(e)
# This is the Development 'sandbox' Cert. Use 'production' for a live app.
provision('sand_pizero1', open('/path/to/combined/cert/and/key/apns-dev.pem').read(), 'sandbox', async=True, callback=provision_callback, errback=provision_errback)
# Send it!
notify('sand_pizero1', device, {'aps':{'alert': alert_text}}, async=False, callback=notify_callback, errback=notify_errback)
-----------------------------------------------------------------
### Detecting when network interfaces go 'up'
Create a script to [stop]/start twistd:
#!/bin/bash
# Kill it first just in case or port will remain in use
cd /to/path/where/twistd.pid will live
sudo kill -9 `sudo cat /path/to/twistd.pid`
sudo twistd -r epoll web --class=pyapns.server.APNSServer --port=7077
Execute this in /etc/rc.local by adding this line to trigger on boot up:
sudo /bin/bash /path/to/your/startstop/script/start_twistd.sh
Create a script to trigger when an interface goes up:
!/bin/bash
# to run when the a network interface goes up
# triggered in /etc/network/interfaces - see below
# @Kosso
echo "interface went up.. restart twistd ..." >> /home/pi/post-up.log
# cd to a directory which will keep the .pid file
cd /home/pi/_PUSH
sudo kill -9 `sudo cat twistd.pid`
sleep 3
sudo twistd -r epoll web --class=pyapns.server.APNSServer --port=7077
echo "WLAN ok. start pinger.." >> /home/pi/post-up.log
# should probably count this and set a limit
while ! ping -c 1 -W 1 8.8.8.8; do
echo "WLAN Waiting for 8.8.8.8 - network interface might be down..." >> /home/pi/post-up.log
sleep 1
done
HOST=`hostname`
echo "WLAN GETTING EXTERNAL IP....."
DATE=`date +%Y-%m-%d:%H:%M:%S`
LOCALIP=`hostname --all-ip-addresses`
WANIP=`wget -qO- http://ipecho.net/plain ; echo`
echo "$DATE : WLAN0 : $HOST ONLINE $LOCALIP - external: $WANIP" >> /home/pi/post-up.log
sleep 3
SEND_PUSH=`/home/pi/_PUSH/send_push.py "$DATE :WLAN0: $HOST ONLINE $LOCALIP - external: $WANIP"`
-------------------
Edit: /etc/network/interfaces to trigger script:
# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d
auto lo
iface lo inet loopback
# On a Pi Zero, I use this to detect when a USB iPhone hotspot is detected.
# See here for how to do that: https://www.daveconroy.com/how-to-tether-your-raspberry-pi-with-your-iphone-5/
allow-hotplug eth0
iface eth0 inet dhcp
up bash /home/pi/_PUSH/_eth0_up.sh
# Detect Wifi up
allow-hotplug wlan0
iface wlan0 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
up bash /home/pi/_PUSH/_wlan0_up.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment