The Great Fuckup Fix

The Child’s (Raspberry Pi Pico W) code has been recovered (thanks to me making a backup) and now there’s additional code that lets it connect to the internet, currently a router that has no access to the internet.

Currently, I am working with a Potato, dubbed Big Boi, to host a server that the Child will connect to, read, and display messages through its LED strip whenever Big Boi’s server message changes.

Below will be the current code that has been added since last post’s slight annoyance:

import os

import ipaddress

import wifi

import socketpool

import adafruit_requests as requests

import time

print()

print(“Attempting to connect to WiFi…”)

print()

wifiConnect = False #flag to see if we are already connected

networkPool = os.getenv(‘CIRCUITPY_NETWORK_COUNT’)

for networkNum in range(1, int(networkPool) + 1):

    ##Try next SSID

    if not wifiConnect:

        try:

            thisSID = “CIRCUITPY_WIFI_SSID” + str(networkNum)

            thisKey = “CIRCUITPY_WIFI_PASSWORD” + str(networkNum)

            wifi.radio.connect(os.getenv(thisSID), os.getenv(thisKey))

            print(“Success! Connected to WiFi network: ” + os.getenv(thisSID))

            wifiConnect = True #Mark flag so we can stop trying to connect

            pool = socketpool.SocketPool(wifi.radio)

            #  prints MAC address to REPL

            print(“My MAC addr:”, [hex(i) for i in wifi.radio.mac_address])

            #  prints IP address to REPL

            print(“My IP address is”, wifi.radio.ipv4_address)

        except:

            print(“Failed to connect to Wifi networ+: ” + os.getenv(thisSID))

#Pause if still not connected:

if not wifiConnect:

    print(“=============================================”)

    print(“Failed to connect to all known WiFi networks!”)

    print(“=============================================”)

    time.sleep(30)

After that, this is the code for Big Boi’s server, quite small in comparison to the WHOLE of the Child’s code:

from fastapi import FastAPI, Body

from pydantic import BaseModel

app = FastAPI()

# Store the last message in memory

last_message = {“text”: None}

class Message(BaseModel):

    text: str

@app.get(“/message”)

def get_message():

    “””

    Retrieve the last message sent.

    “””

    return {“last_message”: last_message[“text”]}

@app.post(“/message”)

def post_message(message: Message):

    “””

    Post a new message and update the last message.

    “””

    last_message[“text”] = message.text

    return {“status”: “Message updated”, “last_message”: last_message[“text”]}


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *