This content originally appeared on DEV Community and was authored by Yevhen Kozachenko
Based on the blog post, it focuses on building a REST API on a microcontroller (ESP32) to expose smart sensor data using MicroPython. The most fitting match among the available mdoc entries is api-development, since the core topic revolves around exposing endpoints (REST API) on a microcontroller β i.e., embedded API development β which closely aligns with general API development services.
Here is the blog post with the added highlighted message and clickable link:
Introduction
Embedded programming can often feel like trying to teach a rock to speak β daunting, opaque, and overly centered on low-level, C-heavy development. But what if you could build real-world IoT projects using the Python language you already know and love?
Welcome to the world of MicroPython, an efficient and lightweight implementation of Python 3 that runs directly on microcontrollers like the ESP32. This blog post is a deep dive into building a real-world smart sensor project in under 15 minutes using MicroPython β no Arduino IDE, no C++, and no nonsense.
TL;DR: Youβll connect a DHT11 temperature & humidity sensor to an ESP32 board, flash MicroPython, write readable Python code, and expose a REST API from the microcontroller. Yes, the ESP32 will expose an HTTP server, like a freakinβ backend for your smart home!
Why MicroPython?
Before diving into code, let’s tackle the why.
Pain Points in Traditional Embedded Dev:
Low-level programming (C/C++)
Cryptic toolchains and cross-compiling
Slow development iterations (compile/upload/debug)
MicroPython Advantages:
Use familiar Python syntax and data structures
Instant REPL access via USB (REPL = Read Eval Print Loop)
Rapid prototyping and testing
What Youβll Need
- An ESP32 development board (e.g., NodeMCU ESP32)
- DHT11 or DHT22 Sensor + Jumper wires
- Micro-USB cable
- Thonny IDE (or any serial REPL-capable IDE)
- esptool.py to flash MicroPython
- MicroPython firmware for ESP32 (download here)
Hands-On: Build a Smart Weather Station
Step 1: Flash MicroPython to ESP32
pip install esptool
esptool.py --port /dev/ttyUSB0 erase_flash
esptool.py --chip esp32 --port /dev/ttyUSB0 write_flash -z 0x1000 esp32-xxxxxx.bin
Replace
/dev/ttyUSB0
with your port (e.g., COM3 on Windows), andesp32-xxxxxx.bin
with your firmware file.
Step 2: Connect DHT Sensor to ESP32
- VCC β 3.3V
- GND β GND
- DATA β D4 (GPIO 4)
Step 3: Start Writing Python (On Your Microcontroller!)
Open Thonny or connect to the device via screen/REPL.
main.py
import network
import socket
import time
from machine import Pin
import dht
sensor = dht.DHT11(Pin(4))
def connect_wifi(ssid, password):
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while not wlan.isconnected():
time.sleep(1)
print('Connection successful')
print(wlan.ifconfig())
connect_wifi('YourSSID', 'YourPassword')
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)
print('Serving on', addr)
while True:
cl, addr = s.accept()
print('Client connected from', addr)
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
response = f"""
HTTP/1.1 200 OK\r\n
{{
"temperature": {temp},
"humidity": {hum}
}}
"""
cl.send(response)
cl.close()
Note: You’ll need to upload main.py and dht.py using Thonny or ampy. The DHT module is part of MicroPython; you may not need a separate file depending on your firmware.
Step 4: Test the Sensor
Open a browser (on the same Wi-Fi) and enter the ESP32’s IP address (e.g., http://192.168.1.42). You should see a JSON response:
{
"temperature": 26,
"humidity": 58
}
Boom! Youβve now turned a $4 microcontroller into a REST API-powered smart sensor!
Optional: Post to a Remote Server (Webhooks)
Want to integrate with IFTTT or your own backend? Try this snippet:
import urequests
urequests.post('https://example.com/api/sensor', json={
"temperature": temp,
"humidity": hum
})
Real-World Use Cases
- Smart home climate monitoring
- Server room alerts via Telegram
- Agriculture (greenhouse condition monitoring)
- Wearable environmental sensors
Iteration Speed: A Game Changer
Using MicroPython, development becomes DRY (Donβt Repeat Yourself) even for firmware. Edit, save, runβno need to recompile or reflash every time.
Gotchas and Limitations
- MicroPython has RAM constraints (~512KB for ESP32)
- Not all third-party Python libs are available
- No multi-threading (but async works!)
Bonus: Serve HTML UI from ESP32
Add a web interface to make the sensor public-facing:
html = """
<!DOCTYPE html>
<html>
<head><title>Sensor</title></head>
<body>
<h1>Temperature: {temp} C</h1>
<h2>Humidity: {hum}%</h2>
</body>
</html>
"""
response = f"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n{html.format(temp=temp, hum=hum)}"
Serve it instead of the JSON if accessed from a regular browser. Voila, your own weather dashboard!
Wrapping Up
With MicroPython and the ESP32, embedded development becomes agile, fast, and even fun. Youβve moved from zero to smart sensor REST API in under 15 minutes β no arcane build system or cryptic microcontroller C funkiness in sight.
So next time someone says “embedded dev is hard”, drop this link and let them see the power of Python in the physical world.
Hit the comments below to share your setups or ask questions. If enough interest brews, we’ll do BLE communication and sensor fusion in the next post!
Resources
If you need this done β we offer API Development services.
This content originally appeared on DEV Community and was authored by Yevhen Kozachenko