Go Down

Topic: print only new sensor data (Read 155 times) previous topic - next topic

Backy

Aug 05, 2014, 08:36 am Last Edit: Aug 05, 2014, 09:15 am by Backy Reason: 1
Hello everyone! Sorry for bothering, I need help: I have GPS  and code http://learn.parallax.com/KickStart/28500
But in loop every 5 second  same data  sent, its little bit messy ,I wanna print only new sensors data not the same with previous . If you have advice or some examples pls share with me
For example result is :
Reading GPS
35.3608, 139.2746
35.3611, 139.2745
35.3610, 139.2745
35.3610, 139.2745
35.3610, 139.2745
35.3610, 139.2745
35.3610, 139.2745
35.3610, 139.2745
35.3610, 139.2745
It will be better if it will be printed like this :
35.3608, 139.2746
35.3611, 139.2745
35.3610, 139.2745


here code:
Code: [Select]
#include <SoftwareSerial.h>
#include "./TinyGPS.h"                 // Special version for 1.0

TinyGPS gps;
SoftwareSerial nss(6, 255);            // Yellow wire to pin 6

void setup() {
 Serial.begin(115200);
 nss.begin(4800);
 Serial.println("Reading GPS");
}

void loop() {
 bool newdata = false;
 unsigned long start = millis();
 while (millis() - start < 5000) {  // Update every 5 seconds
   if (feedgps())
     newdata = true;
 }
 if (newdata) {
   gpsdump(gps);
 }
}

// Get and process GPS data
void gpsdump(TinyGPS &gps) {
 float flat, flon;
 unsigned long age;
 gps.f_get_position(&flat, &flon, &age);
 Serial.print(flat, 4); Serial.print(", ");
 Serial.println(flon, 4);
}

// Feed data as it becomes available
bool feedgps() {
 while (nss.available()) {
   if (gps.encode(nss.read()))
     return true;
 }
 return false;
}

UKHeliBob

If you only want to put the data in the database when it has changed, then each time you read it save it in a variable.  Next time you read the data compare it with the previous saved data and only put it in your database if it has changed.

Note, however, that the data from the GPS may fluctuate slightly on each read even if the unit has not moved.  You might like to consider only putting it in the database if it has changed by a significant amount.  Something like this
Code: [Select]
if abs( (newData - prevData) ) > 10)
  {
    //code to put the data in the database
  }
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Robin2

There is already a newdata variable in your code. What is that doing?

...R

Backy

thank you for reply
yes I also think that way...but the problem is I am not a good in C programming  :smiley-roll:
How can I save previous data?

Backy


There is already a newdata variable in your code. What is that doing?

...R



I think its just new sensor data , it will be the same with previous , only sending time is different

35.3610, 139.2745
35.3610, 139.2745

UKHeliBob


yes I also think that way...but the problem is I am not a good in C programming  :smiley-roll:
How can I save previous data?

Code: [Select]

float prevFlat = flat;
float prevFlon =  flon;
unsigned long prevAge = age;

float flat, flon;
unsigned long age;
gps.f_get_position(&flat, &flon, &age);
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up