plantPi

What is it?

It is a raspberry pi that monitors different aspects of potted plants and updates this information on a local intranet website, for people to monitor things such as soil moisture level, and light levels.

Why is it interesting?

I wanted all of this to be on the local network, as I did not have the ability to open ports on my router, and didn't see the need to find other routes of hosting on the internet as I only wanted the resources locally. 

The main issue that I had with this is that I like to develop my projects wherever I go, so I needed a way to get new versions of the website and anything else I made to the raspberry pi for hosting. This is where I had an intriguing idea to use Discord. I have made a discord bot before (Gareth) and so knew that they could listen for messages and get that message content. This means that I could create functions that allow messages to be read, and if specific messages were sent, scripts could be run to stop the current applications from running, pull the latest branch from GitHub, and start up the application again.

On the development side of the process, I use GitHub for the majority of my version control. It is quick and easy to use, allowing full control
over access and has an array of different functions that I can utilise in my projects. One of these functions is the use of webhooks. These allow GitHub to send messages on specific repository activity. Easily enough I had them sending any activity for my repositories for this project to a channel in a Discord server. 

Now to link the two. The simple Discord bot that runs on the pi, reads incoming messages that are sent to that channel and detects if it is a GitHub commit message to one of this project's repositories. If so, it then passes the information of which repository to a separate function that deals with the updating of the local repos and restarting any necessary services.

TBC...

The Bot Side:

On the bot side, the bot uses discord.py API wrapper to interact with the discord client. It listens to all messages coming
through on the channels that it has access to and will respond to specific ones. These include any relevant GitHub messages or any of the pre-determined messages that trigger certain things on the pi. 

To make the pulls from GitHub, I initially attempted to code it with Python, however, I ran into issues when wanting to authorise with access tokens for each repo. The problem I ran into was how to use a specific access token for each repo. I then moved to calling different bash scripts on the pi, that correspond to each of the relevant repositories. The bash scripts initiate a new ssh-agent, add the relevant key, pull the desired repo into the corresponding local folder, configure the files as needed (mainly give the executable permissions to the new bash scripts), restart any needed service and close the ssh-agent. This method of creating a fresh agent and closing it at the end prevented me from running into issues with having multiple keys in the agent (as GitHub tries the first key in the agent only).

A few more useful commands that I added were a "cmd" command, a "logs" command and a "config" command. The cmd command allowed me to run commands on the pi by executing the message contents locally on the pi. This function especially can only be called from a specific channel, by myself only as to prevent any malicious use and unauthorised access to the pi's systems. The logs and config commands cause the bot to send the logs and config files, respectively, to the discord channel so they can be easily checked when needed. Later during development, I made a general "file" command that takes a given path and sends the appropriate file to the channel so that any file can be checked if needed.