Pages: [1]   Go Down
Author Topic: Sending Data to Server via UDP  (Read 1340 times)
0 Members and 1 Guest are viewing this topic.
London, UK
Offline Offline
Full Member
***
Karma: 4
Posts: 189
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi
« Last Edit: October 23, 2011, 12:47:35 pm by mrjonny2 » Logged

0
Offline Offline
Shannon Member
****
Karma: 131
Posts: 10468
:(){:|:&};:
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

simply use

Code:
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
Logged

sei nuovo? non sai da dove partire? leggi qui: http://playground.arduino.cc/Italiano/Newbie

London, UK
Offline Offline
Full Member
***
Karma: 4
Posts: 189
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Shannon Member
****
Karma: 131
Posts: 10468
:(){:|:&};:
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

sei nuovo? non sai da dove partire? leggi qui: http://playground.arduino.cc/Italiano/Newbie

London, UK
Offline Offline
Full Member
***
Karma: 4
Posts: 189
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Shannon Member
****
Karma: 131
Posts: 10468
:(){:|:&};:
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged

sei nuovo? non sai da dove partire? leggi qui: http://playground.arduino.cc/Italiano/Newbie

London, UK
Offline Offline
Full Member
***
Karma: 4
Posts: 189
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Shannon Member
****
Karma: 131
Posts: 10468
:(){:|:&};:
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

sei nuovo? non sai da dove partire? leggi qui: http://playground.arduino.cc/Italiano/Newbie

London, UK
Offline Offline
Full Member
***
Karma: 4
Posts: 189
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Shannon Member
****
Karma: 131
Posts: 10468
:(){:|:&};:
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

float are 4 byte, so:
Code:
a[0]= float>>(3*8);
a[1]= float>>(2*8);
a[2]= float>>(1*8);
a[3]= float>>(0*8);
that means
Code:
a[0]= float>>(24);
a[1]= float>>(16);
a[2]= float>>(8);
a[3]= float;
to rebuild it (i think):
Code:
float=0;
for (int i=0; i < 4; i++){
  float=float<<8;
  float = a[i];
}
« Last Edit: July 29, 2011, 07:42:31 pm by lesto » Logged

sei nuovo? non sai da dove partire? leggi qui: http://playground.arduino.cc/Italiano/Newbie

Wellington, New Zealand
Offline Offline
Sr. Member
****
Karma: 1
Posts: 404
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe try something like:

Code:
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
}
Logged


Wellington, New Zealand
Offline Offline
Sr. Member
****
Karma: 1
Posts: 404
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Pages: [1]   Go Up
Jump to: