5. Code - Vijay-P/pill-dispenser GitHub Wiki
The Flask application is a self-contained app that runs a standard RESTful service with a modified Boostrap theme for the front end. Javascript is used to generate tuples that represent cylinders and uses AJAX requests to send POST calls to the backend flask service to kick off jobs that will tell the dispenser what time to dispense pills and at what dosage.
In the app itself, we use a form to represent inputs that can come from either a user or a more automated service.
The frontend (javascript) portion of the application takes the form inputs and converts them into a tuple in the format of:
((hour, minute, cylinder number, number of pills),)
Tuple Example for 1 Cylinder Filled:
((12, 0, 0, 2),)
which presents Cylinder 1 with a dosage of 2 pills dispensing once at 12:00AM.
Tuple Example for muliple Cylinder Filled:
((12, 30, 0, 1),(1, 30, 2, 1),(12, 0, 3, 2))
This tuple object is then joined with existing tuple objects in an array that represents all of the cylinders. If the tuple object contains new information, the tuple object in the array that conflicts with the new input is either overwritten or added as a new object in the array.
The array is then sent to the Flask service using a HTTP POST request to the endpoint /api/v1/medication
The RESTful service receives a tuple array that represents each cylinder, directly fires off the backend nanpy process and refreshes the existing queue with the full representation of the cylinders (tuple of tuples)
Due to the simplicity of the Flask framework, we've encapsulated all RESTful API code in app.py and all frontend logic code into /templates/index.html.
Relevant Code:
https://github.com/Vijay-P/pill-dispenser/blob/master/app.py
https://github.com/Vijay-P/pill-dispenser/blob/master/templates/index.html
We use multi-threaded processing in python to generate a queue that awaits a job. This way we can take multiple jobs that runs concurrently with a timer. When these jobs "trigger" based on the assigned hour and the minute, the queue kicks off a process that causes the dispenser to dispense a pill.
Nanpy is a library that uses our Arduino as a slave, controlled by a Paspberry Pi where we run our flask service and multi-threaded worker queue.
Relevant Code: https://github.com/Vijay-P/pill-dispenser/blob/master/embedded.py