The program sends the current location over the cellular network.
Dependencies: aconno_I2C ublox-at-cellular-interface gnss ublox-cellular-base Lis2dh12 ublox-cellular-base-n2xx ublox-at-cellular-interface-n2xx low-power-sleep
Fork of example-gnss by
Revision 9:f943c09d9173, committed 2018-12-19
- Comitter:
- jurica238814
- Date:
- Wed Dec 19 15:12:25 2018 +0100
- Parent:
- 8:2bf886335fd0
- Commit message:
- Stable version
Changed in this revision
--- a/UBloxSara/uBloxSara.cpp Fri Nov 30 16:19:41 2018 +0100 +++ b/UBloxSara/uBloxSara.cpp Wed Dec 19 15:12:25 2018 +0100 @@ -24,8 +24,14 @@ /* Give modem a little time to settle down */ wait_ms(250); - printf("Powering up the modem...\r\n"); - //onboard_modem_power_up(); + if(!CUSTOM_BOARD) + { + // DO NOT CALL THIS IF YOU WORK ON ACONNO CUSTOM MADE BOARD + // POWER UP PIN (PE_14) IS CONNECTED ON OC DIGITAL OUTPUT + // AND POWER UP PIN IS NOT CONNECTED ON SARA MODULE + onboard_modem_power_up(); + } + wait_ms(5000); sendCommand("AT+CFUN=1"); @@ -122,26 +128,35 @@ sprintf(myCommandbuffer,"AT+NSOST=0,\"%s\",%d,%d,\"%s\"", _udp._ip, _udp._port, msgSize, data); - msgSize += strlen(flags); - sprintf(myCommandbuffer,"AT+NSOST=0,\"%s\",%d,%d,\"%s%s\"", - _udp._ip, _udp._port, msgSize, flagsHex, data); + //msgSize += strlen(flags); + //sprintf(myCommandbuffer,"AT+NSOST=0,\"%s\",%d,%d,\"%s%s\"", + // _udp._ip, _udp._port, msgSize, flagsHex, data); sendCommand(myCommandbuffer); } +#define SHORT_DELAY_MS (1000) uint8_t UBloxSara::connectNB() { sendCommand("at+NCONFIG=\"AUTOCONNECT\",\"FALSE\""); + wait_ms(SHORT_DELAY_MS); sendCommand("at+NCONFIG=\"CR_0354_0338_SCRAMBLING\",\"TRUE\""); - + wait_ms(SHORT_DELAY_MS); sendCommand("at+NCONFIG=\"CR_0859_SI_AVOID\",\"TRUE\""); + wait_ms(SHORT_DELAY_MS); sendCommand("at+NCONFIG?"); + wait_ms(SHORT_DELAY_MS); sendCommand("at+cfun=0"); + wait_ms(SHORT_DELAY_MS); sendCommand("AT+CGDCONT=1, \"IP\",\"nb.inetd.gdsp\""); + wait_ms(SHORT_DELAY_MS); sendCommand("at+cfun=1"); + wait_ms(SHORT_DELAY_MS); sendCommand("at+cimi"); + wait_ms(SHORT_DELAY_MS); sendCommand("at+cgatt=1"); + wait_ms(SHORT_DELAY_MS); sendCommand("at+cops=1,2,\"26202\""); wait_ms(5000); @@ -152,9 +167,11 @@ sendCommand("AT+COPS?"); sendCommand("AT+NBAND?"); sendCommand("AT+NBAND=20"); + wait_ms(SHORT_DELAY_MS); sendCommand("AT+NUESTATS"); - + wait_ms(SHORT_DELAY_MS); sendCommand("AT+CGATT?"); wait_ms(5000); sendCommand("AT+CGPADDR"); + wait_ms(SHORT_DELAY_MS); }
--- a/UBloxSara/uBloxSara.h Fri Nov 30 16:19:41 2018 +0100 +++ b/UBloxSara/uBloxSara.h Wed Dec 19 15:12:25 2018 +0100 @@ -10,6 +10,8 @@ #include "mbed.h" #include "udp.h" +#define CUSTOM_BOARD (1) + class UBloxSara { public:
--- a/aconnoConfig/aconnoConfig.h Fri Nov 30 16:19:41 2018 +0100 +++ b/aconnoConfig/aconnoConfig.h Wed Dec 19 15:12:25 2018 +0100 @@ -22,12 +22,15 @@ #define TARGET_STM (1) #define UDP_MSG_SIZE_B (50) +// Max wait time to get new location [s] +#define MAX_TIME_GNSS_WAIT_S (10) #define START_ALARM_S (1) #define STOP_ALARM_S (5) #define BUTTON_PRESSED_SIGNAL (0x12002300) #define ALARM_SIGNAL (0x12341230) #define ALARM_OFF_SIGNAL (0x12341231) #define IDLE_SIGNAL (0x12341232) +#define LOCATION_SEARCH_DONE (0x12341233) #define UPDATE_LOCATION_TIME_MS (5000)
--- a/aconnoHelpers/aconnoHelpers.cpp Fri Nov 30 16:19:41 2018 +0100 +++ b/aconnoHelpers/aconnoHelpers.cpp Wed Dec 19 15:12:25 2018 +0100 @@ -10,13 +10,24 @@ #include "gnss.h" #include "aconnoConfig.h" -bool getGPSData(char *location, GnssSerial *gnss) +bool getGPSData(char *location, GnssSerial *gnss, uint32_t timeoutS) { int gnssReturnCode; - int length; - char buffer[505]; + int length; + char buffer[505]; bool gotLocationFlag = false; - + int begin; + int end; + + if(!timeoutS) + { + // Max number of seconds + timeoutS -= 1; + } + + Timer timeoutTimer; + timeoutTimer.start(); + begin = timeoutTimer.read(); do { gnssReturnCode = gnss->getMessage(buffer, sizeof(buffer)); @@ -26,50 +37,32 @@ length = LENGTH(gnssReturnCode); if ((PROTOCOL(gnssReturnCode) == GnssParser::NMEA) && (length > 6)) { - // Talker is $GA=Galileo $GB=Beidou $GL=Glonass $GN=Combined $GP=GNSS + // Talker is $GA=Galileo $GB=Beidou $GL=Glonass + // $GN=Combined $GP=GNSS if ((buffer[0] == '$') || buffer[1] == 'G') { - if (CHECK_TALKER("GLL")) - { - + if (CHECK_TALKER("GLL")) + { + double latitude = 0, longitude = 0; char ch; if (gnss->getNmeaAngle(1, buffer, length, latitude) && gnss->getNmeaAngle(3, buffer, length, longitude) && - gnss->getNmeaItem(6, buffer, length, ch) && (ch == 'A')) { - sprintf(location, "GNSS: location is %.5f %.5f.", latitude, longitude); - //printf("I am here: https://maps.google.com/?q=%.5f,%.5f\r\n\r\n", - // latitude, longitude); + gnss->getNmeaItem(6, buffer, length, ch) && + (ch == 'A')) + { + sprintf(location, "%.5f %.5f", latitude, longitude); printf("Got location!\r\n"); gotLocationFlag = true; } } - else if (CHECK_TALKER("GGA")) - { - double altitude = 0; - const char *timeString = NULL; - // Altitude - if (gnss->getNmeaItem(9, buffer, length, altitude)) - { - printf("\r\nGNSS: altitude is %.1f m.\r\n", altitude); - } - // Time - timeString = gnss->findNmeaItemPos(1, buffer, buffer + length); - if (timeString != NULL) { - printf("\r\nGNSS: time is %.6s.\r\n\r\n", timeString); - } - } - else if (CHECK_TALKER("VTG")) - { - double speed = 0; - // Speed - if (gnss->getNmeaItem(7, buffer, length, speed)) - { - printf("\r\nGNSS: speed is %.1f km/h.\r\n\r\n", speed); - } - } } } } - }while(!gotLocationFlag); + end = timeoutTimer.read(); + } while (!gotLocationFlag && (end - begin) < timeoutS); + + printf("Location time: %d\r\n", (end - begin)); + return gotLocationFlag; } +
--- a/aconnoHelpers/aconnoHelpers.h Fri Nov 30 16:19:41 2018 +0100 +++ b/aconnoHelpers/aconnoHelpers.h Wed Dec 19 15:12:25 2018 +0100 @@ -25,6 +25,6 @@ */ void reverse(char *str); -bool getGPSData(char *location, GnssSerial *gnss); +bool getGPSData(char *location, GnssSerial *gnss, uint32_t timeoutS = 0); #endif //
--- a/main.cpp Fri Nov 30 16:19:41 2018 +0100 +++ b/main.cpp Wed Dec 19 15:12:25 2018 +0100 @@ -18,8 +18,10 @@ #define RED_LED_PIN (PE_3) #define GREEN_LED_PIN (PE_4) #define BLUE_LED_PIN (PE_1) +#define ALARM_OFF (0) +#define ALARM_ON (1) -MainStates state = STATE_IDLE; +volatile MainStates state = STATE_IDLE; /* File handler */ FileHandle *fh; @@ -30,12 +32,12 @@ char locationGlobal[UDP_MSG_SIZE_B]; bool gotGnssFlag = false; DigitalIn userButton(USER_BUTTON); -DigitalOut gnssPower(PE_0, 1); +DigitalOut gnssPower(PE_0, 0); DigitalOut redLed(RED_LED_PIN); DigitalOut blueLed(BLUE_LED_PIN); DigitalOut greenLed(GREEN_LED_PIN); -DigitalOut alarm(PE_14); +DigitalOut alarm(PE_14, 0); InterruptIn buttonInt(USER_BUTTON); static bool wasAccInt = false; @@ -47,7 +49,6 @@ Thread alarmState; Thread alarmOffState; Thread movementState; -Thread ledState; Thread gnssLocation; void my_bsp_init() @@ -55,7 +56,7 @@ redLed = 1; greenLed = 1; blueLed = 1; - alarm = 0; + alarm = ALARM_OFF; } void buttonRiseHandler(void) @@ -95,24 +96,13 @@ } } -void ledBlinky() -{ - while(!gotGnssFlag) - { - redLed = !redLed; - greenLed = !greenLed; - Thread::wait(1000); - } -} - int main() { bool success = false; MainStates nextState = STATE_IDLE; - - my_bsp_init(); buttonInt.rise(buttonRiseHandler); + my_bsp_init(); printf("Initialising UART for modem communication: "); fh = new UARTSerial(MDMTXD, MDMRXD, 9600); @@ -124,7 +114,7 @@ printf("...done\r\n"); gnssPower = 1; - ledState.start(ledBlinky); + wait_ms(500); if(gnss.init()) { @@ -132,7 +122,6 @@ getGPSData(locationGlobal, &gnss); printf("I have the location.\r\n"); gotGnssFlag = true; - ledState.terminate(); // Turn green led only greenLed = 0; redLed = 1; @@ -142,16 +131,16 @@ printf("Unable to initialise GNSS.\r\n"); } - greenLed = 1; - blueLed = 0; - Udp udp("52.215.10.12", 3334); UBloxSara sara(at, udp); printf("Initializing the modem: \r\n"); sara.setup(); + sara.connectNB(); printf("done...\r\n"); - sara.connectNB(); + alarm = ALARM_OFF; + greenLed = 1; + blueLed = 0; myParams_t myParams; myParams.gnss = &gnss; myParams.sara = &sara; @@ -195,13 +184,13 @@ } else { - alarm = 1; + alarm = ALARM_ON; } break; case STATE_ALARM_OFF: nextState = STATE_IDLE; idleState.signal_set(IDLE_SIGNAL); - alarm = 0; + alarm = ALARM_OFF; blueLed = 0; break; case STATE_LIS_DETECTION:
--- a/tasks/tasks.cpp Fri Nov 30 16:19:41 2018 +0100 +++ b/tasks/tasks.cpp Wed Dec 19 15:12:25 2018 +0100 @@ -10,8 +10,10 @@ #include "gnss.h" #include "aconnoConfig.h" -extern MainStates state; -extern char locationGlobal[UDP_MSG_SIZE_B]; +extern volatile MainStates state; +extern char locationGlobal[5*UDP_MSG_SIZE_B]; +extern Thread alarmState; +locationFlag_t locationFlagGlobal = OLD; void idleCallback() { @@ -29,17 +31,25 @@ void alarmCallback(myParams_t *myParams) { - //char udpMsg[UDP_MSG_SIZE_B]; + char udpMsg[UDP_MSG_SIZE_B]; while(1) { - Thread::signal_wait(ALARM_SIGNAL); - Thread::signal_clr(ALARM_SIGNAL); - printf("Alarm thread.\r\n"); while(state == STATE_ALARM) { + Thread::signal_wait(LOCATION_SEARCH_DONE); + Thread::signal_clr(LOCATION_SEARCH_DONE); + if(locationFlagGlobal) + { + sprintf(udpMsg, "$location_flag:%s;$type:NEW;",locationGlobal); + } + else + { + sprintf(udpMsg, "$location_flag:%s;$type:OLD;",locationGlobal); + } printf("Sending location on server...\r\n"); - myParams->sara->sendUdpMsg(locationGlobal); + printf("%s\r\n", udpMsg); + myParams->sara->sendUdpMsg(udpMsg); wait_ms(5000); } } @@ -77,7 +87,17 @@ { while(state == STATE_ALARM) { - getGPSData(locationGlobal, gnss); + if(getGPSData(locationGlobal, gnss, MAX_TIME_GNSS_WAIT_S)) + { + printf("New location.\r\n"); + locationFlagGlobal = NEW; + } + else + { + printf("Old location.\r\n"); + locationFlagGlobal = OLD; + } + alarmState.signal_set(LOCATION_SEARCH_DONE); Thread::wait(UPDATE_LOCATION_TIME_MS); } }
--- a/tasks/tasks.h Fri Nov 30 16:19:41 2018 +0100 +++ b/tasks/tasks.h Wed Dec 19 15:12:25 2018 +0100 @@ -19,6 +19,12 @@ UBloxSara *sara; }; +enum locationFlag_t +{ + OLD, + NEW +}; + enum MainStates { STATE_IDLE,