From b035a1f00761623f60797417630aa21ee5dfb14b Mon Sep 17 00:00:00 2001 From: Brian Corteil Date: Wed, 26 Oct 2022 00:09:29 +0100 Subject: [PATCH 1/9] Create cheer-lights.py CheerLights MicroPython client. You will need the secrets file with your network details, in the same folder as cheerlights.py to run on start rename cheerlights.py to main.py --- .../examples/galactic_unicorn/cheer-lights.py | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 micropython/examples/galactic_unicorn/cheer-lights.py diff --git a/micropython/examples/galactic_unicorn/cheer-lights.py b/micropython/examples/galactic_unicorn/cheer-lights.py new file mode 100644 index 000000000..6389cb01b --- /dev/null +++ b/micropython/examples/galactic_unicorn/cheer-lights.py @@ -0,0 +1,80 @@ +# cheerlights +from time import sleep +from galactic import GalacticUnicorn +from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY + +# setup graphics + +gu = GalacticUnicorn() +graphics = PicoGraphics(DISPLAY) + +width = GalacticUnicorn.WIDTH +height = GalacticUnicorn.HEIGHT + +# fill display + +def draw(colour): + + for x in range(width): + + for y in range(height): + graphics.set_pen(colour) + graphics.pixel(x, y) + + gu.update(graphics) + + +#setup network connection + +import network + +try: + from secrets import WIFI_SSID, WIFI_PASS +except ImportError: + WIFI_SSID = None + WIFI_PASS = None + +wlan = network.WLAN(network.STA_IF) +wlan.active(True) +wlan.connect(WIFI_SSID, WIFI_PASS) + +# Wait for connect or fail +max_wait = 10 +while max_wait > 0: + if wlan.status() < 0 or wlan.status() >= 3: + break + max_wait -= 1 + print('waiting for connection...') + sleep(1) + +# Handle connection error +if wlan.status() != 3: + raise RuntimeError('network connection failed') +else: + print('connected') + status = wlan.ifconfig() + print( f'ip = {status[0]}' ) + + +# cheerlights + +# cheerlights colours + +colours = {b'red': graphics.create_pen(0xff, 0x00, 0x00), b'green': graphics.create_pen(0x00, 0x80, 0x00), b'blue': graphics.create_pen(0x00, 0x00, 0xff), + b'cyan': graphics.create_pen(0x00, 0xff, 0xff), b'white': graphics.create_pen(0xff, 0xff, 0xff), b'oldlace': graphics.create_pen(0xfd, 0xf5, 0xe6), + b'purple': graphics.create_pen(0x80, 0x00, 0x80), b'magenta': graphics.create_pen(0xff, 0x00, 0xff), b'yellow': graphics.create_pen(0xff, 0xff, 0x00), + b'orange': graphics.create_pen(0xff, 0xa5, 0x00), b'pink':graphics.create_pen(0xff, 0xc0, 0xcb)} + +# get request + +import urequests + +while True: + + r = urequests.get('http://api.thingspeak.com/channels/1417/field/1/last.txt') + print(f'Colour: {r.content}') + if r.content in colours: + draw(colours[r.content]) + + r.close() + sleep(15) From c41b2b210967ad49d44f27716a00d87de2d25648 Mon Sep 17 00:00:00 2001 From: Brian Corteil Date: Wed, 26 Oct 2022 00:31:12 +0100 Subject: [PATCH 2/9] secrets.py file added change name and password to match your network in secrets.py --- micropython/examples/galactic_unicorn/secrets.py | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 micropython/examples/galactic_unicorn/secrets.py diff --git a/micropython/examples/galactic_unicorn/secrets.py b/micropython/examples/galactic_unicorn/secrets.py new file mode 100644 index 000000000..da9983175 --- /dev/null +++ b/micropython/examples/galactic_unicorn/secrets.py @@ -0,0 +1,2 @@ +WIFI_SSID = "Name" +WIFI_PASS = "Password" \ No newline at end of file From 263c61c21aec3b36afd716e363bef353d150a4b0 Mon Sep 17 00:00:00 2001 From: Brian Corteil Date: Mon, 31 Oct 2022 20:00:20 +0000 Subject: [PATCH 3/9] Update secrets.py Add new line to line 2 --- micropython/examples/galactic_unicorn/secrets.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/micropython/examples/galactic_unicorn/secrets.py b/micropython/examples/galactic_unicorn/secrets.py index da9983175..cb0cbf2cd 100644 --- a/micropython/examples/galactic_unicorn/secrets.py +++ b/micropython/examples/galactic_unicorn/secrets.py @@ -1,2 +1,2 @@ WIFI_SSID = "Name" -WIFI_PASS = "Password" \ No newline at end of file +WIFI_PASS = "Password" From 4082d99186679104192e2cbb3f90afc8404505b2 Mon Sep 17 00:00:00 2001 From: Brian Corteil Date: Tue, 1 Nov 2022 20:19:12 +0000 Subject: [PATCH 4/9] Update cheer-lights.py corrected formatting, --- .../examples/galactic_unicorn/cheer-lights.py | 63 ++++++++----------- 1 file changed, 27 insertions(+), 36 deletions(-) diff --git a/micropython/examples/galactic_unicorn/cheer-lights.py b/micropython/examples/galactic_unicorn/cheer-lights.py index 6389cb01b..bc01ad8c6 100644 --- a/micropython/examples/galactic_unicorn/cheer-lights.py +++ b/micropython/examples/galactic_unicorn/cheer-lights.py @@ -2,21 +2,25 @@ from time import sleep from galactic import GalacticUnicorn from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY +import network +import urequests -# setup graphics +try: + from secrets import WIFI_SSID, WIFI_PASS +except ImportError: + WIFI_SSID = None + WIFI_PASS = None +# setup graphics gu = GalacticUnicorn() graphics = PicoGraphics(DISPLAY) - width = GalacticUnicorn.WIDTH height = GalacticUnicorn.HEIGHT -# fill display +# fill display def draw(colour): - for x in range(width): - for y in range(height): graphics.set_pen(colour) graphics.pixel(x, y) @@ -24,16 +28,7 @@ def draw(colour): gu.update(graphics) -#setup network connection - -import network - -try: - from secrets import WIFI_SSID, WIFI_PASS -except ImportError: - WIFI_SSID = None - WIFI_PASS = None - +# setup network connection wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.connect(WIFI_SSID, WIFI_PASS) @@ -41,40 +36,36 @@ def draw(colour): # Wait for connect or fail max_wait = 10 while max_wait > 0: - if wlan.status() < 0 or wlan.status() >= 3: - break - max_wait -= 1 - print('waiting for connection...') - sleep(1) + if wlan.status() < 0 or wlan.status() >= 3: + break + max_wait -= 1 + print('waiting for connection...') + sleep(1) # Handle connection error if wlan.status() != 3: - raise RuntimeError('network connection failed') + raise RuntimeError('network connection failed') else: - print('connected') - status = wlan.ifconfig() - print( f'ip = {status[0]}' ) - + print('connected') + status = wlan.ifconfig() + print(f'ip = {status[0]}') # cheerlights # cheerlights colours - -colours = {b'red': graphics.create_pen(0xff, 0x00, 0x00), b'green': graphics.create_pen(0x00, 0x80, 0x00), b'blue': graphics.create_pen(0x00, 0x00, 0xff), - b'cyan': graphics.create_pen(0x00, 0xff, 0xff), b'white': graphics.create_pen(0xff, 0xff, 0xff), b'oldlace': graphics.create_pen(0xfd, 0xf5, 0xe6), - b'purple': graphics.create_pen(0x80, 0x00, 0x80), b'magenta': graphics.create_pen(0xff, 0x00, 0xff), b'yellow': graphics.create_pen(0xff, 0xff, 0x00), - b'orange': graphics.create_pen(0xff, 0xa5, 0x00), b'pink':graphics.create_pen(0xff, 0xc0, 0xcb)} +colours = {b'red': graphics.create_pen(0xff, 0x00, 0x00), b'green': graphics.create_pen(0x00, 0x80, 0x00), + b'blue': graphics.create_pen(0x00, 0x00, 0xff), + b'cyan': graphics.create_pen(0x00, 0xff, 0xff), b'white': graphics.create_pen(0xff, 0xff, 0xff), + b'oldlace': graphics.create_pen(0xfd, 0xf5, 0xe6), + b'purple': graphics.create_pen(0x80, 0x00, 0x80), b'magenta': graphics.create_pen(0xff, 0x00, 0xff), + b'yellow': graphics.create_pen(0xff, 0xff, 0x00), + b'orange': graphics.create_pen(0xff, 0xa5, 0x00), b'pink': graphics.create_pen(0xff, 0xc0, 0xcb)} # get request - -import urequests - while True: - r = urequests.get('http://api.thingspeak.com/channels/1417/field/1/last.txt') print(f'Colour: {r.content}') if r.content in colours: - draw(colours[r.content]) - + draw(colours[r.content]) r.close() sleep(15) From 47a8373bbbad10851c7f21dd6dde6f0868ab95ac Mon Sep 17 00:00:00 2001 From: Brian Corteil Date: Tue, 14 Feb 2023 23:07:35 +0000 Subject: [PATCH 5/9] Create cheerlights.py Updated to use the Pimoroni standard Wi-Fi method to be inline with other examples and rename filed to cheerlights.py so can be import from REPL --- .../examples/galactic_unicorn/cheerlights.py | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 micropython/examples/galactic_unicorn/cheerlights.py diff --git a/micropython/examples/galactic_unicorn/cheerlights.py b/micropython/examples/galactic_unicorn/cheerlights.py new file mode 100644 index 000000000..04520a0ff --- /dev/null +++ b/micropython/examples/galactic_unicorn/cheerlights.py @@ -0,0 +1,65 @@ +# cheerlights +from time import sleep +from galactic import GalacticUnicorn +from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY +import uasyncio +from network_manager import NetworkManager +import urequests +import WIFI_CONFIG + + +# setup graphics +gu = GalacticUnicorn() +graphics = PicoGraphics(DISPLAY) +width = GalacticUnicorn.WIDTH +height = GalacticUnicorn.HEIGHT + + +# fill display +def draw(colour): + for x in range(width): + for y in range(height): + graphics.set_pen(colour) + graphics.pixel(x, y) + + gu.update(graphics) + + +def status_handler(mode, status, ip): + # reports wifi connection status + print(mode, status, ip) + print('Connecting to wifi...') + if status is not None: + if status: + print('Wifi connection successful!') + else: + print('Wifi connection failed!') + + +# set up wifi +try: + network_manager = NetworkManager(WIFI_CONFIG.COUNTRY, status_handler=status_handler) + uasyncio.get_event_loop().run_until_complete(network_manager.client(WIFI_CONFIG.SSID, WIFI_CONFIG.PSK)) +except Exception as e: + print(f'Wifi connection failed! {e}') + + +# cheerlights + +# cheerlights colours +colours = {b'red': graphics.create_pen(0xff, 0x00, 0x00), b'green': graphics.create_pen(0x00, 0x80, 0x00), + b'blue': graphics.create_pen(0x00, 0x00, 0xff), + b'cyan': graphics.create_pen(0x00, 0xff, 0xff), b'white': graphics.create_pen(0xff, 0xff, 0xff), + b'oldlace': graphics.create_pen(0xfd, 0xf5, 0xe6), + b'purple': graphics.create_pen(0x80, 0x00, 0x80), b'magenta': graphics.create_pen(0xff, 0x00, 0xff), + b'yellow': graphics.create_pen(0xff, 0xff, 0x00), + b'orange': graphics.create_pen(0xff, 0xa5, 0x00), b'pink': graphics.create_pen(0xff, 0xc0, 0xcb)} + +# get request +while True: + r = urequests.get('http://api.thingspeak.com/channels/1417/field/1/last.txt') + print(f'Colour: {r.content}') + if r.content in colours: + draw(colours[r.content]) + r.close() + sleep(15) From ba7a17ff430d1f67431d0efa40a43df16aa0047f Mon Sep 17 00:00:00 2001 From: Brian Corteil Date: Wed, 22 Feb 2023 01:57:33 +0000 Subject: [PATCH 6/9] removed white space on lines 26 and 27 --- .../examples/galactic_unicorn/cheer-lights.py | 71 ------------------- .../examples/galactic_unicorn/cheerlights.py | 6 +- .../examples/galactic_unicorn/secrets.py | 2 - 3 files changed, 3 insertions(+), 76 deletions(-) delete mode 100644 micropython/examples/galactic_unicorn/cheer-lights.py delete mode 100644 micropython/examples/galactic_unicorn/secrets.py diff --git a/micropython/examples/galactic_unicorn/cheer-lights.py b/micropython/examples/galactic_unicorn/cheer-lights.py deleted file mode 100644 index bc01ad8c6..000000000 --- a/micropython/examples/galactic_unicorn/cheer-lights.py +++ /dev/null @@ -1,71 +0,0 @@ -# cheerlights -from time import sleep -from galactic import GalacticUnicorn -from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY -import network -import urequests - -try: - from secrets import WIFI_SSID, WIFI_PASS -except ImportError: - WIFI_SSID = None - WIFI_PASS = None - -# setup graphics -gu = GalacticUnicorn() -graphics = PicoGraphics(DISPLAY) -width = GalacticUnicorn.WIDTH -height = GalacticUnicorn.HEIGHT - - -# fill display -def draw(colour): - for x in range(width): - for y in range(height): - graphics.set_pen(colour) - graphics.pixel(x, y) - - gu.update(graphics) - - -# setup network connection -wlan = network.WLAN(network.STA_IF) -wlan.active(True) -wlan.connect(WIFI_SSID, WIFI_PASS) - -# Wait for connect or fail -max_wait = 10 -while max_wait > 0: - if wlan.status() < 0 or wlan.status() >= 3: - break - max_wait -= 1 - print('waiting for connection...') - sleep(1) - -# Handle connection error -if wlan.status() != 3: - raise RuntimeError('network connection failed') -else: - print('connected') - status = wlan.ifconfig() - print(f'ip = {status[0]}') - -# cheerlights - -# cheerlights colours -colours = {b'red': graphics.create_pen(0xff, 0x00, 0x00), b'green': graphics.create_pen(0x00, 0x80, 0x00), - b'blue': graphics.create_pen(0x00, 0x00, 0xff), - b'cyan': graphics.create_pen(0x00, 0xff, 0xff), b'white': graphics.create_pen(0xff, 0xff, 0xff), - b'oldlace': graphics.create_pen(0xfd, 0xf5, 0xe6), - b'purple': graphics.create_pen(0x80, 0x00, 0x80), b'magenta': graphics.create_pen(0xff, 0x00, 0xff), - b'yellow': graphics.create_pen(0xff, 0xff, 0x00), - b'orange': graphics.create_pen(0xff, 0xa5, 0x00), b'pink': graphics.create_pen(0xff, 0xc0, 0xcb)} - -# get request -while True: - r = urequests.get('http://api.thingspeak.com/channels/1417/field/1/last.txt') - print(f'Colour: {r.content}') - if r.content in colours: - draw(colours[r.content]) - r.close() - sleep(15) diff --git a/micropython/examples/galactic_unicorn/cheerlights.py b/micropython/examples/galactic_unicorn/cheerlights.py index 04520a0ff..528a55cfd 100644 --- a/micropython/examples/galactic_unicorn/cheerlights.py +++ b/micropython/examples/galactic_unicorn/cheerlights.py @@ -23,8 +23,8 @@ def draw(colour): graphics.pixel(x, y) gu.update(graphics) - - + + def status_handler(mode, status, ip): # reports wifi connection status print(mode, status, ip) @@ -62,4 +62,4 @@ def status_handler(mode, status, ip): if r.content in colours: draw(colours[r.content]) r.close() - sleep(15) + sleep(15) \ No newline at end of file diff --git a/micropython/examples/galactic_unicorn/secrets.py b/micropython/examples/galactic_unicorn/secrets.py deleted file mode 100644 index cb0cbf2cd..000000000 --- a/micropython/examples/galactic_unicorn/secrets.py +++ /dev/null @@ -1,2 +0,0 @@ -WIFI_SSID = "Name" -WIFI_PASS = "Password" From a5538da8e6aeaeed1fb22b40f3a2bcc1e34dfc4a Mon Sep 17 00:00:00 2001 From: Brian Corteil Date: Wed, 22 Feb 2023 02:05:20 +0000 Subject: [PATCH 7/9] Update cheerlights.py added newline to last line --- micropython/examples/galactic_unicorn/cheerlights.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/micropython/examples/galactic_unicorn/cheerlights.py b/micropython/examples/galactic_unicorn/cheerlights.py index 528a55cfd..24ea289fb 100644 --- a/micropython/examples/galactic_unicorn/cheerlights.py +++ b/micropython/examples/galactic_unicorn/cheerlights.py @@ -62,4 +62,4 @@ def status_handler(mode, status, ip): if r.content in colours: draw(colours[r.content]) r.close() - sleep(15) \ No newline at end of file + sleep(15) From 37ff6b8b37fc00eb3d848cfc9d42ebefce2f5ce3 Mon Sep 17 00:00:00 2001 From: Brian Corteil Date: Thu, 23 Feb 2023 21:35:29 +0000 Subject: [PATCH 8/9] Update cheerlights.py updated with @Gadgetoid suggestion. --- micropython/examples/galactic_unicorn/cheerlights.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/micropython/examples/galactic_unicorn/cheerlights.py b/micropython/examples/galactic_unicorn/cheerlights.py index 24ea289fb..4e9ed632a 100644 --- a/micropython/examples/galactic_unicorn/cheerlights.py +++ b/micropython/examples/galactic_unicorn/cheerlights.py @@ -17,11 +17,9 @@ # fill display def draw(colour): - for x in range(width): - for y in range(height): - graphics.set_pen(colour) - graphics.pixel(x, y) - + + graphics.set_pen(colour) + graphics.clear() gu.update(graphics) From f882efc90197218e6c9b6166398abf016ed6a451 Mon Sep 17 00:00:00 2001 From: Brian Corteil Date: Tue, 28 Feb 2023 23:39:35 +0000 Subject: [PATCH 9/9] Update cheerlights.py removed white space from line 20 in cheerlights --- micropython/examples/galactic_unicorn/cheerlights.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/micropython/examples/galactic_unicorn/cheerlights.py b/micropython/examples/galactic_unicorn/cheerlights.py index 4e9ed632a..62a06eebc 100644 --- a/micropython/examples/galactic_unicorn/cheerlights.py +++ b/micropython/examples/galactic_unicorn/cheerlights.py @@ -17,7 +17,7 @@ # fill display def draw(colour): - + graphics.set_pen(colour) graphics.clear() gu.update(graphics)