1
- # pi-pico-serial-web-api
1
+ # [ pi-pico-serial-web-api] ( https://chabala.github.io/pi-pico-serial-web-api/ )
2
2
3
3
Web interface & API for an RS-232 controlled device (a Sharp AQUOS TV).
4
4
5
5
Update: This project won the [ third place prize in WIZnet's Ethernet HAT Contest 2022] (
6
6
https://web.archive.org/web/20220511160854/https://maker.wiznet.io/wiznet-ethernet-hat-contest-winners/ ).
7
7
8
+ <div style =" text-align : right " >
9
+ <a href =" https://github.com/chabala/pi-pico-serial-web-api/ " >GitHub repository for this page</a > /
10
+ <a href =" https://chabala.github.io/ " >Up to the root page</a >
11
+ </div >
12
+
8
13
## Abstract
9
14
10
15
A Raspberry Pi Pico with hardwired Ethernet seems like the perfect platform to add web APIs to serial
@@ -56,10 +61,10 @@ just enough for this kind of project, and no more.
56
61
57
62
Step zero in any project should be a search to see if anyone else has already done the work. I
58
63
searched GitHub for other projects that were trying to interface with Sharp TVs and found these:
59
- * https://github.com/partouf/AquosComm [ Pascal, 2009]
60
- * https://github.com/bwkeller/pyaquos [ Python, 2012]
61
- * https://github.com/p4ddy1/aquoscontrol [ Go, 2017]
62
- * https://github.com/neerlent/aquostv_serial [ Python, 2019]
64
+ * [ https://github.com/partouf/AquosComm ] ( https://github.com/partouf/AquosComm ) [ Pascal, 2009]
65
+ * [ https://github.com/bwkeller/pyaquos ] ( https://github.com/bwkeller/pyaquos ) [ Python, 2012]
66
+ * [ https://github.com/p4ddy1/aquoscontrol ] ( https://github.com/p4ddy1/aquoscontrol ) [ Go, 2017]
67
+ * [ https://github.com/neerlent/aquostv_serial ] ( https://github.com/neerlent/aquostv_serial ) [ Python, 2019]
63
68
64
69
These all target running software on a full computer rather than a microcontroller, but at least
65
70
some were also trying to integrate with home automation software. It's always good to see what's
@@ -76,9 +81,9 @@ could connect a common USB-to-serial adapter to it.
76
81
### Selecting a software platform
77
82
78
83
Possible software toolkits available for the Pi Pico and clones:
79
- * C/C++ and/or assembly - https://github.com/raspberrypi/pico-sdk
80
- * MicroPython - https://github.com/micropython/micropython
81
- * CircuitPython - https://github.com/adafruit/circuitpython
84
+ * C/C++ and/or assembly - [ https://github.com/raspberrypi/pico-sdk ] ( https://github.com/raspberrypi/pico-sdk )
85
+ * MicroPython - [ https://github.com/micropython/micropython ] ( https://github.com/micropython/micropython )
86
+ * CircuitPython - [ https://github.com/adafruit/circuitpython ] ( https://github.com/adafruit/circuitpython )
82
87
83
88
My C++ is very rusty, so one of the Python variants seemed like a good place to start. CircuitPython is a
84
89
fork of MicroPython with an emphasis on ease of use for beginners, and is backed by Adafruit to ensure
@@ -125,28 +130,35 @@ CircuitPython.
125
130
Googled the error, but the results are people using CircuitPython on Raspberry Pi (not Pi Pico)
126
131
being advised to install ` adafruit_blinka ` , which is an abstraction layer that I should not need.
127
132
128
- I started poking around in the issues on https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k
129
- and found https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k/pull/49 which links to
130
- https://gist.github.com/anecdata/3c6f37c05a91f7b769dad7517bfe3aa1 which is example code that works
133
+ I started poking around in the issues on [ https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k ] (
134
+ https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k )
135
+ and found [ https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k/pull/49 ] (
136
+ https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k/pull/49 ) which links to
137
+ [ https://gist.github.com/anecdata/3c6f37c05a91f7b769dad7517bfe3aa1 ] (
138
+ https://gist.github.com/anecdata/3c6f37c05a91f7b769dad7517bfe3aa1 ) which is example code that works
131
139
fine.
132
140
133
141
Looking back at the WIZnet Getting Started guide, it seems they may have intended
134
- https://github.com/Wiznet/RP2040-HAT-CircuitPython/blob/master/examples/Network/W5x00_Ping_Test.py
142
+ [ https://github.com/Wiznet/RP2040-HAT-CircuitPython/blob/master/examples/Network/W5x00_Ping_Test.py ] (
143
+ https://github.com/Wiznet/RP2040-HAT-CircuitPython/blob/master/examples/Network/W5x00_Ping_Test.py )
135
144
as initial example code. It's very similar to the working gist. Of interest though, they
136
145
both use different pins for the reset pin. The WIZnet example is correctly using ` GP20 ` , but using
137
146
that pin in the gist's code causes it to fail to initialize. This turns out to be a bug in the
138
- driver library, and I opened https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k/issues/51
147
+ driver library, and I opened [ https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k/issues/51 ] (
148
+ https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k/issues/51 )
139
149
to fix it.
140
150
141
151
Tested web server example code from
142
- https://github.com/Wiznet/RP2040-HAT-CircuitPython/blob/master/examples/HTTP/Webserver/W5x00_WebServer.py
152
+ [ https://github.com/Wiznet/RP2040-HAT-CircuitPython/blob/master/examples/HTTP/Webserver/W5x00_WebServer.py ] (
153
+ https://github.com/Wiznet/RP2040-HAT-CircuitPython/blob/master/examples/HTTP/Webserver/W5x00_WebServer.py )
143
154
which works and shows the basics of URL routing.
144
155
145
156
At this point I've gotten network functionality working, and I want to test the serial end of things.
146
157
Based on some conversation on the [ Adafruit Discord] ( http://adafru.it/discord ) , while the Pi Pico
147
158
hardware supports USB host mode, CircuitPython itself does not yet. There is work underway in the
148
159
USB stack (TinyUSB), and preliminary work on the CircuitPython API for it:
149
- https://github.com/tannewt/circuitpython/tree/usb_host but nothing ready for use.
160
+ [ https://github.com/tannewt/circuitpython/tree/usb_host ] (
161
+ https://github.com/tannewt/circuitpython/tree/usb_host ) but nothing ready for use.
150
162
151
163
My option for using the USB serial adapter in the near term may only be to implement the USB host
152
164
logic myself in C, which is more than I bargained for. As a result, I ordered a [ Waveshare
@@ -177,7 +189,36 @@ https://en.wikipedia.org/wiki/HATEOAS) style, and learned how to handle GET and
177
189
with it. It should be easier to follow that pattern and start flushing out the serial TV control
178
190
endpoint now without making a mess of it.
179
191
180
- https://github.com/chabala/pi-pico-serial-web-api/blob/e81f4f882127e0290d8b4310b3c9e9d4933914b9/CIRCUITPY/code.py#L69-L93
192
+ From: [ https://github.com/chabala/pi-pico-serial-web-api/blob/e81f4f882127e0290d8b4310b3c9e9d4933914b9/CIRCUITPY/code.py#L69-L93 ] (
193
+ https://github.com/chabala/pi-pico-serial-web-api/blob/e81f4f882127e0290d8b4310b3c9e9d4933914b9/CIRCUITPY/code.py#L69-L93 )
194
+
195
+ ``` python
196
+ @web_app.route (" /led/" , [" GET" , " POST" ])
197
+ def led (request ):
198
+ print (f " \n { request.method} { request.path} " )
199
+ set_value = None
200
+ if request.method == " GET" :
201
+ set_value = request.query_params.get(' set' )
202
+ elif request.method == " POST" :
203
+ post_params = request.__parse_query_params(request.body.getvalue())
204
+ request.body.close()
205
+ set_value = post_params.get(' set' )
206
+ if set_value is not None :
207
+ if set_value == ' toggle' :
208
+ user_led.value = not user_led.value
209
+ else :
210
+ user_led.value = True if set_value == ' on' else False
211
+ status = ' on' if user_led.value else ' off'
212
+ return " 200 OK" , [], [html_doc(" User LED status" , f """ LED is { status} <br><br>
213
+ <form action="/led/" method="post">
214
+ Actions:<br>
215
+ <input type="radio" id="on" name="set" value="on"><label for="on"> turn LED on</label><br>
216
+ <input type="radio" id="off" name="set" value="off"><label for="off"> turn LED off</label><br>
217
+ <input type="radio" id="toggle" name="set" value="toggle"><label for="toggle"> toggle LED state</label><br>
218
+ <button type="submit">Submit</button>
219
+ </form><br><br>
220
+ <a href="/" id="root">Back to root</a> """ )]
221
+ ```
181
222
182
223
![ LED interface screenshot] ( documentation/ui-screenshots/led-interface.png " LED interface screenshot ")
183
224
@@ -259,6 +300,7 @@ The `/led/` and `/serial/` endpoints were already shown above.
259
300
* The ` /tv/power/ ` endpoint:
260
301
261
302
![ power] ( documentation/ui-screenshots/tv-power.png " power ")
303
+
262
304
![ power status] ( documentation/ui-screenshots/tv-power-status.png " power status ")
263
305
264
306
Interacting with any of the commands will return to the same page, but the response from the
@@ -268,7 +310,9 @@ level of the API structure.
268
310
* The ` /tv/volume/ ` endpoint:
269
311
270
312
![ volume set] ( documentation/ui-screenshots/tv-volume-set.png " volume set ")
313
+
271
314
![ volume status] ( documentation/ui-screenshots/tv-volume-status.png " volume status ")
315
+
272
316
![ mute status] ( documentation/ui-screenshots/tv-mute-status.png " mute status ")
273
317
274
318
* The ` /tv/input/ ` endpoint:
@@ -366,9 +410,9 @@ final product out of it.
366
410
367
411
All project code is MIT licensed. Adafruit library code is present as a convenience, but it retains
368
412
its own license(s), refer to the library repositories for more detail:
369
- * https://github.com/adafruit/Adafruit_CircuitPython_Requests
370
- * https://github.com/adafruit/Adafruit_CircuitPython_WSGI
371
- * https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k
413
+ * [ https://github.com/adafruit/Adafruit_CircuitPython_Requests ] ( https://github.com/adafruit/Adafruit_CircuitPython_Requests )
414
+ * [ https://github.com/adafruit/Adafruit_CircuitPython_WSGI ] ( https://github.com/adafruit/Adafruit_CircuitPython_WSGI )
415
+ * [ https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k ] ( https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k )
372
416
373
417
This repository contains a reproduction of a single page of a manual, presumed to be under copyright
374
418
by SHARP ELECTRONICS CORPORATION. I assert fair use for including it.
0 commit comments