Example to read the modem IMEI and eUICC/SIM IMSI

Committer:
euygun
Date:
Fri Oct 13 16:09:30 2017 +0000
Revision:
0:b51700dcba34
Child:
1:63483ec4d0ff
First commit of "example-reading-imei-imsi" for C030-U201; ;

Who changed what in which revision?

UserRevisionLine numberNew 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 }
euygun 0:b51700dcba34 75
euygun 0:b51700dcba34 76 // Setup the modem
euygun 0:b51700dcba34 77 bool setup_modem(){
euygun 0:b51700dcba34 78
euygun 0:b51700dcba34 79 bool success = false;
euygun 0:b51700dcba34 80
euygun 0:b51700dcba34 81 /* Initialize GPIO lines */
euygun 0:b51700dcba34 82 onboard_modem_init();
euygun 0:b51700dcba34 83
euygun 0:b51700dcba34 84 /* Give modem a little time to settle down */
euygun 0:b51700dcba34 85 wait_ms(250);
euygun 0:b51700dcba34 86
euygun 0:b51700dcba34 87 for (int retry_count = 0; !success && (retry_count < 20); retry_count++) {
euygun 0:b51700dcba34 88 printf("...wait\r\n");
euygun 0:b51700dcba34 89 onboard_modem_power_up();
euygun 0:b51700dcba34 90 wait_ms(500);
euygun 0:b51700dcba34 91 // The modem tends to sends out some garbage during power up. Needs to clean up.
euygun 0:b51700dcba34 92 at->flush();
euygun 0:b51700dcba34 93
euygun 0:b51700dcba34 94 // Set AT parser timeout to 1sec for AT OK check
euygun 0:b51700dcba34 95 at->set_timeout(1000);
euygun 0:b51700dcba34 96
euygun 0:b51700dcba34 97 // AT OK talk to the modem
euygun 0:b51700dcba34 98 if (at->send("AT")) {
euygun 0:b51700dcba34 99 wait_ms(100);
euygun 0:b51700dcba34 100 if (at->recv("OK")) {
euygun 0:b51700dcba34 101 success = true;
euygun 0:b51700dcba34 102 }
euygun 0:b51700dcba34 103 }
euygun 0:b51700dcba34 104 // Increase the parser time to 8sec
euygun 0:b51700dcba34 105 at->set_timeout(8000);
euygun 0:b51700dcba34 106 }
euygun 0:b51700dcba34 107 if (success) {
euygun 0:b51700dcba34 108 // Set the final baud rate
euygun 0:b51700dcba34 109 if (at->send("AT+IPR=%d", 115200) && at->recv("OK")) {
euygun 0:b51700dcba34 110 // Need to wait for things to be sorted out on the modem side
euygun 0:b51700dcba34 111 wait_ms(100);
euygun 0:b51700dcba34 112 ((UARTSerial *)fh)->set_baud(115200);
euygun 0:b51700dcba34 113 }
euygun 0:b51700dcba34 114
euygun 0:b51700dcba34 115 // Turn off modem echoing and turn on verbose responses
euygun 0:b51700dcba34 116 success = at->send("ATE0;+CMEE=2") && at->recv("OK") &&
euygun 0:b51700dcba34 117 // The following commands are best sent separately
euygun 0:b51700dcba34 118
euygun 0:b51700dcba34 119 // Turn off RTC/CTS handshaking
euygun 0:b51700dcba34 120 at->send("AT&K0") && at->recv("OK") &&
euygun 0:b51700dcba34 121 // Set DCD circuit(109), changes in accordance with
euygun 0:b51700dcba34 122 // the carrier detect status
euygun 0:b51700dcba34 123 at->send("AT&C1") && at->recv("OK") &&
euygun 0:b51700dcba34 124 // Set DTR circuit, we ignore the state change of DTR
euygun 0:b51700dcba34 125 at->send("AT&D0") && at->recv("OK");
euygun 0:b51700dcba34 126 }
euygun 0:b51700dcba34 127 return success;
euygun 0:b51700dcba34 128 }
euygun 0:b51700dcba34 129 /*
euygun 0:b51700dcba34 130 ** Reading the modem IMEI and eUICC/SIM IMSI
euygun 0:b51700dcba34 131 **
euygun 0:b51700dcba34 132 */
euygun 0:b51700dcba34 133
euygun 0:b51700dcba34 134 int main()
euygun 0:b51700dcba34 135 {
euygun 0:b51700dcba34 136 printf("\n\r\n\ru-blox C030-U201 Reading the modem IMEI and eUICC/SIM IMSI\n\r");
euygun 0:b51700dcba34 137 printf("Initialising UART for modem communication");
euygun 0:b51700dcba34 138 fh = new UARTSerial(MDMTXD, MDMRXD, 115200);
euygun 0:b51700dcba34 139 printf("...DONE\r\n");
euygun 0:b51700dcba34 140
euygun 0:b51700dcba34 141 printf("Initialising the Modem AT Command Parser");
euygun 0:b51700dcba34 142 at = new ATCmdParser(fh, OUTPUT_ENTER_KEY, AT_PARSER_BUFFER_SIZE,
euygun 0:b51700dcba34 143 AT_PARSER_TIMEOUT, false);
euygun 0:b51700dcba34 144 printf("...DONE\r\n");
euygun 0:b51700dcba34 145
euygun 0:b51700dcba34 146 printf("Initializing the modem\r\n");
euygun 0:b51700dcba34 147 if (setup_modem()){
euygun 0:b51700dcba34 148 printf("...DONE\r\nThe modem powered up\r\n");
euygun 0:b51700dcba34 149 char imei[15+1];
euygun 0:b51700dcba34 150 char imsi[15+1];
euygun 0:b51700dcba34 151 printf("Reading IMEI and IMSI\r\n");
euygun 0:b51700dcba34 152 if (at->send("AT+CGSN") && at->recv("%15[^\n]\nOK\n", imei)){
euygun 0:b51700dcba34 153 printf("IMEI: %s\r\n",imei);
euygun 0:b51700dcba34 154 }
euygun 0:b51700dcba34 155 if (at->send("AT+CIMI") && at->recv("%15[^\n]\nOK\n", imsi)){
euygun 0:b51700dcba34 156 printf("IMSI: %s\r\n",imsi);
euygun 0:b51700dcba34 157 }
euygun 0:b51700dcba34 158 }
euygun 0:b51700dcba34 159 else {
euygun 0:b51700dcba34 160 printf("Unable To intialize modem\r\n");
euygun 0:b51700dcba34 161 }
euygun 0:b51700dcba34 162
euygun 0:b51700dcba34 163 printf("FINISHED...\n\r");
euygun 0:b51700dcba34 164
euygun 0:b51700dcba34 165 // Create threadUserButtonCheck thread
euygun 0:b51700dcba34 166 Thread threadUserButtonCheck(threadBodyUserButtonCheck);
euygun 0:b51700dcba34 167
euygun 0:b51700dcba34 168 // Set the LED states
euygun 0:b51700dcba34 169 ledRed = 0;
euygun 0:b51700dcba34 170 ledGreen = 1;
euygun 0:b51700dcba34 171 ledBlue = 1;
euygun 0:b51700dcba34 172
euygun 0:b51700dcba34 173 // Main loop
euygun 0:b51700dcba34 174 while(1) {
euygun 0:b51700dcba34 175 wait(delay);
euygun 0:b51700dcba34 176 // Shift the LED states
euygun 0:b51700dcba34 177 int carry = ledBlue;
euygun 0:b51700dcba34 178 ledBlue = ledRed;
euygun 0:b51700dcba34 179 ledRed = ledGreen;
euygun 0:b51700dcba34 180 ledGreen = carry;
euygun 0:b51700dcba34 181 }
euygun 0:b51700dcba34 182 }
euygun 0:b51700dcba34 183
euygun 0:b51700dcba34 184 // End Of File