Guidance needed.... Ethernet/Python/PC etc...

Hi All,

I wonder if anybody could help me to "implement" a Python script and Ethernet to the same sketch, so I could communicate with the Arduino board in Python via Ethernet....

Have been looking around on many, many links, and guides, but nothing explains how to do several things at the same time.... Just how to make the Arduino Uno/Mega to do simple things, one at the time...

The Python script are already uploaded to the Uno and working via USB, just need the Ethernet working....

Cheers ! ;)

Nobody.....??? =(

Can you post the code you have so far?
What is teh goal you want ot accomplish, thatis not clear?
What devices are involved, sensors, servos, etc?

describe (at least a minimal version of) the system you wanna build.

There are zillion examples of how an Arduino works with ethernet (OK at least 10) especially (minimal) webserver

There are also many samples how to get a page from a webserver in python

The Python script are already uploaded to the Uno

I have no idea what you mean by that as the Arduino does not speak Python, but please prove me wrong :slight_smile:

Hi There !

Thanks for your reply !

What I want is controlling servo`s and motors via Uno or Mega.
I have downloaded and installed everything in this : : setup, and it is working on USB.
What I now want to, is to do the exact same thing, via ethernet…
I need to “forward” my COM port to ethernet, AND make my Uno/Mega to talk via ethernet…and ethernet board…

When this is up and running, there will be MANY things to do with this !

When this is working I will try (my self) to add gyro, temp, light, etc…etc… sensors, but as mentioned, my big problem now is to get the ethernet implemented in the sketch…

What I have tried to implement is the UDP send/recieve string, as I believe that this would be the best suitable…please correct me if I am wrong :astonished: (need to send and receive info from the board)

Here is the code that is on the Uno today (I realize now, that the Python is on my laptop… :blush: :



// Import the Arduino Servo library
#include <Servo.h>

// Create a Servo object for each servo
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
// Servo servo5;
// etc…

// Common servo setup values
int minPulse = 600; // minimum servo position, us (microseconds)
int maxPulse = 2400; // maximum servo position, us

// User input for servo and position
int userInput[3]; // raw input from serial buffer, 3 bytes
int startbyte; // start byte, begin reading input
int servo; // which servo to pulse?
int pos; // servo angle 0-180
int i; // iterator

// LED on Pin 13 for digital on/off demo
int ledPin = 13;
int pinState = LOW;

void setup()
// Attach each Servo object to a digital pin
servo1.attach(2, minPulse, maxPulse);
servo2.attach(3, minPulse, maxPulse);
servo3.attach(4, minPulse, maxPulse);
servo4.attach(5, minPulse, maxPulse);
// servo5.attach(YOUR_PIN, minPulse, maxPulse);
// etc…

// LED on Pin 13 for digital on/off demo
pinMode(ledPin, OUTPUT);

// Open the serial connection, 9600 baud

void loop()
// Wait for serial input (min 3 bytes in buffer)
if (Serial.available() > 2) {
// Read the first byte
startbyte =;
// If it’s really the startbyte (255) …
if (startbyte == 255) {
// … then get the next two bytes
for (i=0;i<2;i++) {
userInput =;

  • }*
  • // First byte = servo to move?*
  • servo = userInput[0];*
  • // Second byte = which position?*
  • pos = userInput[1];*
  • // Packet error checking and recovery*
  • if (pos == 255) { servo = 255; }*
  • // Assign new position to appropriate servo*
  • switch (servo) {*
  • case 1:*
  • servo1.write(pos); // move servo1 to ‘pos’*
  • break;*
  • case 2:*
  • servo2.write(pos);*
  • break;*
  • case 3:*
  • servo3.write(pos);*
  • break;*
  • case 4:*
  • servo4.write(pos);*
  • break;*
  • // TO ADD SERVOS:*
  • // case 5:*
  • // servo5.write(pos);*
  • // break;*
  • // etc…*
  • // LED on Pin 13 for digital on/off demo*
  • case 99:*
  • if (pos == 180) {*
  • if (pinState == LOW) { pinState = HIGH; }*
  • else { pinState = LOW; }*
  • }*
  • if (pos == 0) {*
  • pinState = LOW;*
  • }*
  • digitalWrite(ledPin, pinState);*
  • break;*
  • }*
  • }*
  • }*
    I am really looking forward to your reply ! ! $) :wink: :sweat_smile:

On both sites (python and Arduino) you must encapsulate/wrap the communication in a few functions like:

  • status = sendCommand(string)
  • string = receiveCommand();
  • sendStatus(int status); // OK, ERROR

Then you can replace the innerworking of these functions by socket-over the internet.

For this I advice to first look at the ethernet tutorials of the Arduino - - and try to access this from python.

If that works well and you understand the code it is time to integrate.

Hi !

Well, thats where everythings stops..... I am a total newbie with programming, so I really struggle with this....

Could you please help me to do this, so I can see how it should be? :~

What would be really handy is a "drag & drop" function, where you could fex drag a ethernet sketch and a fex blinky sketch in to the same environment, and it would work.... :roll_eyes:

What would be really handy is a "drag & drop" function, where you could fex drag a ethernet sketch and a fex blinky sketch in to the same environment, and it would work

Think you found your challenge for this year :)

hehehehe anyway Rob, do you think you could help me with this ethernet problem I have?

I would really appreciate it if you would :% ;)

if I understand, here is the topology:

You want people to be able to access a dynamic web page hosted by your PC, and have that page be updated, and possibly send data to, an arduino?

If so, start with a hello world webpage, create a server socket in python and wrap some text (i.e. time of day) in html and send it out to any clients that connect to your machine/port.

Then figure out how to get data from your arduino from within python and incorporate it into your html.

Java is really good at this sort of stuff fyi, it is called a proxy basically, there are examples in the sandbox, though you can translate to prettyprint in the same step and go straight to html, and the arduino IDE folks already figured out all the serial comm stuff for you.

If you are already familiar with tomcat, you can create a servlet/jsp and talk to the arduino from there.

Njet.... Joystick connected to PC running the Python scripts mentioned earlier, connected to ethernet, talking to a Uno/Mega board with connected servo`s, motors, sensors etc....controlled by the USB joystick connected to the PC...

So: Joystick PC Ethernet Arduino

In that order....

I will also need the "feedback" from the Uno/Mega sensors etc....

fyi, reading two potentiometers (aka a joystick) with an atmega is too easy. You can get a mega but I havent heard why, bling?

But, if you can do all those things individually within python, then string them together. If you can't, then start there. Divide and conquer.

ok, so you want an ethernet shield? is the arduino ever going to be far from a PC? Can the PC proxy ethernet for the arduino if not?

it wouldnt matter if the arduino was 5 cm or 5 km away over ethernet....thats some of the point....and if you should connect it directly to the PC (without a router) you will be able to do so with a strait cable....or crossed if you have a relatively new PC/MAC etc...

So, again, my problem, I need the multiservo sketch that is talking to the Python script, to be able to talk over ethernet.... And, a way to send my COM port to ethernet/IP....

I am a totally newbie with programming, and dont have a clue..... :cold_sweat:

Please help..... ! :)

