Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 09:55 02 May 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Help to Decipher Arduino Code

Author Message
jman

Guru

Joined: 12/06/2011
Location: New Zealand
Posts: 711
Posted: 07:16pm 14 Nov 2014
Copy link to clipboard 
Print this post

Hi All

As I have little or no clue with Arduino code so could somebody be so kind
as to decipher the below code for me Please.


{
// set all bytes in the buffer to 0
memset(packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
packetBuffer[0] = 0b11100011; // LI, Version, Mode
packetBuffer[1] = 0; // Stratum, or type of clock
packetBuffer[2] = 6; // Polling Interval
packetBuffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;

// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
Udp.beginPacket(address, 123); //NTP requests are to port 123
Udp.write(packetBuffer,NTP_PACKET_SIZE);
Udp.endPacket();
}


So the question is what ends up in packetBuffer and does it contain
a delimiter (I cant see one) also what is the length of packetBuffer

Thanks
Jman
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 07:48pm 14 Nov 2014
Copy link to clipboard 
Print this post

There should be another part of the code, probably in a 8.h file that defines the values NTP_PACKET_SIZE.
Looking at this piece of code it is probably 16 bytes long. (0-15).
NTP is not the easiest to use as you will need to calculate the delays between sending and receiving packets.
If you want to know all about the NTP protocol you can read it in http://www.ietf.org/rfc/rfc5905.txt

Microblocks. Build with logic.
 
Ray B
Senior Member

Joined: 16/02/2007
Location: Australia
Posts: 219
Posted: 08:02pm 14 Nov 2014
Copy link to clipboard 
Print this post

your obviously wanting to look at UDP on ethernet. Look at following:

This code looks to be loading an array then through the communicate using code in the UDP.H library
http://code.google.com/p/arduino/source/browse/trunk/librari es/Ethernet/Udp.h?r=1094

Look in this reference for examples of the complete code.
http://arduino.cc/en/Reference/EthernetUDPBeginPacket

At the top right of the arduino page you can search for each routine

for an overview look here: http://en.wikipedia.org/wiki/User_Datagram_Protocol

Enjoy the challenge
RayB from Perth WA
 
jman

Guru

Joined: 12/06/2011
Location: New Zealand
Posts: 711
Posted: 08:09pm 14 Nov 2014
Copy link to clipboard 
Print this post

Hi
Thanks for the reply's

I can connect to the NTP server with a ESP8266 as all of UDP stuff is taken care of
within the module.

I am having a hard time working out what to send to the NTP server to get a response
Hence my post above.

Jman
 
Ray B
Senior Member

Joined: 16/02/2007
Location: Australia
Posts: 219
Posted: 08:28pm 14 Nov 2014
Copy link to clipboard 
Print this post

Yes obviously Maximite does not have ethernet hardware including an onboard
ESP8266 whereas a plug in shield for Arduino does all of this overhead for maybe $8 off ebay.

I now see your initial code above is from the Arduino supplied sample sketch UdpNTPClient.

Have you been able to successfully run the sketch on an arduino with ethernet shield?

As stated in the commends for the sketch:

Warning: NTP Servers are subject to temporary failure or IP address change.
Plese check http://tf.nist.gov/tf-cgi/servers.cgi
if the time server used in the example didn't work.


Regards


RayB from Perth WA
 
jman

Guru

Joined: 12/06/2011
Location: New Zealand
Posts: 711
Posted: 08:33pm 14 Nov 2014
Copy link to clipboard 
Print this post

Hi Ray

I have my own NTP server so no worries there.
I can see the data getting to the NTP server but the NTP
server has no clue what to do with it as the format is incorrect.

I am using a Micromite with a Wifi module

Regards
Jman
 
mbramwel

Regular Member

Joined: 10/07/2013
Location: Canada
Posts: 42
Posted: 01:11pm 17 Nov 2014
Copy link to clipboard 
Print this post

To answer your questions...

arduino holds data in variables that contain multiple single byte storage positions.

xxx[12] would be the 13th position inside of xxx. Why 13 and not 12th? It starts counting at 0.

packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;

From your code, we see that packetBuffer has 16 bytes, starting at 0 and ends at position 15.

The above states that the byte (in hex) 0x4E is stored in the 14th slot.
49 (base10) is stored in the 15th slot.

This is also a single byte, specified in binary, stored in the 1st slot:
packetBuffer[0] = 0b11100011;

From the code, they seem to be filling a 16-byte buffer with some values and then call the UDP network routine to broadcast it over the network.

 
jman

Guru

Joined: 12/06/2011
Location: New Zealand
Posts: 711
Posted: 10:44pm 18 Nov 2014
Copy link to clipboard 
Print this post

@mbramwel

Thank you very much the explanation
The sending of the value in HEX was the trick I was missing

Regards
Jman
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024