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