In this lesson, we show an improved framework for creating a Client/Server connection between your Arduino and your desktop PC over WiFi. This will serve as the basis for our WiFi projects moving forward. On the arduino side, we have the following code to create the Server:
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;
intPORT=12345;
charmyPacket[255];
intdataLen;
Stringcolor;
Stringresponse;
voidsetup(){
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:
}
voidloop(){
// 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:
Python
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
importsocket
# Arduino’s IP address (from Arduino Serial Monitor)
HOST="192.168.88.77"# Use Your Arduino's IP. It will print when
Ethernet Shields are available which allow the arduino to act as a server
In the previous lessons we have seen that powerful analytic and graphic programs can be written that allow data taken from the arduino to be displayed on a PC via Python. We have shown how the arduino can be connected to a PC by either a serial cable or Xbee radios. To fully unleash the power of the arduino, it can be set up as a server, and connected to a network via Ethernet. In this lesson, we will show how to set the arduino up as a server which is controlled and queried by clients on PC’s on the same network. In order to complete this tutorial, you will need an Arduino Uno and an Ethernet shield. The Ethernet Shield is a relatively expensive component, but I suggest getting an authentic arduino made shield, as I have had poor results from the cheap knock offs. Understand that this lesson is intended for High School students to show them a simple technique for connecting the arduino to a network. It is not an exhaustive treatise on Ethernet communication. The goal is to provide a simple protocol which should allow you to get your arduino talking over Ethernet. It requires that you already know, or can figure out how to assign a mac address and IP address in your router. 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. If you are at school, the network administrator can help you get the router configured. I can not provide support in getting this to work on your network, as there are many variables. The techniques provided in this tutorial should work for most networks. This tutorial does not present the most efficient or elegant solution, but the goal is a simple protocol for people just getting started. The video below gives a step-by-step demonstration of setting up the arduino as a server, and Python on a PC as the client. It uses UDP protocol to transfer data packets.
In order to get the arduino to work over Ethernet, you must first assign an IP address to the arduino in your router. If you just plug the arduino with shield into the network, your router might assign an IP and report a mac address. If this is the case, you need to have the router assign those addresses permanently to the arduino. The bottom line is that the IP address and mac address you identify in the arduino code must match the IP address and mac address assigned in the router. I can not provide any additional help on that issue as there are so many different possible networks. You have to figure out how to do that.
Once you have the IP address and mac address sorted out, you will need to set up the arduino as a server. The video above explains how to do this, and results in this code. Note that your IP address and mac address must be set to a suitable configuration for your router and network. The numbers I use in this code would not work for your network.
This is the code developed in the video to set the arduino up:
Simple Arduino Server Code
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
#include <Ethernet.h> //Load Ethernet Library
#include <EthernetUdp.h> //Load UDP Library
#include <SPI.h> //Load the SPI Library
bytemac[]={0xDE,0xAD,0xBE,0xEF,0xFE,0xEE};//Assign a mac address
IPAddressip(10,1,15,243);//Assign my IP adress
unsignedintlocalPort=5000;//Assign a Port to talk over
Udp.print("You are Asking for Blue");//Send string back to client
Udp.endPacket();//Packet has been sent
}
}
memset(packetBuffer,0,UDP_TX_PACKET_MAX_SIZE);
}
Once you have this code in your arduino, ping the IP address of the arduino from your PC cmd line. Make sure your PC can talk to the arduino by successfully pinging it. If you get an error while you are trying to ping the arduino, you will have to stop and get figured out what is wrong. There is no reason to proceed with the lesson until you can successfully ping the arduino.
Once you can ping the arduino, you are ready to set up a client in Python. The code below is what we developed in the video. You will need to watch the video in order to understand the code. Also note, that the IP address in the code below is the IP address of the Arduino, NOT the PC. You should set this to whatever IP address is of YOUR arduino.
Python
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
fromsocketimport*
importtime
address=('10.1.15.243',5000)#Defind who you are talking to (must match arduino IP and port)
client_socket=socket(AF_INET,SOCK_DGRAM)#Set Up the Socket
client_socket.settimeout(1)#only wait 1 second for a resonse
while(1):#Main Loop
data="Blue"#Set data to Blue Command
client_socket.sendto(data,address)#send command to arduino
try:
rec_data,addr=client_socket.recvfrom(2048)#Read response from arduino
printrec_data#Print the response from Arduino
except:
pass
time.sleep(2)#delay before sending next command
data="Red"#Set data to Blue Command
client_socket.sendto(data,address)#send command to arduino
try:
rec_data,addr=client_socket.recvfrom(2048)#Read response from arduino
printrec_data#Print the response from Arduino
except:
pass
time.sleep(2)#delay before sending next command
data="Green"#Set data to Blue Command
client_socket.sendto(data,address)#send command to arduino
try:
rec_data,addr=client_socket.recvfrom(2048)#Read response from arduino
printrec_data#Print the response from Arduino
except:
pass
time.sleep(2)#delay before sending next command
Again, this is a bit of a tricky thing to set up, but if you get the PC successfully pinging the arduino, everything else should be straightforward.
Making The World a Better Place One High Tech Project at a Time. Enjoy!
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok