In this video lesson, we show how to create a simple Client/Server connection over WiFi. We connect the Arduino Uno R4 WiFi to Python running on the PC. The arduino is the server, and the PC Python program is the client. We show how to pass data from the PC to the Arduino, and then how to pass data from the Arduino back to the PC. With this simple framework, we can control Arduino projects from our desktop PC, and we can use the PC to display data being taken from the Arduino.
In this lesson we develop code for Arduino, and the code in Python. For your convenience, we present the code below.
On the Arduino Side, this is the code to create the ‘Server’.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | #include <WiFiS3.h> #include "secrets.h" WiFiUDP udp; int PORT = 12345; char myPacket[255]; int dataLen; String color; String response; void setup() { Serial.begin(9600); Serial.print("Connecting to "); Serial.println(SSID); WiFi.begin(SSID, PASS); while (WiFi.status() != WL_CONNECTED) { delay(100); Serial.print("."); } Serial.println("\nConnected to WiFi"); Serial.println(WiFi.localIP()); udp.begin(PORT); Serial.print("UDP Server started on port "); Serial.print(PORT); // put your setup code here, to run once: } void loop() { // put your main code here, to run repeatedly: if (udp.parsePacket()) { dataLen = udp.available(); Serial.println(dataLen); udp.read(myPacket,255); Serial.println(myPacket); myPacket[dataLen]=0; Serial.println(myPacket); color=String(myPacket); color.trim(); Serial.println("Received color: "+color); response ="Here is your "+color+" Marble"; udp.beginPacket(udp.remoteIP(), udp.remotePort()); udp.print(response); udp.endPacket(); Serial.println("SENT: "+response); } } |
You also need to add a tab to your program using the ‘Add Tab’ feature of the IDE. The program in the tab should be named secrets.h
Your secrets.h file should look like this:
1 2 | #define SSID "Your WiFi Name" #define PASS "your Wifi Password" |
You can run the program above, and it should connect to your WiFi, and should print out the Arduino’s IP address. Make note of the IP address as it will be needed in the Client program below. On the PC side, this is the python code to create the client:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import socket # Arduino’s IP address (from Arduino Serial Monitor) HOST = "192.168.88.77" # Use Your Arduino's IP. It will print when #You Run the Arduino Server Program PORT = 12345 # Must match Arduino’s UDP port # Create a UDP socket mySocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) mySocket.settimeout(5.0) # 5 seconds timeout for responses print("UDP Client started. Enter a color (or 'quit' to exit).") while True: # Prompt for a color #color = input("Enter a color: ").strip() # Remove whitespace color = input("Enter a color: ") if color.lower() == 'quit': # Exit condition break # Send the color to the server sendColor=color.encode() mySocket.sendto(sendColor, (HOST, PORT)) print('Sent '+color+' to HOST',HOST,PORT) # Try to get a response, skip on timeout try: response, server_address = mySocket.recvfrom(1024) print("Server response:", response.decode()) except socket.timeout: print("No response received from server within 5 seconds") # Close the socket mySocket.close() print("Socket closed") |