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

Files at this revision

API Documentation at this revision

Comitter:
jurica238814
Date:
Fri Nov 30 16:19:41 2018 +0100
Parent:
7:746ae478fdf7
Child:
9:f943c09d9173
Commit message:
Aconno init commit

Changed in this revision

.gitignore Show annotated file Show diff for this revision Revisions of this file
Lis2dh12.lib Show annotated file Show diff for this revision Revisions of this file
README.md Show annotated file Show diff for this revision Revisions of this file
ReadMe.txt Show annotated file Show diff for this revision Revisions of this file
STM32Bsp.lib Show annotated file Show diff for this revision Revisions of this file
STM32Bsp/README.md Show annotated file Show diff for this revision Revisions of this file
STM32Bsp/STM32Bsp.cpp Show annotated file Show diff for this revision Revisions of this file
STM32Bsp/STM32Bsp.h Show annotated file Show diff for this revision Revisions of this file
UBloxSara.lib Show annotated file Show diff for this revision Revisions of this file
UBloxSara/.hgignore Show annotated file Show diff for this revision Revisions of this file
UBloxSara/README.md Show annotated file Show diff for this revision Revisions of this file
UBloxSara/uBloxSara.cpp Show annotated file Show diff for this revision Revisions of this file
UBloxSara/uBloxSara.h Show annotated file Show diff for this revision Revisions of this file
UBloxSara/udp.cpp Show annotated file Show diff for this revision Revisions of this file
UBloxSara/udp.h Show annotated file Show diff for this revision Revisions of this file
aconnoConfig/aconnoConfig.h Show annotated file Show diff for this revision Revisions of this file
aconnoHelpers/aconnoHelpers.cpp Show annotated file Show diff for this revision Revisions of this file
aconnoHelpers/aconnoHelpers.h Show annotated file Show diff for this revision Revisions of this file
aconno_I2C.lib Show annotated file Show diff for this revision Revisions of this file
low-power-sleep.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
tasks/tasks.cpp Show annotated file Show diff for this revision Revisions of this file
tasks/tasks.h Show annotated file Show diff for this revision Revisions of this file
ublox-at-cellular-interface-n2xx.lib Show annotated file Show diff for this revision Revisions of this file
ublox-at-cellular-interface.lib Show annotated file Show diff for this revision Revisions of this file
ublox-cellular-base-n2xx.lib Show annotated file Show diff for this revision Revisions of this file
ublox-cellular-base.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.gitignore	Fri Nov 30 16:19:41 2018 +0100
@@ -0,0 +1,5 @@
+.hg*
+BUILD/
+*.pyc
+
+\.vscode/
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Lis2dh12.lib	Fri Nov 30 16:19:41 2018 +0100
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/jurica238814/code/Lis2dh12/#f1ba533021b8
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/README.md	Fri Nov 30 16:19:41 2018 +0100
@@ -0,0 +1,1 @@
+aconnoCellularGnss
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ReadMe.txt	Fri Nov 30 16:19:41 2018 +0100
@@ -0,0 +1,12 @@
+SAP project
+
+In this version, the following is implemented:
+In the main loop, gnss initializing process is going first. After it, cellular 
+network and NB is initialized.
+When a location is fixed and cellular network is initialized, the program is ready
+to be alarmed. 
+On user button press, program enters in alarm state and sends the current location
+on server. This part had to be modified to send flags and old location if gnss
+does not work for some reason. Also, UBloxSara class has to be improved with
+safety features such as init failure and timeouts.
+And fucking mbed-os version MUST be updated (this one is more than a year old).
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/STM32Bsp.lib	Fri Nov 30 16:19:41 2018 +0100
@@ -0,0 +1,1 @@
+https://git.simvelop.de/aconnoLibs/STM32Bsp/#0d6cd32193813563511f5b1d19522b2d12e59a08
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/STM32Bsp/README.md	Fri Nov 30 16:19:41 2018 +0100
@@ -0,0 +1,1 @@
+STM32Bsp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/STM32Bsp/STM32Bsp.cpp	Fri Nov 30 16:19:41 2018 +0100
@@ -0,0 +1,35 @@
+/*
+* Aconno Board Support Packet
+* Made by Jurica Resetar @ aconno
+* More info @ aconno.de
+*/
+
+#include "mbed.h"
+#include "STM32Bsp.h"
+
+/*
+DigitalOut redLed(RED_LED_PIN);
+DigitalOut blueLed(BLUE_LED_PIN);
+DigitalOut greenLed(GREEN_LED_PIN);
+*/
+void bsp_init()
+{
+    bsp_ctrl_led(redLed, LED_OFF);
+	bsp_ctrl_led(greenLed, LED_OFF);
+	bsp_ctrl_led(blueLed, LED_OFF);
+}
+
+void bsp_ctrl_led(DigitalOut led, led_state state)
+{
+    led = state;
+}
+
+void bsp_toggle_led(DigitalOut led)
+{
+    led = !led;
+}
+
+int bsp_read_button()
+{
+    //return userButton.read();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/STM32Bsp/STM32Bsp.h	Fri Nov 30 16:19:41 2018 +0100
@@ -0,0 +1,35 @@
+/*
+* Aconno Board Support Packet
+* Made by Jurica Resetar @ aconno
+* More info @ aconno.de
+*/
+
+#ifndef __STM_BSP_H__
+#define __STM_BSP_H__
+
+#include "mbed.h"
+
+#define BUTTON_TRUE		(1)
+#define BUTTON_FALSE	(!BUTTON_TRUE)
+
+#define USER_BUTTON_PIN     (PC_13)
+#define RED_LED_PIN			(PE_4)
+#define GREEN_LED_PIN		(PE_3)
+#define BLUE_LED_PIN		(PE_1)
+
+extern DigitalOut redLed;
+extern DigitalOut blueLed;
+extern DigitalOut greenLed;
+
+
+enum led_state{
+    LED_ON = 0,
+    LED_OFF = 1
+};
+
+void bsp_init();
+int bsp_read_button();
+void bsp_ctrl_led(DigitalOut led, led_state state);
+void bsp_toggle_led(DigitalOut led);
+
+#endif // __STM_BSP_H__
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/UBloxSara.lib	Fri Nov 30 16:19:41 2018 +0100
@@ -0,0 +1,1 @@
+https://git.simvelop.de/aconnoLibs/UBloxSara/#6250f29cdd4224eba46d576366937821ba714074
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/UBloxSara/.hgignore	Fri Nov 30 16:19:41 2018 +0100
@@ -0,0 +1,3 @@
+BUILD
+mbed-os/
+.git*
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/UBloxSara/README.md	Fri Nov 30 16:19:41 2018 +0100
@@ -0,0 +1,1 @@
+UBloxSara
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/UBloxSara/uBloxSara.cpp	Fri Nov 30 16:19:41 2018 +0100
@@ -0,0 +1,160 @@
+/**
+ * U-Blox SARA nb-iot module class
+ * Made by Jurica Resetar @ aconno
+ * More info @ aconno.de
+ */
+
+#include "mbed.h"
+#include "uBloxSara.h"
+#include "udp.h"
+#include "onboard_modem_api.h"
+
+UBloxSara::UBloxSara(ATCmdParser *at, Udp udp):
+	_at(at), _udp(udp)
+{
+}
+
+bool UBloxSara::setup()
+{
+	bool success = false;
+
+    /* Initialize GPIO lines */
+    onboard_modem_init();
+
+    /* Give modem a little time to settle down */
+    wait_ms(250);
+
+    printf("Powering up the modem...\r\n");
+    //onboard_modem_power_up();
+    wait_ms(5000);
+
+    sendCommand("AT+CFUN=1");
+
+    // Set AT parser timeout to 1sec for AT OK check
+    _at->set_timeout(1000);
+
+	return true;
+
+    printf("Checking for AT response from the modem\r\n");
+    for (int retry_count = 0; !success && (retry_count < 20); retry_count++)
+    {
+        printf("...wait\r\n");
+        // The modem tends to sends out some garbage during power up.
+        _at->flush();
+
+        // AT OK talk to the modem
+        if (_at->send("AT")) {
+            wait_ms(100);
+            if (_at->recv("OK")) {
+                success = true;
+            }
+        }
+    }
+    // Increase the parser time to 8 sec
+    _at->set_timeout(8000);
+
+    if (success)
+	{
+        printf("Configuring the modem...\r\n");
+        // Turn off modem echoing and turn on verbose responses
+        //success = _at->send("AT+CMEE=1");
+    }
+    return success;
+}
+
+void UBloxSara::sendCommand(char *command)
+{
+	char buffer[505];
+    for (int i=0; i<505; i++)buffer[i]=0;
+    printf("Sending the following command:");
+    printf(command);
+    printf("\r\n");
+
+    if (!_at->send(command)) {
+        printf("Failed!\r\n");
+        return;
+    }
+    printf("Response:\r\n");
+    _at->read(buffer,500);
+    printf(buffer);
+    printf ("\r\n");
+}
+
+void UBloxSara::checkNetworkStatus(char *response)
+{
+    char command[] = "AT+NUESTATS";
+    printf("Checking network status...");
+
+    if(!_at->send(command))
+    {
+        printf("Failed!\r\n");
+        return;
+    }
+    printf("Response:\r\n");
+    _at->read(response,500);
+    printf(response);
+    printf ("\r\n");
+}
+
+void UBloxSara::sendUdpMsg(char *msg, char *flags)
+{
+	char myCommandbuffer[250];
+    int msgSize = strlen(msg);
+    char data[msgSize*2];
+    int flagsSize = strlen(flags);
+    char flagsHex[flagsSize*2];
+
+    // Create UDP socket on port 'port' 
+    sprintf(myCommandbuffer,"AT+NSOCR=\"DGRAM\",17,%d",_udp._port); 
+    sendCommand(myCommandbuffer); 
+	// Prepaire data for transmition
+    for (int i=0, j=0; i<msgSize; i++, j+=2)
+    {
+		// Take i-th byte and make hex string out of it
+	    sprintf((data+j),"%x", *(msg+i));
+	}
+    for(int i=0, j=0; i<flagsSize; i++, j+=2)
+    {
+        // Take i-th byte and make hex string out of it
+        sprintf((flagsHex+j), "%x", *(flags+i));
+    }
+
+	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);
+	sendCommand(myCommandbuffer);
+}
+
+uint8_t UBloxSara::connectNB()
+{
+	sendCommand("at+NCONFIG=\"AUTOCONNECT\",\"FALSE\"");
+    sendCommand("at+NCONFIG=\"CR_0354_0338_SCRAMBLING\",\"TRUE\"");
+
+    sendCommand("at+NCONFIG=\"CR_0859_SI_AVOID\",\"TRUE\"");
+    sendCommand("at+NCONFIG?");
+    sendCommand("at+cfun=0");
+    sendCommand("AT+CGDCONT=1, \"IP\",\"nb.inetd.gdsp\"");
+    sendCommand("at+cfun=1");
+
+    sendCommand("at+cimi");
+
+    sendCommand("at+cgatt=1");
+
+    sendCommand("at+cops=1,2,\"26202\"");
+    wait_ms(5000);
+  	sendCommand("at+cereg?");
+    wait_ms(5000);
+    sendCommand("AT+CSQ");
+    wait_ms(5000);
+    sendCommand("AT+COPS?");
+    sendCommand("AT+NBAND?");
+    sendCommand("AT+NBAND=20");
+    sendCommand("AT+NUESTATS");
+
+    sendCommand("AT+CGATT?");
+    wait_ms(5000);
+    sendCommand("AT+CGPADDR");
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/UBloxSara/uBloxSara.h	Fri Nov 30 16:19:41 2018 +0100
@@ -0,0 +1,28 @@
+/**
+ * U-Blox SARA nb-iot module class
+ * Made by Jurica Resetar @ aconno
+ * More info @ aconno.de
+ */
+
+#ifndef __U_BLOX_SARA_H__
+#define __U_BLOX_SARA_H__
+
+#include "mbed.h"
+#include "udp.h"
+
+class UBloxSara
+{
+	public:
+		UBloxSara(ATCmdParser *at, Udp udp);
+		bool setup();
+		void sendCommand(char *command);
+		void sendUdpMsg(char *msg, char *flags = "");
+		uint8_t connectNB();
+		void checkNetworkStatus(char *response);
+	private:
+		ATCmdParser *_at;
+		Udp _udp;
+	protected:
+};
+
+#endif // __U_BLOX_SARA_H__
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/UBloxSara/udp.cpp	Fri Nov 30 16:19:41 2018 +0100
@@ -0,0 +1,13 @@
+/*
+ * U-Blox SARA UDP class
+ * Made by Jurica @ aconno
+ * More info @ aconno.de
+ */
+
+#include "udp.h"
+
+Udp::Udp(char *ip, int port) :
+		_ip(ip), _port(port)
+{
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/UBloxSara/udp.h	Fri Nov 30 16:19:41 2018 +0100
@@ -0,0 +1,24 @@
+/*
+ * U-Blox SARA UDP class
+ * Made by Jurica @ aconno
+ * More info @ aconno.de
+ */
+
+#ifndef __UDP_H__
+#define __UDP_H__
+
+#include "mbed.h"
+
+class Udp
+{
+	public:
+		Udp(char *ip, int port);
+		uint8_t open();
+		uint8_t close();
+		char *_ip;
+		int _port;
+	private:
+	protected:
+};
+
+#endif 	//__UDP_H__
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/aconnoConfig/aconnoConfig.h	Fri Nov 30 16:19:41 2018 +0100
@@ -0,0 +1,34 @@
+/**
+ * aconnoConfig.h
+ * Config file for SAP project
+ * Made by Jurica Resetar @ aconno
+ * More info @ aconno.de
+ * All rights reserved
+ */
+
+#ifndef __ACONNO_CONFIG_H__
+#define __ACONNO_CONFIG_H__
+
+#define USE_C030_BOARD			(1)
+
+#define USER_BUTTON				(PC_13)
+#define USER_LED				(PE_7)
+
+/* Definitions */
+#define OUTPUT_ENTER_KEY  "\r"
+#define AT_PARSER_BUFFER_SIZE   (256)
+#define AT_PARSER_TIMEOUT       (8*1000) // Milliseconds
+#define STANDBY_TIME_SECONDS 	(25)
+#define TARGET_STM				(1)
+#define UDP_MSG_SIZE_B			(50)
+
+#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 UPDATE_LOCATION_TIME_MS (5000)
+
+#endif // __ACONNO_CONFIG_H__
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/aconnoHelpers/aconnoHelpers.cpp	Fri Nov 30 16:19:41 2018 +0100
@@ -0,0 +1,75 @@
+/**
+ * aconno helpers
+ * Set of general purpose fuctions used within aconno projects
+ *
+ */
+
+#include "mbed.h"
+#include "aconnoHelpers.h"
+#include "stdlib.h"
+#include "gnss.h"
+#include "aconnoConfig.h"
+
+bool getGPSData(char *location, GnssSerial *gnss)
+{
+	int gnssReturnCode;
+    int length;
+    char buffer[505];
+	bool gotLocationFlag = false;
+	
+	do
+	{
+		gnssReturnCode = gnss->getMessage(buffer, sizeof(buffer));
+		if (gnssReturnCode > 0)
+		{
+			// Msg from GNSS module received
+			length = LENGTH(gnssReturnCode);
+			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')) {
+							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);
+							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);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/aconnoHelpers/aconnoHelpers.h	Fri Nov 30 16:19:41 2018 +0100
@@ -0,0 +1,30 @@
+/**
+ * aconno helpers header
+ */
+
+#ifndef __ACONNO_HELPERS_H__
+#define __ACONNO_HELPERS_H__
+
+#include "gnss.h"
+
+#define CHECK_TALKER(s) 		((buffer[3] == s[0]) && (buffer[4] == s[1]) && (buffer[5] == s[2]))
+
+/**
+ * Standard integer to string function
+ * @param  num  	Integer to convert
+ * @param  str  	Pointer to the buffer
+ * @param  base 	Number's base
+ * @return      	Pointer to the output string
+ */
+
+extern "C" char* itoa(int num, char* str, int base);
+
+/**
+ * Standard string reverse function
+ * @param str 		Pointer to the string
+ */
+void reverse(char *str);
+
+bool getGPSData(char *location, GnssSerial *gnss);
+
+#endif	//
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/aconno_I2C.lib	Fri Nov 30 16:19:41 2018 +0100
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/jurica238814/code/aconno_I2C/#b2f0c302ba6d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/low-power-sleep.lib	Fri Nov 30 16:19:41 2018 +0100
@@ -0,0 +1,1 @@
+https://os.mbed.com/teams/ublox/code/low-power-sleep/#691e6b38fc54
--- 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
--- a/mbed-os.lib	Mon Jul 31 10:48:08 2017 +0100
+++ b/mbed-os.lib	Fri Nov 30 16:19:41 2018 +0100
@@ -1,1 +1,1 @@
-https://github.com/ARMmbed/mbed-os/#78a1eddf61efa705ef1e99fc597dcaa5e737fb4c
+https://github.com/ARMmbed/mbed-os/#5b25b6643d24c94442695a858fc9a5845d5c0957
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tasks/tasks.cpp	Fri Nov 30 16:19:41 2018 +0100
@@ -0,0 +1,85 @@
+/**
+ * Collection of all tasks  
+ * Made by Jurica @ aconno
+ * More info @ aconno.de
+ * 
+ */
+
+#include "tasks.h"
+#include "aconnoHelpers.h"
+#include "gnss.h"
+#include "aconnoConfig.h"
+
+extern MainStates state;
+extern char locationGlobal[UDP_MSG_SIZE_B];
+
+void idleCallback()
+{
+    while(1)
+    {
+        Thread::signal_wait(IDLE_SIGNAL);
+        Thread::signal_clr(IDLE_SIGNAL);
+        printf("In idle thread.\r\n");
+        while(state == STATE_IDLE)
+        {   
+            wait_ms(1000);
+        }
+    }
+}
+
+void alarmCallback(myParams_t *myParams)
+{
+    //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)
+        {
+            printf("Sending location on server...\r\n");
+            myParams->sara->sendUdpMsg(locationGlobal);
+            wait_ms(5000);
+        }
+    }
+}
+
+void alarmOffCallback()
+{
+    while(1)
+    {   
+        Thread::signal_wait(ALARM_OFF_SIGNAL);
+        Thread::signal_clr(ALARM_OFF_SIGNAL);
+        printf("In alarm off thread.\r\n");
+        while(state == STATE_ALARM_OFF)
+        {
+            wait_ms(1000);
+        }
+    }
+}
+
+void movementCallback()
+{
+    while(1)
+    {
+        while(state == STATE_LIS_DETECTION)
+        {
+            printf("In movement thread.\r\n");
+            wait_ms(1000);
+        }
+    }
+}
+
+void gnssLocationCallback(GnssSerial *gnss)
+{
+    while(1)
+    {
+        while(state == STATE_ALARM)
+        {
+            getGPSData(locationGlobal, gnss);
+            Thread::wait(UPDATE_LOCATION_TIME_MS);
+        }
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tasks/tasks.h	Fri Nov 30 16:19:41 2018 +0100
@@ -0,0 +1,36 @@
+/**
+ * Collection of all tasks  
+ * Made by Jurica @ aconno
+ * More info @ aconno.de
+ * 
+ */
+
+#ifndef __TASKS_H__
+#define __TASKS_H__
+
+#include "mbed.h"
+#include "aconnoConfig.h"
+#include "uBloxSara.h"
+#include "gnss.h"
+
+struct myParams_t
+{
+    GnssSerial *gnss;
+    UBloxSara *sara;
+};
+
+enum MainStates
+{
+    STATE_IDLE,
+    STATE_ALARM,
+    STATE_ALARM_OFF,
+    STATE_LIS_DETECTION
+};
+
+void idleCallback();
+void alarmCallback(myParams_t *myParams);
+void alarmOffCallback();
+void movementCallback();
+void gnssLocationCallback(GnssSerial *gnss);
+
+#endif // __TASKS_H__
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ublox-at-cellular-interface-n2xx.lib	Fri Nov 30 16:19:41 2018 +0100
@@ -0,0 +1,1 @@
+https://os.mbed.com/teams/ublox/code/ublox-at-cellular-interface-n2xx/#2504d4160042
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ublox-at-cellular-interface.lib	Fri Nov 30 16:19:41 2018 +0100
@@ -0,0 +1,1 @@
+https://os.mbed.com/teams/ublox/code/ublox-at-cellular-interface/#8cc9a80ac0ad
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ublox-cellular-base-n2xx.lib	Fri Nov 30 16:19:41 2018 +0100
@@ -0,0 +1,1 @@
+https://os.mbed.com/teams/ublox/code/ublox-cellular-base-n2xx/#1afe5ed24f0c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ublox-cellular-base.lib	Fri Nov 30 16:19:41 2018 +0100
@@ -0,0 +1,1 @@
+https://os.mbed.com/teams/ublox/code/ublox-cellular-base/#15f31e074d67