jbyerline / flask-purple-powerbase

MIT License
0 stars 0 forks source link

flask-purple-powerbase

This is a Python3 Flask server implementation for a Reverie/Purple Powerbase. This has been created by myself @jbyerline and you are free to use this code and adapt however you may like. Feel free to fork or put up a PR if you feel so inclined, and I will review it as time permits.

This Flask API is designed to work with the Homebridge plugin here.

THIS API SHOULD BE ON A SEPARATE DEVICE FROM YOUR HOMEBRIDGE DEVICE AND IN CLOSE PROXIMITY TO YOUR BED!!! (Less than 10ft)

The tutorial begins here:

Please note, this has been tested on a RPI 0W and a RPI 4. The PI needs to have Bluetooth 4.0 Low Energy which to my knowledge is only available natively on those 2 raspberry PI's


Prerequisite

This guide assumes that you already have the following set up:

Set Up

Step 1

Login to your device (locally or over SSH)

    ssh myUser@myLocalRpiIp

Step 2

Check for Updates

    sudo apt update && sudo apt upgrade

Step 3

Identify your RPI's local IP address

    ifconfig

Your results will look something like this:

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
    inet 127.0.0.1  netmask 255.0.0.0
    inet6 ::1  prefixlen 128  scopeid 0x10<host>
    loop  txqueuelen 1000  (Local Loopback)
    RX packets 0  bytes 0 (0.0 B)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 0  bytes 0 (0.0 B)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    inet 10.0.0.75  netmask 255.0.0.0  broadcast 10.255.255.255
    inet6 fd5d:bd35:1481:1:9d25:753b:25bf:b050  prefixlen 64  scopeid 0x0<global>
    ether b8:27:eb:6c:be:28  txqueuelen 1000  (Ethernet)
    RX packets 300745  bytes 117984533 (112.5 MiB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 12035  bytes 1657489 (1.5 MiB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

The line your looking for will begin with "wlan0:" (assuming you are connected to WiFi and not Ethernet). Under the wlan0 line you will see "inet x.x.x.x". This is your Pi's local IP address.

Step 4

Reserve your RPI's local IP address either in software or in your networks router. Look up how to do this for your specific router.

Step 5

Change directories to home

    cd

Install git

    sudo apt install git -y

Clone this repo:

    git clone https://github.com/jbyerline/flask-purple-powerbase.git

Change directories to flask-purple-powerbase

    cd flask-purple-powerbase

Make the python script executable

    sudo chmod 777 app.py

Step 6

Find your Bed's Bluetooth MAC address. This will be needed for the Raspberry Pi (RPI) to be able to connect to the bed.

   sudo hcitool lescan

This will produce a long list of bluetooth devices near you as well as their MAC addresses. Mine was titled "RevCB_C1". Once you see one with probably a similar name, hit ctrl+c to stop the scan after about 30 seconds. Scroll through the list and write down your MAC address exactly as is.

Note: it is important that there are no other bluetooth devices connected to your bed when you do this scan, IE. the Purple or Reverie apps.

Step 7

Edit app.py to include your MAC address and IP address

   nano app.py

Edit the two lines below to be your MAC and IP addresses we noted earlier Note: This is the Bluetooth MAC address from the step above. NOT the RPIs WiFi MAC Address

    #TODO: You WILL need to change the mac address below to be the address of your bed's bluetooth module
    DEVICE_MAC = "XX:XX:XX:XX:XX:XX"
    # TODO: You WILL need to change the Local IP address below to be the address of your RPI
    RPI_LOCAL_IP = "X.X.X.X"

Hit ctrl+x then y then enter to save

Step 8

Install python pip3

   sudo apt-get -y install python3-pip

Verify python pip3 installation (this step may take a min or so on a RPI 0)

   pip3 --version

Step 9

Install python pip3 required packages (Note: sudo is required for later step)

   sudo pip3 install flask
   sudo pip3 install bluepy

Step 10

Start Flask API for fist time (Note: you must be in the flask-purple-powerbase directory)

   sudo python3 app.py

Step 11

Test out your API by going to:

   http://[yourLocalIP]:8000/moveUpper/50

If all goes well, your bed will move up 50%. Make sure no other bluetooth devices are connected to the bed so that the RPI can.

This will send it back to flat:

   http://[yourLocalIP]:8000/moveUpper/0

Then hit ctrl+c to stop the python program

Step 12

Configure autostart of the API on boot:

    sudo crontab -e

insert this line at the bottom

@reboot /usr/bin/python3 /home/pi/flask-purple-powerbase/app.py >> /home/pi/flask-purple-powerbase/log.txt 2>&1

Hit ctrl+x then y then enter to save

Step 13

Reboot the PI and then use your API.

   sudo reboot

If all is well then you should be able to use all the API endpoints in this server. They are listed below. Test them out and create your own web app. Or hook this flask API up to homebridge for control in the home app. Here is the repo for that: Purple Powerbase Homebridge

Configuration Params

Endpoint Description Method
/ default webpage for api get
/stop turn off massagers get
/flat make bed flat (top then bottom) get
/zeroG set bed to ZeroG mode get
/noSnore set bed to No Snore mode get
/moveUpper/<percentage> move top of bed to int percentage (ie. moveUpper/50) get
/getUpperHeight get current height of top of bed get
/moveLower/<percentage> move bottom of bed to int percentage (ie. moveUpper/50) get
/getLowerHeight get current height of bottom of bed get
/setUpperVib/<percentage> set upper massager to int percentage (ie. setUpperVib/50) get
/getUpperVib get current strength of upper massager get
/setLowerVib/<percentage> set lower massager to int percentage (ie. setUpperVib/50) get
/getLowerVib get current strength of lower massager get
/light/on turn nightlight on (via bluetooth) get
/light/off turn nightlight off (via bluetooth) get
/light/status get nightlight status (via bluetooth) get
/GPIOlight/on turn nightlight on (via GPIO Relay Pin 10) get
/GPIOlight/off turn nightlight off (via GPIO Relay Pin 10) get
/GPIOlight/status get nightlight status (via GPIO Relay Pin 10) get

Hint