Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Connect an Adafruit PyBadge to Wi-fi
Jim Bennett
Jim Bennett

Posted on • Edited on

     

Connect an Adafruit PyBadge to Wi-fi

PyBadge Wi-Fi

This post contains instructions on how to get anAdafruit PyBadge connecting to Wi-Fi using anAdafruit Airlift FeatherWing.

The code here can also be used to connect a PyPortal to Wi-Fi using the built in ESP32 chip

Hardware

  • AnAdafruit PyBadge - this is a Circuit Python device that has a display and controllers similar to a game console. Out of the box it doesn't have Wi-Fi connectivity, but it does have a FeatherWing socket on the back to plug in add-on boards.

  • AnAdafruit Airlift FeatherWing - this is an ESP32 based Wi-Fi add-on board for Adafruit devices. It comes with headers, and when soldered on can plug into the FeatherWing socket on the back of the PyBadge.

Assembling the hardware

  1. Solder the headers to the Airlift FeatherWing

    The feather wing with headers soldered on

  2. Plug the FeatherWing into the socket on the back of the PyBadge

    The feather wing connected to the PyBadge

  3. Connect the PyBadge to your computer via the USB socket

Software

The software to connect to the Wi-Fi needs some Adafruit Circuit Python libraries, as well as some custom code. You can find the code onmy GitHub.

Install the Adafruit libraries

  1. Head to theCircuit Python Releases on the Adafruit GitHub repo

  2. Download the latest version of the libraries for the version of Circuit Python you are using

  3. Unzip the downloaded file if necessary

  4. Locate the PyBadge on your computer. It should be mounted as a drive calledCIRCUITPYTHON

  5. Inside the PyBadge folder, open thelib folder

  6. Copy the following files/folders from the downloaded Circuit Python libraries to thelib folder on the PyBadge

    NameType
    adafruit_bus_deviceFolder
    adafruit_esp32spiFolder
    adafruit_requests.mpyFile

Define a secrets file with Wi-Fi details

Rather than encode Wi-Fi connection details in code, it is better to put them in a separate file that can be hidden from source code control, either by adding it to yourgitignore, or by hiding changes usingthis technique.

  1. Create a file calledsecrets.py in the root of theCIRCUITPYTHON folder.

  2. Add the following to this file

    secrets={'ssid':'<SSID of your Wi-Fi>',# Keep the two '' quotes around the name'password':'<Password>',# Keep the two '' quotes around password}

    Set<SSID of your Wi-Fi> to be the SSID of your Wi-Fi

    Set<Password> to be the password for your Wi-Fi

Connect to the Wi-Fi

  1. Create a new file in the root of theCIRCUITPYTHON folder calledconnection.py

  2. Add the following code to this file. You can find this filemy GitHub if you just want to copy it to your PyBadge

    importboard,busiofromsecretsimportsecretsfromdigitalioimportDigitalInOutimportadafruit_requestsasrequestsimportadafruit_esp32spi.adafruit_esp32spi_socketassocketfromadafruit_esp32spiimportadafruit_esp32spiclassConnection:def__connect(self,spi,cs,ready,reset,log):esp=adafruit_esp32spi.ESP_SPIcontrol(spi,cs,ready,reset)requests.set_socket(socket,esp)iflog:print("Connecting to AP...")whilenotesp.is_connected:try:esp.connect_AP(secrets['ssid'],secrets['password'])exceptRuntimeErrorase:iflog:print("could not connect to AP, retrying:",e)continueiflog:print("Connected to",str(esp.ssid,'utf-8'),"\tRSSI:",esp.rssi)print("My IP address is",esp.pretty_ip(esp.ip_address))# Connect a PyPortaldefconnect_pyportal(self,spi,log=False):esp32_cs=DigitalInOut(board.ESP_CS)esp32_ready=DigitalInOut(board.ESP_BUSY)esp32_reset=DigitalInOut(board.ESP_RESET)self.__connect(spi,esp32_cs,esp32_ready,esp32_reset,log)# Connect a PyBadgedefconnect_pybadge(self,spi,log=False):esp32_cs=DigitalInOut(board.D13)esp32_ready=DigitalInOut(board.D11)esp32_reset=DigitalInOut(board.D12)self.__connect(spi,esp32_cs,esp32_ready,esp32_reset,log)
  3. Add the following code to thecode.py file in theCIRCUITPYTHON folder. This file should already be there as part of the default install, but if it is not there, create it. This file is run when the PyBadge boots up.

    importboardimportbusioimportadafruit_requestsasrequestsfromconnectionimportConnectionspi=busio.SPI(board.SCK,board.MOSI,board.MISO)conn=Connection()conn.connect_pybadge(spi,True)TEXT_URL="http://wifitest.adafruit.com/testwifi/index.html"print("Fetching text from",TEXT_URL)r=requests.get(TEXT_URL)print('-'*40)print(r.text)print('-'*40)r.close()print("Done!")

    This code uses theConnection module to connect to Wi-Fi, then downloads some test to show that it is working.

  4. Save the file. The PyBadge will reboot and connect to the Wi-Fi. You will see the connection on the PyBadge screen and in your terminal if you are connectedto the serial output.

     Press any key to enter the REPL. Use CTRL-D to reload.soft reboot Auto-reload is on. Simply save files over USB to run them or enter REPL to disable. code.py output: Connecting to AP... Connected to <SSID>         RSSI:-52 My IP address is 192.168.197.188 Fetching text from http://wifitest.adafruit.com/testwifi/index.html---------------------------------------- This is atestof Adafruit WiFi! If you canreadthis, its working :)---------------------------------------- Done!

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
andypiper profile image
Andy Piper
Creative Technologist.Freelance #DevRel at #Mastodon.Friendly DEV moderator. LEGO fan. IoT hacker. MicroPython tinkerer.
  • Location
    Kingston upon Thames, London, UK
  • Education
    Modern History, Brasenose College, Oxford
  • Pronouns
    he/they
  • Work
    Freelance consultant; DevRel at Mastodon gGmbH; open to opportunities.
  • Joined

Curious what the battery life is like (and what rating battery you might have used) if the PyBadge is using Wi-Fi often? I guess one option is to only have it connect and grab new info from the interwebs if a button is pressed.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Head of Developer Advocacy at Pieces
  • Location
    Redmond, WA, USA
  • Pronouns
    He/Him
  • Work
    Head of Developer Advocacy at Pieces
  • Joined

More fromJim Bennett

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp