Tag Archives: UDP

Simple Client Server Project for the Raspberry Pi Pico W

In this video lesson we demonstrate a simple client server project on the Raspberry Pi Pico W. The Pico is configures as the server, and your desktop pc or laptop is configures to be the client. You will be running python on your PC. The project requests the user on the PC to specify a desired color. The color is then sent to the Pico, the Server. Then the Pico sets that color to ‘ON’. The pi pico is powered by a breadboard power bank, and there is no need for any connections to the pico. You can pick up the breadBoard Power Bank HERE [Affiliate Link]. Below is the schematic for the Server Side of the project:

Schematic for Circuit to Demonstrate a Client Server Example on Pi Pico

For your convenience, this is the code we developed in the video

Remember you must create a secrets.py file, and save it on the Pi Pico in the lib folder. You need to specify YOUR WiFi name and password in the file.

And finally, here is the code to run on the client side on your PC

 

Sending Data Over WiFi Between Raspberry Pi Pico W and Your PC

In this video lesson we show how we can send and receive data between the Raspberry Pi Pico W, and your PC. We will be running python on the PC, and we will exchange data using the UDP protocol. UDP is simple, and a very reliable way to send data packets back and forth. In this example, we will be demonstrating a simple Client Server relationship between the Pi Pico and PC using UDP over WiFi.

In this lesson, we will just be showing simple exchange of data, but in preparation for future lessons, go ahead and build this circuit.

Schematic for Circuit to Demonstrate a Client Server Example on Pi Pico

For your convenience, this is the “Server” software you will run on the Pi Pico.

Notice that the above code wants to load a “secrets” file that contain your WiFi name, and password

You should edit the code below with your WiFi username and your password, and then save the file in the Pi Pico lib folder, with the name secrets.py (don’t forget the .py)

Now, on the PC side, you will run your client, which will be run in Python. Here is the client software:

 

Python with Arduino LESSON 17: Sending and Receiving Data Over Ethernet

This circuit contains an Arduino Nano and Pressure Sensor Communicating over Ethernet

In LESSON 16 we showed a simple Client Server model that allows us to send strings between Python running on a PC and the arduino over Ethernet. That lesson simply passed strings back and forth to show a very basic Server on Arduino, and Python acting as the Client. In this lesson we show a more practical example, with the Arduino connected to an Adafruit BMP180 Pressure Sensor. In order to complete this lesson, you will need an Arduino, an Ethernet Shield, and the Pressure Sensor. If you do not have this particular pressure sensor, you can probably follow along in the lesson using whatever sensor you have that is of interest. The video will take you through the tutorial step-by-step, and then the code we developed is shown below.

The key issue in getting this project to work is to get your mac address and IP address from your router or network. If you are at school, simply speak to your network administrator, and he will help you get an IP address for your arduino. If you are at home, you will need to connect to your router from a browser, and configure it to assign an IP address and agree on a mac address for your arduino. Some arduino Ethernet shields have a sticker with a mac address. If your Ethernet shield has a sticker with mac address, use that one. If it does not, you will need to come up with a unique mac address. There are thousands of possible routers and networks out there, so I can not help you with that part. But if you look in the router documentation, you should be able to get the IP address and mac address worked out. The arduino itself does not have a hard wired mac address, but you set the mac address in the arduino software, and the IP address as well. The key thing is that the mac address is unique on your network, and the router and arduino agree on the IP address and mac address. If you have a clearer way to explain this, please leave a comment below.

This is the server side software to run on the arduino. Again, you should use a suitable IP address and mac address for your network. Do not think you can just copy the ones I use in the code below.

Once you have this on your arduino, and the arduino connected to the internet via an Ethernet cable, you can test by opening a command line in Windows. Then ping the address you have assigned to the Arduino. If it pings correctly and you get a reply, you are ready to develop the Python code. The Python will be the client. It will send the requests to the Arduino, and the Arduino will respond with data. Since our circuit can measure pressure or temperature, you can request either of those. When the arduino receives a request for temperature, it will go out, make the temperature measurement and then return the data to Python. Similarly, if you request Pressure the arduino will read the request, will make the Pressure measurement, and then return pressure reading to the client (Python).

 This python code will request Temperature, will then read the response, and then will print the data. It then requests Pressure, reads the response, and then prints it. If you look at our earlier lessons you can see graphical techniques to visually present the data. The hard part is getting the data passed back and forth, which we show how to do in this lesson.