TinyGPS and Spark Core

Why is it that when I connect my GPS to a serial it displays a valid GPS of my current location, but when I connect it to the spark core using the

https://api.spark.io/v1/devices/events/?access_token=...... 

it doesnt display a valid GPS location, it only displays 0.0,0.0

Have you tried the example code? If not, then do so and let me know if you get good output using that.

Yes I’ve tried that, but I have a question, I’m using a pocket wifi, is it possible that my code maybe not flashed on the core? Because I’ve been trying this for some time that the core LED says that the code has been flashed but I still not able to control the LED and the button.

So with the example code, did it work?

To figure out if your Core flashed correctly, you could do several things:

  • Create a variable with a version number, which value you can poll
  • Create a function which returns a version number
  • Create a function which publishes a version number
  • Make your Core publish a version number on start-up
  • etc.

I’d like to do the last two, since it will first call the publish automatically, but I can also manually activate it in case I missed it. You could try flashing this code:

// This #include statement was automatically added by the Spark IDE.
#include "TinyGPS/TinyGPS.h"

TinyGPS gps;
char szInfo[64];
int buttonPin = D0;    
int ledPin = D1; 
int held = 5;
int ledState = LOW;         
int buttonState;             
int lastButtonState = LOW;   
long lastDebounceTime = 0;  
long debounceDelay = 50;   

void setup() {
    Serial1.begin(9600);
    pinMode(buttonPin, INPUT);
    pinMode(ledPin, OUTPUT);
    digitalWrite(ledPin, ledState);
    
    //expose version function
    Spark.function("version", pubversion); 
    //publish version on start-up CHANGE BOTH VALUES!
    Spark.publish("version", "1.0"); 
}
 
void loop() {
    bool isValidGPS = false;
    
    for (unsigned long start = millis(); millis() - start < 1000;){
        // Check GPS data is available
        while (Serial1.available()){
            char c = Serial1.read();
            
            // parse GPS data
            if (gps.encode(c))
                isValidGPS = true;
        }
    }
    
    // If we have a valid GPS location then publish it
    if (isValidGPS){
        float lat, lon;
        unsigned long age;
    
        gps.f_get_position(&lat, &lon, &age);
        
        sprintf(szInfo, "%.6f,%.6f", (lat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : lat), (lon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : lon));
        Serial1.write(szInfo);
         Serial1.write("\n\r");
    }
    else{
        // Not a vlid GPS location, jsut pass 0.0,0.0
        // This is not correct because 0.0,0.0 is a valid GPS location, we have to pass a invalid GPS location
        // and check it at the client side
        sprintf(szInfo, "0.0,0.0");
         Serial1.write(szInfo);
         Serial1.write("\n\r");
    }
    
    int reading = digitalRead(buttonPin);
    
    if (reading != lastButtonState) {
        lastDebounceTime = millis();
    } 

    if ((millis() - lastDebounceTime) > debounceDelay) {
        if (reading != buttonState) {
            
            buttonState = reading;
            if (buttonState == HIGH) {
                int cntr=10;
                do
                {
                    digitalWrite(ledPin, HIGH);   
                    delay(200);  
                    digitalWrite(ledPin, LOW);    
                    delay(200);                
                    cntr = cntr-1;
                }       
                while (cntr!=0);
                digitalWrite(ledPin, HIGH);
                Spark.publish("gpsloc", szInfo);
                Serial1.write(szInfo);
                Serial1.write("    button is press\n\r");
                delay(15000);
                ledState = LOW;
                
            }
        }
    }
    digitalWrite(ledPin, ledState);
    lastButtonState = reading;
}

int pubversion(String command){
	
	//publish version. CHANGE BOTH VALUES!
    Spark.publish("version", "1.0"); 
    
    RGB.control(true);
    RGB.color(255,0,0);
    delay(250);
    RGB.color(0,255,0);
    delay(250);
    RGB.color(0,0,255);
    delay(250);
    RGB.control(false);
    return 0;
}

If you go to this page you can see all your Cores, their functions, and their variables. If you press the button on the bottom it will take you to the page where you can see your Server Sent Events. Once you’ve opened that, activate the pubversion function on that page. That will publish an event with the version number (currently set to 1.0), which you should be able to see between your events.

Let me know if that worked for you

I tried your example in an open space, really open space (field) but still it passes 0.0,0.0 I dont know what is happening but when I connected the GPS only through a serial it passes a valid GPS location. Do you what seems to be the problem?

