TinyGPS and Spark Core

#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);
}
 
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;


}

@krvarma @Dave @Moors7 I already made this code to flash with the core but still it doesnt show a marker in the map that you’ve also made. this is the code for the website I revised, does the “<<…>>” needed to be removed in the website for this to work?


<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
    var map;
	var marker;
	var loc;
    function initialize() {
        var mapOptions = {
            zoom: 18,
            minZoom: 10,
            maxZoom: 18,
            draggable: true,
            disableDefaultUI: true,
            center: new google.maps.LatLng(14.6099, 120.9893)
        };
        map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
		
		marker = new google.maps.Marker({position: new google.maps.LatLng(0.0, 0.0), map: map});
		
		marker.setMap(map);
    }

    google.maps.event.addDomListener(window, 'load', initialize);
	
	function start() {
		var deviceID = "<<.......>>";
		var accessToken = "<<.........>>";
		var eventSource = new EventSource("https://api.spark.io/v1/devices/" + deviceID + "/events/?access_token=" + accessToken);

		eventSource.addEventListener('open', function(e) {
			console.log("Opened!"); },false);

		eventSource.addEventListener('error', function(e) {
			console.log("Errored!"); },false);

		eventSource.addEventListener('gpsloc', function(e) {
			var parsedData = JSON.parse(e.data);
			var data = parsedData.data.split(",");
			
			console.log( parsedData.data);
			console.log(data[0]);
			console.log(data[1]);
			
			loc = new google.maps.LatLng(data[0], data[1]);
			
			marker.setMap(null);
			
			marker = new google.maps.Marker({position: loc, map: map});
			
			marker.setMap(null);
			map.panTo(loc);
			marker.setMap(map);
			
		}, false);
	}

	window.onload=start;

please help guys for me to work the marker out in the map. :slight_smile:


I’ve edited your post to properly format the code. Please check out this post, so you know how to do this yourself in the future. Thanks in advance! ~Jordy