Go Down

Topic: How to get timestamp from server (Read 2195 times) previous topic - next topic

aurora123

I am using MKR1000 for my project. I am receiving the data and sending its average values every hour to the XAMPP server. I have set the time in MKR1000, but everytime when the board is reset it starts from the time entered during the programming.
 
I can't use wifi rtc to update the time as the wifi don't have internet access, it just have intranet access to  transfer the data to server.

So my problem is that, is there a way where I can update MKR 1000 time at each and every reset using the server time??

So I don't have to upload the sketch again and again to correct the time after every reset?

Or if my arduino looses power for sometime and its started again, it updates its time from the server itself.
Is it possible??

ballscrewbob

It is possible to get the time from a different server.
You have to set that server and give it a role as a time server in the first place and then point the MKR at it.

My windows server (server 2013)  performs that function here on my home network.
Each server may be different in the approach used so that aspect would probably be up to you to work out.
Once done it should advertise itself across the network.

the NTP sketch for the MKR gives a good example of how to pick up that time but you may have to set it to an IP rather than a fully qualified URL.
It may not be the answer you were looking for but its the one I am giving based on either experience, educated guess, google or the fact that you gave nothing to go with in the first place so I used my wonky crystal ball.

aurora123

After setting my PC as my NTP server, in WifiUdpNtpClient example of wifi library, I changed the server address with the IP address of my PC, It is returning the UTC time but not the time on my PC...

Please elaborate a little how can I achieve this, is there any way to change that using wifirtc example.

ballscrewbob

#3
Mar 15, 2017, 05:46 am Last Edit: Mar 15, 2017, 05:50 am by Ballscrewbob
I thought that you wanted the UTC ?

This is the example I meant to modify or study for your use.

Take note of lines 27 - 33

Once you have the packet you can if you want manipulate it further in your own sketch.



It may not be the answer you were looking for but its the one I am giving based on either experience, educated guess, google or the fact that you gave nothing to go with in the first place so I used my wonky crystal ball.

aurora123

Thanks a lot..It solved a very big problem for me...
Now I got how convert it for my time zone and its working perfect...
Thanks for the fast reply also :)
karma added

Bjain

@ballscrewbob

The link you provided in 'This is the example' text is not available anymore..
do you have any other link for it?

ballscrewbob

The link is working Bjain.

I just tried it.
It may not be the answer you were looking for but its the one I am giving based on either experience, educated guess, google or the fact that you gave nothing to go with in the first place so I used my wonky crystal ball.

Bjain

Not on my PC

I have attached the screenshot

ballscrewbob

There is a problem somewhere as I just checked that link again and its valid from here.

Also had the CREATE team check it and they confirm its OK.

Has to be an issue with your computer / ISP !




It may not be the answer you were looking for but its the one I am giving based on either experience, educated guess, google or the fact that you gave nothing to go with in the first place so I used my wonky crystal ball.

smellai

Hi @Bjain,
there's an issue on accessing this page for users not subscribed to the Create Web Editor. In the meantime, you can see the page after logout or in a private window of your browser. We'll fix the bug as soon as possible

ballscrewbob

Sketch is here anyway until the bug is fixed.


Code: [Select]

/*
  MKR1000 WiFi RTC

  This sketch asks NTP for the Linux epoch and sets the internal Arduino MKR1000's RTC accordingly.

  created 08 Jan 2016
  by Arturo Guadalupi <a.guadalupi@arduino.cc>

  http://arduino.cc/en/Tutorial/WiFiRTC
  This code is in the public domain.
*/

#include <SPI.h>
#include <WiFi101.h>
#include <WiFiUdp.h>
#include <RTCZero.h>

RTCZero rtc;

char ssid[] = "BOB01";     //  your network SSID (name)
char pass[] = "MorseCode1";   // your network password
int keyIndex = 0;            // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;

// Used for NTP
unsigned int localPort = 2390;      // local port to listen for UDP packets
IPAddress timeServer(129, 6, 15, 28); // time.nist.gov NTP server
const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
WiFiUDP Udp; // A UDP instance to let us send and receive packets over UDP

const int GMT = -5; //change this to adapt it to your time zone

void setup() {
  Serial.begin(115200);

  // check if the WiFi module works
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }

  // attempt to connect to WiFi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }

  // you're connected now, so print out the status:
  printWifiStatus();

  rtc.begin();

  unsigned long epoch;
  int numberOfTries = 0, maxTries = 6;
  do {
    epoch = readLinuxEpochUsingNTP();
    numberOfTries++;
  }
  while ((epoch == 0) || (numberOfTries > maxTries));

  if (numberOfTries > maxTries) {
    Serial.print("NTP unreachable!!");
    while (1);
  }
  else {
    Serial.print("Epoch received: ");
    Serial.println(epoch);
    rtc.setEpoch(epoch);

    Serial.println();
  }
}

void loop() {
  printDate();
  printTime();
  Serial.println();
  delay(1000);
}

unsigned long readLinuxEpochUsingNTP()
{
  Udp.begin(localPort);
  sendNTPpacket(timeServer); // send an NTP packet to a time server
  // wait to see if a reply is available
  delay(1000);

  if ( Udp.parsePacket() ) {
    Serial.println("NTP time received");
    // We've received a packet, read the data from it
    Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer

    //the timestamp starts at byte 40 of the received packet and is four bytes,
    // or two words, long. First, esxtract the two words:

    unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
    unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
    // combine the four bytes (two words) into a long integer
    // this is NTP time (seconds since Jan 1 1900):
    unsigned long secsSince1900 = highWord << 16 | lowWord;

    // now convert NTP time into everyday time:
    // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
    const unsigned long seventyYears = 2208988800UL;
    // subtract seventy years:

    Udp.stop();
    return (secsSince1900 - seventyYears);
  }

  else {
    Udp.stop();
    return 0;
  }
}

void printTime()
{
  print2digits(rtc.getHours());
  Serial.print(":");
  print2digits(rtc.getMinutes());
  Serial.print(":");
  print2digits(rtc.getSeconds());
  Serial.println();
}

void printDate()
{
  Serial.print(rtc.getDay());
  Serial.print("/");
  Serial.print(rtc.getMonth());
  Serial.print("/");
  Serial.print(rtc.getYear());

  Serial.print(" ");
}

// send an NTP request to the time server at the given address
unsigned long sendNTPpacket(IPAddress & address)
{
  // 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();
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

void print2digits(int number) {
  if (number < 10) {
    Serial.print("0");
  }
  Serial.print(number);
}


It may not be the answer you were looking for but its the one I am giving based on either experience, educated guess, google or the fact that you gave nothing to go with in the first place so I used my wonky crystal ball.

smellai

The issue with the public sketch page should be fixed now, thanks for your report

Go Up