The one you’ve instructed me to do, yes I’ve done it and it does displayed version 1.0.

Here’s another one with a lot more comments over Serial. Try it out, and make screenshot of the serial output. Maybe that will tell us some more:

// This #include statement was automatically added by the Spark IDE.
#include "TinyGPS/TinyGPS.h"

TinyGPS gps;
char szInfo[64];
int buttonPin = D0;    
int ledPin = D1; 
int held = 5;
int ledState = LOW;         
int buttonState;             
int lastButtonState = LOW;   
long lastDebounceTime = 0;  
long debounceDelay = 50;   

void setup() {
    Serial1.begin(9600);
    pinMode(buttonPin, INPUT);
    pinMode(ledPin, OUTPUT);
    digitalWrite(ledPin, ledState);
    
    //expose version function
    Spark.function("version", pubversion); 
    //publish version on start-up CHANGE BOTH VALUES!
    Spark.publish("version", "2.0"); 
}
 
void loop() {
    bool isValidGPS = false;
    
    for (unsigned long start = millis(); millis() - start < 1000;){
        // Check GPS data is available
        while (Serial1.available()){
            char c = Serial1.read();
            
            // parse GPS data
            if (gps.encode(c))
                isValidGPS = true;
                Serial1.write("I have a valid GPS signal!");
        }
    }
    
    // If we have a valid GPS location then publish it
    if (isValidGPS){
        float lat, lon;
        unsigned long age;
    
        gps.f_get_position(&lat, &lon, &age);
        
        sprintf(szInfo, "%.6f,%.6f", (lat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : lat), (lon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : lon));
        Serial1.write("I just received valid coordinates, and assigned them to szInfo. Here they are: \n");
        Serial1.write(szInfo);
        Serial1.write("\n\r");
    }
    else{
        // Not a vlid GPS location, jsut pass 0.0,0.0
        // This is not correct because 0.0,0.0 is a valid GPS location, we have to pass a invalid GPS location
        // and check it at the client side
        
        sprintf(szInfo, "0.0,0.0");
        Serial1.write("I just received invalid coordinates, and assigned 0.0,0.0 to szInfo, check it out: \n");
        Serial1.write(szInfo);
        Serial1.write("\n\r");
    }
    
    int reading = digitalRead(buttonPin);
    
    if (reading != lastButtonState) {
        lastDebounceTime = millis();
    } 

    if ((millis() - lastDebounceTime) > debounceDelay) {
        if (reading != buttonState) {
            
            buttonState = reading;
            if (buttonState == HIGH) {
                Serial1.write("button is pressed \n\r");
                
                int cntr=10;
                do
                {
                    digitalWrite(ledPin, HIGH);   
                    delay(200);  
                    digitalWrite(ledPin, LOW);    
                    delay(200);                
                    cntr = cntr-1;
                }       
                while (cntr!=0);
                digitalWrite(ledPin, HIGH);
                
                Serial1.write("Check out the szInfo again, which I will now publish: \n");
                Serial1.write(szInfo);
                Serial1.write("finally going to publish now \n");
                Spark.publish("gpsloc", szInfo);
                Serial1.write("published the szInfo, for good measure, here it is again: \n");
                Serial1.write(szInfo);
                Serial1.write("\n\n");
                
                
                delay(15000);
                ledState = LOW;
                
            }
        }
    }
    digitalWrite(ledPin, ledState);
    lastButtonState = reading;
}

int pubversion(String command){
	
    //publish version. CHANGE BOTH VALUES!
    Spark.publish("version", "2.0");
    delay(1000);
    Spark.publish("gpsloc", szInfo);

    Serial1.write("version 2.0");
    RGB.control(true);
    RGB.color(255,0,0);
    delay(250);
    RGB.color(0,255,0);
    delay(250);
    RGB.color(0,0,255);
    delay(250);
    RGB.control(false);
    return 0;
}
2 Likes

Out of interest, have you managed to get this working, @paolosofio01?

Hey @Moors7! Sorry I didn’t managed to reply since then, I already made it worked! It made me jumped on my seat when I pressed the button the marker actually moved to my location. Thank you very much! Right now were just testing with different location and also the internet connection gave me a headache.

2 Likes