And, a way to send my COM port to ethernet/IP....

That is way out of scope for this forum.

I am a totally newbie with programming, and dont have a clue..... smiley-roll-sweat

Please help..... ! smiley

My advise is to really go through the tutorial section of the ethernet client server etc. That is the only way to learn it properly.

Maybe it will be easier to do it the other way around. Start with a working webserver arduino and extend it with servo's You could do this latter part :)

Hi Rob !

I have tried this, but, I cant get it working WITH the servo sketch mentioned earlier.... Webserver with temp/LED etc is working...

When I tried to "implement" ethernet, I got errors on "void setup" and "void loop" etc.... after trying to put the related lines in the correct place (inside void setup/void loop) I just got a bunch of other errors....

Thats why I really would like to see a working sketch, and compare with what I have tried, and failed..........

maybe tommorow as it is becoming quite late here and I have to raise at 6 :(

Have a good night Rob ! ;)

You need to back up a little. You have something talking to a PC, that might not have ethernet, but you expect there will be an ethernet cable readily available for the arduino that you will connect to it via usb? So that the pc can read a joystick and send it to the arduino? When simply using two analog pins you can read the joystick directly with an arduino anyway? And why is python a requirement?

Can you describe the use case here? If you could read the joystick directly from the arduino, do you need usb or ethernet or pc or python?

Its actually pretty easy to send and receive packets in python.

For example, this script sends a UDP packet to with the string "Hello" as the payload data:

#!/usr/bin/env python
import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto("Hello", ("", 8042))

This script is a tiny server that receives UDP packets on port 8042, prints the packet data, converts the data to upper case, and sends the data back to the client:

#!/usr/bin/env python
import SocketServer

class MyUDPHandler(SocketServer.BaseRequestHandler):

    def handle(self):
        data = self.request[0].strip()
        socket = self.request[1]
        print "%s: " % self.client_address[0],
        print data

        # send same data back to client in upper case
        socket.sendto(data.upper(), self.client_address)

if __name__ == "__main__":
    # set up server on local IP address
    server = SocketServer.UDPServer(("", 8042), MyUDPHandler)