bhave.sh
Published

An ESP32 walks into a bar - Jokes web API using MicroPython

In this tutorial, we use the network and urequests libraries in MicroPython to connect an ESP32 to the web and fetch programming jokes!

BeginnerProtip30 minutes154
An ESP32 walks into a bar - Jokes web API using MicroPython

Things used in this project

Software apps and online services

Wokwi

Story

Read more

Code

Final solution

MicroPython
Paste into Wokwi and click the 'play' button
import network
import time
import urequests

print("Connecting to WiFi", end="")
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('Wokwi-GUEST', '')
while not sta_if.isconnected():
  print(".", end="")
  time.sleep(0.1)
print(" Connected!")

resp = urequests.get("https://v2.jokeapi.dev/joke/Programming")
joke_dict = resp.json()
print("Here's a joke!")
if 'joke' in joke_dict:
  print(joke_dict['joke'])
else:
  print(joke_dict['setup'])
  print(joke_dict['delivery'])

print()

Credits

bhave.sh
3 projects • 4 followers
I use electronics and code to solve important problems in science, IIoT, and space exploration
Contact

Comments

Please log in or sign up to comment.