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 u-blox

Committer:
jurica238814
Date:
Wed Dec 19 15:12:25 2018 +0100
Revision:
9:f943c09d9173
Parent:
8:2bf886335fd0
Stable version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jurica238814 8:2bf886335fd0 1 /**
jurica238814 8:2bf886335fd0 2 * SAP Project by aconno
jurica238814 8:2bf886335fd0 3 * Made by Jurica @ aconno
jurica238814 8:2bf886335fd0 4 * More info @ aconno.de
RobMeades 0:5eb7846b73b4 5 *
RobMeades 0:5eb7846b73b4 6 */
RobMeades 0:5eb7846b73b4 7
RobMeades 0:5eb7846b73b4 8 #include "mbed.h"
jurica238814 8:2bf886335fd0 9 #include "ATCmdParser.h"
jurica238814 8:2bf886335fd0 10 #include "FileHandle.h"
RobMeades 0:5eb7846b73b4 11 #include "gnss.h"
jurica238814 8:2bf886335fd0 12 #include "aconnoConfig.h"
jurica238814 8:2bf886335fd0 13 #include "uBloxSara.h"
jurica238814 8:2bf886335fd0 14 #include "aconnoHelpers.h"
jurica238814 8:2bf886335fd0 15 #include "Lis2dh12.h"
jurica238814 8:2bf886335fd0 16 #include "tasks/tasks.h"
RobMeades 0:5eb7846b73b4 17
jurica238814 8:2bf886335fd0 18 #define RED_LED_PIN (PE_3)
jurica238814 8:2bf886335fd0 19 #define GREEN_LED_PIN (PE_4)
jurica238814 8:2bf886335fd0 20 #define BLUE_LED_PIN (PE_1)
jurica238814 9:f943c09d9173 21 #define ALARM_OFF (0)
jurica238814 9:f943c09d9173 22 #define ALARM_ON (1)
jurica238814 8:2bf886335fd0 23
jurica238814 9:f943c09d9173 24 volatile MainStates state = STATE_IDLE;
jurica238814 8:2bf886335fd0 25
jurica238814 8:2bf886335fd0 26 /* File handler */
jurica238814 8:2bf886335fd0 27 FileHandle *fh;
RobMeades 0:5eb7846b73b4 28
jurica238814 8:2bf886335fd0 29 /* AT Command Parser handle */
jurica238814 8:2bf886335fd0 30 ATCmdParser *at;
jurica238814 8:2bf886335fd0 31 GnssSerial gnss; // Serial interface (not I2C)
jurica238814 8:2bf886335fd0 32 char locationGlobal[UDP_MSG_SIZE_B];
jurica238814 8:2bf886335fd0 33 bool gotGnssFlag = false;
jurica238814 8:2bf886335fd0 34 DigitalIn userButton(USER_BUTTON);
jurica238814 9:f943c09d9173 35 DigitalOut gnssPower(PE_0, 0);
jurica238814 8:2bf886335fd0 36
jurica238814 8:2bf886335fd0 37 DigitalOut redLed(RED_LED_PIN);
jurica238814 8:2bf886335fd0 38 DigitalOut blueLed(BLUE_LED_PIN);
jurica238814 8:2bf886335fd0 39 DigitalOut greenLed(GREEN_LED_PIN);
jurica238814 9:f943c09d9173 40 DigitalOut alarm(PE_14, 0);
jurica238814 8:2bf886335fd0 41 InterruptIn buttonInt(USER_BUTTON);
jurica238814 8:2bf886335fd0 42
jurica238814 8:2bf886335fd0 43 static bool wasAccInt = false;
jurica238814 8:2bf886335fd0 44 static bool wasButtPressed = false;
jurica238814 8:2bf886335fd0 45 static bool longPress = false;
jurica238814 8:2bf886335fd0 46 Timer buttonTimer;
jurica238814 8:2bf886335fd0 47 Thread timerThread;
jurica238814 8:2bf886335fd0 48 Thread idleState;
jurica238814 8:2bf886335fd0 49 Thread alarmState;
jurica238814 8:2bf886335fd0 50 Thread alarmOffState;
jurica238814 8:2bf886335fd0 51 Thread movementState;
jurica238814 8:2bf886335fd0 52 Thread gnssLocation;
RobMeades 0:5eb7846b73b4 53
jurica238814 8:2bf886335fd0 54 void my_bsp_init()
jurica238814 8:2bf886335fd0 55 {
jurica238814 8:2bf886335fd0 56 redLed = 1;
jurica238814 8:2bf886335fd0 57 greenLed = 1;
jurica238814 8:2bf886335fd0 58 blueLed = 1;
jurica238814 9:f943c09d9173 59 alarm = ALARM_OFF;
jurica238814 8:2bf886335fd0 60 }
jurica238814 8:2bf886335fd0 61
jurica238814 8:2bf886335fd0 62 void buttonRiseHandler(void)
jurica238814 8:2bf886335fd0 63 {
jurica238814 8:2bf886335fd0 64 buttonTimer.reset();
jurica238814 8:2bf886335fd0 65 buttonTimer.start();
jurica238814 8:2bf886335fd0 66 timerThread.signal_set(BUTTON_PRESSED_SIGNAL);
jurica238814 8:2bf886335fd0 67 }
jurica238814 8:2bf886335fd0 68
jurica238814 8:2bf886335fd0 69 void timerCallback(void)
jurica238814 8:2bf886335fd0 70 {
jurica238814 8:2bf886335fd0 71 float secondsPassed;
jurica238814 8:2bf886335fd0 72 while(true)
jurica238814 8:2bf886335fd0 73 {
jurica238814 8:2bf886335fd0 74 bool nextState = false;
jurica238814 8:2bf886335fd0 75 secondsPassed = 0;
jurica238814 8:2bf886335fd0 76 Thread::signal_wait(BUTTON_PRESSED_SIGNAL);
jurica238814 8:2bf886335fd0 77 Thread::signal_clr(BUTTON_PRESSED_SIGNAL);
jurica238814 8:2bf886335fd0 78 while(!nextState)
jurica238814 8:2bf886335fd0 79 {
jurica238814 8:2bf886335fd0 80 secondsPassed = buttonTimer.read();
jurica238814 8:2bf886335fd0 81 if(state != STATE_ALARM && secondsPassed > START_ALARM_S)
jurica238814 8:2bf886335fd0 82 {
jurica238814 8:2bf886335fd0 83 wasButtPressed = true;
jurica238814 8:2bf886335fd0 84 buttonTimer.stop();
jurica238814 8:2bf886335fd0 85 buttonTimer.reset();
jurica238814 8:2bf886335fd0 86 nextState = true;
jurica238814 8:2bf886335fd0 87 }
jurica238814 8:2bf886335fd0 88 else if(state == STATE_ALARM && secondsPassed > STOP_ALARM_S)
jurica238814 8:2bf886335fd0 89 {
jurica238814 8:2bf886335fd0 90 longPress = true;
jurica238814 8:2bf886335fd0 91 buttonTimer.stop();
jurica238814 8:2bf886335fd0 92 buttonTimer.reset();
jurica238814 8:2bf886335fd0 93 nextState = true;
jurica238814 8:2bf886335fd0 94 }
jurica238814 8:2bf886335fd0 95 }
jurica238814 8:2bf886335fd0 96 }
jurica238814 8:2bf886335fd0 97 }
jurica238814 8:2bf886335fd0 98
RobMeades 0:5eb7846b73b4 99 int main()
RobMeades 0:5eb7846b73b4 100 {
jurica238814 8:2bf886335fd0 101 bool success = false;
jurica238814 8:2bf886335fd0 102 MainStates nextState = STATE_IDLE;
RobMeades 0:5eb7846b73b4 103
jurica238814 8:2bf886335fd0 104 buttonInt.rise(buttonRiseHandler);
jurica238814 9:f943c09d9173 105 my_bsp_init();
rob.meades@u-blox.com 7:746ae478fdf7 106
jurica238814 8:2bf886335fd0 107 printf("Initialising UART for modem communication: ");
jurica238814 8:2bf886335fd0 108 fh = new UARTSerial(MDMTXD, MDMRXD, 9600);
jurica238814 8:2bf886335fd0 109 printf("...done\r\n");
rob.meades@u-blox.com 7:746ae478fdf7 110
jurica238814 8:2bf886335fd0 111 printf("Initialising the modem AT command parser: ");
jurica238814 8:2bf886335fd0 112 at = new ATCmdParser(fh, OUTPUT_ENTER_KEY, AT_PARSER_BUFFER_SIZE,
jurica238814 8:2bf886335fd0 113 AT_PARSER_TIMEOUT, false);
jurica238814 8:2bf886335fd0 114 printf("...done\r\n");
rob.meades@u-blox.com 7:746ae478fdf7 115
jurica238814 8:2bf886335fd0 116 gnssPower = 1;
jurica238814 9:f943c09d9173 117 wait_ms(500);
rob.meades@u-blox.com 7:746ae478fdf7 118
jurica238814 8:2bf886335fd0 119 if(gnss.init())
jurica238814 8:2bf886335fd0 120 {
jurica238814 8:2bf886335fd0 121 printf("Cold start... waiting to get first location data\r\n");
jurica238814 8:2bf886335fd0 122 getGPSData(locationGlobal, &gnss);
jurica238814 8:2bf886335fd0 123 printf("I have the location.\r\n");
jurica238814 8:2bf886335fd0 124 gotGnssFlag = true;
jurica238814 8:2bf886335fd0 125 // Turn green led only
jurica238814 8:2bf886335fd0 126 greenLed = 0;
jurica238814 8:2bf886335fd0 127 redLed = 1;
jurica238814 8:2bf886335fd0 128 }
jurica238814 8:2bf886335fd0 129 else
jurica238814 8:2bf886335fd0 130 {
jurica238814 8:2bf886335fd0 131 printf("Unable to initialise GNSS.\r\n");
RobMeades 0:5eb7846b73b4 132 }
rob.meades@u-blox.com 7:746ae478fdf7 133
jurica238814 8:2bf886335fd0 134 Udp udp("52.215.10.12", 3334);
jurica238814 8:2bf886335fd0 135 UBloxSara sara(at, udp);
jurica238814 8:2bf886335fd0 136 printf("Initializing the modem: \r\n");
jurica238814 8:2bf886335fd0 137 sara.setup();
jurica238814 9:f943c09d9173 138 sara.connectNB();
jurica238814 8:2bf886335fd0 139 printf("done...\r\n");
jurica238814 8:2bf886335fd0 140
jurica238814 9:f943c09d9173 141 alarm = ALARM_OFF;
jurica238814 9:f943c09d9173 142 greenLed = 1;
jurica238814 9:f943c09d9173 143 blueLed = 0;
jurica238814 8:2bf886335fd0 144 myParams_t myParams;
jurica238814 8:2bf886335fd0 145 myParams.gnss = &gnss;
jurica238814 8:2bf886335fd0 146 myParams.sara = &sara;
jurica238814 8:2bf886335fd0 147
jurica238814 8:2bf886335fd0 148 timerThread.start(timerCallback);
jurica238814 8:2bf886335fd0 149 idleState.start(idleCallback);
jurica238814 8:2bf886335fd0 150 alarmState.start(callback(alarmCallback, &myParams));
jurica238814 8:2bf886335fd0 151 alarmOffState.start(alarmOffCallback);
jurica238814 8:2bf886335fd0 152 movementState.start(movementCallback);
jurica238814 8:2bf886335fd0 153 gnssLocation.start(callback(gnssLocationCallback, &gnss));
jurica238814 8:2bf886335fd0 154
jurica238814 8:2bf886335fd0 155 char commandbuffer[100];
jurica238814 8:2bf886335fd0 156
jurica238814 8:2bf886335fd0 157 while(1)
jurica238814 8:2bf886335fd0 158 {
jurica238814 8:2bf886335fd0 159 switch(state)
jurica238814 8:2bf886335fd0 160 {
jurica238814 8:2bf886335fd0 161 case STATE_IDLE:
jurica238814 8:2bf886335fd0 162 if(wasAccInt)
jurica238814 8:2bf886335fd0 163 {
jurica238814 8:2bf886335fd0 164 wasAccInt = false;
jurica238814 8:2bf886335fd0 165 nextState = STATE_LIS_DETECTION;
jurica238814 8:2bf886335fd0 166 }
jurica238814 8:2bf886335fd0 167 if(wasButtPressed)
jurica238814 8:2bf886335fd0 168 {
jurica238814 8:2bf886335fd0 169 wasButtPressed = false;
jurica238814 8:2bf886335fd0 170 nextState = STATE_ALARM;
jurica238814 8:2bf886335fd0 171 alarmState.signal_set(ALARM_SIGNAL);
jurica238814 8:2bf886335fd0 172 greenLed = 1;
jurica238814 8:2bf886335fd0 173 redLed = 0;
jurica238814 8:2bf886335fd0 174 blueLed = 1;
jurica238814 8:2bf886335fd0 175 }
jurica238814 8:2bf886335fd0 176 break;
jurica238814 8:2bf886335fd0 177 case STATE_ALARM:
jurica238814 8:2bf886335fd0 178 if(longPress)
jurica238814 8:2bf886335fd0 179 {
jurica238814 8:2bf886335fd0 180 redLed = 1;
jurica238814 8:2bf886335fd0 181 longPress = false;
jurica238814 8:2bf886335fd0 182 nextState = STATE_ALARM_OFF;
jurica238814 8:2bf886335fd0 183 alarmOffState.signal_set(ALARM_OFF_SIGNAL);
jurica238814 8:2bf886335fd0 184 }
jurica238814 8:2bf886335fd0 185 else
jurica238814 8:2bf886335fd0 186 {
jurica238814 9:f943c09d9173 187 alarm = ALARM_ON;
jurica238814 8:2bf886335fd0 188 }
jurica238814 8:2bf886335fd0 189 break;
jurica238814 8:2bf886335fd0 190 case STATE_ALARM_OFF:
jurica238814 8:2bf886335fd0 191 nextState = STATE_IDLE;
jurica238814 8:2bf886335fd0 192 idleState.signal_set(IDLE_SIGNAL);
jurica238814 9:f943c09d9173 193 alarm = ALARM_OFF;
jurica238814 8:2bf886335fd0 194 blueLed = 0;
jurica238814 8:2bf886335fd0 195 break;
jurica238814 8:2bf886335fd0 196 case STATE_LIS_DETECTION:
jurica238814 8:2bf886335fd0 197 break;
jurica238814 8:2bf886335fd0 198 }
jurica238814 8:2bf886335fd0 199 state = nextState;
jurica238814 8:2bf886335fd0 200 wait_ms(125);
jurica238814 8:2bf886335fd0 201 }
RobMeades 0:5eb7846b73b4 202 }