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

Arduino 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.

21 thoughts on “Python with Arduino LESSON 17: Sending and Receiving Data Over Ethernet”

  1. This is great! I am new to the Arduino and find it to be a lot of fun. my mom is a weather geek.. and I want to build her a weather station with the arduino. My question is can I get it to check the bmp180 like once an hour and export that data to like microsoft excel? ( mom has a spreed sheet there keeping track of weather for the last five year )

    1. Yes, you could save it as CSV . . . comma seperated variables. Have Python write to a file one line at a time with data like time, temp, pressure, such as

      1064,77.45,95,435

      If each line of the file looks like that, and save it as a .txt file, then excel can read it no problem.

  2. I just want to tell you that your tutorials are very addictive.
    I started watching the firstone at 5pm, and finished the 17th at 12am. I don’t see the 18th, 19th and 20th. Are they future production?
    You have made a great job, and it is very well explained. Congratulations.

    Greetings from Mexico

  3. I would like a lesson for send data to pwm from pc to arduino, and read analog input. but with serial port.

  4. Love your tutorials… waiting for you to do a tutorial on how to interface arduino with android phone using MIT app inventor or Eclips .

  5. thank’s for everything
    what happen if i read directlly pin on arduino board like (LM35 on pin A0) and i want to sended like you did in tuto

  6. HI tOM
    You are a veryyyyyyyy gud teacher its very helpful for me al the tutorial i have worked more with this thank you..

    I wand to work want python gui pleade post a tuttorial vith gui

    1. Yes, easiest way is with the ethernet shields and then communicate by UDP. Just use the UDP commands back and forth between two arduinos with ethernet shields that can see each other on network.

  7. Hello sir,
    Your tutorials are awesome. I have learned many things from your tutorials.
    I request you to do some tutorial on making of drone.. I want to learn how to make it..

  8. Thanks! I clicked lesson 1 to the last and learned python+arduino skill much more. I guess you were good teacher, wasn’t you?
    I hope another subjects, another teaching video. If you do that you will guide lots of engineer to the fast and exact way to reach the goal of them.
    Thanks again!
    from Korea, Seoul.

  9. Sjalom Paul,
    You wrote about SPI and uses a SPI library in your code, but the Adafruit_BMP085 should be controled with I2C.
    Am I missing something?

    Be blessed and be a blessing.
    Greetings,
    Michel

  10. Hi Paul,

    Useful in the Arduino IDE is setting the preferences for showing the line numbers on the left side, when you are programming.
    After compiling the line number location will be given after an error. So don’t waste any time in locating yourself where you misspelled the words. Just a useful tip. I am using Arduino version 1.6.7 . 🙂

    Thanks for learning new useful stuff again.

    Sjaak

  11. thanks for the tutorial. Does the port binding need to happen before the arduino sends the data? I have a system where the arduino constantly streams data. I was wondering how that would work?

  12. Dear Sir,
    I have Arduino device which reads my class room temperature and humidity. I want to download the data in my home PC in excel.
    How is it possible? Can u pls help me for the code?

Leave a Reply

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