mirror of
https://github.com/pimoroni/grow-python
synced 2025-10-25 15:19:23 +00:00
Example moisture-reading web API (aiohttp)
This commit is contained in:
@@ -10,64 +10,65 @@ long_description_content_type = text/markdown
|
|||||||
keywords = Raspberry Pi
|
keywords = Raspberry Pi
|
||||||
url = https://www.pimoroni.com
|
url = https://www.pimoroni.com
|
||||||
project_urls =
|
project_urls =
|
||||||
GitHub=https://www.github.com/pimoroni/grow-python
|
GitHub=https://www.github.com/pimoroni/grow-python
|
||||||
license = MIT
|
license = MIT
|
||||||
# This includes the license file(s) in the wheel.
|
# This includes the license file(s) in the wheel.
|
||||||
# https://wheel.readthedocs.io/en/stable/user_guide.html#including-license-files-in-the-generated-wheel-file
|
# https://wheel.readthedocs.io/en/stable/user_guide.html#including-license-files-in-the-generated-wheel-file
|
||||||
license_files = LICENSE.txt
|
license_files = LICENSE.txt
|
||||||
classifiers =
|
classifiers =
|
||||||
Development Status :: 4 - Beta
|
Development Status :: 4 - Beta
|
||||||
Operating System :: POSIX :: Linux
|
Operating System :: POSIX :: Linux
|
||||||
License :: OSI Approved :: MIT License
|
License :: OSI Approved :: MIT License
|
||||||
Intended Audience :: Developers
|
Intended Audience :: Developers
|
||||||
Programming Language :: Python :: 3
|
Programming Language :: Python :: 3
|
||||||
Topic :: Software Development
|
Topic :: Software Development
|
||||||
Topic :: Software Development :: Libraries
|
Topic :: Software Development :: Libraries
|
||||||
Topic :: System :: Hardware
|
Topic :: System :: Hardware
|
||||||
|
|
||||||
[options]
|
[options]
|
||||||
python_requires = >= 3
|
python_requires = >= 3
|
||||||
packages = grow
|
packages = grow
|
||||||
install_requires =
|
install_requires =
|
||||||
ltr559
|
ltr559
|
||||||
st7735
|
st7735
|
||||||
pyyaml
|
pyyaml
|
||||||
fonts
|
fonts
|
||||||
font-roboto
|
font-roboto
|
||||||
|
|
||||||
[flake8]
|
[flake8]
|
||||||
exclude =
|
exclude =
|
||||||
.tox,
|
.tox,
|
||||||
.eggs,
|
.eggs,
|
||||||
.git,
|
.git,
|
||||||
__pycache__,
|
__pycache__,
|
||||||
build,
|
build,
|
||||||
dist
|
dist
|
||||||
ignore =
|
ignore =
|
||||||
E501
|
E501
|
||||||
|
|
||||||
[pimoroni]
|
[pimoroni]
|
||||||
py2deps =
|
py2deps =
|
||||||
python-pip
|
python-pip
|
||||||
python-yaml
|
python-yaml
|
||||||
python-smbus
|
python-smbus
|
||||||
python-pil
|
python-pil
|
||||||
python-spidev
|
python-spidev
|
||||||
python-numpy
|
python-numpy
|
||||||
python-rpi.gpio
|
python-rpi.gpio
|
||||||
py3deps =
|
py3deps =
|
||||||
python3-pip
|
python3-pip
|
||||||
python3-yaml
|
python3-yaml
|
||||||
python3-smbus
|
python3-smbus
|
||||||
python3-pil
|
python3-pil
|
||||||
python3-spidev
|
python3-spidev
|
||||||
python3-numpy
|
python3-numpy
|
||||||
python3-rpi.gpio
|
python3-rpi.gpio
|
||||||
|
python3-aiohttp
|
||||||
commands =
|
commands =
|
||||||
printf "Setting up i2c and SPI..\n"
|
printf "Setting up i2c and SPI..\n"
|
||||||
raspi-config nonint do_spi 0
|
raspi-config nonint do_spi 0
|
||||||
raspi-config nonint do_i2c 0
|
raspi-config nonint do_i2c 0
|
||||||
configtxt =
|
configtxt =
|
||||||
dtoverlay=spi0-cs,cs0_pin=14 # Re-assign CS0 from BCM 8 so that Grow can use it
|
dtoverlay=spi0-cs,cs0_pin=14 # Re-assign CS0 from BCM 8 so that Grow can use it
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
53
server.py
Normal file
53
server.py
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
"""
|
||||||
|
@jorjun Anno Vvii ☉ in ♓ ☽ in ♋
|
||||||
|
|
||||||
|
License: MIT
|
||||||
|
|
||||||
|
Description: Web API for moisture readings: //:8080/api/moisture/
|
||||||
|
"""
|
||||||
|
|
||||||
|
from functools import partial
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from aiohttp import web
|
||||||
|
|
||||||
|
from grow.moisture import Moisture
|
||||||
|
|
||||||
|
|
||||||
|
# Test class
|
||||||
|
#class Moisture():
|
||||||
|
#def __init__(self, channel):
|
||||||
|
#self.channel = channel
|
||||||
|
|
||||||
|
#@property
|
||||||
|
#def moisture(self):
|
||||||
|
#return self.channel *3
|
||||||
|
|
||||||
|
|
||||||
|
json_response = partial(web.json_response, dumps=partial(json.dumps, default=str))
|
||||||
|
|
||||||
|
routes = web.RouteTableDef()
|
||||||
|
|
||||||
|
@routes.get("/api/moisture/")
|
||||||
|
async def reading(request):
|
||||||
|
data = {
|
||||||
|
"m1": meter[0].moisture,
|
||||||
|
"m2": meter[1].moisture,
|
||||||
|
"m3": meter[2].moisture,
|
||||||
|
}
|
||||||
|
return json_response(data)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
app = web.Application()
|
||||||
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
app.add_routes(routes)
|
||||||
|
meter = [Moisture(_+1) for _ in range(3)]
|
||||||
|
|
||||||
|
web.run_app(
|
||||||
|
app,
|
||||||
|
host="0.0.0.0",
|
||||||
|
port=8080,
|
||||||
|
access_log_format='%s %r [%b / %Tf] "%{User-Agent}i"',
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user