PyPortal "Presently" Gratitude List Display

by GrantF in Circuits > Microcontrollers

190 Views, 0 Favorites, 0 Comments

PyPortal "Presently" Gratitude List Display

pyportal2.jpg
presently.jpeg
presentlygrattitudelist.jpeg

This guide will help you use the PyPortal to display Gratitude entries that you enter using your phone using the Presently App. If you don't know what the Presently App is, it's an app that you can record short messages like a diary. The important feature is it allows backup to dropbox. So what we're doing is pulling the csv file from dropbox every night and displaying it on a webserver. Then the pyportal will retrieve the csv. I couldn't figure out a way to get the PyPortal to download the csv directly from dropbox because of the libraries needed which is why we need the intermediary server.

Supplies

PyPortal

Raspberry Pi (or Linux PC or PC with capabilities to run a webserver)

Dropbox Account

Presently App on Google Playstore:

https://play.google.com/store/apps/details?id=journal.gratitude.com.gratitudejournal&hl=en_CA&gl=US

Uploading Files to PyPortal

pyportal_startup.bmp
pyportal_startupbackup.bmp

Download the project files

Connect a usb cable to the PyPortal from your computer and Copy everything to the PyPortal root directory.

Change Wifi Secrets

PyPortalSecrets.png

Replace SSID and Password with your SSID and Password of your wifi network.

Install Webserver on Your Raspberry Pi

I will not go into detail how to create webserver as there are many created. I created using the following guide:

https://pimylifeup.com/raspberry-pi-apache/


One thing that is necessary is to know the IP address of the raspberry Pi. This can be done by opening up a terminal window and typing ifconfig and looking for your wlan0 interface.

Install Presently App on Phone.

PresentlySettings.jpeg

Download the Presently App from the Google Play Store and make sure settings to backup to Dropbox are set to yes. You need an account on dropbox.com to complete this setting, one can be created at this time.

https://play.google.com/store/apps/details?id=journal.gratitude.com.gratitudejournal&hl=en_CA&gl=US

I don't have an Iphone but this could potentially work with any app that uploads to dropbox a csv file.


Create Dropbox "APP" to Generate Key

Create App.png
GenerateToken.png
DropboxAPPSettings.png
CopyToken.png
permissions.png

Navigate to:

https://www.dropbox.com/lp/developers

Create a new App.

Specify Full drive and an app name, it doesn't have to be the same as mine but some apps are taken like "MyApp" so it must be unique.

After creating the App generate a key. Copy this somewhere safe, you will need it in the next step.


Create Python Scripts on Raspberry Pi

Open this file and modify it's contents. Copy the key generated in the step above into the script below where it says:COPYKEYGENERATEDFROMDROPBOXAPPHERE/

In a step above you've created a webserver on the raspberry Pi.

Place this file into /var/www/html (the contents of your webserver).



Downloads

Create Shell Script and CRON Job to Refresh CSV File Every 24hours

cronjob.png

Instructables won't let me upload *.sh files so create a new file called script.sh in your /home/$User directory where user is your current logged in user (by default pi for raspberry pi).

Copy the next three lines into it and save.

#!/bin/sh

python3 /var/www/html/myapp.py

sudo chmod 755 /var/www/html/presently-backup.csv




Now we need to tell Raspbian to run this file every 24 hours.

1)In a new terminal window under pi user execute command: crontab -e

2) Scroll to the bottom of the document and add the following line. This line tells the crontab to execute script.sh at 5am every day.

0 5 * * * /home/script.sh


Change IP Address/URL of Raspberry Pi Webserver

In Step1 you uploaded code.py onto your pyportal. Update the code.py file and change the DATA_SOURCE_CSV url to match the IP of your webserver.


import time
import board
from adafruit_pyportal import PyPortal
from terminalio import FONT


import neopixel
pixel = neopixel.NeoPixel(board.NEOPIXEL,1)
pixel.fill(0)


# Set up where we'll be fetching data from
DATA_SOURCE_CSV = "http://192.168.1.66/presently-backup.csv"
LINE_SEPARATION = "\n"
FIELD_SEPARATION = "," # the "c" in csv ?


pyportal = PyPortal()
pyportal.preload_font()
char_width = FONT.get_bounding_box()[0]


message_label = pyportal.add_text(
    text_scale=2,
    text_position=(20, pyportal.display.height // 2 - 32),
    text_anchor_point=(0, 0.5),
    text_wrap=pyportal.display.width // char_width // 2,
    text="Wait for the goodshit to appear...",
    text_color=0x008080,
)
date_label = pyportal.add_text(
    text_scale=2,
    text_position=(pyportal.display.width - 10, pyportal.display.height - 10),
    text_anchor_point=(1, 1),
    text_wrap=pyportal.display.width // char_width,
    text="1984/12/18",
    text_color=0x8080FF,
)
#     text_color=0, , text_maxlen=0, text_transform=None, text_scale=1, line_spacing=1.25, text_anchor_point=(0, 0.5), is_data=True, text=None)ΒΆ


while True:
    try:
        # blue means we are retrieving data
        pixel.fill(0x0000FF)
        with pyportal.network.fetch(DATA_SOURCE_CSV) as response:
            if response.status_code != 200:
                pixel.fill(0xFF0000)
                raise ConnectionError(f"Network error: {response.status_code}")
            else:
                data = response.text


        pixel.fill(0)
        # here we have the csv.
        for line in data.split(LINE_SEPARATION):
            fields = line.strip().split(FIELD_SEPARATION)


            ignore_list = ['entryDate', 'entryContent']
            if fields != ignore_list:
                if len(fields) > 1:
                    date = fields[0]
                    message = fields[1]
                    pyportal.set_text(message, message_label)
                    pyportal.set_text(date, date_label)
                    time.sleep(5)


    except (ValueError, RuntimeError) as e:
        print("Some error occured, retrying! -", e)
        time.sleep(10)

Initial Setup

Dropbox will upload a csv backup to your drive at night. Usually between 12am and 2am. You need to wait atleast a day for the csv to upload and there must be an entry in Presently.

Once the csv is there you can manually trigger the download by running the ./script.sh in your /home/pi directory.

Once the script is run there should be a file called presently-backup.csv in your /var/www/html file of your raspberry pi.

Once there's a file there, turn on your PyPortal. It should display gratitude items.


Troubleshooting

PyPortal constantly reboots after displaying splash screen.

Check step 8 IP address is entered properly.

If IP is proper, enter ip address in browser and make sure csv file is visible and can be downloaded.

No csv file on webserver

Check Dropbox has file.

When I run myapp.py or script.sh I get token authentication error

Go back to drop-box console and generate a key again. Also check permissions (see screenshot in step 5)