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 * U-Blox SARA nb-iot module class
jurica238814 8:2bf886335fd0 3 * Made by Jurica Resetar @ aconno
jurica238814 8:2bf886335fd0 4 * More info @ aconno.de
jurica238814 8:2bf886335fd0 5 */
jurica238814 8:2bf886335fd0 6
jurica238814 8:2bf886335fd0 7 #include "mbed.h"
jurica238814 8:2bf886335fd0 8 #include "uBloxSara.h"
jurica238814 8:2bf886335fd0 9 #include "udp.h"
jurica238814 8:2bf886335fd0 10 #include "onboard_modem_api.h"
jurica238814 8:2bf886335fd0 11
jurica238814 8:2bf886335fd0 12 UBloxSara::UBloxSara(ATCmdParser *at, Udp udp):
jurica238814 8:2bf886335fd0 13 _at(at), _udp(udp)
jurica238814 8:2bf886335fd0 14 {
jurica238814 8:2bf886335fd0 15 }
jurica238814 8:2bf886335fd0 16
jurica238814 8:2bf886335fd0 17 bool UBloxSara::setup()
jurica238814 8:2bf886335fd0 18 {
jurica238814 8:2bf886335fd0 19 bool success = false;
jurica238814 8:2bf886335fd0 20
jurica238814 8:2bf886335fd0 21 /* Initialize GPIO lines */
jurica238814 8:2bf886335fd0 22 onboard_modem_init();
jurica238814 8:2bf886335fd0 23
jurica238814 8:2bf886335fd0 24 /* Give modem a little time to settle down */
jurica238814 8:2bf886335fd0 25 wait_ms(250);
jurica238814 8:2bf886335fd0 26
jurica238814 9:f943c09d9173 27 if(!CUSTOM_BOARD)
jurica238814 9:f943c09d9173 28 {
jurica238814 9:f943c09d9173 29 // DO NOT CALL THIS IF YOU WORK ON ACONNO CUSTOM MADE BOARD
jurica238814 9:f943c09d9173 30 // POWER UP PIN (PE_14) IS CONNECTED ON OC DIGITAL OUTPUT
jurica238814 9:f943c09d9173 31 // AND POWER UP PIN IS NOT CONNECTED ON SARA MODULE
jurica238814 9:f943c09d9173 32 onboard_modem_power_up();
jurica238814 9:f943c09d9173 33 }
jurica238814 9:f943c09d9173 34
jurica238814 8:2bf886335fd0 35 wait_ms(5000);
jurica238814 8:2bf886335fd0 36
jurica238814 8:2bf886335fd0 37 sendCommand("AT+CFUN=1");
jurica238814 8:2bf886335fd0 38
jurica238814 8:2bf886335fd0 39 // Set AT parser timeout to 1sec for AT OK check
jurica238814 8:2bf886335fd0 40 _at->set_timeout(1000);
jurica238814 8:2bf886335fd0 41
jurica238814 8:2bf886335fd0 42 return true;
jurica238814 8:2bf886335fd0 43
jurica238814 8:2bf886335fd0 44 printf("Checking for AT response from the modem\r\n");
jurica238814 8:2bf886335fd0 45 for (int retry_count = 0; !success && (retry_count < 20); retry_count++)
jurica238814 8:2bf886335fd0 46 {
jurica238814 8:2bf886335fd0 47 printf("...wait\r\n");
jurica238814 8:2bf886335fd0 48 // The modem tends to sends out some garbage during power up.
jurica238814 8:2bf886335fd0 49 _at->flush();
jurica238814 8:2bf886335fd0 50
jurica238814 8:2bf886335fd0 51 // AT OK talk to the modem
jurica238814 8:2bf886335fd0 52 if (_at->send("AT")) {
jurica238814 8:2bf886335fd0 53 wait_ms(100);
jurica238814 8:2bf886335fd0 54 if (_at->recv("OK")) {
jurica238814 8:2bf886335fd0 55 success = true;
jurica238814 8:2bf886335fd0 56 }
jurica238814 8:2bf886335fd0 57 }
jurica238814 8:2bf886335fd0 58 }
jurica238814 8:2bf886335fd0 59 // Increase the parser time to 8 sec
jurica238814 8:2bf886335fd0 60 _at->set_timeout(8000);
jurica238814 8:2bf886335fd0 61
jurica238814 8:2bf886335fd0 62 if (success)
jurica238814 8:2bf886335fd0 63 {
jurica238814 8:2bf886335fd0 64 printf("Configuring the modem...\r\n");
jurica238814 8:2bf886335fd0 65 // Turn off modem echoing and turn on verbose responses
jurica238814 8:2bf886335fd0 66 //success = _at->send("AT+CMEE=1");
jurica238814 8:2bf886335fd0 67 }
jurica238814 8:2bf886335fd0 68 return success;
jurica238814 8:2bf886335fd0 69 }
jurica238814 8:2bf886335fd0 70
jurica238814 8:2bf886335fd0 71 void UBloxSara::sendCommand(char *command)
jurica238814 8:2bf886335fd0 72 {
jurica238814 8:2bf886335fd0 73 char buffer[505];
jurica238814 8:2bf886335fd0 74 for (int i=0; i<505; i++)buffer[i]=0;
jurica238814 8:2bf886335fd0 75 printf("Sending the following command:");
jurica238814 8:2bf886335fd0 76 printf(command);
jurica238814 8:2bf886335fd0 77 printf("\r\n");
jurica238814 8:2bf886335fd0 78
jurica238814 8:2bf886335fd0 79 if (!_at->send(command)) {
jurica238814 8:2bf886335fd0 80 printf("Failed!\r\n");
jurica238814 8:2bf886335fd0 81 return;
jurica238814 8:2bf886335fd0 82 }
jurica238814 8:2bf886335fd0 83 printf("Response:\r\n");
jurica238814 8:2bf886335fd0 84 _at->read(buffer,500);
jurica238814 8:2bf886335fd0 85 printf(buffer);
jurica238814 8:2bf886335fd0 86 printf ("\r\n");
jurica238814 8:2bf886335fd0 87 }
jurica238814 8:2bf886335fd0 88
jurica238814 8:2bf886335fd0 89 void UBloxSara::checkNetworkStatus(char *response)
jurica238814 8:2bf886335fd0 90 {
jurica238814 8:2bf886335fd0 91 char command[] = "AT+NUESTATS";
jurica238814 8:2bf886335fd0 92 printf("Checking network status...");
jurica238814 8:2bf886335fd0 93
jurica238814 8:2bf886335fd0 94 if(!_at->send(command))
jurica238814 8:2bf886335fd0 95 {
jurica238814 8:2bf886335fd0 96 printf("Failed!\r\n");
jurica238814 8:2bf886335fd0 97 return;
jurica238814 8:2bf886335fd0 98 }
jurica238814 8:2bf886335fd0 99 printf("Response:\r\n");
jurica238814 8:2bf886335fd0 100 _at->read(response,500);
jurica238814 8:2bf886335fd0 101 printf(response);
jurica238814 8:2bf886335fd0 102 printf ("\r\n");
jurica238814 8:2bf886335fd0 103 }
jurica238814 8:2bf886335fd0 104
jurica238814 8:2bf886335fd0 105 void UBloxSara::sendUdpMsg(char *msg, char *flags)
jurica238814 8:2bf886335fd0 106 {
jurica238814 8:2bf886335fd0 107 char myCommandbuffer[250];
jurica238814 8:2bf886335fd0 108 int msgSize = strlen(msg);
jurica238814 8:2bf886335fd0 109 char data[msgSize*2];
jurica238814 8:2bf886335fd0 110 int flagsSize = strlen(flags);
jurica238814 8:2bf886335fd0 111 char flagsHex[flagsSize*2];
jurica238814 8:2bf886335fd0 112
jurica238814 8:2bf886335fd0 113 // Create UDP socket on port 'port'
jurica238814 8:2bf886335fd0 114 sprintf(myCommandbuffer,"AT+NSOCR=\"DGRAM\",17,%d",_udp._port);
jurica238814 8:2bf886335fd0 115 sendCommand(myCommandbuffer);
jurica238814 8:2bf886335fd0 116 // Prepaire data for transmition
jurica238814 8:2bf886335fd0 117 for (int i=0, j=0; i<msgSize; i++, j+=2)
jurica238814 8:2bf886335fd0 118 {
jurica238814 8:2bf886335fd0 119 // Take i-th byte and make hex string out of it
jurica238814 8:2bf886335fd0 120 sprintf((data+j),"%x", *(msg+i));
jurica238814 8:2bf886335fd0 121 }
jurica238814 8:2bf886335fd0 122 for(int i=0, j=0; i<flagsSize; i++, j+=2)
jurica238814 8:2bf886335fd0 123 {
jurica238814 8:2bf886335fd0 124 // Take i-th byte and make hex string out of it
jurica238814 8:2bf886335fd0 125 sprintf((flagsHex+j), "%x", *(flags+i));
jurica238814 8:2bf886335fd0 126 }
jurica238814 8:2bf886335fd0 127
jurica238814 8:2bf886335fd0 128 sprintf(myCommandbuffer,"AT+NSOST=0,\"%s\",%d,%d,\"%s\"",
jurica238814 8:2bf886335fd0 129 _udp._ip, _udp._port, msgSize, data);
jurica238814 8:2bf886335fd0 130
jurica238814 9:f943c09d9173 131 //msgSize += strlen(flags);
jurica238814 9:f943c09d9173 132 //sprintf(myCommandbuffer,"AT+NSOST=0,\"%s\",%d,%d,\"%s%s\"",
jurica238814 9:f943c09d9173 133 // _udp._ip, _udp._port, msgSize, flagsHex, data);
jurica238814 8:2bf886335fd0 134 sendCommand(myCommandbuffer);
jurica238814 8:2bf886335fd0 135 }
jurica238814 8:2bf886335fd0 136
jurica238814 9:f943c09d9173 137 #define SHORT_DELAY_MS (1000)
jurica238814 8:2bf886335fd0 138 uint8_t UBloxSara::connectNB()
jurica238814 8:2bf886335fd0 139 {
jurica238814 8:2bf886335fd0 140 sendCommand("at+NCONFIG=\"AUTOCONNECT\",\"FALSE\"");
jurica238814 9:f943c09d9173 141 wait_ms(SHORT_DELAY_MS);
jurica238814 8:2bf886335fd0 142 sendCommand("at+NCONFIG=\"CR_0354_0338_SCRAMBLING\",\"TRUE\"");
jurica238814 9:f943c09d9173 143 wait_ms(SHORT_DELAY_MS);
jurica238814 8:2bf886335fd0 144 sendCommand("at+NCONFIG=\"CR_0859_SI_AVOID\",\"TRUE\"");
jurica238814 9:f943c09d9173 145 wait_ms(SHORT_DELAY_MS);
jurica238814 8:2bf886335fd0 146 sendCommand("at+NCONFIG?");
jurica238814 9:f943c09d9173 147 wait_ms(SHORT_DELAY_MS);
jurica238814 8:2bf886335fd0 148 sendCommand("at+cfun=0");
jurica238814 9:f943c09d9173 149 wait_ms(SHORT_DELAY_MS);
jurica238814 8:2bf886335fd0 150 sendCommand("AT+CGDCONT=1, \"IP\",\"nb.inetd.gdsp\"");
jurica238814 9:f943c09d9173 151 wait_ms(SHORT_DELAY_MS);
jurica238814 8:2bf886335fd0 152 sendCommand("at+cfun=1");
jurica238814 9:f943c09d9173 153 wait_ms(SHORT_DELAY_MS);
jurica238814 8:2bf886335fd0 154
jurica238814 8:2bf886335fd0 155 sendCommand("at+cimi");
jurica238814 9:f943c09d9173 156 wait_ms(SHORT_DELAY_MS);
jurica238814 8:2bf886335fd0 157
jurica238814 8:2bf886335fd0 158 sendCommand("at+cgatt=1");
jurica238814 9:f943c09d9173 159 wait_ms(SHORT_DELAY_MS);
jurica238814 8:2bf886335fd0 160
jurica238814 8:2bf886335fd0 161 sendCommand("at+cops=1,2,\"26202\"");
jurica238814 8:2bf886335fd0 162 wait_ms(5000);
jurica238814 8:2bf886335fd0 163 sendCommand("at+cereg?");
jurica238814 8:2bf886335fd0 164 wait_ms(5000);
jurica238814 8:2bf886335fd0 165 sendCommand("AT+CSQ");
jurica238814 8:2bf886335fd0 166 wait_ms(5000);
jurica238814 8:2bf886335fd0 167 sendCommand("AT+COPS?");
jurica238814 8:2bf886335fd0 168 sendCommand("AT+NBAND?");
jurica238814 8:2bf886335fd0 169 sendCommand("AT+NBAND=20");
jurica238814 9:f943c09d9173 170 wait_ms(SHORT_DELAY_MS);
jurica238814 8:2bf886335fd0 171 sendCommand("AT+NUESTATS");
jurica238814 9:f943c09d9173 172 wait_ms(SHORT_DELAY_MS);
jurica238814 8:2bf886335fd0 173 sendCommand("AT+CGATT?");
jurica238814 8:2bf886335fd0 174 wait_ms(5000);
jurica238814 8:2bf886335fd0 175 sendCommand("AT+CGPADDR");
jurica238814 9:f943c09d9173 176 wait_ms(SHORT_DELAY_MS);
jurica238814 8:2bf886335fd0 177 }