In this tutorial, you'll learn how to create a REST API using Raspberry Pi Pico W and control two LEDs β one red and one green β via Postman. For this project we are going to use Phew MicroPython library.
This project not only provides a practical introduction to hardware interaction through APIs but also demonstrates the capability of the Raspberry Pi Pico W in building IoT solutions.
π What You'll Learn:
- How to connect to Wi-Fi using Phew library.
- How to Create REST API on Raspberry Pi Pico W.
- Creating the GET API for reading the temperature sensor value on Raspberry Pi Pico W.
- Creating the POST API for controlling the LED from Postman using Raspberry Pi Pico W.
First, connect your Raspberry Pi Pico W to a breadboard. Insert the green LED into one slot and the red LED into another. Connect the longer leg (anode) of each LED to separate GPIO pins on the Pico W (we are using GPIO0 for green and GPIO1 for red). Use resistors if desired to protect the LEDs. Connect the shorter leg (cathode) of each LED to the ground.
Before deploying our code, ensure your Raspberry Pi Pico W is connected to the internet through WiFi. Use the following segment in your script to connect:
from phew import connect_to_wifi
ip = connect_to_wifi("YOUR_SSID", "YOUR_PASSWORD")
print("Connected to IP", ip)
Replace "YOUR_SSID"
and "YOUR_PASSWORD"
with your actual WiFi credentials.
Utilize the Phew! framework to write and run a web server on the Pico W:
from phew import server
import machine, json
led_green = machine.Pin(0, machine.Pin.OUT)
led_red = machine.Pin(1, machine.Pin.OUT)
Temperature Sensor Endpoint:This endpoint returns the current temperature using the Pico Wβs ADC capabilities:
@server.route("/api/temperature", methods=["GET"])
def get_temperature(request):
adc = machine.ADC(4)
conversion_factor = 3.3 / 65535
sensor_value = adc.read_u16() * conversion_factor
temperature = 27 - (sensor_value - 0.706) / 0.001721
return json.dumps({"temperature": temperature}), 200, {"Content-Type": "application/json"}
LED Control Endpoint:Create a POST endpoint to control the LEDs:
@server.route("/api/control-led", methods=["POST"])
def ledCommand(request):
led_red.value(request.data["ledRed"])
led_green.value(request.data["ledGreen"])
return json.dumps({"message": "Command sent successfully!"}), 200, {"Content-Type": "application/json"}
Step 4: Running the ServerEnsure your server is set to run with the following line:
server.run()
Step 5: Testing with PostmanOpen Postman and create a new request to control the LEDs. Set the type to POST and enter your Raspberry Pi Pico W's IP address followed by /api/control-led
. In the body of the request, specify the state of each LED in JSON format, e.g., {"ledRed": 1, "ledGreen": 0}
.
If you found this guide helpful, consider buying me a coffee to keep the projects coming!
By completing this tutorial, you've taken a significant step into the IoT world using the Raspberry Pi Pico W. You've learned how to set up hardware, write a server using the Phew! framework, and control electronic components over the internet. Experiment with different configurations and expand upon this project to explore more possibilities.
Comments