In this lesson we are working on a client server connection. The Arduino is the server, and a python program on a desktop PC is the client. The objective is to control the color of an RGB LED from the client. The client will send data to the server as comma delimited string, like 255,0,255 for RGB. The challenge on the server side is to parse this data so we get integer values of:
R=255
G=0
B=255
In this video lesson we start by simply controlling two LED, a red and green one, from the python client. We then show how we can control an RGB LED by sending the data string from the client, and parsing it on the server side. For your convenience, we include the code here:
Server Side code for the Arduino:
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
#include <WiFiS3.h> #include "secrets.h" WiFiUDP udp; int PORT = 12345; char myPacket[255]; int dataLen; String color; String response; int redPin = 8; int greenPin = 9; int R; int G; int B; int firstComma; int secondComma; void setup() { Serial.begin(9600); pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); 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); firstComma=color.indexOf(',',0); secondComma=color.indexOf(',',firstComma+1); R=color.substring(0,firstComma).toInt(); G=color.substring(firstComma+1,secondComma).toInt(); B=color.substring(secondComma+1).toInt(); Serial.println(R); Serial.println(G); Serial.println(B); udp.beginPacket(udp.remoteIP(), udp.remotePort()); udp.print(response); udp.endPacket(); Serial.println("SENT: " + response); } } |
The code above needs a ‘secrets.h’ file which includes your WiFi name and password. This is created in the IDE by choosing ‘Add Tab’. Call the new tab ‘secrets.h’, and edit the code below to include your WiFi name and passwords inside the quotes:
1 2 |
#define SSID "Your WiFi Name" #define PASS "Your WiFi Password" |
Now, on the client side, this is our standard Python 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") |
With this code for creating a client and server, and then parsing comma delimited text, you can easily add the code needed to control the RGB LED color to the arduino program.