Sending Data to Server via UDP

Hi

simply use

Udp.sendPacket( packetBuffer, packetBufferSize,  address, port);

packetBuffer is the array of byte you want sent packetBufferSize is the size of the above packet address is the ip of the destination server port is the port of the destination server

so if I want to send the humidity.GetHumidity() and humidity.GetTemperatureC() as well as sending the current MAC address and the unix time I have just received all in one packet what would the structure of that section be?

Also is it possible to have it work out the packet size itself?

Thanks for the fast reply

the packets structure is up to you. You write the program on arduino that build the packet and the program on server that read it.

if you want a fixed size structure is easy: GetHumidity and GetTemperatureC, if written as int (and NOT as string) are 2 byte every int, so 4 byte. MAC is a fixed size of 6 byte, unix tiume is a unsigned long(?), so 4 byte. add 2 byte of checksum (to be sure you received correct data), and you have 16 byte of packet.

oh ok so if i were to place GetHumidity within the packet definition would that be a string or int? same for get temp thanks

if you want it as int, you have to "break" it in byte using bitshif operation (look on the reference or playground to find something about it)

sorry guys im having some problems working out how to that. im trying to get mac, unixTime, temp, humidity in that order in.

I can't work out how to convert everything into the right formats and get the unix time out

first of all you need a byte array of 16 position so:

byte array[16];

then you fill it: int number= 16;//example array[0]=number>>8; //get the fist 8 byte of int array[1]=number; //get the last 8 byte of int (the overflow bit are truncated, same above but with overrun during bitshift)

and so on... mac is already a byte array, just copy it over the new

the problem i am having is that the get humidty and temperature are all floats

float are 4 byte, so:

a[0]= float>>(3*8);
a[1]= float>>(2*8);
a[2]= float>>(1*8);
a[3]= float>>(0*8);

that means

a[0]= float>>(24);
a[1]= float>>(16);
a[2]= float>>(8);
a[3]= float;

to rebuild it (i think):

float=0;
for (int i=0; i < 4; i++){
  float=float<<8;
  float = a[i];
}

Maybe try something like:

unsigned long sendUDPpacket(byte *address)
{
   struct {
       float temperature;
       float humidity;
   } packet = {humidity.GetTemperatureC(),humidity.GetHumidity()};
   
   Udp.sendPacket( (uint8_t *)&packet, sizeof(packet),  tempServer, 6000); // send an UDP packet to the temperature server
}

You would need to add the right data type for the Mac address, e.g

byte macadd[6];