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