From c21215dc0928648a72a7eadcf81ebb3116b54921 Mon Sep 17 00:00:00 2001 From: roblad Date: Sat, 22 Oct 2016 21:16:13 +0200 Subject: [PATCH 01/26] clean and update --- arduino/gps-tracker2/gps-tracker2.ino | 104 +++++++------------------- 1 file changed, 25 insertions(+), 79 deletions(-) diff --git a/arduino/gps-tracker2/gps-tracker2.ino b/arduino/gps-tracker2/gps-tracker2.ino index 0eaf2dc..d3474d3 100644 --- a/arduino/gps-tracker2/gps-tracker2.ino +++ b/arduino/gps-tracker2/gps-tracker2.ino @@ -25,14 +25,12 @@ #include "config.h" #include -//#include "Time.h" #include #include #include "Adafruit_FONA.h" #include "Adafruit_MQTT.h" #include "Adafruit_MQTT_FONA.h" #include -//#include //#include /*************************** FONA Pins ***********************************/ @@ -42,34 +40,22 @@ #define GSM_TX 3 #define GSM_RST 4 // pin number 16 in sim808 throug diode to ground https://cdn-shop.adafruit.com/datasheets/SIM808_Hardware+Design_V1.00.pdf max 4.3 V SoftwareSerial fonaSS = SoftwareSerial(GSM_TX, GSM_RX); -//SoftwareSerial *FONASERIAL = &fonaSS; -Adafruit_FONA fona = Adafruit_FONA(GSM_RST); -//Adafruit_FONA *FONA = &fona; - -//int ctime[6]; -//char Lat[10]; -//char Lon[10]; -//char Alt[10]; -//uint16_t Course; -//uint16_t Speed; -//char Date[14]; +Adafruit_FONA fona = Adafruit_FONA(GSM_RST); -//byte offset = 0; - -StaticJsonBuffer<120> jsonBuffer; +StaticJsonBuffer<122> jsonBuffer; // global - a tak nie powinno się robić JsonObject& root = jsonBuffer.createObject(); -//JsonObject *ROOT = &root; -char geodata[120]; + +char geodata[122]; /************ Global State (you don't need to change this!) ******************/ // Setup the FONA MQTT class by passing in the FONA class and MQTT server and login details. Adafruit_MQTT_FONA mqtt(&fona, AIO_SERVER, AIO_SERVERPORT); -//Adafruit_MQTT_FONA *MQTT(&mqtt); + // You don't need to change anything below this line! #define halt(s) { Serial.println(F( s )); while(1); } @@ -83,7 +69,7 @@ boolean GPS(); // Setup a feed called 'photocell' for publishing. Adafruit_MQTT_Publish feed = Adafruit_MQTT_Publish(&mqtt, TOPIC); -//Adafruit_MQTT_Publish *FEED = &feed; + /*************************** Sketch Code ************************************/ // How many transmission failures in a row we're willing to be ok with before reset @@ -95,7 +81,7 @@ void setup() { while (!Serial); // Watchdog is optional! - //Watchdog.enable(10000); + Watchdog.enable(8000); Serial.begin(115200); @@ -111,6 +97,7 @@ void setup() { } while (! GPS()) { Serial.println(F("Retrying FONA GPS")); + Watchdog.reset(); } Serial.println(F("Connected to Cellular!")); @@ -135,10 +122,6 @@ void loop() { Watchdog.reset(); // Now we can publish stuff! - - //Serial.print(F("\nSending ")); - //Serial.print(F("...")); - if (! feed.publish(geodata)) { Serial.println(F("Sent Failed")); @@ -164,84 +147,47 @@ void MQTT_connect() { return; } - //Serial.print(F("Connecting to MQTT... ")); + Serial.print(F("Connecting to MQTT... ")); while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected - //Serial.println(mqtt.connectErrorString(ret)); Serial.println(F("Retrying MQTT connection in 10 seconds...")); mqtt.disconnect(); - delay(10000); // wait 8 seconds + Watchdog.disable(); + delay(10000); // wait 10 seconds + Watchdog.enable(8000); + Watchdog.reset(); } Serial.println(F("MQTT Connected!")); - //if (! Serial.println(geodata)) { - // Serial.println(F("Failed")); - // } else { - // Serial.println(F("data OK!")); - - // } - + Watchdog.reset(); } + void prepareData() { int ctime[6]; float gps_data[5]; void GPS_Data(float *fdata, int *idata); GPS_Data(gps_data, ctime); - //dtostrf(gps_data[0], 6, 6, &Lat[0]); - //dtostrf(gps_data[1], 6, 6, &Lon[0]); - //dtostrf(gps_data[2], 6, 2, Speed); - //Speed = gps_data[2]; - //dtostrf(gps_data[3], 6, 2, Alt); - //dtostrf(gps_data[4], 6, 2, Course); - //Course = gps_data[4]; - // prepare data to unixtime setTime(ctime[0], ctime[1], ctime[2], ctime[3], ctime[4], ctime[5]); - // timezone - //adjustTime(offset * SECS_PER_HOUR); - // unixtime to string - //snprintf(&Date[0], 14, "%lu", now()); time_t uxdate = now(); - //Serial.println(test); - - //(int(gps_data[2]) >= 2 ? 0 : int(gps_data[2])); - - //int _speed = int(gps_data[2]); - //if (_speed < SPEED_TR) _speed = 0; int vbat; //if (! fona.getBattPercent(&vbat)) vbat = 0; if (! fona.getBattVoltage(&vbat)) vbat = 0; - //fona.getBattVoltage - //} else { - // Serial.print(F("VPct = ")); Serial.print(vbat); Serial.println(F("%")); //} // {"tst":1476474031,"acc":1000,"_type":"location","alt":140,"lon":-90.48259734672334,"vac":10,"p":100.160530090332,"lat":38.75410327670748,"batt":100,"tid":"JT"} // Owntracks API: // http://owntracks.org/booklet/tech/json/ - //root["_type"] = "location"; - root.set("_type","location"); - //root["acc"] = 10; - root.set("acc",3); - //root["alt"] = atof(Alt); - // root.set("alt",atof(Alt)); - //root["batt"] = 100; - root.set("batt",vbat); - //root["cog"] = atof(Course); - root.set("cog",int(gps_data[4])); - //root["lat"] = Lat; - root.set("lat",(gps_data[0]),6); - //root["lon"] = Lon; - root.set("lon",(gps_data[1]),6); - //root["tid"] = TID; - root.set("tid",TID); - //root["vel"] = _speed; - root.set("vel",(int(gps_data[2]) <= 2 ? 0 : int(gps_data[2]))); - //root["tst"] = Date; - //ROOT->set("tst",atol(Date)); - root.set("tst",uxdate); - root.printTo(geodata, 120); - + root.set("_type","location"); + root.set("acc",3); + root.set("batt",vbat); + root.set("cog",int(gps_data[4])); + root.set("lat",(gps_data[0]),7); + root.set("lon",(gps_data[1]),7); + root.set("tid",TID); + root.set("vel",(int(gps_data[2]) <= 2 ? 0 : int(gps_data[2]))); + root.set("tst",uxdate); + root.printTo(geodata, 122); } From 65ee5e9ce14e541ba2fe6625b303289902565e80 Mon Sep 17 00:00:00 2001 From: roblad Date: Sat, 22 Oct 2016 21:17:34 +0200 Subject: [PATCH 02/26] clean and update --- arduino/gps-tracker2/fonahelper.cpp | 47 ++++++++++++++--------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/arduino/gps-tracker2/fonahelper.cpp b/arduino/gps-tracker2/fonahelper.cpp index e4619b3..339e7f9 100644 --- a/arduino/gps-tracker2/fonahelper.cpp +++ b/arduino/gps-tracker2/fonahelper.cpp @@ -22,39 +22,47 @@ byte gps_enabled = 0; boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *username, const __FlashStringHelper *password) { Watchdog.reset(); - - //Serial.println(F("Initializing FONA....(May take 3 seconds)")); - - //fonaSS.begin(*fonaSerial); // if you're using software serial fonaSS.begin(9600); if (! fona.begin(fonaSS)) { // can also try fona.begin(Serial1) Serial.println(F("Couldn't find FONA")); return false; - } - fonaSS.println("AT+CMEE=2"); - //Serial.println(F("FONA is OK")); - Watchdog.reset(); - // Serial.println(F("Checking for network...")); + } else { + Serial.println(F("Check FONA err:\"AT+CMEE=2\" - expect OK")); + fonaSS.flush(); + //if (sendATcommand("AT+CMEE=2","OK", 200)) { + fonaSS.println(F("AT+CMEE=2")); + delay(20); + + + while(fonaSS.available() >0) { + Serial.print(char(fonaSS.read())); + delay(1); + } + fonaSS.flush(); + Serial.println(); + + } + + Watchdog.reset(); while (fona.getNetworkStatus() != 1) { - delay(500); + Serial.println(F("Waiting for network...")); + delay(1000); + Watchdog.reset(); } Watchdog.reset(); delay(5000); // wait a few seconds to stabilize connection Watchdog.reset(); - fona.setGPRSNetworkSettings(apn, username, password); - //Serial.println(F("Disabling GPRS")); fona.enableGPRS(false); - Watchdog.reset(); delay(5000); // wait a few seconds to stabilize connection Watchdog.reset(); Serial.println(F("Enabling GPRS")); if (!fona.enableGPRS(true)) { - //Serial.println(F("Failed to turn GPRS on")); + Serial.println(F("Failed to turn GPRS on")); return false; } @@ -66,7 +74,7 @@ boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *u boolean GPS() { if (gps_enabled == 0 ) { - //Serial.println(F("Enabling GPS")); + Serial.println(F("Enabling GPS")); if (!fona.enableGPS(true)) { Serial.println(F("Failed to turn GPS on")); return false; @@ -79,9 +87,6 @@ boolean GPS() { Watchdog.reset(); delay(5000); // wait a few seconds to stabilize connection Watchdog.reset(); - - Watchdog.reset(); - //Serial.println(F("Looking for satelites")); byte stat; stat = fona.GPSstatus(); if (stat < 0 or stat == 0 or stat == 1) { @@ -91,22 +96,16 @@ boolean GPS() { if (stat >= 2) { Serial.println(F("GPS Fixed")); } - Watchdog.reset(); - return true; } - void GPS_Data(float *fdata, int *idata) { - - float latitude, longitude, speed_kph, heading, altitude; boolean gps_success = fona.getGPS(&latitude, &longitude, &altitude, &speed_kph, &heading, &utime[0]); sscanf(utime,"%04d%02d%02d%02d%02d%02d",&YY,&MM,&DD,&hh,&mm,&ss); - // put data into array fdata[0] = latitude; fdata[1] = longitude; From 1c0b1c60e97a4b8752661c3c48155ae55f081d5f Mon Sep 17 00:00:00 2001 From: roblad Date: Sun, 23 Oct 2016 23:12:14 +0200 Subject: [PATCH 03/26] Movment les then 10m blocker - not sending --- arduino/gps-tracker2/gps-tracker2.ino | 96 ++++++++++++++++++++------- 1 file changed, 71 insertions(+), 25 deletions(-) diff --git a/arduino/gps-tracker2/gps-tracker2.ino b/arduino/gps-tracker2/gps-tracker2.ino index d3474d3..13cc63e 100644 --- a/arduino/gps-tracker2/gps-tracker2.ino +++ b/arduino/gps-tracker2/gps-tracker2.ino @@ -31,7 +31,7 @@ #include "Adafruit_MQTT.h" #include "Adafruit_MQTT_FONA.h" #include -//#include +#include /*************************** FONA Pins ***********************************/ @@ -48,6 +48,10 @@ StaticJsonBuffer<122> jsonBuffer; // global - a tak nie powinno się robić JsonObject& root = jsonBuffer.createObject(); + // current state LDR +float lastmovement_W = 0; +float lastmovement_L = 0; +uint8_t readytosend; char geodata[122]; @@ -78,14 +82,14 @@ byte txfailures = 0; void setup() { - while (!Serial); + //while (!fonaSS); // Watchdog is optional! - Watchdog.enable(8000); + //Watchdog.enable(8000); Serial.begin(115200); - Serial.println(F("GPS Tracker")); + Serial.println(F("GPS Tracker starting ...")); Watchdog.reset(); delay(5000); // wait a few seconds to stabilize connection @@ -93,6 +97,7 @@ void setup() { // Initialise the FONA module while (! FONAconnect(F(GSM_APN), F(GSM_USERNAME), F(GSM_PASSWORD))) { + Serial.println(F("Retrying FONA GSM")); } while (! GPS()) { @@ -109,32 +114,41 @@ void setup() { void loop() { -// Serial.println(freeMemory()); + //Serial.println(freeMemory()); prepareData(); // Make sure to reset watchdog every loop iteration! - Watchdog.reset(); + //Watchdog.reset(); // Ensure the connection to the MQTT server is alive (this will make the first // connection and automatically reconnect when disconnected). See the MQTT_connect // function definition further below. - MQTT_connect(); + +if (readytosend != 0) { + + MQTT_connect(); - Watchdog.reset(); - // Now we can publish stuff! + Watchdog.reset(); + // Now we can publish stuff! - if (! feed.publish(geodata)) { + if (! feed.publish(geodata)) { - Serial.println(F("Sent Failed")); - txfailures++; - } else { - Serial.println(F("Sent OK!")); - txfailures = 0; - } - + Serial.println(F("Sent Failed")); + txfailures++; + } else { + Serial.println(F("Sent OK!")); + //Serial.println(freeMemory()); + txfailures = 0; + readytosend = 0; + } +} else { + + Serial.println(F("No movement of 10 meters waiting ...")); + readytosend = 0; +} Watchdog.reset(); delay(DELAY*1000); // wait a few seconds to stabilize connection Watchdog.reset(); - + } // Function to connect and reconnect as necessary to the MQTT server. @@ -150,11 +164,11 @@ void MQTT_connect() { Serial.print(F("Connecting to MQTT... ")); while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected - Serial.println(F("Retrying MQTT connection in 10 seconds...")); + Serial.println(F("Retrying MQTT connection in 5 seconds...")); mqtt.disconnect(); - Watchdog.disable(); - delay(10000); // wait 10 seconds - Watchdog.enable(8000); + //Watchdog.disable(); + delay(5000); // wait 5 seconds + //Watchdog.enable(8000); Watchdog.reset(); } Serial.println(F("MQTT Connected!")); @@ -167,7 +181,7 @@ void prepareData() { int ctime[6]; float gps_data[5]; void GPS_Data(float *fdata, int *idata); - GPS_Data(gps_data, ctime); + GPS_Data(&gps_data[0], &ctime[0]); setTime(ctime[0], ctime[1], ctime[2], ctime[3], ctime[4], ctime[5]); time_t uxdate = now(); int vbat; @@ -188,6 +202,38 @@ void prepareData() { root.set("vel",(int(gps_data[2]) <= 2 ? 0 : int(gps_data[2]))); root.set("tst",uxdate); root.printTo(geodata, 122); - - + //sprintf((gps_data[0]),%f, movement_W); + if ((((gps_data[0]) - lastmovement_W ) > float(0.0010000)) or (((gps_data[1]) - lastmovement_L ) > float(0.0010000))) { + + readytosend = 1; + /* + Serial.println(); + Serial.print(F("Latitude OLD: ")); + Serial.print(lastmovement_W,7); + Serial.println(); + Serial.print(F("Longitude OLD: ")); + Serial.print(lastmovement_L,7); + Serial.println(); + */ + lastmovement_W = (gps_data[0]); + lastmovement_L = (gps_data[1]); + /*Serial.println(); + Serial.println(F("Latitude: ")); + Serial.print(lastmovement_W,7); + Serial.println(); + Serial.println(F("Longitude: ")); + Serial.print(lastmovement_L,7); + Serial.println(); + */ + } else { + readytosend = 0; + } + Serial.println(); + Serial.print(F("Latitude OLD: ")); + Serial.print(lastmovement_W,7); + Serial.println(); + Serial.print(F("Longitude OLD: ")); + Serial.print(lastmovement_L,7); + Serial.println(); + } From 3c41294ee22044d3c7195fc79be5192fff45176e Mon Sep 17 00:00:00 2001 From: roblad Date: Sun, 23 Oct 2016 23:15:05 +0200 Subject: [PATCH 04/26] Movment 10 m and change vars of gps_data to pointers, remove watchdog.reset --- arduino/gps-tracker2/fonahelper.cpp | 80 +++++++++++++++-------------- 1 file changed, 41 insertions(+), 39 deletions(-) diff --git a/arduino/gps-tracker2/fonahelper.cpp b/arduino/gps-tracker2/fonahelper.cpp index 339e7f9..6aeca3c 100644 --- a/arduino/gps-tracker2/fonahelper.cpp +++ b/arduino/gps-tracker2/fonahelper.cpp @@ -8,27 +8,21 @@ extern Adafruit_FONA fona; extern SoftwareSerial fonaSS; -char utime[14]; -int YY; -int MM; -int DD; -int hh; -int mm; -int ss; + // flags byte gps_enabled = 0; boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *username, const __FlashStringHelper *password) { - Watchdog.reset(); + //Watchdog.reset(); fonaSS.begin(9600); if (! fona.begin(fonaSS)) { // can also try fona.begin(Serial1) Serial.println(F("Couldn't find FONA")); return false; } else { Serial.println(F("Check FONA err:\"AT+CMEE=2\" - expect OK")); - fonaSS.flush(); + //fonaSS.flush(); //if (sendATcommand("AT+CMEE=2","OK", 200)) { fonaSS.println(F("AT+CMEE=2")); delay(20); @@ -38,27 +32,46 @@ boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *u Serial.print(char(fonaSS.read())); delay(1); } - fonaSS.flush(); + //fonaSS.flush(); Serial.println(); } - Watchdog.reset(); - while (fona.getNetworkStatus() != 1) { + //Watchdog.reset(); + + + while (! fona.getNetworkStatus()) { Serial.println(F("Waiting for network...")); - delay(1000); - Watchdog.reset(); + + + uint8_t n = fona.getRSSI(); + int8_t r; + + Serial.print(F("RSSI = ")); Serial.print(n); Serial.print(": "); + if (n == 0) r = -115; + if (n == 1) r = -111; + if (n == 31) r = -52; + if ((n >= 2) && (n <= 30)) { + r = map(n, 2, 30, -110, -54); + } + Serial.print(r); Serial.println(F(" dBm")); + + delay(2000); + + + //Watchdog.reset(); } - Watchdog.reset(); + //Watchdog.reset(); delay(5000); // wait a few seconds to stabilize connection - Watchdog.reset(); + //Watchdog.reset(); fona.setGPRSNetworkSettings(apn, username, password); //Serial.println(F("Disabling GPRS")); + //delay(1000); fona.enableGPRS(false); - Watchdog.reset(); + //Watchdog.reset(); delay(5000); // wait a few seconds to stabilize connection - Watchdog.reset(); + //Watchdog.reset(); Serial.println(F("Enabling GPRS")); if (!fona.enableGPRS(true)) { @@ -66,7 +79,7 @@ boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *u return false; } - Watchdog.reset(); + //Watchdog.reset(); return true; } @@ -84,40 +97,29 @@ boolean GPS() { } } - Watchdog.reset(); + //Watchdog.reset(); delay(5000); // wait a few seconds to stabilize connection - Watchdog.reset(); + //Watchdog.reset(); byte stat; stat = fona.GPSstatus(); + delay(100); if (stat < 0 or stat == 0 or stat == 1) { + Serial.println(F("Not fixed!")); return false; } if (stat >= 2) { Serial.println(F("GPS Fixed")); } - Watchdog.reset(); + //Watchdog.reset(); return true; } void GPS_Data(float *fdata, int *idata) { - - float latitude, longitude, speed_kph, heading, altitude; - boolean gps_success = fona.getGPS(&latitude, &longitude, &altitude, &speed_kph, &heading, &utime[0]); - sscanf(utime,"%04d%02d%02d%02d%02d%02d",&YY,&MM,&DD,&hh,&mm,&ss); - - // put data into array - fdata[0] = latitude; - fdata[1] = longitude; - fdata[2] = speed_kph; - fdata[3] = altitude; - fdata[4] = heading; - idata[0] = hh; - idata[1] = mm; - idata[2] = ss; - idata[3] = DD; - idata[4] = MM; - idata[5] = YY; + char utime[14]; + //float latitude, longitude, speed_kph, heading, altitude; + boolean gps_success = fona.getGPS(&fdata[0], &fdata[1], &fdata[3], &fdata[2], &fdata[4], &utime[0]); + sscanf(utime,"%04d%02d%02d%02d%02d%02d",&idata[5],&idata[4],&idata[3],&idata[0],&idata[1],&idata[2]); if (&gps_success) { //delete utime; From 205c4f50a14ce6169649670b45dd0c4bc0bdedcc Mon Sep 17 00:00:00 2001 From: roblad Date: Sun, 23 Oct 2016 23:16:52 +0200 Subject: [PATCH 05/26] Remove watchdog reset --- arduino/gps-tracker2/gps-tracker2.ino | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/arduino/gps-tracker2/gps-tracker2.ino b/arduino/gps-tracker2/gps-tracker2.ino index 13cc63e..161aca3 100644 --- a/arduino/gps-tracker2/gps-tracker2.ino +++ b/arduino/gps-tracker2/gps-tracker2.ino @@ -91,9 +91,9 @@ void setup() { Serial.println(F("GPS Tracker starting ...")); - Watchdog.reset(); + //Watchdog.reset(); delay(5000); // wait a few seconds to stabilize connection - Watchdog.reset(); + //Watchdog.reset(); // Initialise the FONA module while (! FONAconnect(F(GSM_APN), F(GSM_USERNAME), F(GSM_PASSWORD))) { @@ -102,13 +102,13 @@ void setup() { } while (! GPS()) { Serial.println(F("Retrying FONA GPS")); - Watchdog.reset(); + //Watchdog.reset(); } Serial.println(F("Connected to Cellular!")); - Watchdog.reset(); + //Watchdog.reset(); delay(5000); // wait a few seconds to stabilize connection - Watchdog.reset(); + //Watchdog.reset(); } @@ -117,7 +117,7 @@ void loop() { //Serial.println(freeMemory()); prepareData(); // Make sure to reset watchdog every loop iteration! - //Watchdog.reset(); + ////Watchdog.reset(); // Ensure the connection to the MQTT server is alive (this will make the first // connection and automatically reconnect when disconnected). See the MQTT_connect @@ -127,7 +127,7 @@ if (readytosend != 0) { MQTT_connect(); - Watchdog.reset(); + //Watchdog.reset(); // Now we can publish stuff! if (! feed.publish(geodata)) { @@ -145,9 +145,9 @@ if (readytosend != 0) { Serial.println(F("No movement of 10 meters waiting ...")); readytosend = 0; } - Watchdog.reset(); + //Watchdog.reset(); delay(DELAY*1000); // wait a few seconds to stabilize connection - Watchdog.reset(); + //Watchdog.reset(); } @@ -169,10 +169,10 @@ void MQTT_connect() { //Watchdog.disable(); delay(5000); // wait 5 seconds //Watchdog.enable(8000); - Watchdog.reset(); + //Watchdog.reset(); } Serial.println(F("MQTT Connected!")); - Watchdog.reset(); + //Watchdog.reset(); } From ebc414e5244fe097565ca4c1b9459d4d38c42203 Mon Sep 17 00:00:00 2001 From: roblad Date: Thu, 27 Oct 2016 20:58:19 +0200 Subject: [PATCH 06/26] change mqtt server and description - private server --- arduino/gps-tracker2/config.h | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/arduino/gps-tracker2/config.h b/arduino/gps-tracker2/config.h index 7eb5915..72a0305 100644 --- a/arduino/gps-tracker2/config.h +++ b/arduino/gps-tracker2/config.h @@ -1,14 +1,24 @@ +/*define mqtt brocker + * https://customer.cloudmqtt.com/login + * register your server (for free 10 connections allowed) + * set direct access to server - config as standard owntrack config + * create user i.e. piesek + * create topic for user RW access owntracks/# + * + * + */ + #define GSM_APN "internet" #define GSM_USERNAME "" #define GSM_PASSWORD "" -#define AIO_SERVER "broker.mqttdashboard.com" -#define AIO_SERVERPORT 1883 -// #define AIO_USERNAME "" -// #define AIO_PASSWORD "" +#define AIO_SERVER "xxxxxx.cloudmqtt.com" +#define AIO_SERVERPORT 11971 +#define AIO_USERNAME "xxxxxxx" +#define AIO_PASSWORD "xxxxxxx" #define TOPIC "owntracks/piesek/yasmina" #define TID "yb" // "tracker-ID which is used by the auto-faces feature to display, say, initials of a user." #define SPEED_TR 2 // bellow that number = 0 -#define DELAY 10 // seconds +#define DELAY 5 // seconds From c31ec6cc485f5a85e6d3897c54c730c0145e650f Mon Sep 17 00:00:00 2001 From: roblad Date: Thu, 27 Oct 2016 20:59:42 +0200 Subject: [PATCH 07/26] added AT command function and tuning connection --- arduino/gps-tracker2/fonahelper.cpp | 101 +++++++++++++++++++++------- 1 file changed, 78 insertions(+), 23 deletions(-) diff --git a/arduino/gps-tracker2/fonahelper.cpp b/arduino/gps-tracker2/fonahelper.cpp index 6aeca3c..99817d0 100644 --- a/arduino/gps-tracker2/fonahelper.cpp +++ b/arduino/gps-tracker2/fonahelper.cpp @@ -14,6 +14,51 @@ extern SoftwareSerial fonaSS; byte gps_enabled = 0; +//send AT command and wait for responce and print + +uint8_t sendATcommand(char* ATcommand,unsigned int *timeout){ + + uint8_t x=0, answer=0; + char response[50]; + unsigned long previous; + + while( fonaSS.available() > 0) fonaSS.read(); // Clean the input buffer + fonaSS.flush(); + if (ATcommand[0] != '\0') + { + fonaSS.println(ATcommand); // Send the AT command + delay(20); + } + + + x = 0; + previous = millis(); + Serial.println(); + Serial.print(F("Execute AT command: ")); + Serial.println(ATcommand); + + Serial.print(F("Result: ")); + // this loop waits for the answer + do{ + if(fonaSS.available() > 0){ // if there are data in the UART input buffer, reads it and checks for the asnwer + response[x] = fonaSS.read(); + + Serial.print(response[x]); + //delay(20); + x++; + answer = 1; + } else { + answer = 0; + } + }while(((millis() - previous) < &timeout)); // Waits for the asnwer with time out + //delete buffer - freeup memory + delete[] response,previous,timeout; + return answer; + +} + + + boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *username, const __FlashStringHelper *password) { //Watchdog.reset(); fonaSS.begin(9600); @@ -21,29 +66,34 @@ boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *u Serial.println(F("Couldn't find FONA")); return false; } else { - Serial.println(F("Check FONA err:\"AT+CMEE=2\" - expect OK")); - //fonaSS.flush(); - //if (sendATcommand("AT+CMEE=2","OK", 200)) { - fonaSS.println(F("AT+CMEE=2")); - delay(20); - - - while(fonaSS.available() >0) { - Serial.print(char(fonaSS.read())); - delay(1); - } - //fonaSS.flush(); - Serial.println(); - - } - - //Watchdog.reset(); + //Serial.print(F("Check FONA err: - expect OK")); + //set additionalcommands to modem + // sendATcommand("AT+CMEE=2", 200); +// Serial.print(F("Delete all SMS - expect OK")); +// sendATcommand("AT+CMGD=1,4",200); + //sendATcommand("AT&W",200); + //Serial.print(F("Registration IP - expect OK")); + //sendATcommand("AT+SAPBR=2,1",200); + //reset GPS + //led blink 0 1 2 + //AT+CSGS=2 + //delete smses from sim + //"AT+CMGD=1" + //check errors + //"AT+CMEE=2" + //disable battery charge - power consumption + //"AT+CGPSRST=0" + //"AT&W" + // reset to default + //"ATZ0"; + //"AT&W"; + } while (! fona.getNetworkStatus()) { Serial.println(F("Waiting for network...")); - - + //dey(500); + /* uint8_t n = fona.getRSSI(); int8_t r; @@ -57,7 +107,7 @@ boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *u Serial.print(r); Serial.println(F(" dBm")); delay(2000); - + */ //Watchdog.reset(); } @@ -67,10 +117,9 @@ boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *u //Watchdog.reset(); fona.setGPRSNetworkSettings(apn, username, password); //Serial.println(F("Disabling GPRS")); - //delay(1000); fona.enableGPRS(false); //Watchdog.reset(); - delay(5000); // wait a few seconds to stabilize connection + //delay(5000); // wait a few seconds to stabilize connection //Watchdog.reset(); Serial.println(F("Enabling GPRS")); @@ -90,6 +139,7 @@ boolean GPS() { Serial.println(F("Enabling GPS")); if (!fona.enableGPS(true)) { Serial.println(F("Failed to turn GPS on")); + return false; } else { @@ -102,11 +152,13 @@ boolean GPS() { //Watchdog.reset(); byte stat; stat = fona.GPSstatus(); - delay(100); + if (stat < 0 or stat == 0 or stat == 1) { Serial.println(F("Not fixed!")); + return false; + delay(1000); } if (stat >= 2) { Serial.println(F("GPS Fixed")); @@ -127,3 +179,6 @@ void GPS_Data(float *fdata, int *idata) { } } + + + From 2efd46d3aa4f10ce8b3b9e4ae05d9348e4600895 Mon Sep 17 00:00:00 2001 From: roblad Date: Thu, 27 Oct 2016 21:01:31 +0200 Subject: [PATCH 08/26] added function for movement and heartbit and tunning connection, hange to private mqtt --- arduino/gps-tracker2/gps-tracker2.ino | 40 +++++++++++++++++++-------- 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/arduino/gps-tracker2/gps-tracker2.ino b/arduino/gps-tracker2/gps-tracker2.ino index 161aca3..03570c3 100644 --- a/arduino/gps-tracker2/gps-tracker2.ino +++ b/arduino/gps-tracker2/gps-tracker2.ino @@ -42,7 +42,7 @@ SoftwareSerial fonaSS = SoftwareSerial(GSM_TX, GSM_RX); Adafruit_FONA fona = Adafruit_FONA(GSM_RST); - +//delay(200); StaticJsonBuffer<122> jsonBuffer; // global - a tak nie powinno się robić @@ -52,13 +52,15 @@ JsonObject& root = jsonBuffer.createObject(); float lastmovement_W = 0; float lastmovement_L = 0; uint8_t readytosend; +uint8_t reporter; char geodata[122]; /************ Global State (you don't need to change this!) ******************/ - +#define MQTT_CONN_KEEPALIVE 900 +//#define MQTT_DEBUG // Setup the FONA MQTT class by passing in the FONA class and MQTT server and login details. -Adafruit_MQTT_FONA mqtt(&fona, AIO_SERVER, AIO_SERVERPORT); +Adafruit_MQTT_FONA mqtt(&fona, AIO_SERVER, AIO_SERVERPORT,AIO_USERNAME,AIO_PASSWORD); // You don't need to change anything below this line! #define halt(s) { Serial.println(F( s )); while(1); } @@ -73,6 +75,7 @@ boolean GPS(); // Setup a feed called 'photocell' for publishing. Adafruit_MQTT_Publish feed = Adafruit_MQTT_Publish(&mqtt, TOPIC); +// Adjust as necessary, in seconds. Default to 5 minutes. /*************************** Sketch Code ************************************/ @@ -139,6 +142,7 @@ if (readytosend != 0) { //Serial.println(freeMemory()); txfailures = 0; readytosend = 0; + //delete[] geodata; } } else { @@ -154,6 +158,7 @@ if (readytosend != 0) { // Function to connect and reconnect as necessary to the MQTT server. // Should be called in the loop function and it will take care if connecting. void MQTT_connect() { + byte ret; // Stop if already connected. @@ -163,13 +168,13 @@ void MQTT_connect() { Serial.print(F("Connecting to MQTT... ")); - while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected - Serial.println(F("Retrying MQTT connection in 5 seconds...")); + while ((ret = mqtt.connect()) != 0 ) { // connect will return 0 for connected + delay(500); + //Serial.println(mqtt.connectErrorString(ret)); mqtt.disconnect(); - //Watchdog.disable(); - delay(5000); // wait 5 seconds - //Watchdog.enable(8000); - //Watchdog.reset(); + Serial.println(F("Retrying MQTT connection in 1 seconds...")); + //mqtt.connect(); + delay(1000); // wait 5 seconds } Serial.println(F("MQTT Connected!")); //Watchdog.reset(); @@ -203,8 +208,8 @@ void prepareData() { root.set("tst",uxdate); root.printTo(geodata, 122); //sprintf((gps_data[0]),%f, movement_W); - if ((((gps_data[0]) - lastmovement_W ) > float(0.0010000)) or (((gps_data[1]) - lastmovement_L ) > float(0.0010000))) { - + if ((((gps_data[0]) - lastmovement_W ) > 0.0010000) or (((gps_data[1]) - lastmovement_L ) > 0.0010000) or reporter == 100) { + reporter=0; readytosend = 1; /* Serial.println(); @@ -227,6 +232,11 @@ void prepareData() { */ } else { readytosend = 0; + + // reporter every 15 min - position reported even not moved + reporter++; + + } Serial.println(); Serial.print(F("Latitude OLD: ")); @@ -235,5 +245,11 @@ void prepareData() { Serial.print(F("Longitude OLD: ")); Serial.print(lastmovement_L,7); Serial.println(); - + Serial.print(F("Times checking, when 100 reporting heartbit now: ")); + Serial.println(reporter); + Serial.println(); + + } + //void sendATcommand(char *ATcommand, unsigned int *timeout); + From 7af5d26685dabde9bdade6d27541e8f9c3aa5885 Mon Sep 17 00:00:00 2001 From: roblad Date: Sat, 29 Oct 2016 19:44:18 +0200 Subject: [PATCH 09/26] moved some parameters to eeprom keep alive mqtt server open --- arduino/gps-tracker2/gps-tracker2.ino | 59 +++++++++++++++++++-------- 1 file changed, 42 insertions(+), 17 deletions(-) diff --git a/arduino/gps-tracker2/gps-tracker2.ino b/arduino/gps-tracker2/gps-tracker2.ino index 03570c3..b7a17bb 100644 --- a/arduino/gps-tracker2/gps-tracker2.ino +++ b/arduino/gps-tracker2/gps-tracker2.ino @@ -52,16 +52,21 @@ JsonObject& root = jsonBuffer.createObject(); float lastmovement_W = 0; float lastmovement_L = 0; uint8_t readytosend; -uint8_t reporter; +uint16_t reporter; char geodata[122]; /************ Global State (you don't need to change this!) ******************/ -#define MQTT_CONN_KEEPALIVE 900 +const char MQTT_SERVER[] PROGMEM = AIO_SERVER; +//const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME; +//const char MQTT_PASSWORD[] PROGMEM = AIO_PASSWORD; +const char MQTT_TOPIC[] PROGMEM = TOPIC; + + //#define MQTT_DEBUG // Setup the FONA MQTT class by passing in the FONA class and MQTT server and login details. -Adafruit_MQTT_FONA mqtt(&fona, AIO_SERVER, AIO_SERVERPORT,AIO_USERNAME,AIO_PASSWORD); - +//Adafruit_MQTT_FONA mqtt(&fona, AIO_SERVER, AIO_SERVERPORT,AIO_USERNAME,AIO_PASSWORD); +Adafruit_MQTT_FONA mqtt(&fona, AIO_SERVER, AIO_SERVERPORT); // You don't need to change anything below this line! #define halt(s) { Serial.println(F( s )); while(1); } @@ -119,6 +124,9 @@ void setup() { void loop() { //Serial.println(freeMemory()); prepareData(); + if ( mqtt.ping()) { + Serial.println(F("MQTT server avaliable")); + } // Make sure to reset watchdog every loop iteration! ////Watchdog.reset(); @@ -142,7 +150,7 @@ if (readytosend != 0) { //Serial.println(freeMemory()); txfailures = 0; readytosend = 0; - //delete[] geodata; + delete[] geodata; } } else { @@ -163,19 +171,36 @@ void MQTT_connect() { // Stop if already connected. if (mqtt.connected()) { + if (! mqtt.ping()) { + mqtt.disconnect(); + } else { return; + } } - Serial.print(F("Connecting to MQTT... ")); + Serial.println(F("Connecting to MQTT... ")); + mqtt.connect(); + delay(1000); + if (! mqtt.ping()) { + mqtt.disconnect(); + Serial.println(F("No MQTT server avaliable")); + }else { + mqtt.disconnect(); + Serial.println(F("MQTT server avaliable")); + } + + while ((ret = mqtt.connect()) != 0 ) { // connect will return 0 for connected + //delay(1000); + //Serial.println(mqtt.connectErrorString(ret)); + + mqtt.disconnect(); + + Serial.println(F("Retrying MQTT connection in 2 seconds...")); + //mqtt.connect(); + delay(2000); // wait 5 seconds - while ((ret = mqtt.connect()) != 0 ) { // connect will return 0 for connected - delay(500); - //Serial.println(mqtt.connectErrorString(ret)); - mqtt.disconnect(); - Serial.println(F("Retrying MQTT connection in 1 seconds...")); - //mqtt.connect(); - delay(1000); // wait 5 seconds - } + } + Serial.println(F("MQTT Connected!")); //Watchdog.reset(); } @@ -204,11 +229,11 @@ void prepareData() { root.set("lat",(gps_data[0]),7); root.set("lon",(gps_data[1]),7); root.set("tid",TID); - root.set("vel",(int(gps_data[2]) <= 2 ? 0 : int(gps_data[2]))); + root.set("vel",(int(gps_data[2]) <= SPEED_TR ? 0 : int(gps_data[2]))); root.set("tst",uxdate); root.printTo(geodata, 122); //sprintf((gps_data[0]),%f, movement_W); - if ((((gps_data[0]) - lastmovement_W ) > 0.0010000) or (((gps_data[1]) - lastmovement_L ) > 0.0010000) or reporter == 100) { + if ((((gps_data[0]) - lastmovement_W ) > 0.0010000) or (((gps_data[1]) - lastmovement_L ) > 0.0010000) or reporter == HEARTBIT) { reporter=0; readytosend = 1; /* @@ -245,7 +270,7 @@ void prepareData() { Serial.print(F("Longitude OLD: ")); Serial.print(lastmovement_L,7); Serial.println(); - Serial.print(F("Times checking, when 100 reporting heartbit now: ")); + Serial.print(F("Times checking, HEARTBIT reporting heartbit now: ")); Serial.println(reporter); Serial.println(); From c0134f2bd14801cc08d3494559b1ae40bfd88878 Mon Sep 17 00:00:00 2001 From: roblad Date: Sat, 29 Oct 2016 19:45:45 +0200 Subject: [PATCH 10/26] moved some setting to eeprom keepalive conection to mqtt --- arduino/gps-tracker2/fonahelper.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/arduino/gps-tracker2/fonahelper.cpp b/arduino/gps-tracker2/fonahelper.cpp index 99817d0..e0024a9 100644 --- a/arduino/gps-tracker2/fonahelper.cpp +++ b/arduino/gps-tracker2/fonahelper.cpp @@ -1,10 +1,11 @@ #include #include #include "Adafruit_FONA.h" - +#include "config.h" #define halt(s) { Serial.println(F( s )); while(1); } +const char MQTT_GSM_APN[] PROGMEM = GSM_APN; extern Adafruit_FONA fona; extern SoftwareSerial fonaSS; @@ -19,7 +20,7 @@ byte gps_enabled = 0; uint8_t sendATcommand(char* ATcommand,unsigned int *timeout){ uint8_t x=0, answer=0; - char response[50]; + char response[30]; unsigned long previous; while( fonaSS.available() > 0) fonaSS.read(); // Clean the input buffer @@ -70,7 +71,7 @@ boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *u //set additionalcommands to modem // sendATcommand("AT+CMEE=2", 200); // Serial.print(F("Delete all SMS - expect OK")); -// sendATcommand("AT+CMGD=1,4",200); +// sendATcommand("AT+CMGD=1,4",200); //sendATcommand("AT&W",200); //Serial.print(F("Registration IP - expect OK")); //sendATcommand("AT+SAPBR=2,1",200); @@ -179,6 +180,3 @@ void GPS_Data(float *fdata, int *idata) { } } - - - From 445e7d2d56f9ebbe1a09d53ce2ddb10327c46158 Mon Sep 17 00:00:00 2001 From: roblad Date: Sun, 30 Oct 2016 14:29:56 +0100 Subject: [PATCH 11/26] small changes with heartbit - better keep alive --- arduino/gps-tracker2/gps-tracker2.ino | 37 ++++++++++++--------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/arduino/gps-tracker2/gps-tracker2.ino b/arduino/gps-tracker2/gps-tracker2.ino index b7a17bb..61c69dc 100644 --- a/arduino/gps-tracker2/gps-tracker2.ino +++ b/arduino/gps-tracker2/gps-tracker2.ino @@ -124,9 +124,7 @@ void setup() { void loop() { //Serial.println(freeMemory()); prepareData(); - if ( mqtt.ping()) { - Serial.println(F("MQTT server avaliable")); - } + // Make sure to reset watchdog every loop iteration! ////Watchdog.reset(); @@ -141,6 +139,8 @@ if (readytosend != 0) { //Watchdog.reset(); // Now we can publish stuff! + + if (! feed.publish(geodata)) { Serial.println(F("Sent Failed")); @@ -150,12 +150,20 @@ if (readytosend != 0) { //Serial.println(freeMemory()); txfailures = 0; readytosend = 0; - delete[] geodata; - } + //delete[] geodata; + + } } else { - + Serial.println(F("No movement of 10 meters waiting ...")); readytosend = 0; + if (mqtt.ping()){ + delay(500); + Serial.println(F("MQTT server avaliable")); + }else{ + delay(500); + Serial.println(F("No MQTT server !!!")); + } } //Watchdog.reset(); delay(DELAY*1000); // wait a few seconds to stabilize connection @@ -171,26 +179,15 @@ void MQTT_connect() { // Stop if already connected. if (mqtt.connected()) { - if (! mqtt.ping()) { - mqtt.disconnect(); - } else { + return; - } + } Serial.println(F("Connecting to MQTT... ")); - mqtt.connect(); - delay(1000); - if (! mqtt.ping()) { - mqtt.disconnect(); - Serial.println(F("No MQTT server avaliable")); - }else { - mqtt.disconnect(); - Serial.println(F("MQTT server avaliable")); - } while ((ret = mqtt.connect()) != 0 ) { // connect will return 0 for connected - //delay(1000); + delay(200); //Serial.println(mqtt.connectErrorString(ret)); mqtt.disconnect(); From 19cd7202d31b23d621c1039ba6c32b2147be8f35 Mon Sep 17 00:00:00 2001 From: roblad Date: Sun, 6 Nov 2016 17:01:44 +0100 Subject: [PATCH 12/26] reconect added and changes for delays --- arduino/gps-tracker2/gps-tracker2.ino | 169 +++++++++++++++++++++----- 1 file changed, 137 insertions(+), 32 deletions(-) diff --git a/arduino/gps-tracker2/gps-tracker2.ino b/arduino/gps-tracker2/gps-tracker2.ino index 61c69dc..bcabfa0 100644 --- a/arduino/gps-tracker2/gps-tracker2.ino +++ b/arduino/gps-tracker2/gps-tracker2.ino @@ -1,3 +1,4 @@ + /*************************************************** Adafruit MQTT Library FONA Example @@ -23,15 +24,22 @@ reconnect GPRS */ +//#include + +//#include #include "config.h" #include -#include #include #include "Adafruit_FONA.h" #include "Adafruit_MQTT.h" #include "Adafruit_MQTT_FONA.h" +//#include "SoftReset.h" #include -#include +//#include + +//#include +//#include + /*************************** FONA Pins ***********************************/ @@ -40,15 +48,18 @@ #define GSM_TX 3 #define GSM_RST 4 // pin number 16 in sim808 throug diode to ground https://cdn-shop.adafruit.com/datasheets/SIM808_Hardware+Design_V1.00.pdf max 4.3 V SoftwareSerial fonaSS = SoftwareSerial(GSM_TX, GSM_RX); +SoftwareSerial *fonaSerial = &fonaSS; + Adafruit_FONA fona = Adafruit_FONA(GSM_RST); +//Adafruit_FONA fona = Adafruit_FONA(); //delay(200); StaticJsonBuffer<122> jsonBuffer; // global - a tak nie powinno się robić JsonObject& root = jsonBuffer.createObject(); - // current state LDR + float lastmovement_W = 0; float lastmovement_L = 0; uint8_t readytosend; @@ -57,16 +68,18 @@ uint16_t reporter; char geodata[122]; /************ Global State (you don't need to change this!) ******************/ -const char MQTT_SERVER[] PROGMEM = AIO_SERVER; -//const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME; -//const char MQTT_PASSWORD[] PROGMEM = AIO_PASSWORD; -const char MQTT_TOPIC[] PROGMEM = TOPIC; - - +//const char MQTT_SERVER[] PROGMEM = AIO_SERVER; +//const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME; +//const char MQTT_PASSWORD[] PROGMEM = AIO_PASSWORD; +//const char MQTT_TOPIC[] PROGMEM = TOPIC; +//const char PHONE_GSM_APN[] PROGMEM = GSM_APN; +#define MQTT_CONN_KEEPALIVE 1800 +#define MQTT_FONA_QUERYDELAY DELAY + //#define MQTT_DEBUG // Setup the FONA MQTT class by passing in the FONA class and MQTT server and login details. -//Adafruit_MQTT_FONA mqtt(&fona, AIO_SERVER, AIO_SERVERPORT,AIO_USERNAME,AIO_PASSWORD); -Adafruit_MQTT_FONA mqtt(&fona, AIO_SERVER, AIO_SERVERPORT); +Adafruit_MQTT_FONA mqtt(&fona,AIO_SERVER,AIO_SERVERPORT,AIO_USERNAME,AIO_PASSWORD); +//Adafruit_MQTT_FONA mqtt(&fona,AIO_SERVER,AIO_SERVERPORT); // You don't need to change anything below this line! #define halt(s) { Serial.println(F( s )); while(1); } @@ -79,7 +92,7 @@ boolean GPS(); /****************************** Feeds ***************************************/ // Setup a feed called 'photocell' for publishing. -Adafruit_MQTT_Publish feed = Adafruit_MQTT_Publish(&mqtt, TOPIC); +Adafruit_MQTT_Publish feed = Adafruit_MQTT_Publish(&mqtt,TOPIC); // Adjust as necessary, in seconds. Default to 5 minutes. /*************************** Sketch Code ************************************/ @@ -87,9 +100,11 @@ Adafruit_MQTT_Publish feed = Adafruit_MQTT_Publish(&mqtt, TOPIC); // How many transmission failures in a row we're willing to be ok with before reset byte txfailures = 0; #define MAXTXFAILURES 3 +//void MQTT_connect(); void setup() { - + + //while (!fonaSS); // Watchdog is optional! @@ -102,29 +117,65 @@ void setup() { //Watchdog.reset(); delay(5000); // wait a few seconds to stabilize connection //Watchdog.reset(); - + // Initialise the FONA module while (! FONAconnect(F(GSM_APN), F(GSM_USERNAME), F(GSM_PASSWORD))) { - Serial.println(F("Retrying FONA GSM")); + Serial.println(F("Retrying FONA GSM - GPRS")); + delay(10000); } - while (! GPS()) { + while (! GPS()) { Serial.println(F("Retrying FONA GPS")); + delay(5000); //Watchdog.reset(); } Serial.println(F("Connected to Cellular!")); //Watchdog.reset(); - delay(5000); // wait a few seconds to stabilize connection + delay(10000); // wait a few seconds to stabilize connection //Watchdog.reset(); } void loop() { + fona.flush(); + delay(50); //Serial.println(freeMemory()); prepareData(); - + + delay(50); + + if ( fona.getNetworkStatus() ){ + //TCPavailable fona.getNetworkStatus() fona.TCPconnected() + Serial.println(F("Network avaliable !")); + + delay(50); + mqtt.ping(); + delay(50); + } else { + Serial.println(F("No network !")); + + fona.enableGPRS(false); + delay(7000); // wait a few seconds to stabilize connection + Serial.println(F("Enabling GPRS again !")); + while (!fona.enableGPRS(true)) { + delay(7000); + Serial.println(F("Failed to turn GPRS on")); + + } + + while (! GPS()) { + Serial.println(F("Restarting FONA GPS - not fixed")); + delay(5000); + } + + Serial.println(F("Retrying FONA GPRS/GPS after crash !")); + delay(10000); + Serial.println(F("Connected to Cellular again !")); + } + + //delay(500); // Make sure to reset watchdog every loop iteration! ////Watchdog.reset(); @@ -134,8 +185,11 @@ void loop() { if (readytosend != 0) { + + + delay(50); MQTT_connect(); - + delay(50); //Watchdog.reset(); // Now we can publish stuff! @@ -148,22 +202,28 @@ if (readytosend != 0) { } else { Serial.println(F("Sent OK!")); //Serial.println(freeMemory()); + //delay(500); + //feed.publish(geodata); txfailures = 0; readytosend = 0; //delete[] geodata; + } } else { Serial.println(F("No movement of 10 meters waiting ...")); + delay(10); readytosend = 0; - if (mqtt.ping()){ - delay(500); + + /*if (mqtt.ping()){ + delay(1000); Serial.println(F("MQTT server avaliable")); }else{ delay(500); Serial.println(F("No MQTT server !!!")); } + */ } //Watchdog.reset(); delay(DELAY*1000); // wait a few seconds to stabilize connection @@ -187,7 +247,7 @@ void MQTT_connect() { Serial.println(F("Connecting to MQTT... ")); while ((ret = mqtt.connect()) != 0 ) { // connect will return 0 for connected - delay(200); + delay(100); //Serial.println(mqtt.connectErrorString(ret)); mqtt.disconnect(); @@ -204,16 +264,21 @@ void MQTT_connect() { void prepareData() { - + int vbat; int ctime[6]; float gps_data[5]; + if (! fona.getBattVoltage(&vbat)) vbat = 0; + delay(100); void GPS_Data(float *fdata, int *idata); GPS_Data(&gps_data[0], &ctime[0]); + delay(100); setTime(ctime[0], ctime[1], ctime[2], ctime[3], ctime[4], ctime[5]); + delay(100); time_t uxdate = now(); - int vbat; + //if (! fona.getBattPercent(&vbat)) vbat = 0; - if (! fona.getBattVoltage(&vbat)) vbat = 0; + + //} // {"tst":1476474031,"acc":1000,"_type":"location","alt":140,"lon":-90.48259734672334,"vac":10,"p":100.160530090332,"lat":38.75410327670748,"batt":100,"tid":"JT"} // Owntracks API: @@ -228,12 +293,41 @@ void prepareData() { root.set("tid",TID); root.set("vel",(int(gps_data[2]) <= SPEED_TR ? 0 : int(gps_data[2]))); root.set("tst",uxdate); + delay(50); root.printTo(geodata, 122); + delay(100); //sprintf((gps_data[0]),%f, movement_W); - if ((((gps_data[0]) - lastmovement_W ) > 0.0010000) or (((gps_data[1]) - lastmovement_L ) > 0.0010000) or reporter == HEARTBIT) { + if ((((gps_data[0]) - lastmovement_W ) > float(0.0010000)) or (((gps_data[1]) - lastmovement_L ) > float(0.0010000)) or reporter == HEARTBIT) { reporter=0; readytosend = 1; +/* +byte stat = fona.GPSstatus(); + + if (stat < 0 or stat == 0 or stat == 1) { + + Serial.println(F("Not fixed!")); + + if (fona.getNetworkStatus() == 1) { + + float lat,lon; + boolean gsmloc_success = fona.getGSMLoc(&lat, &lon); + + if (gsmloc_success) { + Serial.print("GSMLoc lat:"); + Serial.println(lat, 6); + Serial.print("GSMLoc long:"); + Serial.println(lon, 6); + } + } + + + delay(500); + } +*/ + + /* + Serial.println(); Serial.print(F("Latitude OLD: ")); Serial.print(lastmovement_W,7); @@ -242,8 +336,12 @@ void prepareData() { Serial.print(lastmovement_L,7); Serial.println(); */ - lastmovement_W = (gps_data[0]); - lastmovement_L = (gps_data[1]); + + lastmovement_W = (gps_data[0]); + delay(10); + lastmovement_L = (gps_data[1]); + delay(10); + /*Serial.println(); Serial.println(F("Latitude: ")); Serial.print(lastmovement_W,7); @@ -267,11 +365,18 @@ void prepareData() { Serial.print(F("Longitude OLD: ")); Serial.print(lastmovement_L,7); Serial.println(); - Serial.print(F("Times checking, HEARTBIT reporting heartbit now: ")); + Serial.print(F("Times checking: ")); + Serial.print(HEARTBIT); + Serial.print(F(" before reporting, heartbit now waiting: ")); Serial.println(reporter); Serial.println(); - - -} + //Serial.print(F("GPS status: ")); + //Serial.println(fona.GPSstatus()); //void sendATcommand(char *ATcommand, unsigned int *timeout); + //sendATcommand("AT+CGPSSTATUS?",500); + +} + + + From 56ca79882257178d077250ad1705bcad25b1cc53 Mon Sep 17 00:00:00 2001 From: roblad Date: Sun, 6 Nov 2016 17:03:39 +0100 Subject: [PATCH 13/26] roolback to variables delays change --- arduino/gps-tracker2/fonahelper.cpp | 62 ++++++++++++++++++++--------- 1 file changed, 44 insertions(+), 18 deletions(-) diff --git a/arduino/gps-tracker2/fonahelper.cpp b/arduino/gps-tracker2/fonahelper.cpp index e0024a9..75a559f 100644 --- a/arduino/gps-tracker2/fonahelper.cpp +++ b/arduino/gps-tracker2/fonahelper.cpp @@ -1,13 +1,15 @@ +#include "config.h" #include #include #include "Adafruit_FONA.h" -#include "config.h" + #define halt(s) { Serial.println(F( s )); while(1); } -const char MQTT_GSM_APN[] PROGMEM = GSM_APN; + extern Adafruit_FONA fona; extern SoftwareSerial fonaSS; +extern SoftwareSerial *fonaSerial; @@ -23,11 +25,11 @@ uint8_t sendATcommand(char* ATcommand,unsigned int *timeout){ char response[30]; unsigned long previous; - while( fonaSS.available() > 0) fonaSS.read(); // Clean the input buffer + while( fonaSerial->available() > 0) fonaSerial->read(); // Clean the input buffer fonaSS.flush(); if (ATcommand[0] != '\0') { - fonaSS.println(ATcommand); // Send the AT command + fonaSerial->println(ATcommand); // Send the AT command delay(20); } @@ -41,8 +43,8 @@ uint8_t sendATcommand(char* ATcommand,unsigned int *timeout){ Serial.print(F("Result: ")); // this loop waits for the answer do{ - if(fonaSS.available() > 0){ // if there are data in the UART input buffer, reads it and checks for the asnwer - response[x] = fonaSS.read(); + if(fonaSerial->available() > 0){ // if there are data in the UART input buffer, reads it and checks for the asnwer + response[x] = fonaSerial->read(); Serial.print(response[x]); //delay(20); @@ -62,11 +64,13 @@ uint8_t sendATcommand(char* ATcommand,unsigned int *timeout){ boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *username, const __FlashStringHelper *password) { //Watchdog.reset(); - fonaSS.begin(9600); - if (! fona.begin(fonaSS)) { // can also try fona.begin(Serial1) + fonaSerial->begin(9600); + if (! fona.begin(*fonaSerial)) { // can also try fona.begin(Serial1) Serial.println(F("Couldn't find FONA")); return false; } else { + //fona.deleteSMS(1); + //Serial.print(F("Check FONA err: - expect OK")); //set additionalcommands to modem // sendATcommand("AT+CMEE=2", 200); @@ -88,12 +92,12 @@ boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *u // reset to default //"ATZ0"; //"AT&W"; - + //delay(1000); } while (! fona.getNetworkStatus()) { Serial.println(F("Waiting for network...")); - //dey(500); + delay(1000); /* uint8_t n = fona.getRSSI(); int8_t r; @@ -107,25 +111,28 @@ boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *u } Serial.print(r); Serial.println(F(" dBm")); - delay(2000); + */ //Watchdog.reset(); } //Watchdog.reset(); - delay(5000); // wait a few seconds to stabilize connection + delay(7000); // wait a few seconds to stabilize connection //Watchdog.reset(); fona.setGPRSNetworkSettings(apn, username, password); + delay(20); //Serial.println(F("Disabling GPRS")); fona.enableGPRS(false); //Watchdog.reset(); - //delay(5000); // wait a few seconds to stabilize connection + delay(10000); // wait a few seconds to stabilize connection //Watchdog.reset(); Serial.println(F("Enabling GPRS")); - if (!fona.enableGPRS(true)) { + while (!fona.enableGPRS(true)) { + delay(10000); Serial.println(F("Failed to turn GPRS on")); + return false; } @@ -159,7 +166,7 @@ boolean GPS() { Serial.println(F("Not fixed!")); return false; - delay(1000); + //delay(5000); } if (stat >= 2) { Serial.println(F("GPS Fixed")); @@ -170,13 +177,32 @@ boolean GPS() { void GPS_Data(float *fdata, int *idata) { char utime[14]; + int YY; + int MM; + int DD; + int hh; + int mm; + int ss; //float latitude, longitude, speed_kph, heading, altitude; boolean gps_success = fona.getGPS(&fdata[0], &fdata[1], &fdata[3], &fdata[2], &fdata[4], &utime[0]); - sscanf(utime,"%04d%02d%02d%02d%02d%02d",&idata[5],&idata[4],&idata[3],&idata[0],&idata[1],&idata[2]); - + delay(20); + sscanf(utime,"%04d%02d%02d%02d%02d%02d",&YY,&MM,&DD,&hh,&mm,&ss); + delay(20); + idata[0] = hh; + idata[1] = mm; + idata[2] = ss; + idata[3] = DD; + idata[4] = MM; + idata[5] = YY; + + delay(20); + if (&gps_success) { - //delete utime; + return &fdata; } } + + + From 16204bad37e9c3d5f812e4f30bfbd4c98c69989b Mon Sep 17 00:00:00 2001 From: roblad Date: Sun, 6 Nov 2016 17:05:08 +0100 Subject: [PATCH 14/26] own server added --- arduino/gps-tracker2/config.h | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/arduino/gps-tracker2/config.h b/arduino/gps-tracker2/config.h index 72a0305..41c54bb 100644 --- a/arduino/gps-tracker2/config.h +++ b/arduino/gps-tracker2/config.h @@ -8,17 +8,30 @@ * */ -#define GSM_APN "internet" -#define GSM_USERNAME "" -#define GSM_PASSWORD "" +#define GSM_APN "internet" +#define GSM_USERNAME "" +#define GSM_PASSWORD "" -#define AIO_SERVER "xxxxxx.cloudmqtt.com" -#define AIO_SERVERPORT 11971 -#define AIO_USERNAME "xxxxxxx" -#define AIO_PASSWORD "xxxxxxx" -#define TOPIC "owntracks/piesek/yasmina" -#define TID "yb" // "tracker-ID which is used by the auto-faces feature to display, say, initials of a user." -#define SPEED_TR 2 // bellow that number = 0 +//#define SUBSCRIPTIONDATALEN 122 -#define DELAY 5 // seconds + + +//#define AIO_SERVER "m20.cloudmqtt.com" + +//#define AIO_SERVER "broker.mqttdashboard.com" +//#define AIO_SERVER "broker.hivemq.com" +#define AIO_SERVER "155.133.46.194" +#define AIO_SERVERPORT 1883 +//#define AIO_SERVERPORT 11971 +//#define AIO_SERVERPORT 10429 +#define AIO_USERNAME "xxxxxxxxxxx" +#define AIO_PASSWORD "xxxxxxxxxxx" +//#define AIO_USERNAME "gosecwhh" +//#define AIO_PASSWORD "OQoeB0JUxk22" + +#define TOPIC "owntracks/piesek/yasmina" +#define TID "yb" // "tracker-ID which is used by the auto-faces feature to display, say, initials of a user." +#define SPEED_TR 2 // bellow that number = 0 +#define HEARTBIT 220 // there ic multiply by DELAY and sending heartbit - set for every 30 min when movement trying is 5 sec. +#define DELAY 7 // seconds From 9dd651b4b2f03ea76cfd37e27890fe5c32dcf886 Mon Sep 17 00:00:00 2001 From: roblad Date: Wed, 9 Nov 2016 07:50:37 +0100 Subject: [PATCH 15/26] last but not well - 714 byte ram - not help --- arduino/gps-tracker2/gps-tracker2.ino | 160 +++++++++++++++----------- 1 file changed, 96 insertions(+), 64 deletions(-) diff --git a/arduino/gps-tracker2/gps-tracker2.ino b/arduino/gps-tracker2/gps-tracker2.ino index bcabfa0..f457a4d 100644 --- a/arduino/gps-tracker2/gps-tracker2.ino +++ b/arduino/gps-tracker2/gps-tracker2.ino @@ -29,12 +29,15 @@ //#include #include "config.h" #include + + #include #include "Adafruit_FONA.h" #include "Adafruit_MQTT.h" #include "Adafruit_MQTT_FONA.h" //#include "SoftReset.h" -#include +#include "ArduinoJson.h" + //#include //#include @@ -48,33 +51,30 @@ #define GSM_TX 3 #define GSM_RST 4 // pin number 16 in sim808 throug diode to ground https://cdn-shop.adafruit.com/datasheets/SIM808_Hardware+Design_V1.00.pdf max 4.3 V SoftwareSerial fonaSS = SoftwareSerial(GSM_TX, GSM_RX); -SoftwareSerial *fonaSerial = &fonaSS; - +//SoftwareSerial *fonaSerial = &fonaSS; Adafruit_FONA fona = Adafruit_FONA(GSM_RST); //Adafruit_FONA fona = Adafruit_FONA(); //delay(200); -StaticJsonBuffer<122> jsonBuffer; +//StaticJsonBuffer<122> jsonBuffer; // global - a tak nie powinno się robić -JsonObject& root = jsonBuffer.createObject(); +//JsonObject& root = jsonBuffer.createObject(); -float lastmovement_W = 0; -float lastmovement_L = 0; -uint8_t readytosend; -uint16_t reporter; - -char geodata[122]; +uint8_t readytosend = 0; +uint16_t reporter = 0; +char geodata[125]; +//char *geodatapoint = geodata; /************ Global State (you don't need to change this!) ******************/ //const char MQTT_SERVER[] PROGMEM = AIO_SERVER; //const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME; //const char MQTT_PASSWORD[] PROGMEM = AIO_PASSWORD; //const char MQTT_TOPIC[] PROGMEM = TOPIC; //const char PHONE_GSM_APN[] PROGMEM = GSM_APN; -#define MQTT_CONN_KEEPALIVE 1800 -#define MQTT_FONA_QUERYDELAY DELAY +//#define MQTT_CONN_KEEPALIVE 1800 +//#define MQTT_FONA_QUERYDELAY 50 //#define MQTT_DEBUG // Setup the FONA MQTT class by passing in the FONA class and MQTT server and login details. @@ -95,6 +95,7 @@ boolean GPS(); Adafruit_MQTT_Publish feed = Adafruit_MQTT_Publish(&mqtt,TOPIC); // Adjust as necessary, in seconds. Default to 5 minutes. + /*************************** Sketch Code ************************************/ // How many transmission failures in a row we're willing to be ok with before reset @@ -103,8 +104,6 @@ byte txfailures = 0; //void MQTT_connect(); void setup() { - - //while (!fonaSS); // Watchdog is optional! @@ -112,6 +111,7 @@ void setup() { Serial.begin(115200); + Serial.println(F("GPS Tracker starting ...")); //Watchdog.reset(); @@ -122,7 +122,7 @@ void setup() { while (! FONAconnect(F(GSM_APN), F(GSM_USERNAME), F(GSM_PASSWORD))) { Serial.println(F("Retrying FONA GSM - GPRS")); - delay(10000); + delay(10); } while (! GPS()) { Serial.println(F("Retrying FONA GPS")); @@ -130,6 +130,7 @@ void setup() { //Watchdog.reset(); } Serial.println(F("Connected to Cellular!")); + delay(10); //Watchdog.reset(); delay(10000); // wait a few seconds to stabilize connection @@ -139,43 +140,48 @@ void setup() { void loop() { - fona.flush(); - delay(50); + //delay(100); + //fona.flush(); + delay(100); //Serial.println(freeMemory()); + prepareData(); - delay(50); + delay(100); - if ( fona.getNetworkStatus() ){ + while( fona.GPRSstate() != 1 ){ //TCPavailable fona.getNetworkStatus() fona.TCPconnected() - Serial.println(F("Network avaliable !")); - delay(50); - mqtt.ping(); - delay(50); - } else { - Serial.println(F("No network !")); + + + Serial.println(F("No network ! traing to bring up ...")); + delay(10); - fona.enableGPRS(false); - delay(7000); // wait a few seconds to stabilize connection - Serial.println(F("Enabling GPRS again !")); - while (!fona.enableGPRS(true)) { - delay(7000); - Serial.println(F("Failed to turn GPRS on")); - } + //void sendATcommand(char* ATcommand,unsigned int *timeout); + //sendATcommand("AT+CGATT=0",1000); + fonaSS.println("AT+CGATT=0"); + delay(1000); // wait a few seconds to stabilize connection + fonaSS.println("AT+CGATT=1"); + //sendATcommand("AT+CGATT=1",1000); + delay(5000); + + Serial.println(F("Enabling GPRS again !")); + delay(10); + while (! GPS()) { Serial.println(F("Restarting FONA GPS - not fixed")); delay(5000); } Serial.println(F("Retrying FONA GPRS/GPS after crash !")); - delay(10000); + delay(5000); Serial.println(F("Connected to Cellular again !")); + delay(10); } - - //delay(500); + Serial.println(F("Network avaliable !")); + delay(10); // Make sure to reset watchdog every loop iteration! ////Watchdog.reset(); @@ -184,38 +190,42 @@ void loop() { // function definition further below. if (readytosend != 0) { - - + + - delay(50); MQTT_connect(); delay(50); + //Watchdog.reset(); // Now we can publish stuff! - - - + if (! feed.publish(geodata)) { Serial.println(F("Sent Failed")); + delay(10); txfailures++; } else { Serial.println(F("Sent OK!")); + delay(10); + //delete[] geodata; //Serial.println(freeMemory()); //delay(500); //feed.publish(geodata); txfailures = 0; - readytosend = 0; + readytosend = 0; //delete[] geodata; } } else { - Serial.println(F("No movement of 10 meters waiting ...")); + Serial.println(F("No movement of 10 meters waiting ...")); delay(10); - readytosend = 0; - + readytosend = 0; + + //delay(50); + mqtt.ping(1); + delay(100); /*if (mqtt.ping()){ delay(1000); Serial.println(F("MQTT server avaliable")); @@ -238,68 +248,87 @@ void MQTT_connect() { byte ret; // Stop if already connected. - if (mqtt.connected()) { + if (mqtt.connected()) { return; - - } + } Serial.println(F("Connecting to MQTT... ")); - + delay(10); while ((ret = mqtt.connect()) != 0 ) { // connect will return 0 for connected delay(100); //Serial.println(mqtt.connectErrorString(ret)); - mqtt.disconnect(); Serial.println(F("Retrying MQTT connection in 2 seconds...")); + delay(10); //mqtt.connect(); delay(2000); // wait 5 seconds } Serial.println(F("MQTT Connected!")); + delay(10); //Watchdog.reset(); + } void prepareData() { + + float lastmovement_W = 0; + float lastmovement_L = 0; int vbat; int ctime[6]; float gps_data[5]; if (! fona.getBattVoltage(&vbat)) vbat = 0; delay(100); void GPS_Data(float *fdata, int *idata); - GPS_Data(&gps_data[0], &ctime[0]); + GPS_Data(gps_data, ctime); delay(100); setTime(ctime[0], ctime[1], ctime[2], ctime[3], ctime[4], ctime[5]); delay(100); time_t uxdate = now(); - - //if (! fona.getBattPercent(&vbat)) vbat = 0; - + delay(100); + //if (! fona.getBattPercent(&vbat)) vbat = 0; //} // {"tst":1476474031,"acc":1000,"_type":"location","alt":140,"lon":-90.48259734672334,"vac":10,"p":100.160530090332,"lat":38.75410327670748,"batt":100,"tid":"JT"} // Owntracks API: // http://owntracks.org/booklet/tech/json/ +// char loc[50]; + +// sprintf(loc, "{\"longitude\":%s,\"latitude\":%s}", Lon, Lat); + // dtostrf(longitude, 6, 6, Lon); + // dtostrf(latitude, 6, 6, Lat); + //dtostrf(hdop, 1, 1, HDOP); + //root["_type"] = "location"; + //double_with_n_digits( + StaticJsonBuffer<140> jsonBuffer; + delay(100); + JsonObject& root = jsonBuffer.createObject(); + delay(100); root.set("_type","location"); root.set("acc",3); root.set("batt",vbat); root.set("cog",int(gps_data[4])); + delay(2); root.set("lat",(gps_data[0]),7); + delay(2); root.set("lon",(gps_data[1]),7); root.set("tid",TID); root.set("vel",(int(gps_data[2]) <= SPEED_TR ? 0 : int(gps_data[2]))); + delay(10); root.set("tst",uxdate); - delay(50); - root.printTo(geodata, 122); + delay(2); + root.printTo(geodata, 125); delay(100); + //delete[] &jsonBuffer,&uxdate,&vbat,root,&ctime; //sprintf((gps_data[0]),%f, movement_W); - if ((((gps_data[0]) - lastmovement_W ) > float(0.0010000)) or (((gps_data[1]) - lastmovement_L ) > float(0.0010000)) or reporter == HEARTBIT) { - reporter=0; - readytosend = 1; + if ((((gps_data[0]) - lastmovement_W ) > float(0.0010000)) or (((gps_data[1]) - lastmovement_L ) > float(0.0010000)) or reporter == HEARTBIT) { + reporter=0; + readytosend = 1; /* byte stat = fona.GPSstatus(); @@ -341,7 +370,7 @@ byte stat = fona.GPSstatus(); delay(10); lastmovement_L = (gps_data[1]); delay(10); - + //delete[] gps_data; /*Serial.println(); Serial.println(F("Latitude: ")); Serial.print(lastmovement_W,7); @@ -350,24 +379,28 @@ byte stat = fona.GPSstatus(); Serial.print(lastmovement_L,7); Serial.println(); */ - } else { + } else { readytosend = 0; // reporter every 15 min - position reported even not moved reporter++; - } + } Serial.println(); Serial.print(F("Latitude OLD: ")); + delay(10); Serial.print(lastmovement_W,7); Serial.println(); Serial.print(F("Longitude OLD: ")); + delay(10); Serial.print(lastmovement_L,7); Serial.println(); Serial.print(F("Times checking: ")); + delay(10); Serial.print(HEARTBIT); Serial.print(F(" before reporting, heartbit now waiting: ")); + delay(10); Serial.println(reporter); Serial.println(); //Serial.print(F("GPS status: ")); @@ -379,4 +412,3 @@ byte stat = fona.GPSstatus(); - From c2fced42553fb2833f5523fc294611208728411c Mon Sep 17 00:00:00 2001 From: roblad Date: Wed, 9 Nov 2016 07:51:28 +0100 Subject: [PATCH 16/26] last but not well - 714 byte ram - not help --- arduino/gps-tracker2/fonahelper.cpp | 81 +++++++++++++++++++++-------- 1 file changed, 60 insertions(+), 21 deletions(-) diff --git a/arduino/gps-tracker2/fonahelper.cpp b/arduino/gps-tracker2/fonahelper.cpp index 75a559f..5ed6840 100644 --- a/arduino/gps-tracker2/fonahelper.cpp +++ b/arduino/gps-tracker2/fonahelper.cpp @@ -9,7 +9,14 @@ extern Adafruit_FONA fona; extern SoftwareSerial fonaSS; -extern SoftwareSerial *fonaSerial; +//extern SoftwareSerial *fonaSerial; + + + + + + + @@ -17,19 +24,22 @@ extern SoftwareSerial *fonaSerial; byte gps_enabled = 0; + + //send AT command and wait for responce and print + uint8_t sendATcommand(char* ATcommand,unsigned int *timeout){ uint8_t x=0, answer=0; - char response[30]; + char response[20]; unsigned long previous; - while( fonaSerial->available() > 0) fonaSerial->read(); // Clean the input buffer + while( fonaSS.available() > 0) fonaSS.read(); // Clean the input buffer fonaSS.flush(); if (ATcommand[0] != '\0') { - fonaSerial->println(ATcommand); // Send the AT command + fonaSS.println(ATcommand); // Send the AT command delay(20); } @@ -38,13 +48,15 @@ uint8_t sendATcommand(char* ATcommand,unsigned int *timeout){ previous = millis(); Serial.println(); Serial.print(F("Execute AT command: ")); + delay(10); Serial.println(ATcommand); Serial.print(F("Result: ")); + delay(10); // this loop waits for the answer do{ - if(fonaSerial->available() > 0){ // if there are data in the UART input buffer, reads it and checks for the asnwer - response[x] = fonaSerial->read(); + if(fonaSS.available() > 0){ // if there are data in the UART input buffer, reads it and checks for the asnwer + response[x] = fonaSS.read(); Serial.print(response[x]); //delay(20); @@ -64,21 +76,24 @@ uint8_t sendATcommand(char* ATcommand,unsigned int *timeout){ boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *username, const __FlashStringHelper *password) { //Watchdog.reset(); - fonaSerial->begin(9600); - if (! fona.begin(*fonaSerial)) { // can also try fona.begin(Serial1) + fonaSS.begin(9600); + if (! fona.begin(fonaSS)) { // can also try fona.begin(Serial1) Serial.println(F("Couldn't find FONA")); + delay(10); return false; } else { //fona.deleteSMS(1); - + //keep alive tcp +// fonaSS.println("AT&V",500); + //delay(200); //Serial.print(F("Check FONA err: - expect OK")); //set additionalcommands to modem - // sendATcommand("AT+CMEE=2", 200); + //sendATcommand("AT+CGATT=1",1000); // Serial.print(F("Delete all SMS - expect OK")); // sendATcommand("AT+CMGD=1,4",200); //sendATcommand("AT&W",200); //Serial.print(F("Registration IP - expect OK")); - //sendATcommand("AT+SAPBR=2,1",200); + //sendATcommand("AT+CGMSCLASS=12",200); //reset GPS //led blink 0 1 2 //AT+CSGS=2 @@ -94,10 +109,15 @@ boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *u //"AT&W"; //delay(1000); } + while (! fona.getNetworkStatus()) { Serial.println(F("Waiting for network...")); - delay(1000); + delay (10); + + + + //delay(1000); /* uint8_t n = fona.getRSSI(); int8_t r; @@ -111,27 +131,33 @@ boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *u } Serial.print(r); Serial.println(F(" dBm")); + + + + */ //Watchdog.reset(); } //Watchdog.reset(); - delay(7000); // wait a few seconds to stabilize connection + delay(10000); // wait a few seconds to stabilize connection //Watchdog.reset(); fona.setGPRSNetworkSettings(apn, username, password); - delay(20); + delay(50); //Serial.println(F("Disabling GPRS")); fona.enableGPRS(false); + //Watchdog.reset(); - delay(10000); // wait a few seconds to stabilize connection + delay(5000); // wait a few seconds to stabilize connection //Watchdog.reset(); Serial.println(F("Enabling GPRS")); while (!fona.enableGPRS(true)) { - delay(10000); + delay(50); Serial.println(F("Failed to turn GPRS on")); + delay(10); return false; } @@ -145,8 +171,10 @@ boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *u boolean GPS() { if (gps_enabled == 0 ) { Serial.println(F("Enabling GPS")); + delay(10); if (!fona.enableGPS(true)) { - Serial.println(F("Failed to turn GPS on")); + Serial.println(F("Failed to turn GPS on")); + delay(10); return false; } @@ -157,24 +185,32 @@ boolean GPS() { //Watchdog.reset(); delay(5000); // wait a few seconds to stabilize connection + + //Watchdog.reset(); + byte stat; stat = fona.GPSstatus(); - if (stat < 0 or stat == 0 or stat == 1) { + if (&stat < 0 or &stat == 0 or &stat == 1) { Serial.println(F("Not fixed!")); + delay(10); return false; //delay(5000); } if (stat >= 2) { Serial.println(F("GPS Fixed")); + delay(10); } + //Watchdog.reset(); + return true; } + void GPS_Data(float *fdata, int *idata) { char utime[14]; int YY; @@ -187,9 +223,10 @@ void GPS_Data(float *fdata, int *idata) { boolean gps_success = fona.getGPS(&fdata[0], &fdata[1], &fdata[3], &fdata[2], &fdata[4], &utime[0]); delay(20); sscanf(utime,"%04d%02d%02d%02d%02d%02d",&YY,&MM,&DD,&hh,&mm,&ss); + //sscanf(utime,"%04d%02d%02d%02d%02d%02d",&idata[5],&idata[4],&idata[3],&idata[0],&idata[1],&idata[2]); delay(20); idata[0] = hh; - idata[1] = mm; + idata[1] = mm; idata[2] = ss; idata[3] = DD; idata[4] = MM; @@ -198,11 +235,13 @@ void GPS_Data(float *fdata, int *idata) { delay(20); if (&gps_success) { + + + //sendATcommand("AT+CGATT=1",1000); + return &fdata; } } - - From 34edaa19fe2ed870d0f5b947d9bb17b07ecf142e Mon Sep 17 00:00:00 2001 From: roblad Date: Tue, 15 Nov 2016 19:22:50 +0100 Subject: [PATCH 17/26] many changes added auto reset - pin 4 soldering needed --- arduino/gps-tracker2/gps-tracker2.ino | 430 +++++++++++++------------- 1 file changed, 217 insertions(+), 213 deletions(-) diff --git a/arduino/gps-tracker2/gps-tracker2.ino b/arduino/gps-tracker2/gps-tracker2.ino index f457a4d..0dc9788 100644 --- a/arduino/gps-tracker2/gps-tracker2.ino +++ b/arduino/gps-tracker2/gps-tracker2.ino @@ -19,31 +19,18 @@ MIT license, all text above must be included in any redistribution ****************************************************/ -/* - TODO: - reconnect GPRS -*/ -//#include - -//#include #include "config.h" #include - - -#include #include "Adafruit_FONA.h" #include "Adafruit_MQTT.h" #include "Adafruit_MQTT_FONA.h" -//#include "SoftReset.h" #include "ArduinoJson.h" - -//#include - -//#include -//#include - - +#include +#define FONA_DEFAULT_TIMEOUT_MS 750 +#define MQTT_CONN_KEEPALIVE 1800 +#define MQTT_FONA_INTERAVAILDELAY 200 +#define MQTT_FONA_QUERYDELAY 750 /*************************** FONA Pins ***********************************/ // GSm pins @@ -51,19 +38,14 @@ #define GSM_TX 3 #define GSM_RST 4 // pin number 16 in sim808 throug diode to ground https://cdn-shop.adafruit.com/datasheets/SIM808_Hardware+Design_V1.00.pdf max 4.3 V SoftwareSerial fonaSS = SoftwareSerial(GSM_TX, GSM_RX); -//SoftwareSerial *fonaSerial = &fonaSS; +//oftwareSerial *fonaSerial = &fonaSS; Adafruit_FONA fona = Adafruit_FONA(GSM_RST); -//Adafruit_FONA fona = Adafruit_FONA(); -//delay(200); -//StaticJsonBuffer<122> jsonBuffer; -// global - a tak nie powinno się robić -//JsonObject& root = jsonBuffer.createObject(); - -uint8_t readytosend = 0; +int readytosend = 0; uint16_t reporter = 0; +uint32_t logCounter = 0; char geodata[125]; //char *geodatapoint = geodata; @@ -73,22 +55,20 @@ char geodata[125]; //const char MQTT_PASSWORD[] PROGMEM = AIO_PASSWORD; //const char MQTT_TOPIC[] PROGMEM = TOPIC; //const char PHONE_GSM_APN[] PROGMEM = GSM_APN; -//#define MQTT_CONN_KEEPALIVE 1800 -//#define MQTT_FONA_QUERYDELAY 50 + //#define MQTT_DEBUG // Setup the FONA MQTT class by passing in the FONA class and MQTT server and login details. Adafruit_MQTT_FONA mqtt(&fona,AIO_SERVER,AIO_SERVERPORT,AIO_USERNAME,AIO_PASSWORD); //Adafruit_MQTT_FONA mqtt(&fona,AIO_SERVER,AIO_SERVERPORT); -// You don't need to change anything below this line! -#define halt(s) { Serial.println(F( s )); while(1); } // FONAconnect is a helper function that sets up the FONA and connects to // the GPRS network. See the fonahelper.cpp tab above for the source! boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *username, const __FlashStringHelper *password); boolean GPS(); - +//reset +void(* resetFunc) (void) = 0; /****************************** Feeds ***************************************/ // Setup a feed called 'photocell' for publishing. @@ -98,134 +78,120 @@ Adafruit_MQTT_Publish feed = Adafruit_MQTT_Publish(&mqtt,TOPIC); /*************************** Sketch Code ************************************/ -// How many transmission failures in a row we're willing to be ok with before reset -byte txfailures = 0; -#define MAXTXFAILURES 3 -//void MQTT_connect(); void setup() { - //while (!fonaSS); - - // Watchdog is optional! - //Watchdog.enable(8000); - + Serial.begin(115200); - + while (!Serial); + delay(20); + fonaSS.begin(4800); + while (!fonaSS); + delay(20); + fona.begin(fonaSS); Serial.println(F("GPS Tracker starting ...")); + + delay(20000); // wait a few seconds to stabilize connection - //Watchdog.reset(); - delay(5000); // wait a few seconds to stabilize connection - //Watchdog.reset(); - + while ( !fonaSS); // Initialise the FONA module while (! FONAconnect(F(GSM_APN), F(GSM_USERNAME), F(GSM_PASSWORD))) { + + Serial.println(F("Retrying FONA GPRS ...")); + delay(20000); + while ( !fonaSS); +} - Serial.println(F("Retrying FONA GSM - GPRS")); - delay(10); - } - while (! GPS()) { - Serial.println(F("Retrying FONA GPS")); - delay(5000); - //Watchdog.reset(); - } + delay(10000); + while ( !fonaSS); + byte trying =0; + while (! GPS() or trying == 10) { + Serial.println(F("Retrying FONA GPS ...")); + //Serial.println(trying); + delay(15000); + while ( !fonaSS); + trying++; + } + if (trying == 10)resetFunc(); Serial.println(F("Connected to Cellular!")); - delay(10); - //Watchdog.reset(); + + delay(10000); // wait a few seconds to stabilize connection - //Watchdog.reset(); + } void loop() { - //delay(100); - //fona.flush(); - delay(100); - //Serial.println(freeMemory()); - - prepareData(); + while ( !fonaSS); + delay(50); + fonaSS.flush(); + delay(50); + Serial.flush(); + delay(50); - delay(100); - while( fona.GPRSstate() != 1 ){ - //TCPavailable fona.getNetworkStatus() fona.TCPconnected() - - - - Serial.println(F("No network ! traing to bring up ...")); - delay(10); - - - //void sendATcommand(char* ATcommand,unsigned int *timeout); - //sendATcommand("AT+CGATT=0",1000); - fonaSS.println("AT+CGATT=0"); - delay(1000); // wait a few seconds to stabilize connection - fonaSS.println("AT+CGATT=1"); - //sendATcommand("AT+CGATT=1",1000); - delay(5000); - - Serial.println(F("Enabling GPRS again !")); - delay(10); +//-------------main function not generate when no GPS, GPRS, GSM - - while (! GPS()) { - Serial.println(F("Restarting FONA GPS - not fixed")); - delay(5000); - } - - Serial.println(F("Retrying FONA GPRS/GPS after crash !")); - delay(5000); - Serial.println(F("Connected to Cellular again !")); - delay(10); - } - Serial.println(F("Network avaliable !")); - delay(10); - // Make sure to reset watchdog every loop iteration! - ////Watchdog.reset(); - - // Ensure the connection to the MQTT server is alive (this will make the first - // connection and automatically reconnect when disconnected). See the MQTT_connect - // function definition further below. - -if (readytosend != 0) { - - +do{ + clearbuffer(); + while (!fonaSS); + fona.deleteSMS(1); + + Serial.println(F("Network avaliable GPS fixed GPRS connected, processing ...")); + delay(50); + clearbuffer(); + + prepareData(); + delay(100); + +if (readytosend == 1) { + + clearbuffer(); MQTT_connect(); delay(50); - //Watchdog.reset(); + // Now we can publish stuff! - + clearbuffer(); + byte check =0; + do { + while (!fonaSS); if (! feed.publish(geodata)) { - Serial.println(F("Sent Failed")); - delay(10); - txfailures++; - } else { + delay(1000); + check++; + + } else { Serial.println(F("Sent OK!")); - delay(10); - //delete[] geodata; + delay(50); + //Serial.println(freeMemory()); - //delay(500); - //feed.publish(geodata); - txfailures = 0; - readytosend = 0; + check = 3; + readytosend = 0; + //delete[] geodata; - + } + } while (check < 3); + + - } } else { Serial.println(F("No movement of 10 meters waiting ...")); - delay(10); + delay(50); readytosend = 0; - //delay(50); - mqtt.ping(1); - delay(100); + delay(50); + while ( !fonaSS); + mqtt.ping(3); + delay(100); + while (!fonaSS); + if (!fona.TCPconnected()){ + return; + } /*if (mqtt.ping()){ delay(1000); Serial.println(F("MQTT server avaliable")); @@ -235,16 +201,74 @@ if (readytosend != 0) { } */ } - //Watchdog.reset(); + + clearbuffer(); + + + delay(DELAY*1000); // wait a few seconds to stabilize connection - //Watchdog.reset(); + while ( !fonaSS); + } while(fonaSS && fona.GPRSstate() == 1 && fona.GPSstatus() > 1 ); + + Serial.println(F("No GPS fixed or GPRS connected, waiting for restart ...")); + delay(1000); + while ( !fonaSS); + if( fona.GPRSstate() != 1 or fona.getRSSI() < 6){ + Serial.println(F("No network !!! traing to bring up ...")); + delay(50); + //asm volatile ( "jmp 0"); + //albo tak ale reset przed setup void(* resetFunc) (void) = 0; wywolanie resetFunc(); + fonaSS.end(); + resetFunc(); + } + + while (!fonaSS); + if (fona.GPSstatus() <= 1){ + Serial.println(F("Restarting FONA GPS - not fixed ...")); + delay(50); + byte retry=0; + while (! GPS() or retry == 10) { + Serial.println(F("Trying up FONA GPS ...")); + delay(10000); + while (!fonaSS); + retry++; + } + + delay(5000); + while (!fonaSS); + if (fona.GPSstatus() <= 1) { + fonaSS.end(); + resetFunc();//asm volatile ( "jmp 0"); + } + + } + + //OCR0A = 0xAF; + //TIMSK0 |= _BV(OCIE0A); } + +void clearbuffer(){ + // Read all available serial input to flush pending data. + uint16_t timeoutloop = 0; + while (!fonaSS); + fonaSS.peek(); + while (timeoutloop++ < 60) { + while(fonaSS.available()) { + fonaSS.read(); + timeoutloop = 0; // If char was received reset the timer + } + delay(1); + } +} // Function to connect and reconnect as necessary to the MQTT server. // Should be called in the loop function and it will take care if connecting. + + + void MQTT_connect() { - + while ( !fonaSS); byte ret; // Stop if already connected. @@ -253,43 +277,50 @@ void MQTT_connect() { return; } - Serial.println(F("Connecting to MQTT... ")); - delay(10); + Serial.println(F("Connecting to MQTT ... ")); + delay(50); + while (!fonaSS); while ((ret = mqtt.connect()) != 0 ) { // connect will return 0 for connected - delay(100); + delay(50); //Serial.println(mqtt.connectErrorString(ret)); + mqtt.disconnect(); - Serial.println(F("Retrying MQTT connection in 2 seconds...")); - delay(10); + Serial.println(F("Retrying MQTT connection in 2 seconds ...")); + delay(50); //mqtt.connect(); delay(2000); // wait 5 seconds - + while (!fonaSS); } - + // Configure timer0 compare interrupt to run and decrease the log counter every millisecond. + Serial.println(F("MQTT Connected!")); - delay(10); - //Watchdog.reset(); + delay(50); + } void prepareData() { - float lastmovement_W = 0; - float lastmovement_L = 0; + static float lastmovement_W = 0; + static float lastmovement_L = 0; int vbat; int ctime[6]; float gps_data[5]; + while (!fonaSS); if (! fona.getBattVoltage(&vbat)) vbat = 0; - delay(100); - void GPS_Data(float *fdata, int *idata); - GPS_Data(gps_data, ctime); - delay(100); + delay(50); + void GPS_Data(float *fdata, int *idata); + //GPS_Data(gps_data, ctime); + while (!fonaSS); + GPS_Data(&gps_data[0], &ctime[0]); + delay(50); setTime(ctime[0], ctime[1], ctime[2], ctime[3], ctime[4], ctime[5]); - delay(100); + //setTime(&ctime[0], &ctime[1], &ctime[2], &ctime[3], &ctime[4], &ctime[5]); + delay(20); time_t uxdate = now(); - delay(100); + delay(20); //if (! fona.getBattPercent(&vbat)) vbat = 0; //} @@ -297,16 +328,10 @@ void prepareData() { // Owntracks API: // http://owntracks.org/booklet/tech/json/ -// char loc[50]; - -// sprintf(loc, "{\"longitude\":%s,\"latitude\":%s}", Lon, Lat); - // dtostrf(longitude, 6, 6, Lon); - // dtostrf(latitude, 6, 6, Lat); - //dtostrf(hdop, 1, 1, HDOP); - //root["_type"] = "location"; - //double_with_n_digits( + StaticJsonBuffer<140> jsonBuffer; - delay(100); + //memset(jsonBuffer, 0, sizeof(jsonBuffer)); + //delay(50); JsonObject& root = jsonBuffer.createObject(); delay(100); root.set("_type","location"); @@ -319,95 +344,74 @@ void prepareData() { root.set("lon",(gps_data[1]),7); root.set("tid",TID); root.set("vel",(int(gps_data[2]) <= SPEED_TR ? 0 : int(gps_data[2]))); - delay(10); + delay(50); root.set("tst",uxdate); delay(2); + memset(geodata, 0, sizeof(geodata)); + delay(50); root.printTo(geodata, 125); - delay(100); + delay(50); //delete[] &jsonBuffer,&uxdate,&vbat,root,&ctime; //sprintf((gps_data[0]),%f, movement_W); if ((((gps_data[0]) - lastmovement_W ) > float(0.0010000)) or (((gps_data[1]) - lastmovement_L ) > float(0.0010000)) or reporter == HEARTBIT) { reporter=0; readytosend = 1; -/* -byte stat = fona.GPSstatus(); + +//byte stat = fona.GPSstatus(); - if (stat < 0 or stat == 0 or stat == 1) { + //if (stat < 0 or stat == 0 or stat == 1) { - Serial.println(F("Not fixed!")); + //Serial.println(F("Not fixed!")); - if (fona.getNetworkStatus() == 1) { + //if (fona.getNetworkStatus() == 1) { - float lat,lon; - boolean gsmloc_success = fona.getGSMLoc(&lat, &lon); - - if (gsmloc_success) { - Serial.print("GSMLoc lat:"); - Serial.println(lat, 6); - Serial.print("GSMLoc long:"); - Serial.println(lon, 6); - } - } - + //float lat,lon; + //fona.getGSMLoc(&lat, &lon); - delay(500); - } -*/ + + //Serial.print("GSMLoc lat:"); + //Serial.println(lat, 7); + //Serial.print("GSMLoc long:"); + //Serial.println(lon, 7); + //} - /* - - Serial.println(); - Serial.print(F("Latitude OLD: ")); - Serial.print(lastmovement_W,7); - Serial.println(); - Serial.print(F("Longitude OLD: ")); - Serial.print(lastmovement_L,7); - Serial.println(); - */ + delay(100); lastmovement_W = (gps_data[0]); - delay(10); + delay(50); lastmovement_L = (gps_data[1]); - delay(10); - //delete[] gps_data; - /*Serial.println(); - Serial.println(F("Latitude: ")); - Serial.print(lastmovement_W,7); - Serial.println(); - Serial.println(F("Longitude: ")); - Serial.print(lastmovement_L,7); - Serial.println(); - */ + delay(50); + } else { - readytosend = 0; - - // reporter every 15 min - position reported even not moved - reporter++; - + + readytosend = 0; + + // reporter every xxx min - position reported even not moved + ++reporter; - } + } + Serial.println(); - Serial.print(F("Latitude OLD: ")); - delay(10); + Serial.print(F(" Latitude OLD: ")); + delay(50); Serial.print(lastmovement_W,7); Serial.println(); - Serial.print(F("Longitude OLD: ")); - delay(10); + Serial.print(F(" Longitude OLD: ")); + delay(50); Serial.print(lastmovement_L,7); Serial.println(); - Serial.print(F("Times checking: ")); - delay(10); + Serial.println(); + Serial.print(F(" Times checking: ")); + delay(50); Serial.print(HEARTBIT); - Serial.print(F(" before reporting, heartbit now waiting: ")); - delay(10); + Serial.print(F(" before reporting heartbit, waiting: ")); + delay(50); Serial.println(reporter); Serial.println(); - //Serial.print(F("GPS status: ")); - //Serial.println(fona.GPSstatus()); - //void sendATcommand(char *ATcommand, unsigned int *timeout); - //sendATcommand("AT+CGPSSTATUS?",500); - + Serial.println(); + + } From 7942a6e16ed2430cde53d9b2b35d26b4dcf37291 Mon Sep 17 00:00:00 2001 From: roblad Date: Tue, 15 Nov 2016 19:23:56 +0100 Subject: [PATCH 18/26] many changes reset added --- arduino/gps-tracker2/fonahelper.cpp | 245 ++++++++++++---------------- 1 file changed, 106 insertions(+), 139 deletions(-) diff --git a/arduino/gps-tracker2/fonahelper.cpp b/arduino/gps-tracker2/fonahelper.cpp index 5ed6840..3998927 100644 --- a/arduino/gps-tracker2/fonahelper.cpp +++ b/arduino/gps-tracker2/fonahelper.cpp @@ -1,36 +1,24 @@ #include "config.h" -#include -#include #include "Adafruit_FONA.h" - - -#define halt(s) { Serial.println(F( s )); while(1); } +#include +#define FONA_DEFAULT_TIMEOUT_MS 750 extern Adafruit_FONA fona; extern SoftwareSerial fonaSS; -//extern SoftwareSerial *fonaSerial; - - - - - - - +//extern SoftwareSerial *fonaSerial; // flags byte gps_enabled = 0; - - //send AT command and wait for responce and print - +/* uint8_t sendATcommand(char* ATcommand,unsigned int *timeout){ - + uint8_t x=0, answer=0; char response[20]; unsigned long previous; @@ -48,11 +36,11 @@ uint8_t sendATcommand(char* ATcommand,unsigned int *timeout){ previous = millis(); Serial.println(); Serial.print(F("Execute AT command: ")); - delay(10); + delay(50); Serial.println(ATcommand); Serial.print(F("Result: ")); - delay(10); + delay(50); // this loop waits for the answer do{ if(fonaSS.available() > 0){ // if there are data in the UART input buffer, reads it and checks for the asnwer @@ -72,174 +60,153 @@ uint8_t sendATcommand(char* ATcommand,unsigned int *timeout){ } - +*/ boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *username, const __FlashStringHelper *password) { - //Watchdog.reset(); - fonaSS.begin(9600); + + + while (!fonaSS); + fonaSS.begin(4800); + delay(20); + + if (! fona.begin(fonaSS)) { // can also try fona.begin(Serial1) Serial.println(F("Couldn't find FONA")); - delay(10); + delay(50); return false; } else { - //fona.deleteSMS(1); - //keep alive tcp -// fonaSS.println("AT&V",500); - //delay(200); - //Serial.print(F("Check FONA err: - expect OK")); - //set additionalcommands to modem - //sendATcommand("AT+CGATT=1",1000); -// Serial.print(F("Delete all SMS - expect OK")); -// sendATcommand("AT+CMGD=1,4",200); - //sendATcommand("AT&W",200); - //Serial.print(F("Registration IP - expect OK")); - //sendATcommand("AT+CGMSCLASS=12",200); - //reset GPS - //led blink 0 1 2 - //AT+CSGS=2 - //delete smses from sim - //"AT+CMGD=1" - //check errors - //"AT+CMEE=2" - //disable battery charge - power consumption - //"AT+CGPSRST=0" - //"AT&W" - // reset to default - //"ATZ0"; - //"AT&W"; - //delay(1000); - } - while (! fona.getNetworkStatus()) { - Serial.println(F("Waiting for network...")); - delay (10); - - - - //delay(1000); - /* - uint8_t n = fona.getRSSI(); - int8_t r; - - Serial.print(F("RSSI = ")); Serial.print(n); Serial.print(": "); - if (n == 0) r = -115; - if (n == 1) r = -111; - if (n == 31) r = -52; - if ((n >= 2) && (n <= 30)) { - r = map(n, 2, 30, -110, -54); - } - Serial.print(r); Serial.println(F(" dBm")); - - - - - + + } + + while (!fonaSS); + - */ - - //Watchdog.reset(); + byte net =0; + while (fona.getNetworkStatus() != 1) { + Serial.println(F("Waiting for network ...")); + delay(20000); + while ( !fonaSS); + net++; + if (net == 20) { + fonaSS.end(); + asm volatile ( "jmp 0"); + } } - //Watchdog.reset(); - delay(10000); // wait a few seconds to stabilize connection - //Watchdog.reset(); + + delay(20000); // wait a few seconds to stabilize connection + while (!fonaSS); fona.setGPRSNetworkSettings(apn, username, password); - delay(50); //Serial.println(F("Disabling GPRS")); fona.enableGPRS(false); - - //Watchdog.reset(); - delay(5000); // wait a few seconds to stabilize connection - //Watchdog.reset(); - + delay(2000); // wait a few seconds to stabilize connection Serial.println(F("Enabling GPRS")); - while (!fona.enableGPRS(true)) { - delay(50); - Serial.println(F("Failed to turn GPRS on")); - delay(10); - + while (!fonaSS); + if (!fona.enableGPRS(true)) { + Serial.println(F("Failed to turn GPRS on")); + delay(50); return false; } - //Watchdog.reset(); + fona.enableRTC(1); return true; } - boolean GPS() { + if (gps_enabled == 0 ) { Serial.println(F("Enabling GPS")); - delay(10); - if (!fona.enableGPS(true)) { + delay(50); + while ( !fonaSS); + fona.enableGPS(false); + delay(2000); + while ( !fonaSS); + if(!fona.enableGPS(true)) { Serial.println(F("Failed to turn GPS on")); - delay(10); - + delay(50); return false; - } - else { + } else { gps_enabled = 1; + delay(20000); } } - - //Watchdog.reset(); - delay(5000); // wait a few seconds to stabilize connection - - - //Watchdog.reset(); - byte stat; + while ( !fonaSS); stat = fona.GPSstatus(); - - if (&stat < 0 or &stat == 0 or &stat == 1) { + delay(1000); + if (stat < 0 or stat == 0 or stat == 1) { Serial.println(F("Not fixed!")); - delay(10); - + delay(50); return false; //delay(5000); - } - if (stat >= 2) { + } + if (stat >= 2) { Serial.println(F("GPS Fixed")); - delay(10); - } - - //Watchdog.reset(); + delay(50); + return true; + } else { + return false; + } - return true; +return false; } void GPS_Data(float *fdata, int *idata) { + + while ( !fonaSS); + while (fona.GPSstatus() < 2) { + fona.enableGPS(false); + delay(1000); + while ( !fonaSS); + fona.enableGPS(true); + delay(10000); + byte checking=0; + do{ + Serial.println(F("Not fixed!")); + delay(2000); + checking++; + while ( !fonaSS); + } while (fona.GPSstatus() < 2 or checking == 10); + + } + char utime[14]; - int YY; - int MM; - int DD; - int hh; - int mm; - int ss; - //float latitude, longitude, speed_kph, heading, altitude; - boolean gps_success = fona.getGPS(&fdata[0], &fdata[1], &fdata[3], &fdata[2], &fdata[4], &utime[0]); - delay(20); - sscanf(utime,"%04d%02d%02d%02d%02d%02d",&YY,&MM,&DD,&hh,&mm,&ss); - //sscanf(utime,"%04d%02d%02d%02d%02d%02d",&idata[5],&idata[4],&idata[3],&idata[0],&idata[1],&idata[2]); - delay(20); - idata[0] = hh; - idata[1] = mm; - idata[2] = ss; - idata[3] = DD; - idata[4] = MM; - idata[5] = YY; - - delay(20); + //int YY; + //int MM; + //int DD; + //int hh; + //int mm; + //int ss; + float latitude, longitude, speed_kph,cog; + //, speed_kph, heading, altitude; + //boolean gps_success = fona.getGPS(&fdata[0], &fdata[1], &fdata[3], &fdata[2], &fdata[4], &utime[0]); + boolean gps_success = fona.getGPS(&latitude,&longitude, &fdata[3], &speed_kph, &cog, &utime[0]); + delay(50); + //sscanf(utime,"%04d%02d%02d%02d%02d%02d",&YY,&MM,&DD,&hh,&mm,&ss); + sscanf(utime,"%04d%02d%02d%02d%02d%02d",&idata[5],&idata[4],&idata[3],&idata[0],&idata[1],&idata[2]); + delay(50); + //idata[0] = hh; + //idata[1] = mm; + //idata[2] = ss; + //idata[3] = DD; + //idata[4] = MM; + //idata[5] = YY; + if (&gps_success) { - + fdata[0]=latitude; + fdata[1]=longitude; + fdata[2]=speed_kph; + fdata[4]=cog; //sendATcommand("AT+CGATT=1",1000); + delay(50); - return &fdata; } From aa553196138f88be093996806d9b2f17ea98a486 Mon Sep 17 00:00:00 2001 From: roblad Date: Tue, 15 Nov 2016 19:25:27 +0100 Subject: [PATCH 19/26] _SS_MAX_RX_BUFF to 128 --- arduino/gps-tracker2/config.h | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/arduino/gps-tracker2/config.h b/arduino/gps-tracker2/config.h index 41c54bb..01c87b7 100644 --- a/arduino/gps-tracker2/config.h +++ b/arduino/gps-tracker2/config.h @@ -7,31 +7,27 @@ * * */ - +//soft serial buffer +#define _SS_MAX_RX_BUFF 128 #define GSM_APN "internet" #define GSM_USERNAME "" #define GSM_PASSWORD "" -//#define SUBSCRIPTIONDATALEN 122 - - //#define AIO_SERVER "m20.cloudmqtt.com" - //#define AIO_SERVER "broker.mqttdashboard.com" //#define AIO_SERVER "broker.hivemq.com" #define AIO_SERVER "155.133.46.194" #define AIO_SERVERPORT 1883 +#define AIO_SERVER "155.133.46.194" +#define AIO_SERVERPORT 1883 //#define AIO_SERVERPORT 11971 //#define AIO_SERVERPORT 10429 #define AIO_USERNAME "xxxxxxxxxxx" #define AIO_PASSWORD "xxxxxxxxxxx" -//#define AIO_USERNAME "gosecwhh" -//#define AIO_PASSWORD "OQoeB0JUxk22" - #define TOPIC "owntracks/piesek/yasmina" #define TID "yb" // "tracker-ID which is used by the auto-faces feature to display, say, initials of a user." -#define SPEED_TR 2 // bellow that number = 0 -#define HEARTBIT 220 // there ic multiply by DELAY and sending heartbit - set for every 30 min when movement trying is 5 sec. -#define DELAY 7 // seconds +#define SPEED_TR 3 // bellow that number = 0 +#define HEARTBIT 120 // there ic multiply by DELAY and sending heartbit - set for every 30 min when movement trying is 5 sec. +#define DELAY 13 // seconds +2 From fc04699a0160f48056546767cce52e2e4e8338bd Mon Sep 17 00:00:00 2001 From: roblad Date: Wed, 16 Nov 2016 10:33:21 +0100 Subject: [PATCH 20/26] cleaning and changes for reseting - quicker --- arduino/gps-tracker2/gps-tracker2.ino | 62 +++++++++++++-------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/arduino/gps-tracker2/gps-tracker2.ino b/arduino/gps-tracker2/gps-tracker2.ino index 0dc9788..7664a2c 100644 --- a/arduino/gps-tracker2/gps-tracker2.ino +++ b/arduino/gps-tracker2/gps-tracker2.ino @@ -27,13 +27,13 @@ #include "Adafruit_MQTT_FONA.h" #include "ArduinoJson.h" #include +/*************************** FONA Params ***********************************/ #define FONA_DEFAULT_TIMEOUT_MS 750 #define MQTT_CONN_KEEPALIVE 1800 #define MQTT_FONA_INTERAVAILDELAY 200 #define MQTT_FONA_QUERYDELAY 750 /*************************** FONA Pins ***********************************/ -// GSm pins #define GSM_RX 2 #define GSM_TX 3 #define GSM_RST 4 // pin number 16 in sim808 throug diode to ground https://cdn-shop.adafruit.com/datasheets/SIM808_Hardware+Design_V1.00.pdf max 4.3 V @@ -48,14 +48,8 @@ uint16_t reporter = 0; uint32_t logCounter = 0; char geodata[125]; -//char *geodatapoint = geodata; -/************ Global State (you don't need to change this!) ******************/ -//const char MQTT_SERVER[] PROGMEM = AIO_SERVER; -//const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME; -//const char MQTT_PASSWORD[] PROGMEM = AIO_PASSWORD; -//const char MQTT_TOPIC[] PROGMEM = TOPIC; -//const char PHONE_GSM_APN[] PROGMEM = GSM_APN; +/************ Global State (you don't need to change this!) ******************/ //#define MQTT_DEBUG // Setup the FONA MQTT class by passing in the FONA class and MQTT server and login details. @@ -73,7 +67,7 @@ void(* resetFunc) (void) = 0; // Setup a feed called 'photocell' for publishing. Adafruit_MQTT_Publish feed = Adafruit_MQTT_Publish(&mqtt,TOPIC); -// Adjust as necessary, in seconds. Default to 5 minutes. + /*************************** Sketch Code ************************************/ @@ -88,7 +82,11 @@ void setup() { while (!fonaSS); delay(20); fona.begin(fonaSS); - + delay(500); + while (!fonaSS); + clearbuffer(); + while (!Serial); + Serial.println(F("GPS Tracker starting ...")); delay(20000); // wait a few seconds to stabilize connection @@ -105,14 +103,15 @@ void setup() { delay(10000); while ( !fonaSS); byte trying =0; - while (! GPS() or trying == 10) { + while (! GPS() ) { Serial.println(F("Retrying FONA GPS ...")); //Serial.println(trying); delay(15000); while ( !fonaSS); trying++; + if (trying == 15)resetFunc(); } - if (trying == 10)resetFunc(); + Serial.println(F("Connected to Cellular!")); @@ -172,7 +171,6 @@ if (readytosend == 1) { check = 3; readytosend = 0; - //delete[] geodata; } } while (check < 3); @@ -190,7 +188,7 @@ if (readytosend == 1) { delay(100); while (!fonaSS); if (!fona.TCPconnected()){ - return; + break; } /*if (mqtt.ping()){ delay(1000); @@ -214,7 +212,7 @@ if (readytosend == 1) { Serial.println(F("No GPS fixed or GPRS connected, waiting for restart ...")); delay(1000); while ( !fonaSS); - if( fona.GPRSstate() != 1 or fona.getRSSI() < 6){ + if( fona.GPRSstate() != 1 ){ Serial.println(F("No network !!! traing to bring up ...")); delay(50); //asm volatile ( "jmp 0"); @@ -226,26 +224,21 @@ if (readytosend == 1) { while (!fonaSS); if (fona.GPSstatus() <= 1){ Serial.println(F("Restarting FONA GPS - not fixed ...")); - delay(50); + delay(50); byte retry=0; - while (! GPS() or retry == 10) { + while (! GPS() ) { Serial.println(F("Trying up FONA GPS ...")); - delay(10000); + delay(15000); while (!fonaSS); retry++; - } - - delay(5000); - while (!fonaSS); - if (fona.GPSstatus() <= 1) { - fonaSS.end(); - resetFunc();//asm volatile ( "jmp 0"); - } - + if (retry == 10) { + fonaSS.end(); + resetFunc();//asm volatile ( "jmp 0"); + } + } } - //OCR0A = 0xAF; - //TIMSK0 |= _BV(OCIE0A); + } @@ -280,6 +273,7 @@ void MQTT_connect() { Serial.println(F("Connecting to MQTT ... ")); delay(50); while (!fonaSS); + byte test=0; while ((ret = mqtt.connect()) != 0 ) { // connect will return 0 for connected delay(50); //Serial.println(mqtt.connectErrorString(ret)); @@ -291,6 +285,12 @@ void MQTT_connect() { //mqtt.connect(); delay(2000); // wait 5 seconds while (!fonaSS); + test ++; + if (test == 20) { + fonaSS.end(); + resetFunc();//asm volatile ( "jmp 0"); + } + } // Configure timer0 compare interrupt to run and decrease the log counter every millisecond. @@ -351,7 +351,7 @@ void prepareData() { delay(50); root.printTo(geodata, 125); delay(50); - //delete[] &jsonBuffer,&uxdate,&vbat,root,&ctime; + //delete[] &jsonBuffer; //sprintf((gps_data[0]),%f, movement_W); if ((((gps_data[0]) - lastmovement_W ) > float(0.0010000)) or (((gps_data[1]) - lastmovement_L ) > float(0.0010000)) or reporter == HEARTBIT) { reporter=0; @@ -405,7 +405,7 @@ void prepareData() { Serial.print(F(" Times checking: ")); delay(50); Serial.print(HEARTBIT); - Serial.print(F(" before reporting heartbit, waiting: ")); + Serial.print(F(" before reporting heartbit, now is: ")); delay(50); Serial.println(reporter); Serial.println(); From 199ad867cb29217ac6bf00118ba8a25b18d69389 Mon Sep 17 00:00:00 2001 From: roblad Date: Wed, 16 Nov 2016 10:34:18 +0100 Subject: [PATCH 21/26] cleaning - smal changes --- arduino/gps-tracker2/fonahelper.cpp | 81 ++++++----------------------- 1 file changed, 17 insertions(+), 64 deletions(-) diff --git a/arduino/gps-tracker2/fonahelper.cpp b/arduino/gps-tracker2/fonahelper.cpp index 3998927..2e0de1e 100644 --- a/arduino/gps-tracker2/fonahelper.cpp +++ b/arduino/gps-tracker2/fonahelper.cpp @@ -14,54 +14,6 @@ extern SoftwareSerial fonaSS; byte gps_enabled = 0; -//send AT command and wait for responce and print -/* - -uint8_t sendATcommand(char* ATcommand,unsigned int *timeout){ - - uint8_t x=0, answer=0; - char response[20]; - unsigned long previous; - - while( fonaSS.available() > 0) fonaSS.read(); // Clean the input buffer - fonaSS.flush(); - if (ATcommand[0] != '\0') - { - fonaSS.println(ATcommand); // Send the AT command - delay(20); - } - - - x = 0; - previous = millis(); - Serial.println(); - Serial.print(F("Execute AT command: ")); - delay(50); - Serial.println(ATcommand); - - Serial.print(F("Result: ")); - delay(50); - // this loop waits for the answer - do{ - if(fonaSS.available() > 0){ // if there are data in the UART input buffer, reads it and checks for the asnwer - response[x] = fonaSS.read(); - - Serial.print(response[x]); - //delay(20); - x++; - answer = 1; - } else { - answer = 0; - } - }while(((millis() - previous) < &timeout)); // Waits for the asnwer with time out - //delete buffer - freeup memory - delete[] response,previous,timeout; - return answer; - -} - -*/ - boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *username, const __FlashStringHelper *password) { @@ -76,8 +28,7 @@ boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *u return false; } else { - - + // to test } while (!fonaSS); @@ -159,21 +110,23 @@ return false; void GPS_Data(float *fdata, int *idata) { while ( !fonaSS); - while (fona.GPSstatus() < 2) { - fona.enableGPS(false); - delay(1000); - while ( !fonaSS); - fona.enableGPS(true); - delay(10000); - byte checking=0; - do{ - Serial.println(F("Not fixed!")); - delay(2000); - checking++; + delay(100); + if (fona.GPSstatus() < 2) { + byte retry = 0; + while (! GPS()) { + Serial.println(F("Retrying FONA GPS ...")); + delay(15000); while ( !fonaSS); - } while (fona.GPSstatus() < 2 or checking == 10); - - } + retry++; + if (retry == 10) { + fonaSS.end(); + asm volatile ( "jmp 0"); + } + + } + + + } char utime[14]; //int YY; From 665ff66720bbc892999cd72c57c5e215546e2964 Mon Sep 17 00:00:00 2001 From: roblad Date: Wed, 16 Nov 2016 10:35:14 +0100 Subject: [PATCH 22/26] delay change --- arduino/gps-tracker2/config.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/arduino/gps-tracker2/config.h b/arduino/gps-tracker2/config.h index 01c87b7..f17acde 100644 --- a/arduino/gps-tracker2/config.h +++ b/arduino/gps-tracker2/config.h @@ -29,5 +29,6 @@ #define TOPIC "owntracks/piesek/yasmina" #define TID "yb" // "tracker-ID which is used by the auto-faces feature to display, say, initials of a user." #define SPEED_TR 3 // bellow that number = 0 -#define HEARTBIT 120 // there ic multiply by DELAY and sending heartbit - set for every 30 min when movement trying is 5 sec. -#define DELAY 13 // seconds +2 +#define SPEED_TR 3 // bellow that number = 0 +#define HEARTBIT 90 // there ic multiply by DELAY and sending heartbit - set for every 30 min when movement trying is 5 sec. +#define DELAY 13 // seconds + ~6 From 50b5b48da749a364cfdb58ee00181f1fd3485ccb Mon Sep 17 00:00:00 2001 From: roblad Date: Sat, 26 Nov 2016 15:25:43 +0100 Subject: [PATCH 23/26] final version --- arduino/gps-tracker2/config.h | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/arduino/gps-tracker2/config.h b/arduino/gps-tracker2/config.h index f17acde..5f33fb6 100644 --- a/arduino/gps-tracker2/config.h +++ b/arduino/gps-tracker2/config.h @@ -8,7 +8,8 @@ * */ //soft serial buffer -#define _SS_MAX_RX_BUFF 128 +#define _SS_MAX_RX_BUFF 128 +//gsm #define GSM_APN "internet" #define GSM_USERNAME "" #define GSM_PASSWORD "" @@ -18,17 +19,14 @@ //#define AIO_SERVER "m20.cloudmqtt.com" //#define AIO_SERVER "broker.mqttdashboard.com" //#define AIO_SERVER "broker.hivemq.com" -#define AIO_SERVER "155.133.46.194" +#define AIO_SERVER "xxx.xxx.xxx.xxx" #define AIO_SERVERPORT 1883 -#define AIO_SERVER "155.133.46.194" -#define AIO_SERVERPORT 1883 -//#define AIO_SERVERPORT 11971 -//#define AIO_SERVERPORT 10429 -#define AIO_USERNAME "xxxxxxxxxxx" -#define AIO_PASSWORD "xxxxxxxxxxx" +#define AIO_USERNAME "piesek" +#define AIO_PASSWORD "xxxxxxxxx" #define TOPIC "owntracks/piesek/yasmina" #define TID "yb" // "tracker-ID which is used by the auto-faces feature to display, say, initials of a user." #define SPEED_TR 3 // bellow that number = 0 -#define SPEED_TR 3 // bellow that number = 0 -#define HEARTBIT 90 // there ic multiply by DELAY and sending heartbit - set for every 30 min when movement trying is 5 sec. -#define DELAY 13 // seconds + ~6 +#define HEARTBIT 120 // there ic multiply by DELAY and sending heartbit - set for every 30 min when movement trying is 5 sec. +#define DELAY 10 // seconds + ~5 +#define MOVEMENT 50.0 //metters moved to report +//#define DEBUG From 0caa351001fa780bad827f96d3a5307d2a1ad4b7 Mon Sep 17 00:00:00 2001 From: roblad Date: Sat, 26 Nov 2016 15:26:49 +0100 Subject: [PATCH 24/26] final version --- arduino/gps-tracker2/fonahelper.cpp | 79 ++++++++++++++++------------- 1 file changed, 45 insertions(+), 34 deletions(-) diff --git a/arduino/gps-tracker2/fonahelper.cpp b/arduino/gps-tracker2/fonahelper.cpp index 2e0de1e..eee3130 100644 --- a/arduino/gps-tracker2/fonahelper.cpp +++ b/arduino/gps-tracker2/fonahelper.cpp @@ -1,7 +1,17 @@ -#include "config.h" +include "config.h" #include "Adafruit_FONA.h" #include -#define FONA_DEFAULT_TIMEOUT_MS 750 + + +/* +//prog mem helper - change buffer size for topic if needed +const char stringBuffer[30]; +const char* getString(const char* str[30]) { +strcpy_P(stringBuffer, (const char*)str); +delay(50); +return stringBuffer; +} +*/ extern Adafruit_FONA fona; @@ -18,13 +28,16 @@ boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *u while (!fonaSS); - fonaSS.begin(4800); - delay(20); + fonaSS.begin(9800); + delay(50); if (! fona.begin(fonaSS)) { // can also try fona.begin(Serial1) + delay(50); +#ifdef DEBUG Serial.println(F("Couldn't find FONA")); delay(50); +#endif return false; } else { @@ -36,8 +49,11 @@ boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *u byte net =0; while (fona.getNetworkStatus() != 1) { + delay(500); +#ifdef DEBUG Serial.println(F("Waiting for network ...")); - delay(20000); +#endif + delay(15000); while ( !fonaSS); net++; if (net == 20) { @@ -47,20 +63,26 @@ boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *u } - delay(20000); // wait a few seconds to stabilize connection - while (!fonaSS); + //delay(20000); // wait a few seconds to stabilize connection + fona.setGPRSNetworkSettings(apn, username, password); //Serial.println(F("Disabling GPRS")); + while (!fonaSS); fona.enableGPRS(false); - delay(2000); // wait a few seconds to stabilize connection + delay(500); // wait a few seconds to stabilize connection +#ifdef DEBUG Serial.println(F("Enabling GPRS")); + delay(50); +#endif while (!fonaSS); if (!fona.enableGPRS(true)) { +#ifdef DEBUG Serial.println(F("Failed to turn GPRS on")); delay(50); +#endif return false; } - + while (!fonaSS); fona.enableRTC(1); return true; @@ -69,35 +91,42 @@ boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *u boolean GPS() { if (gps_enabled == 0 ) { +#ifdef DEBUG Serial.println(F("Enabling GPS")); delay(50); +#endif while ( !fonaSS); fona.enableGPS(false); - delay(2000); + delay(500); while ( !fonaSS); if(!fona.enableGPS(true)) { +#ifdef DEBUG Serial.println(F("Failed to turn GPS on")); - delay(50); + delay(50); +#endif return false; } else { gps_enabled = 1; - delay(20000); + delay(10000); } } byte stat; while ( !fonaSS); stat = fona.GPSstatus(); - delay(1000); + delay(500); if (stat < 0 or stat == 0 or stat == 1) { - +#ifdef DEBUG Serial.println(F("Not fixed!")); delay(50); +#endif return false; - //delay(5000); + } if (stat >= 2) { +#ifdef DEBUG Serial.println(F("GPS Fixed")); delay(50); +#endif return true; } else { return false; @@ -109,25 +138,6 @@ return false; void GPS_Data(float *fdata, int *idata) { - while ( !fonaSS); - delay(100); - if (fona.GPSstatus() < 2) { - byte retry = 0; - while (! GPS()) { - Serial.println(F("Retrying FONA GPS ...")); - delay(15000); - while ( !fonaSS); - retry++; - if (retry == 10) { - fonaSS.end(); - asm volatile ( "jmp 0"); - } - - } - - - } - char utime[14]; //int YY; //int MM; @@ -138,6 +148,7 @@ void GPS_Data(float *fdata, int *idata) { float latitude, longitude, speed_kph,cog; //, speed_kph, heading, altitude; //boolean gps_success = fona.getGPS(&fdata[0], &fdata[1], &fdata[3], &fdata[2], &fdata[4], &utime[0]); + while (!fonaSS); boolean gps_success = fona.getGPS(&latitude,&longitude, &fdata[3], &speed_kph, &cog, &utime[0]); delay(50); //sscanf(utime,"%04d%02d%02d%02d%02d%02d",&YY,&MM,&DD,&hh,&mm,&ss); From 7a3cb3223c0f5bb6ca93bb352303a7807b450e86 Mon Sep 17 00:00:00 2001 From: roblad Date: Sat, 26 Nov 2016 15:27:37 +0100 Subject: [PATCH 25/26] final version --- arduino/gps-tracker2/gps-tracker2.ino | 290 +++++++++++++++++--------- 1 file changed, 192 insertions(+), 98 deletions(-) diff --git a/arduino/gps-tracker2/gps-tracker2.ino b/arduino/gps-tracker2/gps-tracker2.ino index 7664a2c..9e23c0e 100644 --- a/arduino/gps-tracker2/gps-tracker2.ino +++ b/arduino/gps-tracker2/gps-tracker2.ino @@ -27,11 +27,18 @@ #include "Adafruit_MQTT_FONA.h" #include "ArduinoJson.h" #include + /*************************** FONA Params ***********************************/ -#define FONA_DEFAULT_TIMEOUT_MS 750 -#define MQTT_CONN_KEEPALIVE 1800 -#define MQTT_FONA_INTERAVAILDELAY 200 -#define MQTT_FONA_QUERYDELAY 750 + +//#undef FONA_DEFAULT_TIMEOUT_MS +//#define FONA_DEFAULT_TIMEOUT_MS 600 +//#undef MQTT_CONN_KEEPALIVE +//#define MQTT_CONN_KEEPALIVE 1800 +//#undef MQTT_FONA_INTERAVAILDELAY +//#define MQTT_FONA_INTERAVAILDELAY 200 +//#undef MQTT_FONA_QUERYDELAY +//#define MQTT_FONA_QUERYDELAY 600 + /*************************** FONA Pins ***********************************/ #define GSM_RX 2 @@ -45,12 +52,12 @@ Adafruit_FONA fona = Adafruit_FONA(GSM_RST); int readytosend = 0; uint16_t reporter = 0; -uint32_t logCounter = 0; char geodata[125]; + /************ Global State (you don't need to change this!) ******************/ - +//const __FlashStringHelper *MQTT_TOPIC; //#define MQTT_DEBUG // Setup the FONA MQTT class by passing in the FONA class and MQTT server and login details. Adafruit_MQTT_FONA mqtt(&fona,AIO_SERVER,AIO_SERVERPORT,AIO_USERNAME,AIO_PASSWORD); @@ -64,12 +71,10 @@ boolean GPS(); //reset void(* resetFunc) (void) = 0; /****************************** Feeds ***************************************/ - // Setup a feed called 'photocell' for publishing. Adafruit_MQTT_Publish feed = Adafruit_MQTT_Publish(&mqtt,TOPIC); - /*************************** Sketch Code ************************************/ @@ -77,26 +82,34 @@ void setup() { Serial.begin(115200); while (!Serial); - delay(20); - fonaSS.begin(4800); + fonaSS.begin(9600); while (!fonaSS); - delay(20); + //delay(20); + //fonaSS.println("ATZ"); fona.begin(fonaSS); - delay(500); - while (!fonaSS); - clearbuffer(); - while (!Serial); - + delay(100); + //while (!fonaSS); + //fonaSS.println("AT&F1"); + //delay(500); + //fonaSS.println("AT&W0"); + //delay(500); + //clearbuffer(); +#ifdef DEBUG Serial.println(F("GPS Tracker starting ...")); - - delay(20000); // wait a few seconds to stabilize connection +#endif +#ifndef DEBUG + Serial.println(F("Debug is off")); +#endif + + delay(10000); // wait a few seconds to stabilize connection while ( !fonaSS); // Initialise the FONA module while (! FONAconnect(F(GSM_APN), F(GSM_USERNAME), F(GSM_PASSWORD))) { - +#ifdef DEBUG Serial.println(F("Retrying FONA GPRS ...")); - delay(20000); +#endif + delay(15000); while ( !fonaSS); } @@ -104,16 +117,17 @@ void setup() { while ( !fonaSS); byte trying =0; while (! GPS() ) { +#ifdef DEBUG Serial.println(F("Retrying FONA GPS ...")); - //Serial.println(trying); +#endif delay(15000); while ( !fonaSS); trying++; - if (trying == 15)resetFunc(); + if (trying == 20)resetFunc(); } - +#ifdef DEBUG Serial.println(F("Connected to Cellular!")); - +#endif delay(10000); // wait a few seconds to stabilize connection @@ -124,7 +138,6 @@ void setup() { void loop() { while ( !fonaSS); - delay(50); fonaSS.flush(); delay(50); Serial.flush(); @@ -134,106 +147,117 @@ void loop() { //-------------main function not generate when no GPS, GPRS, GSM do{ - clearbuffer(); + //clearbuffer(); + delay(DELAY*1000); // wait a few seconds to stabilize connection while (!fonaSS); fona.deleteSMS(1); - + delay(500); +#ifdef DEBUG Serial.println(F("Network avaliable GPS fixed GPRS connected, processing ...")); delay(50); - clearbuffer(); - +#endif + //clearbuffer(); prepareData(); - delay(100); - + if (readytosend == 1) { - clearbuffer(); + //clearbuffer(); MQTT_connect(); - delay(50); + //delay(50); // Now we can publish stuff! - clearbuffer(); - byte check =0; + //clearbuffer(); + int check =3; do { while (!fonaSS); + //clearbuffer(); if (! feed.publish(geodata)) { +#ifdef DEBUG Serial.println(F("Sent Failed")); - delay(1000); - check++; - + delay(50); +#endif + check--; + //clearbuffer(); + MQTT_connect(); + if (check == 0) return; + delay(1000); } else { +#ifdef DEBUG Serial.println(F("Sent OK!")); delay(50); - +#endif //Serial.println(freeMemory()); - check = 3; + check = 0; readytosend = 0; - + if (check == 0) return; + delay(1000); } - } while (check < 3); + } while (check != 0); } else { - - Serial.println(F("No movement of 10 meters waiting ...")); +#ifdef DEBUG + Serial.print(F("No movement of ")); + Serial.print(MOVEMENT); + Serial.println(F(" meters waiting ...")); delay(50); +#endif readytosend = 0; - delay(50); + while ( !fonaSS); mqtt.ping(3); - delay(100); + delay(50); while (!fonaSS); if (!fona.TCPconnected()){ - break; + return; } - /*if (mqtt.ping()){ - delay(1000); - Serial.println(F("MQTT server avaliable")); - }else{ - delay(500); - Serial.println(F("No MQTT server !!!")); - } - */ } - clearbuffer(); - + //clearbuffer(); + - delay(DELAY*1000); // wait a few seconds to stabilize connection + while ( !fonaSS); } while(fonaSS && fona.GPRSstate() == 1 && fona.GPSstatus() > 1 ); - +#ifdef DEBUG Serial.println(F("No GPS fixed or GPRS connected, waiting for restart ...")); +#endif delay(1000); while ( !fonaSS); if( fona.GPRSstate() != 1 ){ +#ifdef DEBUG Serial.println(F("No network !!! traing to bring up ...")); delay(50); +#endif //asm volatile ( "jmp 0"); //albo tak ale reset przed setup void(* resetFunc) (void) = 0; wywolanie resetFunc(); fonaSS.end(); - resetFunc(); + resetFunc(); //reset } while (!fonaSS); if (fona.GPSstatus() <= 1){ +#ifdef DEBUG Serial.println(F("Restarting FONA GPS - not fixed ...")); delay(50); +#endif byte retry=0; while (! GPS() ) { +#ifdef DEBUG Serial.println(F("Trying up FONA GPS ...")); - delay(15000); +#endif + delay(10000); while (!fonaSS); retry++; if (retry == 10) { fonaSS.end(); - resetFunc();//asm volatile ( "jmp 0"); + resetFunc();//asm volatile ( "jmp 0"); //reset } } } @@ -242,9 +266,11 @@ if (readytosend == 1) { } +/* void clearbuffer(){ // Read all available serial input to flush pending data. uint16_t timeoutloop = 0; + while (!fonaSS); fonaSS.peek(); while (timeoutloop++ < 60) { @@ -254,7 +280,9 @@ void clearbuffer(){ } delay(1); } + } +*/ // Function to connect and reconnect as necessary to the MQTT server. // Should be called in the loop function and it will take care if connecting. @@ -269,21 +297,32 @@ void MQTT_connect() { return; } - +#ifdef DEBUG Serial.println(F("Connecting to MQTT ... ")); delay(50); +#endif while (!fonaSS); byte test=0; while ((ret = mqtt.connect()) != 0 ) { // connect will return 0 for connected + /* + switch (ret) { + case 1: Serial.println(F("Wrong protocol")); break; + case 2: Serial.println(F("ID rejected")); break; + case 3: Serial.println(F("Server unavail")); break; + case 4: Serial.println(F("Bad user/pass")); break; + case 5: Serial.println(F("Not authed")); break; + case 6: Serial.println(F("Failed to subscribe")); break; + default: Serial.println(F("Connection failed")); break; + } + */ delay(50); //Serial.println(mqtt.connectErrorString(ret)); mqtt.disconnect(); - - Serial.println(F("Retrying MQTT connection in 2 seconds ...")); - delay(50); - //mqtt.connect(); - delay(2000); // wait 5 seconds +#ifdef DEBUG + Serial.println(F("Retrying MQTT connection in 5 seconds ...")); +#endif + delay(5000); // wait 2 seconds while (!fonaSS); test ++; if (test == 20) { @@ -292,26 +331,26 @@ void MQTT_connect() { } } - // Configure timer0 compare interrupt to run and decrease the log counter every millisecond. - +#ifdef DEBUG Serial.println(F("MQTT Connected!")); delay(50); - - +#endif } void prepareData() { - static float lastmovement_W = 0; - static float lastmovement_L = 0; +static float lastmovement_W = 0; +static float lastmovement_L = 0; + int vbat; int ctime[6]; float gps_data[5]; while (!fonaSS); if (! fona.getBattVoltage(&vbat)) vbat = 0; delay(50); - void GPS_Data(float *fdata, int *idata); + while (!fonaSS); + void GPS_Data(float *fdata, int *idata); //GPS_Data(gps_data, ctime); while (!fonaSS); GPS_Data(&gps_data[0], &ctime[0]); @@ -327,21 +366,33 @@ void prepareData() { // {"tst":1476474031,"acc":1000,"_type":"location","alt":140,"lon":-90.48259734672334,"vac":10,"p":100.160530090332,"lat":38.75410327670748,"batt":100,"tid":"JT"} // Owntracks API: // http://owntracks.org/booklet/tech/json/ - - - StaticJsonBuffer<140> jsonBuffer; + float converter[2]; + converter[0]=gps_data[0]; + converter[1]=gps_data[1]; + + float movement = 0; + movement = distanceCoordinates(converter[0],converter[1],lastmovement_W,lastmovement_L); + if ((int)movement > 1000 or (int)movement < 0 ) movement = MOVEMENT + 1.0; //1000 means more than 200 km/h, it is added before first initialosation of last loc. + StaticJsonBuffer<125> jsonBuffer; //memset(jsonBuffer, 0, sizeof(jsonBuffer)); //delay(50); JsonObject& root = jsonBuffer.createObject(); delay(100); + + root.set("_type","location"); - root.set("acc",3); + root.set("acc",int(movement)); root.set("batt",vbat); root.set("cog",int(gps_data[4])); delay(2); - root.set("lat",(gps_data[0]),7); + root.set("lat",converter[0],7); delay(2); - root.set("lon",(gps_data[1]),7); + root.set("lon",converter[1],7); + + //root.set("lat",gps_data[0],7); + //delay(2); + //root.set("lon",gps_data[1],7); + root.set("tid",TID); root.set("vel",(int(gps_data[2]) <= SPEED_TR ? 0 : int(gps_data[2]))); delay(50); @@ -351,9 +402,12 @@ void prepareData() { delay(50); root.printTo(geodata, 125); delay(50); - //delete[] &jsonBuffer; - //sprintf((gps_data[0]),%f, movement_W); - if ((((gps_data[0]) - lastmovement_W ) > float(0.0010000)) or (((gps_data[1]) - lastmovement_L ) > float(0.0010000)) or reporter == HEARTBIT) { + + + //if ((((gps_data[0]) - lastmovement_W ) > float(0.0010000)) or (((gps_data[1]) - lastmovement_L ) > float(0.0010000)) or reporter == HEARTBIT) { + // if (((converter[0] - lastmovement_W ) > float(0.0010000)) or ((converter[1] - lastmovement_L ) > float(0.0010000)) or reporter == HEARTBIT) { + + if ( movement > MOVEMENT or reporter == HEARTBIT){ reporter=0; readytosend = 1; @@ -375,23 +429,34 @@ void prepareData() { //Serial.println(lon, 7); //} + lastmovement_W = converter[0]; + lastmovement_L = converter[1]; + - delay(100); - lastmovement_W = (gps_data[0]); - delay(50); - lastmovement_L = (gps_data[1]); - delay(50); +/* +lastmovement_W = gps_data[0]; +delay(50); +lastmovement_L = gps_data[1]; +delay(50); +*/ + + delete[] converter,movement,root; } else { readytosend = 0; // reporter every xxx min - position reported even not moved - ++reporter; + reporter++; + //simulator for test only + //float adding = random(0,2) * 0.0010000; + //lastmovement_W = converter[0] + adding; + //lastmovement_L = converter[1] + adding; + } - +#ifdef DEBUG Serial.println(); Serial.print(F(" Latitude OLD: ")); delay(50); @@ -402,17 +467,46 @@ void prepareData() { Serial.print(lastmovement_L,7); Serial.println(); Serial.println(); - Serial.print(F(" Times checking: ")); + Serial.print(F(" Seconds waiting: ")); delay(50); - Serial.print(HEARTBIT); - Serial.print(F(" before reporting heartbit, now is: ")); + Serial.print((HEARTBIT * (DELAY +5))); + Serial.print(F(" seconds before heartbit: ")); delay(50); - Serial.println(reporter); - Serial.println(); + Serial.println( ((HEARTBIT * (DELAY +5)) - (reporter *(DELAY +5)))); Serial.println(); - +#endif + delete[] converter,movement,root; } - +// Calculate distance between two points +//distanceCoordinates(latitude, longitude, initialLatitude, initialLongitude); +float distanceCoordinates(float flat1, float flon1, float flat2, float flon2) { + + // Variables + float dist_calc=0; + float dist_calc2=0; + float diflat=0; + float diflon=0; + + // Calculations + diflat = radians(flat2-flat1); + flat1 = radians(flat1); + flat2 = radians(flat2); + diflon = radians((flon2)-(flon1)); + + dist_calc = (sin(diflat/2.0)*sin(diflat/2.0)); + dist_calc2 = cos(flat1); + dist_calc2*=cos(flat2); + dist_calc2*=sin(diflon/2.0); + dist_calc2*=sin(diflon/2.0); + dist_calc +=dist_calc2; + dist_calc=(2*atan2(sqrt(dist_calc),sqrt(1.0-dist_calc))); + + dist_calc*=6371000.0; //Converting to meters + + return dist_calc; +} + + From 9159afdd2787c057a3cbae14871a44818f1a64d7 Mon Sep 17 00:00:00 2001 From: roblad Date: Sat, 26 Nov 2016 17:27:11 +0100 Subject: [PATCH 26/26] Update fonahelper.cpp --- arduino/gps-tracker2/fonahelper.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arduino/gps-tracker2/fonahelper.cpp b/arduino/gps-tracker2/fonahelper.cpp index eee3130..c253bcf 100644 --- a/arduino/gps-tracker2/fonahelper.cpp +++ b/arduino/gps-tracker2/fonahelper.cpp @@ -1,4 +1,4 @@ -include "config.h" +#include "config.h" #include "Adafruit_FONA.h" #include