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

Revision:
8:2bf886335fd0
Parent:
7:746ae478fdf7
Child:
9:f943c09d9173
--- a/main.cpp	Mon Jul 31 10:48:08 2017 +0100
+++ b/main.cpp	Fri Nov 30 16:19:41 2018 +0100
@@ -1,117 +1,213 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2017 u-blox
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+/**
+ * SAP Project by aconno
+ * Made by Jurica @ aconno
+ * More info @ aconno.de
  *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
  */
 
 #include "mbed.h"
+#include "ATCmdParser.h"
+#include "FileHandle.h"
 #include "gnss.h"
+#include "aconnoConfig.h"
+#include "uBloxSara.h"
+#include "aconnoHelpers.h"
+#include "Lis2dh12.h"
+#include "tasks/tasks.h"
 
-#define CHECK_TALKER(s) ((buffer[3] == s[0]) && (buffer[4] == s[1]) && (buffer[5] == s[2]))
+#define RED_LED_PIN			(PE_3)
+#define GREEN_LED_PIN		(PE_4)
+#define BLUE_LED_PIN		(PE_1)
+
+MainStates state = STATE_IDLE;
+
+/* File handler */
+FileHandle *fh;
 
-// LEDs
-DigitalOut ledRed(LED1, 1);
-DigitalOut ledGreen(LED2, 1);
-DigitalOut ledBlue(LED3, 1);
+/* AT Command  Parser handle */
+ATCmdParser *at;
+GnssSerial gnss;	// Serial interface (not I2C)
+char locationGlobal[UDP_MSG_SIZE_B];
+bool gotGnssFlag = false;
+DigitalIn userButton(USER_BUTTON);
+DigitalOut gnssPower(PE_0, 1);
+
+DigitalOut redLed(RED_LED_PIN);
+DigitalOut blueLed(BLUE_LED_PIN);
+DigitalOut greenLed(GREEN_LED_PIN);
+DigitalOut alarm(PE_14);
+InterruptIn buttonInt(USER_BUTTON);
+
+static bool wasAccInt = false;
+static bool wasButtPressed = false;
+static bool longPress = false;
+Timer buttonTimer;
+Thread timerThread;
+Thread idleState; 
+Thread alarmState; 
+Thread alarmOffState; 
+Thread movementState; 
+Thread ledState;
+Thread gnssLocation;
 
