ESP32 Wi-Fi Weather Station: Wiring, Code and Simulator
To build this ESP32 Wi-Fi Weather Station project you need an ESP32 DevKit and a 16x2 LCD. Joins Wi-Fi, fetches the live temperature from the Open-Meteo API with urequests, and shows it on an I2C LCD. Make the 4 connections in the wiring table, upload the MicroPython code, and open the Network tab to watch the request go out. Change the latitude and longitude to your own city.
Open this circuit in the free ESP32 DevKit simulator
Parts list
| Qty | Part | What it does |
|---|---|---|
| 1 | ESP32 DevKit | the microcontroller board |
| 1 | 16x2 LCD | Character LCD (HD44780). Wiring "full": RS, E and D4–D7 to pins (LiquidCrystal). Wiring "i2c": SDA to A4, SCL to A5 (LiquidCrystal_I2C, address 0x27). |
Wiring
| # | From | To |
|---|---|---|
| 1 | ESP32 DevKit D21 | 16x2 LCD SDA |
| 2 | ESP32 DevKit D22 | 16x2 LCD SCL |
| 3 | ESP32 DevKit VIN | 16x2 LCD VCC |
| 4 | ESP32 DevKit GND | 16x2 LCD GND |
Step by step
- Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the ESP32 DevKit. No install or account is needed.
- Add the parts. Add a 16x2 LCD from the parts palette.
- Wire 1. Connect ESP32 DevKit D21 to 16x2 LCD SDA.
- Wire 2. Connect ESP32 DevKit D22 to 16x2 LCD SCL.
- Wire 3. Connect ESP32 DevKit VIN to 16x2 LCD VCC.
- Wire 4. Connect ESP32 DevKit GND to 16x2 LCD GND.
- Paste the MicroPython code. Paste the MicroPython code below into the code editor.
- Run it. Press Run. Open the Network tab to watch the request go out. Change the latitude and longitude to your own city.
MicroPython code
# ESP32 weather station: join Wi-Fi, ask a real weather API, show it on the LCD.
# Open-Meteo is free and needs no key. Change the latitude/longitude to your city.
# The LCD is a 16x2 with a PCF8574 backpack on the ESP32's I2C pins (SDA 21, SCL 22).
import network, urequests, time
from machine import Pin, I2C
i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=100000)
ADDR = 0x27
def nibble(v, rs):
d = (v << 4) | 0x08 | rs # 0x08 keeps the backlight on
i2c.writeto(ADDR, bytes([d | 0x04, d])) # pulse E
def send(v, rs=0):
nibble(v >> 4, rs)
nibble(v & 0x0F, rs)
def lcd_init():
time.sleep_ms(50)
for _ in range(3):
nibble(0x03, 0)
time.sleep_ms(5)
nibble(0x02, 0)
for c in (0x28, 0x0C, 0x06, 0x01):
send(c)
time.sleep_ms(2)
def write(text, row=0):
send(0x80 | (0x40 * row))
for ch in "{:<16}".format(text)[:16]:
send(ord(ch), 1)
lcd_init()
write("Joining Wi-Fi...")
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("BFB-Lab", "") # the lab's open access point
while not wlan.isconnected():
time.sleep_ms(200)
print("connected, IP", wlan.ifconfig()[0])
write(wlan.ifconfig()[0], 1)
URL = ("https://api.open-meteo.com/v1/forecast"
"?latitude=28.61&longitude=77.21¤t=temperature_2m,relative_humidity_2m")
while True:
try:
r = urequests.get(URL)
now = r.json()["current"]
r.close()
t, h = now["temperature_2m"], now["relative_humidity_2m"]
print("New Delhi:", t, "C,", h, "% humidity")
write("New Delhi")
write("%.1fC %d%%RH" % (t, h), 1)
except OSError as e:
print("request failed:", e)
write("request failed", 1)
time.sleep(30)
What to try
Open the Network tab to watch the request go out. Change the latitude and longitude to your own city.
Frequently asked questions
How do I wire the Wi-Fi Weather Station project on an ESP32 DevKit?
Make 4 connections: ESP32 DevKit D21 to 16x2 LCD SDA; ESP32 DevKit D22 to 16x2 LCD SCL; ESP32 DevKit VIN to 16x2 LCD VCC; ESP32 DevKit GND to 16x2 LCD GND.
What parts do I need for this ESP32 project?
An ESP32 DevKit and a 16x2 LCD.
Can I simulate this ESP32 project online for free?
Yes. The BitForBytes Embedded Lab runs this exact circuit and MicroPython code in your browser for free, with no install and no hardware. MicroPython runs on an emulated core, with Wi-Fi served by a virtual network.
What does the code do?
Joins Wi-Fi, fetches the live temperature from the Open-Meteo API with urequests, and shows it on an I2C LCD.