ZotPonics API Documentation - Senior-Design-ZotPonics/ZotPonicsRaspPi GitHub Wiki
There are two options for setting up the API in this documentation. Either set up the API using pythonanywhere or using a local SQLite database.
Pythonanywhere Setup
Pythonanywhere is an online service where users can host a python website quickly. We decided to use this and host it online because it was easy to use and so that our app can access the data from anywhere as long as it was connected to the web. The limitations of using the local SQLite version we created was that, the database could only be access from a device connected to the Local Area network of where the SQLite data base was hosted.
Here are some basic steps to help you set up hosting the ZotPonics API:
- Create a Pythonanywhere account.
- Click on the "Web" tab of the dashboard.
- Click "add a new web app"
- Select "Flask" as your framework
- Choose your python version. We used Python version 3.7 at the time of development. No guarantees that our code will be compatible with future python versions.
- Create the new Flask Project
- Either overwrite the
flask_app.py
file hosted on Python Anywhere with the one we have in this repository or copy-paste the code into the the online editor.
Changing the Timezone for Python Anywhere
This is how you change the timezone for the bash consoles in Python Anywhere:
- Open the bash console for your environment and run
pip install mysqlclient
2.. Edit the.bashrc
profile to include the line:export TZ="US/Pacific"
. This will edit the timezone to be Pacific Standard Time.
This is how you change the timezone for the code:
- In the WSGI file, include the following:
import os
import time
os.environ["TZ"] = "US/Pacific"
time.tzset()
Set up the Config File
Create a file called config.json
and write the following:
{"account":
[{"username":"your_database_username",
"password":"your_database_password",
"database":"your_database_name"}]
}
Local SQLite Setup
First, make sure that you set up python flask as seen in the README file
Open up the terminal and make sure you are in the local_version
folder. Run the command `cd local_version' if you aren't in it already.
Then in the terminal, run the ZotPonics_api.py
file: python ZotPonics_api.py
or in the Debian environment python3 ZotPonics_api.py
Your terminal should look something like this:
Your domain name is now your machine's ip address.
GET Request
The GET request is used for getting the must recent sensor data collected in the ZotPonics database
Calling [domain name]/recentsensordata
will return:
readings = [
{
'lastWateredTimestamp': lastWateredTimestamp,
'timestamp': timestamp,
'temperature': temperature,
'humidity': humidity,
'baseLevel': baseLevel,
'plantHeight': plantHeight,
'lightStatus': lightStatus
},
]
PUT Request
The PUT request is used to post the user's control growth factors inputs into the ZotPonics database
Post request can be called on [domain name]/userControlGrowth
and the json object must be in the following format:
{"controlfactors":
[{
"timestamp":your_timestamp,
"lightstart":your_lightstart,
"lightend":your_lightend,
"humidity":your_humidity,
"temp":your_temp,
"waterfreq":your_waterfreq,
"nutrientratio": your_nutrientratio,
"baselevel":your_baselevel
}]
}