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
Diff: UBloxSara/uBloxSara.cpp
- Revision:
- 8:2bf886335fd0
- Child:
- 9:f943c09d9173
--- /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"); +}