Python with Arduino LESSON 16: Simple Client Server Configuration over Ethernet

Arduino and Ethernet Shield
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:

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.

 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.

10 thoughts on “Python with Arduino LESSON 16: Simple Client Server Configuration over Ethernet”

  1. Awesome! As usual. After watching I can now easily hook up the Raspberry Pi and Arduino over Ethernet. Opens up a world of possibilities. Thanks!!! Great Job! Can’t wait for your next Lesson!

    1. Yes, that is actually one of the major benefits of this . . . interfacing the Pi and Arduino. Python and Ethernet are the best way to do this.

  2. It’s very good project. so, If you don’t mind, I would like to introduce on WIZnet museum for everyone.
    WIZnet produce the W5100 on Ethernet shield. Hopefully, you will allow this.
    and one more thing to say, I have almost made a new W5500 Ethernet shield.
    if you have interest, then I could send free sample when it releases. just send your address to my email suhwan@wiznet.co.kr. I think, it is good for your future experiment :). Thanks.~

  3. Hello,

    thanks for your great lessons! I’m interested to develop a similar project with an Arduino Uno board using an ESP8266 module with UDP communication, instead of an ethernet shield W5100. I programmed the ESP8266 module with AT commands thru the arduino serial port (D0,D1) and it work fine, it connect automatically to the network when powered up and it looks very stable. But I’m still confused how sketch use on Arduino side?! Almost all tutorials explain how to build a webserver on Arduino or on the ESP8266 moduel directly, but that’s not what I’m looking for. I was wondering if one day you can make a lesson explaining UDP communication between python and arduino+ESP8266 wifi. Thanks in advance

  4. Python 3:

    from socket import *
    import time

    address = ( ‘10.0.0.177’, 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 = b”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
    print (rec_data.decode(‘unicode_escape’)) #Print the response from Arduino
    except:
    pass

    time.sleep(2) #delay before sending next command

    data = b”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
    print (rec_data.decode(‘unicode_escape’)) #Print the response from Arduino
    except:
    pass

    time.sleep(2) #delay before sending next command

    data = b”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
    print (rec_data.decode(‘unicode_escape’)) #Print the response from Arduino
    except:
    pass
    time.sleep(2) #delay before sending next command

  5. Hi I have copied everything exactly and am using an arduino uno with a seed ethernet shield v2.0 and i got the shield online and whenever a udp packet is sent to the shield the light blinks but i get no response from the arduino.

  6. Thank you for your ‘most excellent’ tutorials. So clearly crafted. Two thumbs up! You are an amazing teacher. Please consider more tutorials – they are highly prized.

  7. Wow, this is Excellent! had to change the Ethernet library on mine, as I have the shield with the Wiznet W5500 chip, but otherwise it was perfect 🙂 Thank you so much for such a Fab tutorial!

Leave a Reply

Your email address will not be published. Required fields are marked *