0

I am working on an ARDUINO based wireless network. In this network, each node or device is having its own GPS module. Each GPS module has to get location data and have to send to a single base-station. Let say we have 10 nodes in our network. A specific time(let say 1 sec) is assigned to each node when it can receive location and send it to the base-station (everything to be done in that 1 sec allocated time). So, all nodes will be done sending the data to the station and now the 2nd cycle repeats and so on.

For experiment purpose, I have created two nodes and connected LEDs each one. I am getting time from GPS module using tinygps library. To verify the time synchronization, I have programmed MCU in such a way that it turns ON the LED for 50 ms after every 10 sec. Uploaded the same code on both devices. If both devices are sync then LEDs will TURN ON and OFF at the same time. But that's is not the case. LEDs of both devices are turning ON and OFF with a huge difference of time (almost 4 to 5 sec). Which shows that GPS times are not synchronized. Following is the code:

#include <TinyGPS++.h>
#include <SoftwareSerial.h>
/*
   This sample code demonstrates the normal use of a TinyGPS++ (TinyGPSPlus) object.
   It requires the use of SoftwareSerial, and assumes that you have a
   4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
*/
static const int RXPin = 4, TXPin = 3;
static const uint32_t GPSBaud = 9600;

// The TinyGPS++ object
TinyGPSPlus gps;

// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);

const int LEDPIN = 13;
void setup()
{
  Serial.begin(9600);
  ss.begin(GPSBaud);

  Serial.println(F("FullExample.ino"));
  Serial.println(F("An extensive example of many interesting TinyGPS++ features"));
  Serial.print(F("Testing TinyGPS++ library v. ")); Serial.println(TinyGPSPlus::libraryVersion());
  Serial.println(F("by Mikal Hart"));
  Serial.println();
  Serial.println(F("Sats HDOP  Latitude   Longitude   Fix  Date       Time     Date Alt    Course Speed Card  Distance Course Card  Chars Sentences Checksum"));
  Serial.println(F("           (deg)      (deg)       Age                      Age  (m)    --- from GPS ----  ---- to London  ----  RX    RX        Fail"));
  Serial.println(F("----------------------------------------------------------------------------------------------------------------------------------------"));

  pinMode(LEDPIN,OUTPUT);
  digitalWrite(LEDPIN,LOW);
}

unsigned double interval = 50;
bool pinState = LOW;
unsigned double startmillis;
void loop()
{
  static const double LONDON_LAT = 51.508131, LONDON_LON = -0.128002;

  printDateTime(gps.date, gps.time);
  double sec = gps.time.second();

    while(1){

      if(millis()-startmillis >= interval){
            // 50 milli seconds passed
            digitalWrite(LEDPIN,LOW);
            Serial.println("50 millis passed.");
            smartDelay(1000);
          }
          else{
             // Still in loop. Turn ON LED
             digitalWrite(LEDPIN,HIGH);
          }

          }


  smartDelay(0);

  if (millis() > 5000 && gps.charsProcessed() < 10)
    Serial.println(F("No GPS data received: check wiring"));
}

// This custom version of delay() ensures that the gps object
// is being "fed".
static void smartDelay(unsigned long ms)
{
  unsigned long start = millis();
  do 
  {
    while (ss.available())
      gps.encode(ss.read());
  } while (millis() - start < ms);
}

static void printFloat(float val, bool valid, int len, int prec)
{
  if (!valid)
  {
    while (len-- > 1)
      Serial.print('*');
    Serial.print(' ');
  }
  else
  {
    Serial.print(val, prec);
    int vi = abs((int)val);
    int flen = prec + (val < 0.0 ? 2 : 1); // . and -
    flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
    for (int i=flen; i<len; ++i)
      Serial.print(' ');
  }
  smartDelay(0);
}

static void printInt(unsigned long val, bool valid, int len)
{
  char sz[32] = "*****************";
  if (valid)
    sprintf(sz, "%ld", val);
  sz[len] = 0;
  for (int i=strlen(sz); i<len; ++i)
    sz[i] = ' ';
  if (len > 0) 
    sz[len-1] = ' ';
  Serial.print(sz);
  smartDelay(0);
}

static void printDateTime(TinyGPSDate &d, TinyGPSTime &t)
{
  if (!d.isValid())
  {
    Serial.print(F("********** "));
  }
  else
  {
    char sz[32];
    sprintf(sz, "%02d/%02d/%02d ", d.month(), d.day(), d.year());
    Serial.print(sz);
  }

  if (!t.isValid())
  {
    Serial.print(F("******** "));
  }
  else
  {
    char sz[32];
    sprintf(sz, "%02d:%02d:%02d ", t.hour(), t.minute(), t.second());
    Serial.print(sz);
  }

  printInt(d.age(), d.isValid(), 5);
  smartDelay(0);
}

static void printStr(const char *str, int len)
{
  int slen = strlen(str);
  for (int i=0; i<len; ++i)
    Serial.print(i<slen ? str[i] : ' ');
  smartDelay(0);
}

After a bit research, I found the following paper which is very helpful to solve my problem:

https://www.frontiersin.org/articles/10.3389/fbuil.2018.00082/full

The problem is, they have not provided the code anywhere which I can use for my problem. Please let me know what should I do to synchronize the GPS time for two devices and then, in the long run, for a network.

  • Your question is unclear, but somehow I doubt that your problem will be solved with the techniques and software described in that paper. How do you know (for example) that your two units are taking a time reading at the same time? Please try to be more specific. – Seamus May 29 at 9:53
  • I am getting the time readings from GPS continue-sly. At every 10th sec (10th means 10, then 20 then 30 sec), program turns ON the LED for 50 ms. Same program is burnt on both devices. I hope that makes it more clearer. – Ali Hassan Raza May 29 at 10:05
  • 1
    Start by removing from the code everything you do not need, so that you get a minimal example. Why the infinite while(1) loop? Why did you put unreachable code after that loop? Why don't you ever set startmillis? – Edgar Bonet May 29 at 13:21
  • I am breaking the while(1) loop after it reaches the condition. startmillis stores the millis when condition of 10 sec is met. I am breaking that loop when difference between millis from when the timer started and when 50 ms have been passed after that. – Ali Hassan Raza Jun 1 at 5:21
  • The mentioned paper solves my problem but I am not getting any code. – Ali Hassan Raza Jun 1 at 5:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.