In this guide, we'll create a simple server using a Raspberry Pi Pico. The server will be accessible from within a local Wi-Fi network and can be used to serve pages, read sensors, and control hardware. In our case, we'll drive an LED and read the state of a button. While quite simple, this server code is a great foundation for really useful projects like our Wi-Fi connected garage door controller based on a Pico.
To follow along, it's best if you're already familiar with connecting a Pico to Wi-Fi and we have a guide linked in the article to get you started. There, you'll also need the following hardware: a Pico with pins already soldered, an LED with current limiting resistor, a button, and a breadboard. Let's get started!
Plug your Pico into the breadboard right at the top so that the first pin is on row one. Connect the cathode of the LED to the bottom left pin on the Pico that's on row 20 on the breadboard and the short lead is going to row 21. Use a resistor to bridge row 21 all the way up to the third pin from the bottom on the Pico that would be row 18. So, we now have GP 16 driving an LED and going through a resistor to ground.
I'll plug a button into row 30 and connect row 30 to row 20 on the right hand side that's GP 16 and connect the other side of the button row 32 to ground on the Pico at row 18. And then connect to your computer with a USB cable.
It's best to check that our hardware is working before proceeding. You can find a hardware test script in the article that will flash the LED slowly when the button is not pressed and flush it quickly while the button is being held down. If you observe that behavior, you're right to proceed. If you have any problems, then let us know in the comments at the bottom of the article.
Find the example code in the article and copy that into your favourite text editor.
Near the top of the script, enter your WiFi credentials and run the script. When we run the script, we can see some information in the shell. We are waiting for a connection and then when we connect to the WiFi network, the Pico W responds with "Connected" and then tells us its IP address on the local network. Copy that IP address and navigate to it in your favourite browser (I'm going to use Firefox).
How's that? We have a simple web page with a heading and some text. So we have the Pico HTTP server, we have the text "Hello World" and then we have what looks like a status message - the LED is off and the button is not pressed. First things first, let's see if the button is working. I'll hold down the button and then refresh the page and here we can see the text has changed to "Button is pressed". If I release the button and refresh again, we see the button is not pressed. But what about the LED?
To control the LED, we're going to use a query. Append that address with a slash, question mark, LED equals on, and just like that we can see the LED has lit up. And of course, we can turn the LED off by saying LED equals off. If you experience this error at any time after running your script, say multiple times, just power cycle your Pico, reconnect with funny and rerun the script.
Let's take a look at the code. The content of the web page is defined by the HTML variable near the top of the script and you can see this is just a multi-line string in Python that contains a bunch of simple HTML code to create a web page. Here we can see a title, we can see a heading, there's a paragraph. And since this is a Python script, we can also inject variable content by using formatting. Here we've got the syntax for formatted strings, so we can change the content of what appears in the page. And that is how we can update the page.
We import the necessary modules for networking and controlling GPIO, and then set up the LED on pin 15 and the button on pin 16. We enable Wi-Fi and connect to our Wi-Fi network, and then define the HTML page. We attempt to connect to the Wi-Fi network, and after some error handling, if a connection was not successful, we open a socket. This is like the end point where communication occurs on the Pico.
We have the infinite loop for the server code, where we scan for connecting clients and copy any requests into a request variable which we can then print to the shell. There's quite a lot going on in this request, including metadata about the browser type and settings. Importantly, at the start of the request, we have our query, "led equals on". We can use the dot find method to scan the request for this text. Here, we're looking for "led equals on" and "led equals off", and what's returned by find is the position in the string where it found this text.
We then check if that's equal to eight and if so, we can turn the LED on. We also update the string that displays the state of the LED, and check if the button is pressed, updating a string for that state as well. Finally, we concatenate the LED state string and the button state string, and format that into our HTML variable. tasks but you can also use this same technique to control more complex hardware like servos and motors
Variable strings can be sent as a response to the client (our computer). This example established that it is possible to use queries to send information to our server, such as the intended state of the LED. To make this more user friendly, we can serve a web page with a button interface for controlling our hardware.
We can copy the new HTML code and replace the existing HTML variable. After rerunning the script and navigating to the IP address of the Pico, we have two buttons to control the LED. Clicking one of these buttons will automatically send the corresponding query to the Pico.
The name of each button is defined a little higher up and this does some nice styling like setting the background color and text formatting. This simple HTTP server is a practical starting point for other projects. Controlling an LED and reading a button are simple tasks, but this same technique can be used to control more complex hardware like servos and motors.
We encourage you to stay curious and keep experimenting! Examples of this hardware can be replaced by any sensor or actuator that you can interface with a Raspberry Pi. For example, we made a Wi-Fi connected garage door controller with minimal changes to what's shown in this guide.
There's additional resources at the bottom of the article, as well as a document which will help you make an asynchronous web server which handles connections in a much more robust way. If you make something cool from this guide or have any questions, please leave a comment below. We're full-time makers and happy to help. Until next time, happy making!
Makers love reviews as much as you do, please follow this link to review the products you have purchased.