Jython
Deutsch   English 

12. Push Notifications

The micro:bit can send push notifications using the internet connection via the LinkUp module. Push messages are temporarily stored on a cloud server and retrieved by a smartphone push application.  On the smartphone, the push notifications usually trigger an audible alarm and are displayed in a notification window, even if the smartphone is idle or used for other purposes.

To receive push messages, first install the Pushover app on the smartphone. (from Google Play or App Store). The free version works for one week only, then you have to reinstall it. But a always working version costs only about CHF 5. When starting the app the first time, you create a user account with email address and password. You will be asked to give your smartphone a name of your choice, in this example aplu. Personal account details are not used when sending push notifications.

To send push messages you need a user key and an API token key. For setting up  your account is recommended to use a browser on a PC. Select the URL https://www.pushover.net and log in with the account details specified above.

For a test you can type something in the message window to get the first push notification.

However, it is important to click on Send Notification to display the User Key that you have to note down.

Then you have to click on the link Application

and enter any name in the field Name field (here abc ). Confirm the Terms of Service checkbox and click Create Application.

Then an API Token Key appears, which you have to note down too:

 

Example 1: Sending a message

To send messages, the mbRobot must be connected to the Internet via an access point. In this first example a very simple text message is sent. In the code you have to insert the user and token keys as well as the URL of the pushover server. A notification is sent with the function httpPost(url, content), where content contains the named parameters token, user, title and message. After the program execution, the push message is displayed on the smartphone within a short time.

   

# Push1.py
from linkup import *

token = "ainrm1ongcvfgbfpfqeryi8prixb8g"
user = "setghjcbt8qibfibdgsew33q3abx"
title =  "micro:bit"
message = "Greetings from mbRobot"


connectAP("mySSID", "myPassword")
url  = "https://api.pushover.net/1/messages.json"
content = "token=%s&user=%s&title=%s&message=%s"  %(token, user, title, message)
response = httpPost(url, content)
print("response:", response)
► Copy to clipboard
 

 

Explanations of the program code:

token = "ainrm1vdgzvfgbfpfqeryi8prixb8g": API token key you got when you created your pushover account
print("response:", response): Show the server's response in the terminal window

 

Example 2: Use robot with distance sensor for monitoring nearby objects

The robot sends a push notification when an object is detected at a distance of 5 to 50 cm. Then it becomes inactive for 10 seconds..

 

# Push2.py
from linkup import *
from mbrobot import *

token = "aeqskhjloizzo9h4v8okpc5ug74desn"
user = "u781hdp6qe8oiuznnejrm6wx8fi36q6"
title =  "movement detector"

def sendNotification(distance):
    print("Sending push notification")
    connectAP("mySSID", "myPassword")
    url  = "https://api.pushover.net/1/messages.json"
    message = "Object detected. Distance: " + str(distance)
    content = "token=%s&user=%s&title=%s&message=%s"  %(token, user, title, message)
    httpPost(url, content)

while True:
    d = getDistance()
    print(d)
    if 50 < d < 500:
        sendNotification(d)
    delay(10000)    
► Copy to clipboard
 

 

Explanations of the program code:

if 50 < d < 500: Same as d > 50 and d < 500, d is between 50 mm and 500 mm
sendNotification(distance): Triggers the notification


Example 3
: Monitoring moisture in the soil with push notifications

The system for measuring the moisture of the plant soil in the chapters "Physical Computing" and "IoT " can be supplemented with a push notification.

The crocodile clamps on P1 and 3V are used to set up the electric circuit. At low humidity, the voltage obtained with the pin1.read_analog() is much smaller than when the soil is wet.

sendNotifikation() is called if v < 500. Then a push message is sent once and the owner of the flower can react accordingly.

.

 

# Puah3.py
from linkup import *
from mbrobot import *

token = "aeqsgkdrnj5zo9h4v8okpc5ug74desn"
user = "jlutg81hdp6qe8pqynnejrm6wx8fi36q6"
title =  "mbRobot"

def sendNotification(distance):
    print("Sending push notification")
    connectAP("mySSID", "myPassword")
    url  = "https://api.pushover.net/1/messages.json"
    message = "I need water!"
    content = "token=%s&user=%s&title=%s&message=%s"  %(token, user, title, message)
    httpPost(url, content)

while True:
    v = pin1.read_analog()
    print(v)
    if v < 500:
        sendNotification(v)
        break
    delay(1000)    
► Copy to clipboard
 

 



Example 4: Using the PIR motion detector for surveying the neighborhood

The pyroelectric infrared sensor (PIR) uses the property of some semiconductors that a temperature change causes an electrical voltage. Since living organisms emit temperature radiation in the infrared range, PIR sensors can detect the movement of living organisms in the range of up to several meters. They are mainly used in alarm systems. The sensor emits a digital signal HIGH when a movement is detected, which returns to LOW after a few seconds if there is no more movement.

  There are different types of  PIR sensors; the HC SR505 is very cheap (source of supply: bastelgarage.ch). The sensor is connected to P2 with three F-F jumper cables.
 

Attention: Since P2 is also used for the ultrasonic sensor, it must be removed from the socket. The sensor works like a digital sensor, i.e. has only voltage levels. At the analog input of the micro:bit these values are about 800 (alarm triggered) and 100 (idle state).

# Push4.py
from microbit import *
from linkup import *

def sendNotification(v):
    print("Sending push notification")
    connectAP("mySSID", "MyPassword")
    url  = "https://api.pushover.net/1/messages.json"
    message = "Movement detected"
    content = "token=%s&user=%s&title=%s&message=%s"  %(token, user, title, message)
    httpPost(url, content)

token = "amnvfdsnj5zo9h4v8okpc5ug74desn"
user = "kjugfhdp6qe8pqynnejrm6wx8fi36098"
title =  "motion detector"
state = "IDLE"

while True:
    v = pin2.read_analog()
    print(v) 
    if v > 500 and state =="IDLE":
        sendNotification(v)         
    if v < 200 and state == "ALARM":
        state = "IDLE"
    delay(500)
► Copy to clipboard
 

 

Explanations of the program code:

state = "IDLE": A state variable is used which describes whether the alarm has just been triggered (ALARM) or whether the sensor is in detection mode (IDLE).