Sana Masood / Mbed OS example-ublox-at-cellular-interface-ext7

Dependencies:   ublox-cellular-driver-gen gnss ublox-cellular-base ublox-at-cellular-interface-ext

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2017 u-blox
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed.h"
00018 #include "UbloxATCellularInterfaceExt.h"
00019 #include "gnss.h"
00020 
00021 // The credentials of the SIM in the board.  If PIN checking is enabled
00022 // for your SIM card you must set this to the required PIN.
00023 #define PIN "0000"
00024 
00025 // Network credentials.  You should set this according to your
00026 // network/SIM card.  For C030 boards, leave the parameters as NULL
00027 // otherwise, if you do not know the APN for your network, you may
00028 // either try the fairly common "internet" for the APN (and leave the
00029 // username and password NULL), or you may leave all three as NULL and then
00030 // a lookup will be attempted for a small number of known networks
00031 // (see APN_db.h in mbed-os/features/netsocket/cellular/utils).
00032 #define APN         NULL
00033 #define USERNAME    NULL
00034 #define PASSWORD    NULL
00035 
00036 // LEDs
00037 DigitalOut ledRed(LED1, 1);
00038 DigitalOut ledGreen(LED2, 1);
00039 DigitalOut ledBlue(LED3, 1);
00040 
00041 // The user button
00042 volatile bool buttonPressed = false;
00043 
00044 static void good() {
00045     ledGreen = 0;
00046     ledBlue = 1;
00047     ledRed = 1;
00048 }
00049 
00050 static void bad() {
00051     ledRed = 0;
00052     ledGreen = 1;
00053     ledBlue = 1;
00054 }
00055 
00056 static void event() {
00057     ledBlue = 0;
00058     ledRed = 1;
00059     ledGreen = 1;
00060 }
00061 
00062 static void pulseEvent() {
00063     event();
00064     wait_ms(500);
00065     good();
00066 }
00067 
00068 static void ledOff() {
00069     ledBlue = 1;
00070     ledRed = 1;
00071     ledGreen = 1;
00072 }
00073 
00074 static void printCellLocateData(UbloxATCellularInterfaceExt::CellLocData *pData)
00075 {
00076     char timeString[25];
00077 
00078     printf("Cell Locate data:\n");
00079     if (strftime(timeString, sizeof(timeString), "%F %T", (const tm *) &(pData->time)) > 0) {
00080         printf("  time:               %s\n", timeString);
00081     }
00082     printf("  longitude:          %.6f\n", pData->longitude);
00083     printf("  latitude:           %.6f\n", pData->latitude);
00084     printf("  altitude:           %d metre(s)\n", pData->altitude);
00085     switch (pData->sensor) {
00086         case UbloxATCellularInterfaceExt::CELL_LAST:
00087             printf("  sensor type:        last\n");
00088             break;
00089         case UbloxATCellularInterfaceExt::CELL_GNSS:
00090             printf("  sensor type:        GNSS\n");
00091             break;
00092         case UbloxATCellularInterfaceExt::CELL_LOCATE:
00093             printf("  sensor type:        Cell Locate\n");
00094             break;
00095         case UbloxATCellularInterfaceExt::CELL_HYBRID:
00096             printf("  sensor type:        hybrid\n");
00097             break;
00098         default:
00099             printf("  sensor type:        unknown\n");
00100             break;
00101     }
00102     printf("  uncertainty:        %d metre(s)\n", pData->uncertainty);
00103     printf("  speed:              %d metre(s)/second\n", pData->speed);
00104     printf("  direction:          %d degree(s)\n", pData->direction);
00105     printf("  vertical accuracy:  %d metre(s)/second\n", pData->speed);
00106     printf("  satellite(s) used:  %d\n", pData->svUsed);
00107     printf("I am here: "
00108            "https://maps.google.com/?q=%.5f,%.5f\n", pData->latitude, pData->longitude);       
00109 }
00110 
00111 static void cbButton()
00112 {
00113     buttonPressed = true;
00114     pulseEvent();
00115 }
00116 
00117 /* This example program for the u-blox C030 and C027 boards instantiates
00118  * the UbloxAtCellularInterfaceExt to do FTP, HTTP and CellLocate operations.
00119  * It uses the site test.rebex.net for FTP testing and the site
00120  * developer.mbed.org for HTTP GET testing.
00121  * Progress may be monitored with a serial terminal running at 9600 baud.
00122  * The LED on the C030 board will turn green when this program is
00123  * operating correctly, pulse blue when an FTP get, HTTP get or CellLocate
00124  * operation is completed and turn red if there is a failure.
00125  */
00126 
00127 int main()
00128 {
00129     UbloxATCellularInterfaceExt *interface = new UbloxATCellularInterfaceExt();
00130     // If you need to debug the cellular interface, comment out the
00131     // instantiation above and uncomment the one below.
00132 //    UbloxATCellularInterfaceExt *interface = new UbloxATCellularInterfaceExt(MDMTXD, MDMRXD,
00133 //                                                                             MBED_CONF_UBLOX_CELL_BAUD_RATE,
00134 //                                                                             true);
00135     UbloxATCellularInterfaceExt::Error *err;
00136     UbloxATCellularInterfaceExt::CellLocData data;
00137     const int c_size_of_buffer = 2048;
00138     char *buf;
00139     int httpProfile;
00140     int numRes;
00141     GnssSerial gnssSerial; // This needed purely to power on the GNSS chip in 
00142                            // order that Cell Locate on the module can use it
00143 #ifdef TARGET_UBLOX_C027
00144     // No user button on C027
00145     InterruptIn userButton(NC);
00146 #else
00147     InterruptIn userButton(SW0);
00148 #endif
00149     
00150     // Attach a function to the user button
00151     userButton.rise(&cbButton);
00152     
00153     // Power up GNSS to assist with the Cell Locate bit
00154     gnssSerial.init();
00155     good();
00156     printf("Starting up, please wait up to 180 seconds for network registration to complete...\n");
00157 
00158     if (interface->init(PIN)) {
00159         int bytesRead = 0;
00160 
00161         pulseEvent();
00162         interface->set_credentials(APN, USERNAME, PASSWORD);
00163         printf("Registered, connecting to the packet network...\n");
00164         for (int x = 0; interface->connect() != 0; x++) {
00165             if (x > 0) {
00166                 bad();
00167                 printf("Retrying (have you checked that an antenna is plugged in and your APN is correct?)...\n");
00168             }
00169         }
00170         pulseEvent();
00171 
00172         buf = (char *) malloc(c_size_of_buffer);
00173 
00174         // FTP OPERATIONS
00175         // Reset FTP parameters to default then set things up
00176         printf("=== FTP ===\n");
00177         interface->ftpResetPar();
00178         interface->ftpSetTimeout(60000);
00179         interface->ftpSetPar(UbloxATCellularInterfaceExt::FTP_SERVER_NAME, "test.rebex.net");
00180         interface->ftpSetPar(UbloxATCellularInterfaceExt::FTP_USER_NAME, "demo");
00181         interface->ftpSetPar(UbloxATCellularInterfaceExt::FTP_PASSWORD, "password");
00182         interface->ftpSetPar(UbloxATCellularInterfaceExt::FTP_MODE, "1");
00183 
00184         // Log into the FTP server
00185         printf("Logging into FTP server \"test.rebex.net\"...\n");
00186         err = interface->ftpCommand(UbloxATCellularInterfaceExt::FTP_LOGIN);
00187         if (err == NULL) {
00188             pulseEvent();
00189              // Get a directory listing from the server
00190              if (interface->ftpCommand(UbloxATCellularInterfaceExt::FTP_LS,
00191                                       NULL, NULL, buf, c_size_of_buffer) == NULL) {
00192                 pulseEvent();
00193                 printf ("Directory listing of FTP server:\n"
00194                         "--------------------------------\n%s"
00195                         "--------------------------------\n", buf);
00196              }
00197              // FTP GET a file known to be on test.rebex.net into the module file system,
00198              // making sure it's not there to begin with
00199              interface->delFile("readme.txt");
00200              if (interface->ftpCommand(UbloxATCellularInterfaceExt::FTP_GET_FILE, "readme.txt") == NULL) {
00201                 pulseEvent();
00202                 // Read the file from the module file system into buf
00203                 bytesRead = interface->readFile("readme.txt", buf, c_size_of_buffer);
00204 
00205                 if (bytesRead) {
00206                     if (bytesRead < c_size_of_buffer) {
00207                         // Add null terminator
00208                         *(buf + bytesRead) = 0;
00209 
00210                         printf("FTP GET of file \"readme.txt\" completed.  The file contained:\n"
00211                                 "------------------------------------------------------------\n%s"
00212                                 "------------------------------------------------------------\n", buf);
00213                     }
00214                     else {
00215                         printf("Error: buffer overflow...\n");
00216                     }
00217 
00218                 }
00219                 else {
00220                     printf("Error: data not read from module's FS...\n");
00221                 }
00222              }
00223         } else {
00224             bad();
00225             printf ("Unable to log in, error class %d, error code %d.\n", err->eClass, err->eCode);
00226         }
00227 
00228         // HTTP OPERATIONS
00229         // Set up HTTP parameters
00230         printf("=== HTTP ===\n");
00231         printf("Performing HTTP GET on \"Github\"...\n");
00232         // Make sure bytesRead is equal to 0
00233         bytesRead = 0;
00234 
00235         httpProfile = interface->httpAllocProfile();
00236         interface->httpSetTimeout(httpProfile, 30000);
00237         interface->httpSetPar(httpProfile, UbloxATCellularInterfaceExt::HTTP_SECURE, "1");
00238         interface->httpSetPar(httpProfile, UbloxATCellularInterfaceExt::HTTP_SERVER_NAME, "raw.githubusercontent.com");
00239 
00240         // Do the HTTP command
00241         err = interface->httpCommand(httpProfile, UbloxATCellularInterfaceExt::HTTP_GET,
00242                                      "/u-blox/mbed-os/master/features/cellular/mbed_lib.json",
00243                                      NULL, NULL, 0, NULL,
00244                                      buf, c_size_of_buffer, &bytesRead);
00245 
00246         if (err == NULL) {
00247             pulseEvent();
00248 
00249             if(bytesRead > 0 && bytesRead < c_size_of_buffer) {
00250                 //Add null terminator
00251                 *(buf + bytesRead) = 0;
00252 
00253                 printf("HTTP GET of \"/u-blox/mbed-os/master/features/cellular/mbed_lib.json\" completed.  The response contained:\n"
00254                        "----------------------------------------------------------------------------------------\n%s"
00255                        "----------------------------------------------------------------------------------------\n", buf);
00256             }
00257             else {
00258                 printf("Error: Bytes read is zero");
00259             }
00260         } else {
00261             bad();
00262             printf("Unable to get \"/u-blox/mbed-os/master/features/cellular/mbed_lib.json\" from github, "
00263                    "error class %d, error code %d.\n", err->eClass, err->eCode);
00264         }
00265 #ifndef TARGET_UBLOX_C030_R41XM
00266         // CELL LOCATE OPERATIONS (in a loop)
00267         printf("=== Cell Locate ===\n");
00268 
00269         printf("Sending Cell Locate requests in a loop (until the user button is pressed on C030 or forever on C027)...\n");
00270         while (!buttonPressed) {
00271             interface->cellLocSrvUdp();
00272             interface->cellLocConfig(1); // Deep scan mode
00273             printf("Sending Cell Locate request...\n");
00274             if (interface->cellLocRequest(UbloxATCellularInterfaceExt::CELL_HYBRID, 10, 100,
00275                                          (UbloxATCellularInterfaceExt::CellRespType) 1, 1)) {
00276                 // Wait for the response
00277                 numRes = 0;
00278                 for (int x = 0; (numRes == 0) && (x < 10); x++) {
00279                     numRes = interface->cellLocGetRes();
00280                 }
00281 
00282                 if (numRes > 0) {
00283                     interface->cellLocGetData(&data);
00284                     if (data.validData) {
00285                         pulseEvent();
00286                         printCellLocateData(&data);
00287                     }
00288                 } else {
00289                     bad();
00290                     printf("No response from Cell Locate server.\n");
00291                 }
00292             }
00293             wait_ms(5000);
00294 
00295 #ifndef TARGET_UBLOX_C027
00296             printf("[Checking if user button has been pressed]\n");
00297 #endif
00298         }
00299 
00300         free(buf);
00301 
00302 #else
00303         while(!buttonPressed) {
00304             wait_ms(500);
00305             // Shift the LED states
00306             int carry = ledBlue;
00307             ledBlue = ledRed;
00308             ledRed = ledGreen;
00309             ledGreen = carry;
00310         }
00311 #endif
00312 
00313         pulseEvent();
00314         printf("User button was pressed, stopping...\n");
00315         gnssSerial.powerOff();
00316 
00317         interface->disconnect();
00318         interface->deinit();
00319         ledOff();
00320         printf("Stopped.\n");
00321     } else {
00322         bad();
00323         printf("Unable to initialise the interface.\n");
00324     }
00325 }
00326 
00327 // End Of File