From acbff3e98d8759e9d5a3b191c9320a407e1414eb Mon Sep 17 00:00:00 2001 From: jorjun Date: Sat, 12 Mar 2022 16:08:58 +0000 Subject: [PATCH 1/3] Example moisture-reading web API (aiohttp) --- library/setup.cfg | 79 ++++++++++++++++++++++++----------------------- server.py | 53 +++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 39 deletions(-) create mode 100644 server.py diff --git a/library/setup.cfg b/library/setup.cfg index ef65b6d..6fd4766 100644 --- a/library/setup.cfg +++ b/library/setup.cfg @@ -10,64 +10,65 @@ long_description_content_type = text/markdown keywords = Raspberry Pi url = https://www.pimoroni.com project_urls = - GitHub=https://www.github.com/pimoroni/grow-python + GitHub=https://www.github.com/pimoroni/grow-python license = MIT # 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 license_files = LICENSE.txt classifiers = - Development Status :: 4 - Beta - Operating System :: POSIX :: Linux - License :: OSI Approved :: MIT License - Intended Audience :: Developers - Programming Language :: Python :: 3 - Topic :: Software Development - Topic :: Software Development :: Libraries - Topic :: System :: Hardware + Development Status :: 4 - Beta + Operating System :: POSIX :: Linux + License :: OSI Approved :: MIT License + Intended Audience :: Developers + Programming Language :: Python :: 3 + Topic :: Software Development + Topic :: Software Development :: Libraries + Topic :: System :: Hardware [options] python_requires = >= 3 packages = grow install_requires = - ltr559 - st7735 - pyyaml - fonts - font-roboto + ltr559 + st7735 + pyyaml + fonts + font-roboto [flake8] exclude = - .tox, - .eggs, - .git, - __pycache__, - build, - dist + .tox, + .eggs, + .git, + __pycache__, + build, + dist ignore = - E501 + E501 [pimoroni] py2deps = - python-pip - python-yaml - python-smbus - python-pil - python-spidev - python-numpy - python-rpi.gpio + python-pip + python-yaml + python-smbus + python-pil + python-spidev + python-numpy + python-rpi.gpio py3deps = - python3-pip - python3-yaml - python3-smbus - python3-pil - python3-spidev - python3-numpy - python3-rpi.gpio + python3-pip + python3-yaml + python3-smbus + python3-pil + python3-spidev + python3-numpy + python3-rpi.gpio + python3-aiohttp commands = - printf "Setting up i2c and SPI..\n" - raspi-config nonint do_spi 0 - raspi-config nonint do_i2c 0 + printf "Setting up i2c and SPI..\n" + raspi-config nonint do_spi 0 + raspi-config nonint do_i2c 0 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 diff --git a/server.py b/server.py new file mode 100644 index 0000000..19b238f --- /dev/null +++ b/server.py @@ -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"', + ) From 90c351e6e38a781ff75558c552b2da6ffcbc68a9 Mon Sep 17 00:00:00 2001 From: jorjun Date: Thu, 19 Jan 2023 15:45:44 +0000 Subject: [PATCH 2/3] Revert "Example moisture-reading web API (aiohttp)" This reverts commit dfa1e464ec63682c2edef593984e043561509daf. --- library/setup.cfg | 79 +++++++++++++++++++++++------------------------ server.py | 53 ------------------------------- 2 files changed, 39 insertions(+), 93 deletions(-) delete mode 100644 server.py diff --git a/library/setup.cfg b/library/setup.cfg index 6fd4766..ef65b6d 100644 --- a/library/setup.cfg +++ b/library/setup.cfg @@ -10,65 +10,64 @@ long_description_content_type = text/markdown keywords = Raspberry Pi url = https://www.pimoroni.com project_urls = - GitHub=https://www.github.com/pimoroni/grow-python + GitHub=https://www.github.com/pimoroni/grow-python license = MIT # 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 license_files = LICENSE.txt classifiers = - Development Status :: 4 - Beta - Operating System :: POSIX :: Linux - License :: OSI Approved :: MIT License - Intended Audience :: Developers - Programming Language :: Python :: 3 - Topic :: Software Development - Topic :: Software Development :: Libraries - Topic :: System :: Hardware + Development Status :: 4 - Beta + Operating System :: POSIX :: Linux + License :: OSI Approved :: MIT License + Intended Audience :: Developers + Programming Language :: Python :: 3 + Topic :: Software Development + Topic :: Software Development :: Libraries + Topic :: System :: Hardware [options] python_requires = >= 3 packages = grow install_requires = - ltr559 - st7735 - pyyaml - fonts - font-roboto + ltr559 + st7735 + pyyaml + fonts + font-roboto [flake8] exclude = - .tox, - .eggs, - .git, - __pycache__, - build, - dist + .tox, + .eggs, + .git, + __pycache__, + build, + dist ignore = - E501 + E501 [pimoroni] py2deps = - python-pip - python-yaml - python-smbus - python-pil - python-spidev - python-numpy - python-rpi.gpio + python-pip + python-yaml + python-smbus + python-pil + python-spidev + python-numpy + python-rpi.gpio py3deps = - python3-pip - python3-yaml - python3-smbus - python3-pil - python3-spidev - python3-numpy - python3-rpi.gpio - python3-aiohttp + python3-pip + python3-yaml + python3-smbus + python3-pil + python3-spidev + python3-numpy + python3-rpi.gpio commands = - printf "Setting up i2c and SPI..\n" - raspi-config nonint do_spi 0 - raspi-config nonint do_i2c 0 + printf "Setting up i2c and SPI..\n" + raspi-config nonint do_spi 0 + raspi-config nonint do_i2c 0 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 diff --git a/server.py b/server.py deleted file mode 100644 index 19b238f..0000000 --- a/server.py +++ /dev/null @@ -1,53 +0,0 @@ -""" -@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"', - ) From 7ed9f7fca14e3de15d7e885876ece8bd700f015e Mon Sep 17 00:00:00 2001 From: jorjun Date: Thu, 19 Jan 2023 15:53:10 +0000 Subject: [PATCH 3/3] tidy up PR Thanks for feedback and for publishing this. Co-authored-by: Phil Howard --- examples/web_serve.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 examples/web_serve.py diff --git a/examples/web_serve.py b/examples/web_serve.py new file mode 100644 index 0000000..1b68ab6 --- /dev/null +++ b/examples/web_serve.py @@ -0,0 +1,36 @@ +""" +@jorjun Anno Vvii ☉ in ♓ ☽ in ♋ +License: MIT +Description: Web API for moisture readings: http://:8080/ +""" +from functools import partial +import json +import logging +from aiohttp import web +from grow.moisture import Moisture + +json_response = partial(web.json_response, dumps=partial(json.dumps, default=str)) +routes = web.RouteTableDef() + + +@routes.get("/") # Or whatever URL path you want +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"', + )