使用aiohttp可以创建本地API接口用于远程调用SenseHat,再配合frp,可以实现在线调用SenseHat。
将系统python版本切换为3(aiohttp依赖python3)
1 2 3 4 |
ls /usr/bin/python* update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1 update-alternatives --install /usr/bin/python python /usr/bin/python3.7 2 update-alternatives --config python |
安装pip
1 |
apt install python3-pip |
安装aiohttp
1 |
pip3 install aiohttp |
创建测试nano ./test.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
from aiohttp import web from sense_hat import SenseHat sense = SenseHat() async def hello(request): data = {'some': 'data'} return web.json_response(data) async def test(request): sense.show_letter("Z") #return Response(text="All right!") async def clear(request): sense.clear() async def Environmental(request): get_temperature_from_pressure = sense.get_temperature_from_pressure() get_temperature_from_humidity = sense.get_temperature_from_humidity() get_temperature = sense.get_temperature() get_humidity = sense.get_humidity() get_pressure = sense.get_pressure() data = {'get_temperature_from_pressure': get_temperature_from_pressure, 'get_temperature_from_humidity': get_temperature_from_humidity, 'get_temperature': get_temperature, 'get_humidity': get_humidity, 'get_pressure': get_pressure} return web.json_response(data) async def show_message(request): sense.set_rotation(180) sense.show_message("One small step for Pi!", text_colour=[255, 0, 0]) app = web.Application() app.add_routes([web.get('/', hello)]) app.add_routes([web.get('/test', test)]) app.add_routes([web.get('/clear', clear)]) app.add_routes([web.get('/env', Environmental)]) app.add_routes([web.get('/show_message', show_message)]) web.run_app(app) |
运行测试
1 |
python ./test.py |
输出
1 2 |
======== Running on http://0.0.0.0:8080 ======== (Press CTRL+C to quit) |
访问http://your-raspberry-ip-address:8080
成功返回 {some: "data"}
There are no comments yet