Go Down

Topic: Sending Data to Server via UDP (Read 1 time) previous topic - next topic

mrjonny2

Jul 29, 2011, 01:45 am Last Edit: Oct 23, 2011, 07:47 pm by mrjonny2 Reason: 1
Hi

lesto

simply use

Code: [Select]
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
sei nuovo? non sai da dove partire? leggi qui: http://playground.arduino.cc/Italiano/Newbie

mrjonny2

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

lesto

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.
sei nuovo? non sai da dove partire? leggi qui: http://playground.arduino.cc/Italiano/Newbie

mrjonny2

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

lesto

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)
sei nuovo? non sai da dove partire? leggi qui: http://playground.arduino.cc/Italiano/Newbie

mrjonny2

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

lesto

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
sei nuovo? non sai da dove partire? leggi qui: http://playground.arduino.cc/Italiano/Newbie

mrjonny2

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

lesto

#9
Jul 30, 2011, 02:39 am Last Edit: Jul 30, 2011, 02:42 am by lesto Reason: 1
float are 4 byte, so:
Code: [Select]

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

that means
Code: [Select]

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

to rebuild it (i think):
Code: [Select]

float=0;
for (int i=0; i < 4; i++){
  float=float<<8;
  float = a[i];
}
sei nuovo? non sai da dove partire? leggi qui: http://playground.arduino.cc/Italiano/Newbie

dhunt

Maybe try something like:

Code: [Select]

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
}

dhunt

You would need to add the right data type for the Mac address, e.g
Code: [Select]
byte macadd[6];

Go Up