Author Options:

problem with ethernet udp Answered

im try to set up a simple client server model that allows me to send strings between Python running on PC and the arduino over Ethernet.

code of the server side software is:

#include <Ethernet.h> //Load Ethernet Library
#include <EthernetUdp.h> //Load the Udp Library
#include <SPI.h> //Load SPI Library

float tempC; //Declare variable for Temp in C
float temp;

byte mac[] ={ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; //Assign mac address
IPAddress ip(192,168,0,252); //Assign the IP Adress
unsigned int localPort = 80; // Assign a port to talk over
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //dimensian a char array to hold our data packet
String datReq; //String for our data
int packetSize; //Size of the packet
EthernetUDP Udp; // Create a UDP Object

void setup()

Serial.begin(9600); //Initialize Serial Port
Ethernet.begin( mac, ip); //Inialize the Ethernet
Udp.begin(localPort); //Initialize Udp
delay(1500); //delay


void loop()

packetSize =Udp.parsePacket(); //Reads the packet size

if(packetSize>0)  //if packetSize is >0, that means someone has sent a request


            Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE); //Read the data request
            String datReq(packetBuffer); //Convert char array packetBuffer into a string called datReq

if (datReq =="Temperature")  //Do the following if Temperature is requested

      temp = analogRead(0); //read analog input on pin A0
      tempC = temp * 5.0 * 100.0/1024.0; // ADC to celcius conversion
      Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()); //Initialize packet send
      Udp.print(tempC); //Send the temperature data
      Udp.endPacket(); //End the packet

  memset(packetBuffer, 0, UDP_TX_PACKET_MAX_SIZE); //clear out the packetBuffer array


code python of the client is:

from socket import *
import time

address = ('', 80) #define server IP and port
client_socket =socket(AF_INET, SOCK_DGRAM) #Set up the Socket
client_socket.settimeout(1) #Only wait 1 second for a response


    data = "Temperature" #Set data request to Temperature

    client_socket.sendto( data, address) #Send the data request


        rec_data, addr = client_socket.recvfrom(2048) #Read response from arduino
        temp = float(rec_data) #Convert string rec_data to float temp
        print "The Measured Temperature is ", temp, " degrees C." # Print the result


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

print ""

please any help

when i run the programe nothing happen and i don't no why ???