-/* This example program for the u-blox C030 and C027 boards instantiates
- * the gnss interface and waits for time/position to be received from a satellite.
- * Progress may be monitored with a serial terminal running at 9600 baud.
- * The LED on the C030 board will turn green when this program is
- * operating correctly, pulse blue when a time reading has been received,
- * pulse white when GNSS position has been received or turn red if there is
- * a failure.
- * On the C027 and C030 boards the green/red (respectively) LED near the
- * GNSS module will flash as the module achieves a fix.
- */
+void my_bsp_init()
+{
+    redLed = 1;
+    greenLed = 1;
+    blueLed = 1;
+    alarm = 0;
+}
+
+void buttonRiseHandler(void)
+{
+    buttonTimer.reset();
+    buttonTimer.start();
+    timerThread.signal_set(BUTTON_PRESSED_SIGNAL);
+}
+
+void timerCallback(void)
+{
+    float secondsPassed;
+    while(true)
+    {
+        bool nextState = false;
+        secondsPassed = 0;
+        Thread::signal_wait(BUTTON_PRESSED_SIGNAL);
+        Thread::signal_clr(BUTTON_PRESSED_SIGNAL);
+        while(!nextState)
+        {
+            secondsPassed = buttonTimer.read();
+            if(state != STATE_ALARM && secondsPassed > START_ALARM_S)
+            {
+                wasButtPressed = true;
+                buttonTimer.stop();
+                buttonTimer.reset();
+                nextState = true;
+            }
+            else if(state == STATE_ALARM && secondsPassed > STOP_ALARM_S)
+            {
+                longPress = true;
+                buttonTimer.stop();
+                buttonTimer.reset();
+                nextState = true;
+            }
+        }
+    }
+}
+
+void ledBlinky()
+{
+    while(!gotGnssFlag)
+    {
+        redLed = !redLed;
+        greenLed = !greenLed;
+        Thread::wait(1000);
+    }
+}
 
 int main()
 {
-    GnssSerial gnss;
-    int gnssReturnCode;
-    int length;
-    char buffer[256];
+    bool success = false;
+    MainStates nextState = STATE_IDLE;
+
+    my_bsp_init();
     
-    printf ("Starting up...\n");
-    if (gnss.init()) {
-        printf ("Waiting for GNSS to receive something...\n");
-        while (1) {
-            gnssReturnCode = gnss.getMessage(buffer, sizeof(buffer));
-            if (gnssReturnCode > 0) {
-                ledGreen = 0;
-                ledBlue = 1;
-                ledRed = 1;
-                length = LENGTH(gnssReturnCode);
+    buttonInt.rise(buttonRiseHandler);
 
-                printf("NMEA: %.*s\n", length - 2, buffer);
+    printf("Initialising UART for modem communication: ");
+    fh = new UARTSerial(MDMTXD, MDMRXD, 9600);
+    printf("...done\r\n");
 
-                if ((PROTOCOL(gnssReturnCode) == GnssParser::NMEA) && (length > 6)) {
-                    // Talker is $GA=Galileo $GB=Beidou $GL=Glonass $GN=Combined $GP=GNSS
-                    if ((buffer[0] == '$') || buffer[1] == 'G') {
-                      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')) {
-                                ledBlue = 0;
-                                ledRed = 0;
-                                ledGreen = 0;
+    printf("Initialising the modem AT command parser: ");
+    at = new ATCmdParser(fh, OUTPUT_ENTER_KEY, AT_PARSER_BUFFER_SIZE,
+                         AT_PARSER_TIMEOUT, false);
+    printf("...done\r\n");
 
-                                printf("\nGNSS: location is %.5f %.5f.\n\n", latitude, longitude); 
-                                printf("I am here: https://maps.google.com/?q=%.5f,%.5f\n\n",
-                                       latitude, longitude); 
-                            }
-                        } else if (CHECK_TALKER("GGA") || CHECK_TALKER("GNS")) {
-                            double altitude = 0; 
-                            const char *timeString = NULL;
-
-                            // Altitude
-                            if (gnss.getNmeaItem(9, buffer, length, altitude)) {
-                                printf("\nGNSS: altitude is %.1f m.\n", altitude); 
-                            }
+	gnssPower = 1;
+    ledState.start(ledBlinky);
 
-                            // Time
-                            timeString = gnss.findNmeaItemPos(1, buffer, buffer + length);
-                            if (timeString != NULL) {
-                                ledBlue = 0;
-                                ledRed = 1;
-                                ledGreen = 1;
-
-                                printf("\nGNSS: time is %.6s.\n\n", timeString);
-                            }
-                        } else if (CHECK_TALKER("VTG")) {
-                            double speed = 0; 
-
-                            // Speed
-                            if (gnss.getNmeaItem(7, buffer, length, speed)) {
-                                printf("\nGNSS: speed is %.1f km/h.\n\n", speed);
-                            }
-                        }
-                    }
-                }
-            }
-        }
-    } else {
-        printf("Unable to initialise GNSS.\n");
+    if(gnss.init())
+	{
+        printf("Cold start... waiting to get first location data\r\n");     
+        getGPSData(locationGlobal, &gnss);
+        printf("I have the location.\r\n");
+        gotGnssFlag = true;
+        ledState.terminate();
+        // Turn green led only
+        greenLed = 0;
+        redLed = 1;
+	}
+	else
+	{
+        printf("Unable to initialise GNSS.\r\n");
     }
 
-    ledRed = 0;
-    ledGreen = 1;
-    ledBlue = 1;
-    printf("Should never get here.\n");
-    MBED_ASSERT(false);
+    greenLed = 1; 
+    blueLed = 0;
+
+    Udp udp("52.215.10.12", 3334);
+    UBloxSara sara(at, udp);
+    printf("Initializing the modem: \r\n");
+	sara.setup();
+    printf("done...\r\n");
+    sara.connectNB();
+
+    myParams_t myParams;
+    myParams.gnss = &gnss;
+    myParams.sara = &sara;
+    
+    timerThread.start(timerCallback);
+    idleState.start(idleCallback);
+    alarmState.start(callback(alarmCallback, &myParams));
+    alarmOffState.start(alarmOffCallback);
+    movementState.start(movementCallback);
+    gnssLocation.start(callback(gnssLocationCallback, &gnss));
+    
+    char commandbuffer[100];
+	
+    while(1)
+	{
+        switch(state)
+        {
+            case STATE_IDLE:
+				if(wasAccInt)
+                {
+                    wasAccInt = false;
+                    nextState = STATE_LIS_DETECTION;
+                }
+                if(wasButtPressed)
+                {
+                    wasButtPressed = false;
+                    nextState = STATE_ALARM;
+                    alarmState.signal_set(ALARM_SIGNAL);
+                    greenLed = 1;
+                    redLed = 0;
+                    blueLed = 1;
+                }
+            	break;
+            case STATE_ALARM:
+				if(longPress)
+                {
+                    redLed = 1;
+                    longPress = false;
+                    nextState = STATE_ALARM_OFF;
+                    alarmOffState.signal_set(ALARM_OFF_SIGNAL);
+                }
+                else
+                {
+                    alarm = 1;
+                }
+            	break;
+            case STATE_ALARM_OFF:
+                nextState = STATE_IDLE;
+                idleState.signal_set(IDLE_SIGNAL);
+                alarm = 0;
+                blueLed = 0;
+				break;
+            case STATE_LIS_DETECTION:
+            	break;
+        }
+        state = nextState;
+        wait_ms(125);
+	}
 }
-
-// End Of File
\ No newline at end of file