126Views0Replies

Author Options:

problem with UDP.print Answered

Hi everyone
my problem is that i want the send 3 sensor data over ethernet UDP in one time 
and i don't know the way to do that
thank's for your time



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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEE}; //Assign a mac address
IPAddress ip(192, 168, 1, 252); //Assign my IP adress
unsigned int localPort = 4000; //Assign a Port to talk over
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
String datReq; //String for our data
int packetSize; //Size of Packet
EthernetUDP Udp; //Define UDP Object
float temp; //temp means temperature
float speed;
float charge;
float dataSend[] = {temp, speed, charge};
int temp_ADC;
int speed_ADC;
int charge_ADC;


void setup() {
 
Serial.begin(9600); //Turn on Serial Port
Ethernet.begin(mac, ip); //Initialize Ethernet
Udp.begin(localPort); //Initialize Udp
delay(1500); //delay
}

void loop() {
 
  packetSize = Udp.parsePacket(); //Read theh packetSize
 
  if(packetSize>0){ //Check to see if a request is present
 
  Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE); //Reading the data request on the Udp
  String datReq(packetBuffer); //Convert packetBuffer array to string datReq
 
  if (datReq =="ordre") { //See if Red was requested
 
    temp_ADC = analogRead(0); //read analog input on pin A0
    temp = temp_ADC * 5.0 * 100.0/1024.0; // ADC to celcius conversion 
    speed = analogRead(1); //read analog input on pin A1
    charge= analogRead(2); //read analog input on pin A2
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());  //Initialize Packet send
    Udp.print(temp); //Send string back to client
    Udp.print(speed); //Send string back to client
    Udp.print(charge); //Send string back to client
    Udp.endPacket(); //Packet has been sent
    delay(1500); //delay

  }   
  }
memset(packetBuffer, 0, UDP_TX_PACKET_MAX_SIZE);

}

Tags:arduino

Discussions