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
UBloxSara/uBloxSara.cpp@8:2bf886335fd0, 2018-11-30 (annotated)
- Committer:
- jurica238814
- Date:
- Fri Nov 30 16:19:41 2018 +0100
- Revision:
- 8:2bf886335fd0
- 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 | * 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 | 8:2bf886335fd0 | 27 | printf("Powering up the modem...\r\n"); |
jurica238814 | 8:2bf886335fd0 | 28 | //onboard_modem_power_up(); |
jurica238814 | 8:2bf886335fd0 | 29 | wait_ms(5000); |
jurica238814 | 8:2bf886335fd0 | 30 | |
jurica238814 | 8:2bf886335fd0 | 31 | sendCommand("AT+CFUN=1"); |
jurica238814 | 8:2bf886335fd0 | 32 | |
jurica238814 | 8:2bf886335fd0 | 33 | // Set AT parser timeout to 1sec for AT OK check |
jurica238814 | 8:2bf886335fd0 | 34 | _at->set_timeout(1000); |
jurica238814 | 8:2bf886335fd0 | 35 | |
jurica238814 | 8:2bf886335fd0 | 36 | return true; |
jurica238814 | 8:2bf886335fd0 | 37 | |
jurica238814 | 8:2bf886335fd0 | 38 | printf("Checking for AT response from the modem\r\n"); |
jurica238814 | 8:2bf886335fd0 | 39 | for (int retry_count = 0; !success && (retry_count < 20); retry_count++) |
jurica238814 | 8:2bf886335fd0 | 40 | { |
jurica238814 | 8:2bf886335fd0 | 41 | printf("...wait\r\n"); |
jurica238814 | 8:2bf886335fd0 | 42 | // The modem tends to sends out some garbage during power up. |
jurica238814 | 8:2bf886335fd0 | 43 | _at->flush(); |
jurica238814 | 8:2bf886335fd0 | 44 | |
jurica238814 | 8:2bf886335fd0 | 45 | // AT OK talk to the modem |
jurica238814 | 8:2bf886335fd0 | 46 | if (_at->send("AT")) { |
jurica238814 | 8:2bf886335fd0 | 47 | wait_ms(100); |
jurica238814 | 8:2bf886335fd0 | 48 | if (_at->recv("OK")) { |
jurica238814 | 8:2bf886335fd0 | 49 | success = true; |
jurica238814 | 8:2bf886335fd0 | 50 | } |
jurica238814 | 8:2bf886335fd0 | 51 | } |
jurica238814 | 8:2bf886335fd0 | 52 | } |
jurica238814 | 8:2bf886335fd0 | 53 | // Increase the parser time to 8 sec |
jurica238814 | 8:2bf886335fd0 | 54 | _at->set_timeout(8000); |
jurica238814 | 8:2bf886335fd0 | 55 | |
jurica238814 | 8:2bf886335fd0 | 56 | if (success) |
jurica238814 | 8:2bf886335fd0 | 57 | { |
jurica238814 | 8:2bf886335fd0 | 58 | printf("Configuring the modem...\r\n"); |
jurica238814 | 8:2bf886335fd0 | 59 | // Turn off modem echoing and turn on verbose responses |
jurica238814 | 8:2bf886335fd0 | 60 | //success = _at->send("AT+CMEE=1"); |
jurica238814 | 8:2bf886335fd0 | 61 | } |
jurica238814 | 8:2bf886335fd0 | 62 | return success; |
jurica238814 | 8:2bf886335fd0 | 63 | } |
jurica238814 | 8:2bf886335fd0 | 64 | |
jurica238814 | 8:2bf886335fd0 | 65 | void UBloxSara::sendCommand(char *command) |
jurica238814 | 8:2bf886335fd0 | 66 | { |
jurica238814 | 8:2bf886335fd0 | 67 | char buffer[505]; |
jurica238814 | 8:2bf886335fd0 | 68 | for (int i=0; i<505; i++)buffer[i]=0; |
jurica238814 | 8:2bf886335fd0 | 69 | printf("Sending the following command:"); |
jurica238814 | 8:2bf886335fd0 | 70 | printf(command); |
jurica238814 | 8:2bf886335fd0 | 71 | printf("\r\n"); |
jurica238814 | 8:2bf886335fd0 | 72 | |
jurica238814 | 8:2bf886335fd0 | 73 | if (!_at->send(command)) { |
jurica238814 | 8:2bf886335fd0 | 74 | printf("Failed!\r\n"); |
jurica238814 | 8:2bf886335fd0 | 75 | return; |
jurica238814 | 8:2bf886335fd0 | 76 | } |
jurica238814 | 8:2bf886335fd0 | 77 | printf("Response:\r\n"); |
jurica238814 | 8:2bf886335fd0 | 78 | _at->read(buffer,500); |
jurica238814 | 8:2bf886335fd0 | 79 | printf(buffer); |
jurica238814 | 8:2bf886335fd0 | 80 | printf ("\r\n"); |
jurica238814 | 8:2bf886335fd0 | 81 | } |
jurica238814 | 8:2bf886335fd0 | 82 | |
jurica238814 | 8:2bf886335fd0 | 83 | void UBloxSara::checkNetworkStatus(char *response) |
jurica238814 | 8:2bf886335fd0 | 84 | { |
jurica238814 | 8:2bf886335fd0 | 85 | char command[] = "AT+NUESTATS"; |
jurica238814 | 8:2bf886335fd0 | 86 | printf("Checking network status..."); |
jurica238814 | 8:2bf886335fd0 | 87 | |
jurica238814 | 8:2bf886335fd0 | 88 | if(!_at->send(command)) |
jurica238814 | 8:2bf886335fd0 | 89 | { |
jurica238814 | 8:2bf886335fd0 | 90 | printf("Failed!\r\n"); |
jurica238814 | 8:2bf886335fd0 | 91 | return; |
jurica238814 | 8:2bf886335fd0 | 92 | } |
jurica238814 | 8:2bf886335fd0 | 93 | printf("Response:\r\n"); |
jurica238814 | 8:2bf886335fd0 | 94 | _at->read(response,500); |
jurica238814 | 8:2bf886335fd0 | 95 | printf(response); |
jurica238814 | 8:2bf886335fd0 | 96 | printf ("\r\n"); |
jurica238814 | 8:2bf886335fd0 | 97 | } |
jurica238814 | 8:2bf886335fd0 | 98 | |
jurica238814 | 8:2bf886335fd0 | 99 | void UBloxSara::sendUdpMsg(char *msg, char *flags) |
jurica238814 | 8:2bf886335fd0 | 100 | { |
jurica238814 | 8:2bf886335fd0 | 101 | char myCommandbuffer[250]; |
jurica238814 | 8:2bf886335fd0 | 102 | int msgSize = strlen(msg); |
jurica238814 | 8:2bf886335fd0 | 103 | char data[msgSize*2]; |
jurica238814 | 8:2bf886335fd0 | 104 | int flagsSize = strlen(flags); |
jurica238814 | 8:2bf886335fd0 | 105 | char flagsHex[flagsSize*2]; |
jurica238814 | 8:2bf886335fd0 | 106 | |
jurica238814 | 8:2bf886335fd0 | 107 | // Create UDP socket on port 'port' |
jurica238814 | 8:2bf886335fd0 | 108 | sprintf(myCommandbuffer,"AT+NSOCR=\"DGRAM\",17,%d",_udp._port); |
jurica238814 | 8:2bf886335fd0 | 109 | sendCommand(myCommandbuffer); |
jurica238814 | 8:2bf886335fd0 | 110 | // Prepaire data for transmition |
jurica238814 | 8:2bf886335fd0 | 111 | for (int i=0, j=0; i<msgSize; i++, j+=2) |
jurica238814 | 8:2bf886335fd0 | 112 | { |
jurica238814 | 8:2bf886335fd0 | 113 | // Take i-th byte and make hex string out of it |
jurica238814 | 8:2bf886335fd0 | 114 | sprintf((data+j),"%x", *(msg+i)); |
jurica238814 | 8:2bf886335fd0 | 115 | } |
jurica238814 | 8:2bf886335fd0 | 116 | for(int i=0, j=0; i<flagsSize; i++, j+=2) |
jurica238814 | 8:2bf886335fd0 | 117 | { |
jurica238814 | 8:2bf886335fd0 | 118 | // Take i-th byte and make hex string out of it |
jurica238814 | 8:2bf886335fd0 | 119 | sprintf((flagsHex+j), "%x", *(flags+i)); |
jurica238814 | 8:2bf886335fd0 | 120 | } |
jurica238814 | 8:2bf886335fd0 | 121 | |
jurica238814 | 8:2bf886335fd0 | 122 | sprintf(myCommandbuffer,"AT+NSOST=0,\"%s\",%d,%d,\"%s\"", |
jurica238814 | 8:2bf886335fd0 | 123 | _udp._ip, _udp._port, msgSize, data); |
jurica238814 | 8:2bf886335fd0 | 124 | |
jurica238814 | 8:2bf886335fd0 | 125 | msgSize += strlen(flags); |
jurica238814 | 8:2bf886335fd0 | 126 | sprintf(myCommandbuffer,"AT+NSOST=0,\"%s\",%d,%d,\"%s%s\"", |
jurica238814 | 8:2bf886335fd0 | 127 | _udp._ip, _udp._port, msgSize, flagsHex, data); |
jurica238814 | 8:2bf886335fd0 | 128 | sendCommand(myCommandbuffer); |
jurica238814 | 8:2bf886335fd0 | 129 | } |
jurica238814 | 8:2bf886335fd0 | 130 | |
jurica238814 | 8:2bf886335fd0 | 131 | uint8_t UBloxSara::connectNB() |
jurica238814 | 8:2bf886335fd0 | 132 | { |
jurica238814 | 8:2bf886335fd0 | 133 | sendCommand("at+NCONFIG=\"AUTOCONNECT\",\"FALSE\""); |
jurica238814 | 8:2bf886335fd0 | 134 | sendCommand("at+NCONFIG=\"CR_0354_0338_SCRAMBLING\",\"TRUE\""); |
jurica238814 | 8:2bf886335fd0 | 135 | |
jurica238814 | 8:2bf886335fd0 | 136 | sendCommand("at+NCONFIG=\"CR_0859_SI_AVOID\",\"TRUE\""); |
jurica238814 | 8:2bf886335fd0 | 137 | sendCommand("at+NCONFIG?"); |
jurica238814 | 8:2bf886335fd0 | 138 | sendCommand("at+cfun=0"); |
jurica238814 | 8:2bf886335fd0 | 139 | sendCommand("AT+CGDCONT=1, \"IP\",\"nb.inetd.gdsp\""); |
jurica238814 | 8:2bf886335fd0 | 140 | sendCommand("at+cfun=1"); |
jurica238814 | 8:2bf886335fd0 | 141 | |
jurica238814 | 8:2bf886335fd0 | 142 | sendCommand("at+cimi"); |
jurica238814 | 8:2bf886335fd0 | 143 | |
jurica238814 | 8:2bf886335fd0 | 144 | sendCommand("at+cgatt=1"); |
jurica238814 | 8:2bf886335fd0 | 145 | |
jurica238814 | 8:2bf886335fd0 | 146 | sendCommand("at+cops=1,2,\"26202\""); |
jurica238814 | 8:2bf886335fd0 | 147 | wait_ms(5000); |
jurica238814 | 8:2bf886335fd0 | 148 | sendCommand("at+cereg?"); |
jurica238814 | 8:2bf886335fd0 | 149 | wait_ms(5000); |
jurica238814 | 8:2bf886335fd0 | 150 | sendCommand("AT+CSQ"); |
jurica238814 | 8:2bf886335fd0 | 151 | wait_ms(5000); |
jurica238814 | 8:2bf886335fd0 | 152 | sendCommand("AT+COPS?"); |
jurica238814 | 8:2bf886335fd0 | 153 | sendCommand("AT+NBAND?"); |
jurica238814 | 8:2bf886335fd0 | 154 | sendCommand("AT+NBAND=20"); |
jurica238814 | 8:2bf886335fd0 | 155 | sendCommand("AT+NUESTATS"); |
jurica238814 | 8:2bf886335fd0 | 156 | |
jurica238814 | 8:2bf886335fd0 | 157 | sendCommand("AT+CGATT?"); |
jurica238814 | 8:2bf886335fd0 | 158 | wait_ms(5000); |
jurica238814 | 8:2bf886335fd0 | 159 | sendCommand("AT+CGPADDR"); |
jurica238814 | 8:2bf886335fd0 | 160 | } |