esp8266 Pushbullet Android Notification

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
Roffey
Posts: 4
Joined: Wed Apr 10, 2019 9:38 am

esp8266 Pushbullet Android Notification

Post by Roffey » Wed Apr 10, 2019 10:32 am

Been using micropython for a couple of weeks now and getting more impressed as I go, particularly with the guru's who seem to be more than helpful.
I'm using upyCraft V1.1, which I find the most useful IDE.
I find having a project is my best way of learning at my senior years!
Anyway, since I always find snippets of code very handy, and I have finally got Pushbullet (free for my limited notifications) to work, I thought I'd share my bit.
Of course it's not original, with bits pulled from various sources, but it works for me.

I do not pretend to know much at all about python etc, I just dig, try, try again until I get things to work.

One of my biggest problems was with HTTPS and required certificates etc … definitely not my forte.

So, this 'project' is simple … I wanted my doorbell to send me a notification, so I could be out of range of the ringers and still know.
The principle for me was to rectify the bell speaker output, (simple diode, cap and bleed resistor), shove that into the a/d port for a trigger.
The bell workings were encapsulated... so no digital trigger output.

First thing was Pushbullet and setting up an account …. ok .. sign up using google account .. done and retrieve a code to use.

Find out it uses an HTTPS Push … uh oh... going to need key certificates.. No knowledge here.
Ok... look around... can use Openssl to generate them apparently.

So find this site https://slproweb.com/products/Win32OpenSSL.html where I can download a lite version. Download it. Find the exe file in Openssl -Win32(64)\bin\ directory. Create a C:\certs2 directory for the 2 files generated Client .key and .crt files.
Open the openssl.exe file in run as administrator mode.
Now type in -newkey rsa:2048 -nodes -keyout C:\certs2\client.key -x509 -days 365 -out C:\certs2\client.crt and fill in country, county, initials for company, name and email only to discover discover the files have been generated.
Great.
To get these files into the esp using upyCraft required renaming these files as txt files, then renaming them.
ie client.key to clientkey.txt and client.crt to clientcrt.txt. Opened in upyCraft in txt format and downloaded to the esp.
Then on the repl line …. import os … os.rename('clientkey.txt','client.key') and same for the crt file.

The main file now …

# would like start up checking sending notification
# fill in ssid and pwd
# fill in api key from pushbullet
import json
import urequests
import machine
import network
import time
from machine import ADC

adc0=ADC(0) #create ADC object on ADC pin
bell = machine.Pin(0, machine.Pin.IN)
led = machine.Pin(2, machine.Pin.OUT)
led.value(1) #turn led off
flag = 0
startFlag = 1 #flag for sending notification at start up
def connect():
# import network

ssid = "**************"
password = "**********"
station = network.WLAN(network.STA_IF)
if station.isconnected() == True:
print("Already connected")
led.value(0) #led on
return

station.active(True)
station.connect(ssid, password)

while station.isconnected() == False:
pass

print("Connection successful")
led.value(0) #led on
print(station.ifconfig())


connect()

body = "Front Door"
title = "Bell Push"
data_sent = {"type": "note", "title": title, "body": body}
API_KEY = 'o.--------------------- issued from pushbullet'

startbody= "Connection"
start_title ="Check ok"
start_data ={"type": "note", "title": start_title, "body":startbody}

pb_headers = {
'Authorization': 'Bearer ' + API_KEY,
'Content-Type': 'application/json',
'Host': 'api.pushbullet.com'
}

def bellnotify():
r = urequests.post(
'https://api.pushbullet.com/v2/pushes',
data=json.dumps(data_sent),
headers=pb_headers)
global flag
time.sleep(5)
flag = 0

def startcheck():
r = urequests.post(
'https://api.pushbullet.com/v2/pushes',
data=json.dumps(start_data),
headers=pb_headers)
global startFlag
startFlag = 0
#print(r)

while (True):

if adc0.read() > 500 and flag == 0 : #read value, 0-1024
print(bell.value())
flag = 1
bellnotify()

if startFlag == 1:
startcheck()

station = network.WLAN(network.STA_IF)
if station.isconnected() == False:
connect()

time.sleep_ms(200)

So.…. I think that is it …… Works for me....
I hope this might be useful .

Dave....

Cevc
Posts: 10
Joined: Thu Apr 25, 2019 7:54 am

Re: esp8266 Pushbullet Android Notification

Post by Cevc » Thu Apr 25, 2019 7:59 am

Very handy, thank you for posting this!
I have a few projects going that I'd like to enable notifications for, so this is going to be super useful. Might throw a question or two your way. :D

DasDune
Posts: 1
Joined: Sat Aug 17, 2019 1:39 am

Re: esp8266 Pushbullet Android Notification

Post by DasDune » Sat Aug 17, 2019 1:59 am

Indeed micropython is nice. I used arduino IDE for a while using the edit, compile, download endless cycles. Much more faster with micropython with the bonus of debugging online with WebREPL.

My two cents regarding notification: I use it with Firebase Cloud Messaging technology (FCM) and a custom app on my phone using Flutter. This approach give you full flexibility on the client and server sides.

Firebase is nice as they provide you with a lot of different services for free, like a database if you want to log anything from your ESP8266 using simple python post/get requests as you to for your notification.

Me too I keep me busy with such projects during my retirement. ;)

shyam.sunder91
Posts: 1
Joined: Wed Dec 18, 2019 1:13 pm

Re: esp8266 Pushbullet Android Notification

Post by shyam.sunder91 » Wed Dec 18, 2019 1:20 pm

Roffey wrote:
Wed Apr 10, 2019 10:32 am
Find out it uses an HTTPS Push … uh oh... going to need key certificates.. No knowledge here.
So find this site https://slproweb.com/products/Win32OpenSSL.html where I can download a lite version. Download it. Find the exe file in Openssl -Win32(64)\bin\ directory. Create a C:\certs2 directory for the 2 files generated Client .key and .crt files.
Open the openssl.exe file in run as administrator mode.
Now type in -newkey rsa:2048 -nodes -keyout C:\certs2\client.key -x509 -days 365 -out C:\certs2\client.crt and fill in country, county, initials for company, name and email only to discover discover the files have been generated.
Great.
Hi ,
can you please guide more elaborate on these quoted steps
i was not able to create the keys you said, can you mention the proper command to be used in commandline

Thanks

Post Reply