Great to hear you’ve been able to make it work. Would you mind sharing what you did in order to achieve that? It might help other people in the future facing similar issues.
Looking forward to seeing your project when it’s done. I expect a detailled write-up of course ;)!

I have a question, is there any way I can connect my core via usb to check its serial, because right now I’m using a 3rd party hardware device to connect it via my computer and check its serial connection. I’ve tried it using putty or coolterm and it doesn’t show the core which is connected to my laptop. Thanks!

Have you seen/tried this? http://docs.spark.io/connect/#connecting-your-core-connect-over-usb
This was also very helpful:
https://community.spark.io/t/installing-the-usb-driver-on-windows-serial-debugging/882

Still no output from the TeraTerm and even the serial viewer in the SparkDev. I’m thinking when i’m flashing other programs with a serial connection, it does output a message pertaining with the code, but when I flash my own code with the GPS and button it doesn’t even connect with COM3 (my serial port).

Is your Core in listening mode (RGB LED blinking blue)?

Sorry! Managed to got it working, I just removed the “1” from Serial1… Thanks btw @Moors7

2 Likes

Hi @Moors7 just browsing over the spark community and I found this IFTTT that lets me control extra things using spark core and the internet, however can I use this or embed this in my Thesis project?

*** first is that, I’m using a GPS right? So what I think is that, when the GPS doesn’t found a fix because he/she is in a room or in a building. Can I make use of the IFTTT to still make a way so that students can still use my project when in danger and need for help.

*** Rooms in the building does have a access point or router. I think this will be a great help so that the IFTTT can work.

*** Lastly is that since the IFTTT is like a thrower and the certain apps or function is the receiver, can I make use of the website and database to make it as the receiver of whichever data is embedded on the core using the Wi-Fi?

Thanks!

Without some sort of localisation method, you can call for help, but it'll be difficult to determine where you are. If you have some sort of input method on your device (button), you could send out an event that causes IFTTT to trigger. You could use that to send a mail, text message, or something else entirely. You could implement a "last known location" to get an idea of where the person was last seen.

That doesn't really have as much to do with IFTTT as it has with the fact that you need a valid internet connection for your Spark to work.

[quote="paolosofio01, post:97, topic:4942"]
Lastly is that since the IFTTT is like a thrower and the certain apps or function is the receiver, can I make use of the website and database to make it as the receiver of whichever data is embedded on the core using the Wi-Fi?
[/quote] I'm not sure what you mean by "thrower", but there isn't really a database on IFTTT. It's mostly a middleman for internet connected services. You can use it to log data to Google spreadsheets for example.

I moved a post to an existing topic: Core keeps flashing green

1 Like

Hey @krvarma and @Moors7 I’m a student with about 3 months of CLI experience and very little experience with spark but lots of Front-End dev experience (HTML, CSS/Sass, js/jQuery, some Node.js) and lots motivation to learn. I’m working on creating a prototype of a gps location band by August and would love some basic help getting started!

I bought the Spark Core Maker Kit and the Adafruit Ultimate GPS Breakout module, and I’m trying to get them to communicate/post to the web using this example.

I’m getting logs in the web console saying that deviceID and accessToken are coming through (I know it’s a manually entered var in the script). It’s also saying “Opened! Listening for updates!” (I’m using your gps.ino code in post 87, @Moors7), and there are no errors - so the spark_location.html is working.

I’m assuming my problem is what has been flashed to the Core. I only flashed the TinyGPS.h and TinyGPS.cpp. What exactly do I need to flash to the core to make it work?? And do you know what the Adafruit GPS module does when it’s working?? I know it was mentioned that the MediaTek has a steady LED signal when it’s working but is it the same with Adafruit??

Sorry that was long, just wanted to update you with where I’m at and explain my situation best as possible.

@realjoet, all the very best for your new project, looking forward to it!.

Regarding the TinyGPS library, you can check with the sample application provided. To use the sample application, on the Web IDE using the library tool, search for TinyGPS.

  1. Click on the Library Tool and search for TinyGPS, click on the TinyGPS library found.

  1. Use the example application

  1. Flash it to Spark Core.

The sample application publishes every the current location, if any otherwise 0.0,0.0, every 15 minutes. You can use your web application to listen for this publishes.

I don’t have Adafruit GPS Breakout, but from their site it says that, “The LED blinks at about 1Hz while it’s searching for satellites and blinks once every 15 seconds when a fix is found to conserve power

Let us know your progress.

5 Likes