![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
changed at for testing
Fork of example-reading-imei-imsi by
main.cpp@2:c845f4f6c2b6, 2018-10-12 (annotated)
- Committer:
- simudvarac
- Date:
- Fri Oct 12 06:34:14 2018 +0000
- Revision:
- 2:c845f4f6c2b6
- Parent:
- 1:63483ec4d0ff
- Child:
- 3:103f58fac4cf
changed AT for testing;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
euygun | 0:b51700dcba34 | 1 | /* mbed Microcontroller Library |
euygun | 0:b51700dcba34 | 2 | * Copyright (c) 2017 u-blox |
euygun | 0:b51700dcba34 | 3 | * |
euygun | 0:b51700dcba34 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
euygun | 0:b51700dcba34 | 5 | * you may not use this file except in compliance with the License. |
euygun | 0:b51700dcba34 | 6 | * You may obtain a copy of the License at |
euygun | 0:b51700dcba34 | 7 | * |
euygun | 0:b51700dcba34 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
euygun | 0:b51700dcba34 | 9 | * |
euygun | 0:b51700dcba34 | 10 | * Unless required by applicable law or agreed to in writing, software |
euygun | 0:b51700dcba34 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
euygun | 0:b51700dcba34 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
euygun | 0:b51700dcba34 | 13 | * See the License for the specific language governing permissions and |
euygun | 0:b51700dcba34 | 14 | * limitations under the License. |
euygun | 0:b51700dcba34 | 15 | */ |
euygun | 0:b51700dcba34 | 16 | |
euygun | 0:b51700dcba34 | 17 | #include "mbed.h" |
euygun | 0:b51700dcba34 | 18 | #include "ATCmdParser.h" |
euygun | 0:b51700dcba34 | 19 | #include "FileHandle.h" |
euygun | 0:b51700dcba34 | 20 | #include "onboard_modem_api.h" |
euygun | 0:b51700dcba34 | 21 | |
euygun | 0:b51700dcba34 | 22 | /* Definitions */ |
euygun | 0:b51700dcba34 | 23 | #define OUTPUT_ENTER_KEY "\r" |
euygun | 0:b51700dcba34 | 24 | #define AT_PARSER_BUFFER_SIZE 256 |
euygun | 0:b51700dcba34 | 25 | #define AT_PARSER_TIMEOUT 8*1000 // Milliseconds |
euygun | 0:b51700dcba34 | 26 | |
euygun | 0:b51700dcba34 | 27 | /* The example program for the u-blox C030 boards. It sets up the Cellular Module then read out the IMSI of the eUICC/SIM. |
euygun | 0:b51700dcba34 | 28 | * The Uer LEDs (RGB) and User Button are utilised to show the activity on the board. |
euygun | 0:b51700dcba34 | 29 | * When GNSS has a location fix, the Red Timepulse LED, next to the User LEDs, blinks every 1 second. |
euygun | 0:b51700dcba34 | 30 | */ |
euygun | 0:b51700dcba34 | 31 | |
euygun | 0:b51700dcba34 | 32 | /* File handler */ |
euygun | 0:b51700dcba34 | 33 | FileHandle *fh; |
euygun | 0:b51700dcba34 | 34 | |
euygun | 0:b51700dcba34 | 35 | /* AT Command Parser handle */ |
euygun | 0:b51700dcba34 | 36 | ATCmdParser *at; |
euygun | 0:b51700dcba34 | 37 | |
euygun | 0:b51700dcba34 | 38 | // User LEDs |
euygun | 0:b51700dcba34 | 39 | DigitalOut ledRed(LED1, 1); |
euygun | 0:b51700dcba34 | 40 | DigitalOut ledGreen(LED2, 1); |
euygun | 0:b51700dcba34 | 41 | DigitalOut ledBlue(LED3, 1); |
euygun | 0:b51700dcba34 | 42 | |
euygun | 0:b51700dcba34 | 43 | // Ethernet socket LED |
euygun | 0:b51700dcba34 | 44 | DigitalOut ledYellow(LED4,1); |
euygun | 0:b51700dcba34 | 45 | |
euygun | 0:b51700dcba34 | 46 | // User Button |
euygun | 0:b51700dcba34 | 47 | #ifdef TARGET_UBLOX_C027 |
euygun | 0:b51700dcba34 | 48 | // No user button on C027 |
euygun | 0:b51700dcba34 | 49 | InterruptIn userButton(NC); |
euygun | 0:b51700dcba34 | 50 | #else |
euygun | 0:b51700dcba34 | 51 | InterruptIn userButton(SW0); |
euygun | 0:b51700dcba34 | 52 | #endif |
euygun | 0:b51700dcba34 | 53 | |
euygun | 0:b51700dcba34 | 54 | // Delay between LED changes in second |
euygun | 0:b51700dcba34 | 55 | volatile float delay = 0.5; |
euygun | 0:b51700dcba34 | 56 | |
euygun | 0:b51700dcba34 | 57 | // To check if the user pressed the User Button or not |
euygun | 0:b51700dcba34 | 58 | void threadBodyUserButtonCheck(void const *args){ |
euygun | 0:b51700dcba34 | 59 | float delayToggle = delay; |
euygun | 0:b51700dcba34 | 60 | while (1){ |
euygun | 0:b51700dcba34 | 61 | if (userButton.read() == 1 ) { |
euygun | 0:b51700dcba34 | 62 | // User Button is pressed |
euygun | 0:b51700dcba34 | 63 | delay = 0.1; |
euygun | 0:b51700dcba34 | 64 | // Indicate with the Yellow LED on Ethernet socket |
euygun | 0:b51700dcba34 | 65 | ledYellow = 0; |
euygun | 0:b51700dcba34 | 66 | } |
euygun | 0:b51700dcba34 | 67 | else { |
euygun | 0:b51700dcba34 | 68 | // User button is released |
euygun | 0:b51700dcba34 | 69 | delay = 0.5; |
euygun | 0:b51700dcba34 | 70 | // Turn off the Yellow LED on Ethernet socket |
euygun | 0:b51700dcba34 | 71 | ledYellow = 1; |
euygun | 0:b51700dcba34 | 72 | } |
euygun | 0:b51700dcba34 | 73 | } |
euygun | 0:b51700dcba34 | 74 | } |
simudvarac | 2:c845f4f6c2b6 | 75 | void sendATCommandAndPrintResponse(char* command){ |
simudvarac | 2:c845f4f6c2b6 | 76 | char buffer[505];//sa crtom |
simudvarac | 2:c845f4f6c2b6 | 77 | for (int i=0;i<505;i++)buffer[i]=0; |
simudvarac | 2:c845f4f6c2b6 | 78 | printf ("Sending following command:"); |
simudvarac | 2:c845f4f6c2b6 | 79 | printf(command); |
simudvarac | 2:c845f4f6c2b6 | 80 | printf ("\r\n"); |
euygun | 0:b51700dcba34 | 81 | |
simudvarac | 2:c845f4f6c2b6 | 82 | if (!at->send(command)){ |
simudvarac | 2:c845f4f6c2b6 | 83 | printf("failed!!!!\r\n"); |
simudvarac | 2:c845f4f6c2b6 | 84 | return; |
simudvarac | 2:c845f4f6c2b6 | 85 | } |
simudvarac | 2:c845f4f6c2b6 | 86 | printf ("RESPONSE:\r\n"); |
simudvarac | 2:c845f4f6c2b6 | 87 | at->read(buffer,500); |
simudvarac | 2:c845f4f6c2b6 | 88 | printf(buffer); |
simudvarac | 2:c845f4f6c2b6 | 89 | printf ("\r\n"); |
simudvarac | 2:c845f4f6c2b6 | 90 | } |
euygun | 0:b51700dcba34 | 91 | // Setup the modem |
euygun | 0:b51700dcba34 | 92 | bool setup_modem(){ |
euygun | 0:b51700dcba34 | 93 | |
euygun | 0:b51700dcba34 | 94 | bool success = false; |
euygun | 0:b51700dcba34 | 95 | |
euygun | 0:b51700dcba34 | 96 | /* Initialize GPIO lines */ |
euygun | 0:b51700dcba34 | 97 | onboard_modem_init(); |
euygun | 0:b51700dcba34 | 98 | |
euygun | 0:b51700dcba34 | 99 | /* Give modem a little time to settle down */ |
euygun | 0:b51700dcba34 | 100 | wait_ms(250); |
euygun | 0:b51700dcba34 | 101 | |
RobMeades | 1:63483ec4d0ff | 102 | printf("Powering up the modem\r\n"); |
RobMeades | 1:63483ec4d0ff | 103 | onboard_modem_power_up(); |
RobMeades | 1:63483ec4d0ff | 104 | #ifdef TARGET_UBLOX_C030_N211 |
RobMeades | 1:63483ec4d0ff | 105 | wait_ms(5000); |
RobMeades | 1:63483ec4d0ff | 106 | #else |
RobMeades | 1:63483ec4d0ff | 107 | wait_ms(500); |
RobMeades | 1:63483ec4d0ff | 108 | #endif |
RobMeades | 1:63483ec4d0ff | 109 | |
RobMeades | 1:63483ec4d0ff | 110 | // Set AT parser timeout to 1sec for AT OK check |
RobMeades | 1:63483ec4d0ff | 111 | at->set_timeout(1000); |
RobMeades | 1:63483ec4d0ff | 112 | |
RobMeades | 1:63483ec4d0ff | 113 | printf("Checking for AT response from the modem\r\n"); |
euygun | 0:b51700dcba34 | 114 | for (int retry_count = 0; !success && (retry_count < 20); retry_count++) { |
euygun | 0:b51700dcba34 | 115 | printf("...wait\r\n"); |
RobMeades | 1:63483ec4d0ff | 116 | // The modem tends to sends out some garbage during power up. |
euygun | 0:b51700dcba34 | 117 | at->flush(); |
euygun | 0:b51700dcba34 | 118 | |
euygun | 0:b51700dcba34 | 119 | // AT OK talk to the modem |
euygun | 0:b51700dcba34 | 120 | if (at->send("AT")) { |
euygun | 0:b51700dcba34 | 121 | wait_ms(100); |
euygun | 0:b51700dcba34 | 122 | if (at->recv("OK")) { |
euygun | 0:b51700dcba34 | 123 | success = true; |
euygun | 0:b51700dcba34 | 124 | } |
euygun | 0:b51700dcba34 | 125 | } |
euygun | 0:b51700dcba34 | 126 | } |
RobMeades | 1:63483ec4d0ff | 127 | |
RobMeades | 1:63483ec4d0ff | 128 | // Increase the parser time to 8 sec |
RobMeades | 1:63483ec4d0ff | 129 | at->set_timeout(8000); |
RobMeades | 1:63483ec4d0ff | 130 | |
euygun | 0:b51700dcba34 | 131 | if (success) { |
RobMeades | 1:63483ec4d0ff | 132 | printf("Configuring the modem\r\n"); |
RobMeades | 1:63483ec4d0ff | 133 | |
RobMeades | 1:63483ec4d0ff | 134 | #ifdef TARGET_UBLOX_C030_N211 |
simudvarac | 2:c845f4f6c2b6 | 135 | printf("TARGET IS N211"); |
RobMeades | 1:63483ec4d0ff | 136 | // Turn off modem echoing and turn on verbose responses |
RobMeades | 1:63483ec4d0ff | 137 | success = at->send("AT+CMEE=1"); |
RobMeades | 1:63483ec4d0ff | 138 | #else |
simudvarac | 2:c845f4f6c2b6 | 139 | printf("TARGET IS NOT N211"); |
euygun | 0:b51700dcba34 | 140 | // Set the final baud rate |
euygun | 0:b51700dcba34 | 141 | if (at->send("AT+IPR=%d", 115200) && at->recv("OK")) { |
euygun | 0:b51700dcba34 | 142 | // Need to wait for things to be sorted out on the modem side |
euygun | 0:b51700dcba34 | 143 | wait_ms(100); |
euygun | 0:b51700dcba34 | 144 | ((UARTSerial *)fh)->set_baud(115200); |
euygun | 0:b51700dcba34 | 145 | } |
RobMeades | 1:63483ec4d0ff | 146 | |
euygun | 0:b51700dcba34 | 147 | // Turn off modem echoing and turn on verbose responses |
euygun | 0:b51700dcba34 | 148 | success = at->send("ATE0;+CMEE=2") && at->recv("OK") && |
euygun | 0:b51700dcba34 | 149 | // The following commands are best sent separately |
euygun | 0:b51700dcba34 | 150 | // Turn off RTC/CTS handshaking |
RobMeades | 1:63483ec4d0ff | 151 | at->send("AT&K0") && at->recv("OK") && |
euygun | 0:b51700dcba34 | 152 | // Set DCD circuit(109), changes in accordance with |
euygun | 0:b51700dcba34 | 153 | // the carrier detect status |
euygun | 0:b51700dcba34 | 154 | at->send("AT&C1") && at->recv("OK") && |
euygun | 0:b51700dcba34 | 155 | // Set DTR circuit, we ignore the state change of DTR |
euygun | 0:b51700dcba34 | 156 | at->send("AT&D0") && at->recv("OK"); |
RobMeades | 1:63483ec4d0ff | 157 | #endif |
euygun | 0:b51700dcba34 | 158 | } |
RobMeades | 1:63483ec4d0ff | 159 | |
euygun | 0:b51700dcba34 | 160 | return success; |
euygun | 0:b51700dcba34 | 161 | } |
euygun | 0:b51700dcba34 | 162 | /* |
euygun | 0:b51700dcba34 | 163 | ** Reading the modem IMEI and eUICC/SIM IMSI |
euygun | 0:b51700dcba34 | 164 | ** |
euygun | 0:b51700dcba34 | 165 | */ |
euygun | 0:b51700dcba34 | 166 | |
euygun | 0:b51700dcba34 | 167 | int main() |
euygun | 0:b51700dcba34 | 168 | { |
RobMeades | 1:63483ec4d0ff | 169 | bool success = false; |
RobMeades | 1:63483ec4d0ff | 170 | |
RobMeades | 1:63483ec4d0ff | 171 | printf("\n\r\n\ru-blox C030 reading the modem IMEI and eUICC/SIM IMSI\n\r"); |
euygun | 0:b51700dcba34 | 172 | printf("Initialising UART for modem communication"); |
RobMeades | 1:63483ec4d0ff | 173 | fh = new UARTSerial(MDMTXD, MDMRXD, 9600); |
euygun | 0:b51700dcba34 | 174 | printf("...DONE\r\n"); |
euygun | 0:b51700dcba34 | 175 | |
RobMeades | 1:63483ec4d0ff | 176 | // NOTE: if you are experiencing problems with the AT command |
RobMeades | 1:63483ec4d0ff | 177 | // exchange, change the "false" below to "true" to get debug output |
RobMeades | 1:63483ec4d0ff | 178 | // from the AT command parser |
RobMeades | 1:63483ec4d0ff | 179 | printf("Initialising the modem AT command parser"); |
euygun | 0:b51700dcba34 | 180 | at = new ATCmdParser(fh, OUTPUT_ENTER_KEY, AT_PARSER_BUFFER_SIZE, |
euygun | 0:b51700dcba34 | 181 | AT_PARSER_TIMEOUT, false); |
euygun | 0:b51700dcba34 | 182 | printf("...DONE\r\n"); |
euygun | 0:b51700dcba34 | 183 | |
euygun | 0:b51700dcba34 | 184 | printf("Initializing the modem\r\n"); |
RobMeades | 1:63483ec4d0ff | 185 | if (setup_modem()) { |
euygun | 0:b51700dcba34 | 186 | printf("...DONE\r\nThe modem powered up\r\n"); |
RobMeades | 1:63483ec4d0ff | 187 | char imei[25+1]; |
euygun | 0:b51700dcba34 | 188 | char imsi[15+1]; |
simudvarac | 2:c845f4f6c2b6 | 189 | char buffer[512]; |
euygun | 0:b51700dcba34 | 190 | printf("Reading IMEI and IMSI\r\n"); |
RobMeades | 1:63483ec4d0ff | 191 | |
RobMeades | 1:63483ec4d0ff | 192 | #ifdef TARGET_UBLOX_C030_N211 |
RobMeades | 1:63483ec4d0ff | 193 | if (at->send("AT+CGSN=1") && at->recv("\nOK\n%25[^\n]\nOK\n", imei)){ |
RobMeades | 1:63483ec4d0ff | 194 | printf("IMEI: %s\r\n",imei + 6); // Avoid the "+CGSN:" prefix |
RobMeades | 1:63483ec4d0ff | 195 | } |
simudvarac | 2:c845f4f6c2b6 | 196 | |
RobMeades | 1:63483ec4d0ff | 197 | #else |
euygun | 0:b51700dcba34 | 198 | if (at->send("AT+CGSN") && at->recv("%15[^\n]\nOK\n", imei)){ |
euygun | 0:b51700dcba34 | 199 | printf("IMEI: %s\r\n",imei); |
euygun | 0:b51700dcba34 | 200 | } |
RobMeades | 1:63483ec4d0ff | 201 | #endif |
RobMeades | 1:63483ec4d0ff | 202 | // Sometimes it takes a little while for the SIM to be initialised, |
RobMeades | 1:63483ec4d0ff | 203 | // so retry this until done |
RobMeades | 1:63483ec4d0ff | 204 | at->set_timeout(1000); |
RobMeades | 1:63483ec4d0ff | 205 | for (int retry_count = 0; !success && (retry_count < 10); retry_count++) { |
RobMeades | 1:63483ec4d0ff | 206 | if (at->send("AT+CIMI") && at->recv("%15[^\n]\nOK\n", imsi)){ |
RobMeades | 1:63483ec4d0ff | 207 | printf("IMSI: %s\r\n",imsi); |
RobMeades | 1:63483ec4d0ff | 208 | success = true; |
RobMeades | 1:63483ec4d0ff | 209 | } |
RobMeades | 1:63483ec4d0ff | 210 | } |
RobMeades | 1:63483ec4d0ff | 211 | if (!success) { |
RobMeades | 1:63483ec4d0ff | 212 | printf("Unable to read IMSI: has a SIM been inserted?\r\n"); |
euygun | 0:b51700dcba34 | 213 | } |
RobMeades | 1:63483ec4d0ff | 214 | } else { |
RobMeades | 1:63483ec4d0ff | 215 | printf("Unable to intialize modem\r\n"); |
euygun | 0:b51700dcba34 | 216 | } |
simudvarac | 2:c845f4f6c2b6 | 217 | char buffer[500+5]; |
RobMeades | 1:63483ec4d0ff | 218 | printf("FINISHED...\r\n"); |
simudvarac | 2:c845f4f6c2b6 | 219 | printf ("sending nconfig"); |
simudvarac | 2:c845f4f6c2b6 | 220 | printf ("\r\n"); |
simudvarac | 2:c845f4f6c2b6 | 221 | |
simudvarac | 2:c845f4f6c2b6 | 222 | |
simudvarac | 2:c845f4f6c2b6 | 223 | |
simudvarac | 2:c845f4f6c2b6 | 224 | |
simudvarac | 2:c845f4f6c2b6 | 225 | sendATCommandAndPrintResponse("at+NCONFIG=\"AUTOCONNECT\",\"FALSE\""); |
simudvarac | 2:c845f4f6c2b6 | 226 | sendATCommandAndPrintResponse("at+NCONFIG=\"CR_0354_0338_SCRAMBLING\",\"TRUE\""); |
simudvarac | 2:c845f4f6c2b6 | 227 | |
simudvarac | 2:c845f4f6c2b6 | 228 | |
simudvarac | 2:c845f4f6c2b6 | 229 | sendATCommandAndPrintResponse("at+NCONFIG=\"CR_0859_SI_AVOID\",\"TRUE\""); |
simudvarac | 2:c845f4f6c2b6 | 230 | sendATCommandAndPrintResponse("at+NCONFIG?"); |
simudvarac | 2:c845f4f6c2b6 | 231 | sendATCommandAndPrintResponse("at+cfun=0"); |
simudvarac | 2:c845f4f6c2b6 | 232 | sendATCommandAndPrintResponse("AT+CGDCONT=1, \"IP\",\"nb.inetd.gdsp\""); |
simudvarac | 2:c845f4f6c2b6 | 233 | sendATCommandAndPrintResponse("at+cfun=1"); |
simudvarac | 2:c845f4f6c2b6 | 234 | |
simudvarac | 2:c845f4f6c2b6 | 235 | sendATCommandAndPrintResponse("at+cimi"); |
simudvarac | 2:c845f4f6c2b6 | 236 | |
simudvarac | 2:c845f4f6c2b6 | 237 | |
simudvarac | 2:c845f4f6c2b6 | 238 | sendATCommandAndPrintResponse("at+cgatt=1"); |
simudvarac | 2:c845f4f6c2b6 | 239 | |
simudvarac | 2:c845f4f6c2b6 | 240 | |
simudvarac | 2:c845f4f6c2b6 | 241 | |
simudvarac | 2:c845f4f6c2b6 | 242 | sendATCommandAndPrintResponse("at+cops=1,2,\"26202\""); |
euygun | 0:b51700dcba34 | 243 | |
simudvarac | 2:c845f4f6c2b6 | 244 | sendATCommandAndPrintResponse("AT+CSQ"); |
simudvarac | 2:c845f4f6c2b6 | 245 | sendATCommandAndPrintResponse("AT+CGAT?"); |
simudvarac | 2:c845f4f6c2b6 | 246 | sendATCommandAndPrintResponse("AT+COPS?"); |
simudvarac | 2:c845f4f6c2b6 | 247 | sendATCommandAndPrintResponse("AT+NBAND?"); |
simudvarac | 2:c845f4f6c2b6 | 248 | sendATCommandAndPrintResponse("AT+NBAND=20"); |
simudvarac | 2:c845f4f6c2b6 | 249 | sendATCommandAndPrintResponse("AT+NUESTATS?"); |
simudvarac | 2:c845f4f6c2b6 | 250 | sendATCommandAndPrintResponse("AT+CGAT=1"); |
simudvarac | 2:c845f4f6c2b6 | 251 | sendATCommandAndPrintResponse("AT+CGAT?"); |
simudvarac | 2:c845f4f6c2b6 | 252 | |
simudvarac | 2:c845f4f6c2b6 | 253 | sendATCommandAndPrintResponse("AT+NSOCR=DGRAM,17,3333"); |
simudvarac | 2:c845f4f6c2b6 | 254 | |
simudvarac | 2:c845f4f6c2b6 | 255 | sendATCommandAndPrintResponse("AT> AT+NSOST=0,52.215.10.12,17,4,AAAAAAAA"); |
simudvarac | 2:c845f4f6c2b6 | 256 | |
euygun | 0:b51700dcba34 | 257 | // Create threadUserButtonCheck thread |
euygun | 0:b51700dcba34 | 258 | Thread threadUserButtonCheck(threadBodyUserButtonCheck); |
euygun | 0:b51700dcba34 | 259 | |
euygun | 0:b51700dcba34 | 260 | // Set the LED states |
euygun | 0:b51700dcba34 | 261 | ledRed = 0; |
euygun | 0:b51700dcba34 | 262 | ledGreen = 1; |
euygun | 0:b51700dcba34 | 263 | ledBlue = 1; |
euygun | 0:b51700dcba34 | 264 | |
euygun | 0:b51700dcba34 | 265 | // Main loop |
euygun | 0:b51700dcba34 | 266 | while(1) { |
euygun | 0:b51700dcba34 | 267 | wait(delay); |
euygun | 0:b51700dcba34 | 268 | // Shift the LED states |
euygun | 0:b51700dcba34 | 269 | int carry = ledBlue; |
euygun | 0:b51700dcba34 | 270 | ledBlue = ledRed; |
euygun | 0:b51700dcba34 | 271 | ledRed = ledGreen; |
euygun | 0:b51700dcba34 | 272 | ledGreen = carry; |
euygun | 0:b51700dcba34 | 273 | } |
euygun | 0:b51700dcba34 | 274 | } |
euygun | 0:b51700dcba34 | 275 | |
euygun | 0:b51700dcba34 | 276 | // End Of File |