diff --git a/examples/AGNSS_example/AGNSS_example.ino b/examples/AGNSS_example/AGNSS_example.ino index d0a9b96..913aa84 100644 --- a/examples/AGNSS_example/AGNSS_example.ino +++ b/examples/AGNSS_example/AGNSS_example.ino @@ -142,9 +142,10 @@ void setup() { Serial.println("Set on/off GNSS controller"); rc = myME310.gnss_controller_power_management(1); Serial.println(ME310::return_string(rc)); + #if NMEA_DEBUG if (rc == ME310::RETURN_VALID) { - #if NMEA_DEBUG + ///////////////////////////////////// // Set GNGSA, GLGSV and GNRMC as available sentence in the unsolicited NMEA sentences. // AT$GPSNMUNEX=0,1,1,0,0,0,0,0,0,0,0,1,0 @@ -158,15 +159,20 @@ void setup() { ///////////////////////////////////// rc = myME310.gnss_nmea_data_configuration(2,1,0,1,1,1,0); int i = 0; - while(strcmp(myME310.buffer_cstr(i), "OK") != 0) + char* buff = (char*) myME310.buffer_cstr(0); + if(buff != NULL) { - Serial.println(myME310.buffer_cstr(i)); - i++; + while(strcmp(buff, "OK") != 0) + { + buff = (char*) myME310.buffer_cstr(i); + Serial.println(buff); + i++; + } } } - #endif - } + } + #endif ///////////////////////////////////// // Set the real-time clock of the module. // AT+CCLK="05/07/21,12:40:00+00" @@ -208,12 +214,16 @@ void loop() { /*When the position is fixed, the led blinks*/ if (rc == ME310::RETURN_VALID) { - Serial.println(myME310.buffer_cstr(1)); + char* buff = (char*)myME310.buffer_cstr(1); + Serial.println(buff); std::string tmp_pos; - tmp_pos = myME310.buffer_cstr(1); - std::size_t len_pos = tmp_pos.find(":"); - if(len_pos != std::string::npos) + + if(buff != NULL) { + tmp_pos = buff; + std::size_t len_pos = tmp_pos.find(":"); + if(len_pos != std::string::npos) + { std::size_t len_pos2 = tmp_pos.find(","); char valid_pos[64]; if(len_pos2 != std::string::npos) @@ -227,6 +237,7 @@ void loop() { delay(1000); } } + } } } delay(5000); @@ -244,7 +255,7 @@ void loop() { rc = myME310.gnss_nmea_extended_data_configuration(0); if(rc == ME310::RETURN_VALID) { - Serial.println("De-activate unsolicited NMEA sentences flow"); + Serial.println("De-activate unsolicited NMEA sentences flow"); } #endif ///////////////////////////////////// diff --git a/examples/CLIP_example/CLIP_example.ino b/examples/CLIP_example/CLIP_example.ino index eca083c..dc86124 100644 --- a/examples/CLIP_example/CLIP_example.ino +++ b/examples/CLIP_example/CLIP_example.ino @@ -70,50 +70,69 @@ void setup() { Serial.println("AT Command"); myME310.report_mobile_equipment_error(2); //issue command AT+CMEE=2 and wait for answer or timeout rc = myME310.read_enter_pin(); //issue command AT+cpin? in read mode, check that the SIM is inserted and the module is not waiting for the PIN - if(strcmp(myME310.buffer_cstr(2), "OK") == 0) // read response in 2 array position + char *resp = (char*)myME310.buffer_cstr(2); + if(resp != NULL) { - delay(2000); - Serial.println("Define PDP Context"); - rc = myME310.define_pdp_context(cID, ipProt, APN); //issue command AT+CGDCONT=cid,PDP_type,APN - if(rc == ME310::RETURN_VALID) + if(strcmp(resp, "OK") == 0) // read response in 2 array position { - myME310.read_define_pdp_context(); //issue command AT+CGDCONT=? (read mode) - Serial.print("pdp context read :"); - Serial.println(myME310.buffer_cstr(1)); //print second line of modem answer - - Serial.print("gprs network registration status :"); - rc = myME310.read_gprs_network_registration_status(); //issue command AT+CGREG? (read mode) - Serial.println(myME310.buffer_cstr(1)); + delay(2000); + Serial.println("Define PDP Context"); + rc = myME310.define_pdp_context(cID, ipProt, APN); //issue command AT+CGDCONT=cid,PDP_type,APN if(rc == ME310::RETURN_VALID) { - while ((strcmp(myME310.buffer_cstr(1), "+CGREG: 0,1") != 0) && (strcmp(myME310.buffer_cstr(1), "+CGREG: 0,5") != 0)) + myME310.read_define_pdp_context(); //issue command AT+CGDCONT=? (read mode) + Serial.print("pdp context read :"); + Serial.println(myME310.buffer_cstr(1)); //print second line of modem answer + + Serial.print("gprs network registration status :"); + rc = myME310.read_gprs_network_registration_status(); //issue command AT+CGREG? (read mode) + Serial.println(myME310.buffer_cstr(1)); + if(rc == ME310::RETURN_VALID) + { + resp = (char*)myME310.buffer_cstr(1); + while(resp != NULL) { + if((strcmp(resp, "+CGREG: 0,1") != 0) && (strcmp(resp, "+CGREG: 0,5") != 0)) + { delay(3000); - myME310.read_gprs_network_registration_status(); + rc = myME310.read_gprs_network_registration_status(); + if(rc != ME310::RETURN_VALID) + { + Serial.println("ERROR"); + Serial.println(myME310.return_string(rc)); + break; + } Serial.println(myME310.buffer_cstr(1)); + resp = (char*) myME310.buffer_cstr(1); + } + else + { + break; + } } + } + Serial.println("Activate context"); + myME310.context_activation(cID, 1); //issue command AT#SGACT=cid,state and wait for answer or timeout + + rc = myME310.calling_line_identification_presentation(1); //issue command AT+CLIP=enable and wait for answer or timeout + Serial.println(myME310.return_string(rc)); + if(rc == ME310::RETURN_VALID) + { + Serial.println("Calling Line Identity is enable"); + flag = true; + } + else + { + Serial.println((String)"Error: " + rc + " Error string: " + myME310.buffer_cstr(2)); + flag = false; + } } - Serial.println("Activate context"); - myME310.context_activation(cID, 1); //issue command AT#SGACT=cid,state and wait for answer or timeout - - rc = myME310.calling_line_identification_presentation(1); //issue command AT+CLIP=enable and wait for answer or timeout - Serial.println(rc); - if(rc == ME310::RETURN_VALID) - { - Serial.println("Calling Line Identity is enable"); - flag = true; - } - else - { + } + else + { Serial.println((String)"Error: " + rc + " Error string: " + myME310.buffer_cstr(2)); - flag = false; - } } } - else - { - Serial.println((String)"Error: " + rc + " Error string: " + myME310.buffer_cstr(2)); - } } void loop() { diff --git a/examples/FTP_example/FTP_example.ino b/examples/FTP_example/FTP_example.ino index faf2de2..2b84abd 100644 --- a/examples/FTP_example/FTP_example.ino +++ b/examples/FTP_example/FTP_example.ino @@ -31,11 +31,11 @@ #include #include -#define APN "apn" +#define APN "APN" -#define FTP_ADDR_PORT "ftp.telit.com" -#define FTP_USER "Username" -#define FTP_PASS "Password" +#define FTP_ADDR_PORT "PORT" +#define FTP_USER "CLIENTUSER" +#define FTP_PASS "PASSWORD" #ifndef ARDUINO_TELIT_SAMD_CHARLIE #define ON_OFF 6 /*Select the GPIO to control ON_OFF*/ @@ -72,41 +72,64 @@ void setup() { Serial.println("AT Command"); myME310.report_mobile_equipment_error(2); //issue command AT+CMEE=2 and wait for answer or timeout rc = myME310.read_enter_pin(); //issue command AT+cpin? in read mode, check that the SIM is inserted and the module is not waiting for the PIN - if(strcmp(myME310.buffer_cstr(2), "OK") == 0) // read response in 2 array position + if(rc == ME310::RETURN_VALID) { - Serial.println("Define PDP Context"); - rc = myME310.define_pdp_context(cID, ipProt, APN); //issue command AT+CGDCONT=cid,PDP_type,APN - if(rc == ME310::RETURN_VALID) + char *resp = (char*)myME310.buffer_cstr(2); + if(resp != NULL) { - myME310.read_define_pdp_context(); //issue command AT+CGDCONT=? (read mode) - Serial.print("pdp context read :"); - Serial.println(myME310.buffer_cstr(1)); //print second line of modem answer - - Serial.print("gprs network registration status :"); - rc = myME310.read_gprs_network_registration_status(); //issue command AT+CGREG? (read mode) - Serial.println(myME310.buffer_cstr(1)); - if(rc == ME310::RETURN_VALID) - { - while ((strcmp(myME310.buffer_cstr(1), "+CGREG: 0,1") != 0) && (strcmp(myME310.buffer_cstr(1), "+CGREG: 0,5") != 0)) + if(strcmp(resp, "OK") == 0) // read response in 2 array position + { + Serial.println("Define PDP Context"); + rc = myME310.define_pdp_context(cID, ipProt, APN); //issue command AT+CGDCONT=cid,PDP_type,APN + if(rc == ME310::RETURN_VALID) { - delay(3000); - myME310.read_gprs_network_registration_status(); - Serial.println(myME310.buffer_cstr(1)); + myME310.read_define_pdp_context(); //issue command AT+CGDCONT=? (read mode) + Serial.print("pdp context read :"); + Serial.println(myME310.buffer_cstr(1)); //print second line of modem answer + + Serial.print("gprs network registration status :"); + rc = myME310.read_gprs_network_registration_status(); //issue command AT+CGREG? (read mode) + Serial.println(myME310.buffer_cstr(1)); + if(rc == ME310::RETURN_VALID) + { + resp = (char*)myME310.buffer_cstr(1); + while(resp != NULL) + { + if ((strcmp(resp, "+CGREG: 0,1") != 0) && (strcmp(resp, "+CGREG: 0,5") != 0)) + { + delay(3000); + rc = myME310.read_gprs_network_registration_status(); + if(rc != ME310::RETURN_VALID) + { + Serial.println("ERROR"); + Serial.println(myME310.return_string(rc)); + break; + } + Serial.println(myME310.buffer_cstr(1)); + resp = (char*)myME310.buffer_cstr(1); + } + else + { + break; + } + } + } + Serial.println("Activate context"); + myME310.context_activation(cID, 1); //issue command AT#SGACT=cid,state and wait for answer or timeout } - } - Serial.println("Activate context"); - myME310.context_activation(cID, 1); //issue command AT#SGACT=cid,state and wait for answer or timeout + } + else + { + Serial.println((String)"Error: " + rc + " Error string: " + myME310.buffer_cstr(2)); + } } } - else - { - Serial.println((String)"Error: " + rc + " Error string: " + myME310.buffer_cstr(2)); - } + } void loop() { - char data[] = "We are appending some FTP data\nAnother data"; + const char data[] = "We are appending some FTP data\nAnother data"; rc = myME310.ftp_close(); if(rc == ME310::RETURN_VALID) @@ -140,7 +163,7 @@ void loop() { Serial.println((String)"Ftp put result: " +myME310.return_string(rc)); if(rc == ME310::RETURN_VALID) { - rc = myME310.ftp_append_extended((int)sizeof(data), data, 1); //issue command AT#FTPAPPEXT=dataSize, data and wait for answer or timeout + rc = myME310.ftp_append_extended((int)sizeof(data), (char*)data, 1); //issue command AT#FTPAPPEXT=dataSize, data and wait for answer or timeout Serial.println((String)"Ftp append extended result: " +myME310.return_string(rc)); if(rc == ME310::RETURN_VALID) { diff --git a/examples/GNSS_example/GNSS_example.ino b/examples/GNSS_example/GNSS_example.ino index b703307..e0ea4ed 100644 --- a/examples/GNSS_example/GNSS_example.ino +++ b/examples/GNSS_example/GNSS_example.ino @@ -131,30 +131,30 @@ void setup() { // 1 : GNSS controller is powered up ///////////////////////////////////// rc = myME310.gnss_controller_power_management(1); - if (rc == ME310::RETURN_VALID) - { - #if NMEA_DEBUG - ///////////////////////////////////// - // Set GNGSA, GLGSV and GNRMC as available sentence in the unsolicited NMEA sentences. - // AT$GPSNMUNEX=0,1,1,0,0,0,0,0,0,0,0,1,0 - ///////////////////////////////////// - rc = myME310.gnss_nmea_extended_data_configuration(0,1,1,0,0,0,0,0,0,0,0,1,0); - if(rc == ME310::RETURN_VALID) + #if NMEA_DEBUG + if (rc == ME310::RETURN_VALID) { ///////////////////////////////////// - // Activate unsolicited NMEA sentences flow in the AT port and GPGGA,GPRMC, GPGSA and GPGSV sentences. - // AT$GPSNMUN=2,1,0,1,1,1,0 + // Set GNGSA, GLGSV and GNRMC as available sentence in the unsolicited NMEA sentences. + // AT$GPSNMUNEX=0,1,1,0,0,0,0,0,0,0,0,1,0 ///////////////////////////////////// - rc = myME310.gnss_nmea_data_configuration(2,1,0,1,1,1,0); - int i = 0; - while(strcmp(myME310.buffer_cstr(i), "OK") != 0) + rc = myME310.gnss_nmea_extended_data_configuration(0,1,1,0,0,0,0,0,0,0,0,1,0); + if(rc == ME310::RETURN_VALID) { - Serial.println(myME310.buffer_cstr(i)); - i++; + ///////////////////////////////////// + // Activate unsolicited NMEA sentences flow in the AT port and GPGGA,GPRMC, GPGSA and GPGSV sentences. + // AT$GPSNMUN=2,1,0,1,1,1,0 + ///////////////////////////////////// + rc = myME310.gnss_nmea_data_configuration(2,1,0,1,1,1,0); + int i = 0; + while(strcmp(myME310.buffer_cstr(i), "OK") != 0) + { + Serial.println(myME310.buffer_cstr(i)); + i++; + } } } - #endif - } + #endif } void loop() { @@ -170,11 +170,14 @@ void loop() { if (rc == ME310::RETURN_VALID) { Serial.println(myME310.buffer_cstr(1)); + char* buff = (char*)myME310.buffer_cstr(1); std::string tmp_pos; - tmp_pos = myME310.buffer_cstr(1); - std::size_t len_pos = tmp_pos.find(":"); - if(len_pos != std::string::npos) + if(buff != NULL) { + tmp_pos = buff; + std::size_t len_pos = tmp_pos.find(":"); + if(len_pos != std::string::npos) + { std::size_t len_pos2 = tmp_pos.find(","); char valid_pos[64]; if(len_pos2 != std::string::npos) @@ -188,6 +191,7 @@ void loop() { delay(1000); } } + } } } delay(5000); diff --git a/examples/LWM2M_example/LWM2M_example_2G/LWM2M_example_2G.ino b/examples/LWM2M_example/LWM2M_example_2G/LWM2M_example_2G.ino index 64a0126..38f11f3 100644 --- a/examples/LWM2M_example/LWM2M_example_2G/LWM2M_example_2G.ino +++ b/examples/LWM2M_example/LWM2M_example_2G/LWM2M_example_2G.ino @@ -59,7 +59,7 @@ ME310 myME310; void setup() { char pin[] = "XXXX"; - int rc; + ME310::return_t rc; pinMode(ON_OFF, OUTPUT); pinMode(LED_BUILTIN, OUTPUT); Serial.begin(115200); @@ -85,76 +85,114 @@ void setup() { myME310.read_enter_pin(); // read response in 1 array position - if (strcmp(myME310.buffer_cstr(1), "+CPIN: SIM PIN") == 0) + char *resp = (char*)myME310.buffer_cstr(1); + if(resp != NULL) { - Serial.println("Insert SIM PIN"); - - //issue command AT+CPIN=pin - rc = myME310.enter_pin(pin); - if (rc == ME310::RETURN_VALID) + if (strcmp(resp, "+CPIN: SIM PIN") == 0) { - Serial.println("PIN inserted"); - Serial.println("Select wireless tecnology "); + Serial.println("Insert SIM PIN"); - //issue command AT+WS46=12(2G) - rc = myME310.select_wireless_network(12); + //issue command AT+CPIN=pin + rc = myME310.enter_pin(pin); if (rc == ME310::RETURN_VALID) { - //issue command AT+CREG? to check the network status - Serial.println("Network status"); - rc = myME310.read_network_registration_status(); - Serial.println(myME310.buffer_cstr(1)); + Serial.println("PIN inserted"); + Serial.println("Select wireless tecnology "); + + //issue command AT+WS46=12(2G) + rc = myME310.select_wireless_network(12); if (rc == ME310::RETURN_VALID) { - //if +CREG!=0,1 and +CREG!=0,5, wait 3 second than retry the reading - while ((strcmp(myME310.buffer_cstr(1), "+CREG: 0,1") != 0) && (strcmp(myME310.buffer_cstr(1), "+CREG: 0,5") != 0)) + //issue command AT+CREG? to check the network status + Serial.println("Network status"); + rc = myME310.read_network_registration_status(); + Serial.println(myME310.buffer_cstr(1)); + if (rc == ME310::RETURN_VALID) { - delay(3000); - myME310.read_network_registration_status(); - Serial.println(myME310.buffer_cstr(1)); + //if +CREG!=0,1 and +CREG!=0,5, wait 3 second than retry the reading + resp = (char*)myME310.buffer_cstr(1); + + while(resp != NULL) + { + if ((strcmp(resp, "+CREG: 0,1") != 0) && (strcmp(resp, "+CREG: 0,5") != 0)) + { + delay(3000); + rc = myME310.read_network_registration_status(); + if(rc != ME310::RETURN_VALID) + { + Serial.println("ERROR"); + Serial.println(myME310.return_string(rc)); + break; + } + Serial.println(myME310.buffer_cstr(1)); + resp = (char*)myME310.buffer_cstr(1); + } + else + { + break; + } + } } } - } } - } else if (strcmp(myME310.buffer_cstr(1), "+CPIN: READY") == 0) { - Serial.println("Select wireless tecnology "); - rc = myME310.select_wireless_network(12); //issue command AT+WS46=12(2G) - if (rc == ME310::RETURN_VALID) + else if (strcmp(resp, "+CPIN: READY") == 0) { - //issue command AT+CREG? to check the network status - Serial.println("Network status"); - rc = myME310.read_network_registration_status(); - Serial.println(myME310.buffer_cstr(1)); + Serial.println("Select wireless tecnology "); + rc = myME310.select_wireless_network(12); //issue command AT+WS46=12(2G) if (rc == ME310::RETURN_VALID) { - //if +CREG!=0,1 and +CREG!=0,5, wait 3 second than retry the reading - while ((strcmp(myME310.buffer_cstr(1), "+CREG: 0,1") != 0) && (strcmp(myME310.buffer_cstr(1), "+CREG: 0,5") != 0)) + //issue command AT+CREG? to check the network status + Serial.println("Network status"); + rc = myME310.read_network_registration_status(); + Serial.println(myME310.buffer_cstr(1)); + if (rc == ME310::RETURN_VALID) { - delay(3000); - myME310.read_network_registration_status(); - Serial.println(myME310.buffer_cstr(1)); + //if +CREG!=0,1 and +CREG!=0,5, wait 3 second than retry the reading + resp = (char*)myME310.buffer_cstr(1); + while(resp != NULL) + { + if ((strcmp(resp, "+CREG: 0,1") != 0) && (strcmp(resp, "+CREG: 0,5") != 0)) + { + delay(3000); + rc = myME310.read_network_registration_status(); + if(rc != ME310::RETURN_VALID) + { + Serial.println("ERROR"); + Serial.println(myME310.return_string(rc)); + break; + } + Serial.println(myME310.buffer_cstr(1)); + resp = (char*)myME310.buffer_cstr(1); + } + else + { + break; + } + } } } - } - } + /*Registers setup for detecting freefall. Some register can be fine-tuned*/ Wire.begin(); while (!Serial); Serial.println("BMA400 Raw Data"); - while (1) { - if (bma400.isConnection()) { + while (1) + { + if (bma400.isConnection()) + { bma400.initialize(); Serial.println("BMA400 is connected"); break; - } else { + } + else + { Serial.println("BMA400 is not connected"); } - delay(2000); } uint8_t i = 0; @@ -175,13 +213,16 @@ void setup() { Serial.print("Buffer LEN="); Serial.print(len, DEC); Serial.print("\n"); - while (i < len) + if(buf != NULL) { - Serial.print("0x"); - Serial.print(reg++, HEX); - Serial.print("\t0x"); - Serial.print(buf[i++], HEX); - Serial.print("\n"); + while (i < len) + { + Serial.print("0x"); + Serial.print(reg++, HEX); + Serial.print("\t0x"); + Serial.print(buf[i++], HEX); + Serial.print("\n"); + } } pinMode (INTERRUPT, INPUT); @@ -221,11 +262,13 @@ void loop() { Serial.println("Enabling and registering LWM2M client"); r1 = myME310.enableLWM2M(1, 1, ME310::TOUT_45SEC); - if (r1 == ME310::RETURN_VALID) { + if (r1 == ME310::RETURN_VALID) + { delay(20000); Serial.println("Registered on server DM"); } - else { + else + { Serial.println("Enabling LWM2M returned error"); } @@ -239,42 +282,55 @@ void loop() { ME310::return_t r = myME310.read_gnss_configuration(); Serial.println(myME310.return_string(r)); Serial.println(myME310.buffer_cstr(1)); - if (r == ME310::RETURN_VALID) { + if (r == ME310::RETURN_VALID) + { delay(100); - - //switch to GNSS priority Serial.println("Switching to GNSS priority"); r = myME310.gnss_configuration(3, 0, ME310::TOUT_30SEC); - if (r == ME310::RETURN_VALID) { + if (r == ME310::RETURN_VALID) + { Serial.println("Switched to GNSS"); - } else { + } + else + { Serial.println("Couldn't switch to GNSS"); } /*While loop in which the MCU issue the command AT#LWM2MR=0,6,0,3 if the value f is 0.0< f < 20.00000, exit form while loop and set radius to true else continue to issue the command AT#LWM2MR every 10 seconds */ - while (radius == false) { + while (radius == false) + { myME310.readResourcefloat(0, 6, 0, 3, 0, ME310::TOUT_10SEC); resp = myME310.buffer_cstr(1); - char buffFloat[] = {resp[9], resp[10], resp[11], resp[12], resp[13], resp[14], resp[15], resp[16], resp[18], resp[19]}; - //Serial.println(buffFloat); - float f = atof(buffFloat); - if (f > 0.0 && f < 20.00000) { - radius = true; - } else { - //do nothing + if(resp != NULL) + { + char buffFloat[] = {resp[9], resp[10], resp[11], resp[12], resp[13], resp[14], resp[15], resp[16], resp[18], resp[19]}; + //Serial.println(buffFloat); + float f = atof(buffFloat); + if (f > 0.0 && f < 20.00000) + { + radius = true; + } + else + { + //do nothing + } + delay(10000); } - delay(10000); } - if (radius == true) { + if (radius == true) + { //switch to WWAN priority r = myME310.gnss_configuration(3, 1, ME310::TOUT_30SEC); - if (r == ME310::RETURN_VALID) { + if (r == ME310::RETURN_VALID) + { Serial.println("Switched to WWAN"); - } else { + } + else + { Serial.println("Couldn't switch to WWAN"); } myME310.setResourceBool(0, 3200, 0, 5500, 0, 1, ME310::TOUT_30SEC); @@ -293,7 +349,8 @@ void loop() { delay(2000); Serial.println("Disabling client"); r = myME310.disableLWM2M(0, ME310::TOUT_10SEC); - if (r == ME310::RETURN_VALID) { + if (r == ME310::RETURN_VALID) + { Serial.print("Client disabled"); } exit(0); diff --git a/examples/LWM2M_example/LWM2M_example_4G/LWM2M_example_4G.ino b/examples/LWM2M_example/LWM2M_example_4G/LWM2M_example_4G.ino index 193537c..51a19ef 100644 --- a/examples/LWM2M_example/LWM2M_example_4G/LWM2M_example_4G.ino +++ b/examples/LWM2M_example/LWM2M_example_4G/LWM2M_example_4G.ino @@ -60,7 +60,7 @@ ME310 myME310; void setup() { char pin[] = "XXXX"; - int rc; + ME310::return_t rc; pinMode(ON_OFF, OUTPUT); pinMode(LED_BUILTIN, OUTPUT); @@ -86,51 +86,84 @@ void setup() { myME310.read_enter_pin(); // read response in 1 array position - if (strcmp(myME310.buffer_cstr(1), "+CPIN: SIM PIN") == 0) + char *resp = (char*)myME310.buffer_cstr(1); + if(resp != NULL) { - Serial.println("Insert SIM PIN"); + if (strcmp(resp, "+CPIN: SIM PIN") == 0) + { + Serial.println("Insert SIM PIN"); + + //issue command AT+CPIN=pin + rc = myME310.enter_pin(pin); + if (rc == ME310::RETURN_VALID) + { + Serial.println("PIN inserted"); - //issue command AT+CPIN=pin - rc = myME310.enter_pin(pin); - if (rc == ME310::RETURN_VALID) + //issue command AT+CREG? to check the network status + Serial.println("Network status"); + rc = myME310.read_eps_network_registration_status(); + if (rc == ME310::RETURN_VALID) + { + //if +CEREG!=0,1 and +CEREG!=0,5, wait 3 second than retry the reading + resp = (char*)myME310.buffer_cstr(1); + Serial.println(resp); + while(resp != NULL) + { + if ((strcmp(resp, "+CEREG: 0,1") != 0) && (strcmp(resp, "+CEREG: 0,5") != 0)) + { + delay(3000); + rc = myME310.read_eps_network_registration_status(); + if(rc != ME310::RETURN_VALID) + { + Serial.println("ERROR"); + Serial.println(myME310.return_string(rc)); + break; + } + Serial.println(myME310.buffer_cstr(1)); + resp = (char*)myME310.buffer_cstr(1); + } + else + { + break; + } + } + } + } + } + else if (strcmp(resp, "+CPIN: READY") == 0) { - Serial.println("PIN inserted"); //issue command AT+CREG? to check the network status Serial.println("Network status"); rc = myME310.read_eps_network_registration_status(); - Serial.println(myME310.buffer_cstr(1)); if (rc == ME310::RETURN_VALID) { //if +CEREG!=0,1 and +CEREG!=0,5, wait 3 second than retry the reading - while ((strcmp(myME310.buffer_cstr(1), "+CEREG: 0,1") != 0) && (strcmp(myME310.buffer_cstr(1), "+CEREG: 0,5") != 0)) + resp = (char*)myME310.buffer_cstr(1); + Serial.println(resp); + while(resp != NULL) { - delay(3000); - myME310.read_eps_network_registration_status(); - Serial.println(myME310.buffer_cstr(1)); + if ((strcmp(resp, "+CEREG: 0,1") != 0) && (strcmp(resp, "+CEREG: 0,5") != 0)) + { + delay(3000); + rc = myME310.read_eps_network_registration_status(); + if(rc != ME310::RETURN_VALID) + { + Serial.println("ERROR"); + Serial.println(myME310.return_string(rc)); + break; + } + Serial.println(myME310.buffer_cstr(1)); + resp = (char*)myME310.buffer_cstr(1); + } + else + { + break; + } } } - - } - } else if (strcmp(myME310.buffer_cstr(1), "+CPIN: READY") == 0) { - - //issue command AT+CREG? to check the network status - Serial.println("Network status"); - rc = myME310.read_eps_network_registration_status(); - Serial.println(myME310.buffer_cstr(1)); - if (rc == ME310::RETURN_VALID) - { - //if +CEREG!=0,1 and +CEREG!=0,5, wait 3 second than retry the reading - while ((strcmp(myME310.buffer_cstr(1), "+CEREG: 0,1") != 0) && (strcmp(myME310.buffer_cstr(1), "+CEREG: 0,5") != 0)) - { - delay(3000); - myME310.read_eps_network_registration_status(); - Serial.println(myME310.buffer_cstr(1)); - } } - } - /*Registers setup for detecting freefall. Some register can be fine-tuned*/ Wire.begin(); while (!Serial); @@ -165,15 +198,17 @@ void setup() { Serial.print("Buffer LEN="); Serial.print(len, DEC); Serial.print("\n"); - while (i < len) + if(buf != NULL) { - Serial.print("0x"); - Serial.print(reg++, HEX); - Serial.print("\t0x"); - Serial.print(buf[i++], HEX); - Serial.print("\n"); + while (i < len) + { + Serial.print("0x"); + Serial.print(reg++, HEX); + Serial.print("\t0x"); + Serial.print(buf[i++], HEX); + Serial.print("\n"); + } } - pinMode (INTERRUPT, INPUT); digitalWrite (INTERRUPT, LOW); // internal pull-down resistor attachInterrupt(digitalPinToInterrupt(INTERRUPT), freefall, RISING); @@ -211,11 +246,13 @@ void loop() { Serial.println("Enabling and registering LWM2M client"); r1 = myME310.enableLWM2M(1, 1, ME310::TOUT_45SEC); - if (r1 == ME310::RETURN_VALID) { + if (r1 == ME310::RETURN_VALID) + { delay(20000); Serial.println("Registered on server DM"); } - else { + else + { Serial.println("Enabling LWM2M returned error"); } @@ -247,23 +284,32 @@ void loop() { while (radius == false) { myME310.readResourcefloat(0, 6, 0, 3, 0, ME310::TOUT_10SEC); resp = myME310.buffer_cstr(1); - char buffFloat[] = {resp[9], resp[10], resp[11], resp[12], resp[13], resp[14], resp[15], resp[16], resp[18], resp[19]}; - //Serial.println(buffFloat); - float f = atof(buffFloat); - if (f > 0.0 && f < 20.00000) { - radius = true; - } else { - //do nothing + if(resp != NULL) + { + char buffFloat[] = {resp[9], resp[10], resp[11], resp[12], resp[13], resp[14], resp[15], resp[16], resp[18], resp[19]}; + //Serial.println(buffFloat); + float f = atof(buffFloat); + if (f > 0.0 && f < 20.00000) + { + radius = true; + } + else + { + //do nothing + } + delay(10000); } - delay(10000); } if (radius == true) { //switch to WWAN priority r = myME310.gnss_configuration(3, 1, ME310::TOUT_30SEC); - if (r == ME310::RETURN_VALID) { + if (r == ME310::RETURN_VALID) + { Serial.println("Switched to WWAN"); - } else { + } + else + { Serial.println("Couldn't switch to WWAN"); } myME310.setResourceBool(0, 3200, 0, 5500, 0, 1, ME310::TOUT_30SEC); diff --git a/examples/ME310_AT_Test/ME310_AT_Test.ino b/examples/ME310_AT_Test/ME310_AT_Test.ino index 0a89620..61e8fca 100644 --- a/examples/ME310_AT_Test/ME310_AT_Test.ino +++ b/examples/ME310_AT_Test/ME310_AT_Test.ino @@ -47,20 +47,20 @@ void print_buffer(ME310 &aME310, const char *term = "OK"); void setup() { - pinMode(ON_OFF, OUTPUT); - pinMode(LED_BUILTIN, OUTPUT); + pinMode(ON_OFF, OUTPUT); + pinMode(LED_BUILTIN, OUTPUT); - Serial.begin(115200); - myME310.begin(115200); - delay(1000); + Serial.begin(115200); + myME310.begin(115200); + delay(1000); - Serial.println("SERCOMM Telit Test AT"); - - myME310.powerOn(); - - Serial.println("ME310 ON"); + Serial.println("SERCOMM Telit Test AT"); + + myME310.powerOn(); + + Serial.println("ME310 ON"); - Serial.println(); + Serial.println(); Serial.println("AT Command"); ME310::return_t rc = myME310.attention(); // issue command and wait for answer or timeout Serial.println(myME310.buffer_cstr()); // print first line of modem answer @@ -94,51 +94,63 @@ void setup() { if(rc == ME310::RETURN_VALID) // print all rows returned from ME310 except command echo (index = 0) { Serial.println(myME310.buffer_cstr(1)); - if(strstr(myME310.buffer_cstr(1),"#QSS: 0,1")) + char *resp = (char* )myME310.buffer_cstr(1); + if(resp != NULL) { - Serial.println("SIM is inserted"); - - rc = myME310.read_enter_pin(); - if(rc == ME310::RETURN_VALID) // print all rows returned from ME310 except command echo (index = 0) + if(strstr(resp,"#QSS: 0,1")) { - if(strstr(myME310.buffer_cstr(1),"READY")) - Serial.println("PIN not required"); - else - Serial.println("PIN required"); - } - else return; + Serial.println("SIM is inserted"); + + rc = myME310.read_enter_pin(); + if(rc == ME310::RETURN_VALID) // print all rows returned from ME310 except command echo (index = 0) + { + resp = (char*) myME310.buffer_cstr(1); + if(resp != NULL) + { + if(strstr(resp,"READY")) + Serial.println("PIN not required"); + else + Serial.println("PIN required"); + } + } + else return; - Serial.println(); - Serial.print("Print ICCID : "); - rc = myME310.read_iccid(); - if(rc == ME310::RETURN_VALID) - { - const char *pLabel = strstr(myME310.buffer_cstr(1),"+CCID: "); - if(pLabel) + Serial.println(); + Serial.print("Print ICCID : "); + rc = myME310.read_iccid(); + if(rc == ME310::RETURN_VALID) { - Serial.print("["); - Serial.write(pLabel+7,19); - Serial.println("]"); + resp = (char*) myME310.buffer_cstr(1); + if(resp != NULL) + { + const char *pLabel = strstr(resp,"+CCID: "); + if(pLabel) + { + Serial.print("["); + Serial.write(pLabel+7,19); + Serial.println("]"); + } + } } - } - else return; + else return; - Serial.print("Print IMSI : "); - rc = myME310.imsi(); // no command echo - if(rc == ME310::RETURN_VALID) - Serial.println(myME310.buffer_cstr(1)); - else return; + Serial.print("Print IMSI : "); + rc = myME310.imsi(); // no command echo + if(rc == ME310::RETURN_VALID) + Serial.println(myME310.buffer_cstr(1)); + else return; - Serial.println(); - Serial.println("List Capabilities : "); - rc = myME310.capabilities_list(); - if(rc == ME310::RETURN_VALID) // print all rows returned from ME310 except command echo (index = 0) - Serial.println(myME310.buffer_cstr(1)); - else return; + Serial.println(); + Serial.println("List Capabilities : "); + rc = myME310.capabilities_list(); + if(rc == ME310::RETURN_VALID) // print all rows returned from ME310 except command echo (index = 0) + Serial.println(myME310.buffer_cstr(1)); + else return; + } + else + Serial.println("SIM not inserted"); } - else - Serial.println("SIM not inserted"); } else return; // exit on error diff --git a/examples/MQTT_example/MQTT_example.ino b/examples/MQTT_example/MQTT_example.ino index 864d0ca..4006d3b 100644 --- a/examples/MQTT_example/MQTT_example.ino +++ b/examples/MQTT_example/MQTT_example.ino @@ -12,15 +12,15 @@ @details In this example sketch, it is shown how to use MQTT management, using ME310 library.\n - MQTT enable, MQTT configuration, MQTT connect, MQTT topic subscribe, MQTT publish, MQTT read + MQTT enable, MQTT configuration, MQTT connect, MQTT topic subscribe, MQTT publish, MQTT read and MQTT disconnect methods are used and responses are printed.\n NOTE:\n For the sketch to work properly, you need to set the correct APN, client_id, client_username, client_password. - @version + @version 1.0.0 - + @note @author @@ -48,11 +48,11 @@ using namespace me310; /* - * If a Telit-Board Charlie is not in use, the ME310 class needs the Uart Serial instance in the constructor, that will be used to communicate with the modem.\n + * If a Telit-Board Charlie is not in use, the ME310 class needs the Uart Serial instance in the constructor, that will be used to communicate with the modem.\n * Please refer to your board configuration in variant.h file. * Example: * Uart Serial1(&sercom4, PIN_MODULE_RX, PIN_MODULE_TX, PAD_MODULE_RX, PAD_MODULE_TX, PIN_MODULE_RTS, PIN_MODULE_CTS); - * ME310 myME310 (Serial1); + * ME310 myME310 (Serial1); */ ME310 myME310; ME310::return_t rc; //Enum of return value methods @@ -71,7 +71,7 @@ void setup() { myME310.begin(115200); delay(1000); - + myME310.powerOn(); Serial.println("Telit Test AT MQTT command"); Serial.println("ME310 ON"); @@ -83,96 +83,117 @@ void setup() { /////////////////////////////////// myME310.report_mobile_equipment_error(2); //issue command AT+CMEE=2 and wait for answer or timeout rc = myME310.read_enter_pin(); //issue command AT+cpin? in read mode, check that the SIM is inserted and the module is not waiting for the PIN - if(strcmp(myME310.buffer_cstr(2), "OK") == 0) // read response in 2 array position + char *resp = (char* )myME310.buffer_cstr(2); + if(resp != NULL) { - Serial.println("Define PDP Context"); - rc = myME310.define_pdp_context(cID, ipProt, APN); //issue command AT+CGDCONT=cid,PDP_type,APN - Serial.print(rc); - if(rc == ME310::RETURN_VALID) + if(strcmp(resp, "OK") == 0) // read response in 2 array position { - myME310.read_define_pdp_context(); //issue command AT+CGDCONT=? (read mode) - Serial.print("pdp context read: "); - Serial.println(myME310.buffer_cstr(1)); //print second line of modem answer - - Serial.print("gprs network registration status: "); - rc = myME310.read_gprs_network_registration_status(); //issue command AT+CGREG=? (read mode) - Serial.println(myME310.buffer_cstr(1)); + Serial.println("Define PDP Context"); + rc = myME310.define_pdp_context(cID, ipProt, APN); //issue command AT+CGDCONT=cid,PDP_type,APN + Serial.print(rc); if(rc == ME310::RETURN_VALID) { - while ((strcmp(myME310.buffer_cstr(1), "+CGREG: 0,1") != 0) && (strcmp(myME310.buffer_cstr(1), "+CGREG: 0,5") != 0)) + myME310.read_define_pdp_context(); //issue command AT+CGDCONT=? (read mode) + Serial.print("pdp context read: "); + Serial.println(myME310.buffer_cstr(1)); //print second line of modem answer + + Serial.print("gprs network registration status: "); + rc = myME310.read_gprs_network_registration_status(); //issue command AT+CGREG=? (read mode) + if(rc == ME310::RETURN_VALID) + { + resp = (char*)myME310.buffer_cstr(1); + Serial.println(resp); + while(resp != NULL) { - delay(3000); - myME310.read_gprs_network_registration_status(); - Serial.println(myME310.buffer_cstr(1)); + if ((strcmp(resp, "+CGREG: 0,1") != 0) && (strcmp(resp, "+CGREG: 0,5") != 0)) + { + delay(3000); + rc = myME310.read_gprs_network_registration_status(); + if(rc != ME310::RETURN_VALID) + { + Serial.println("ERROR"); + break; + } + resp = (char*)myME310.buffer_cstr(1); + Serial.println(resp); + } } - } - /////////////////////////////////// - // Context Activation - // AT#SGACT=cid,stat (1 enable) - /////////////////////////////////// - Serial.println("Activate context"); - myME310.context_activation(cID, 1); //issue command AT#SGACT=cid,state and wait for answer or timeout - - /////////////////////////////////// - // AT#MQEN? - /////////////////////////////////// - Serial.println("mqtt enable read"); - rc = myME310.read_mqtt_enable(); //issue command AT#MQEN? and wait for answer or timeout - Serial.println(myME310.buffer_cstr(1)); - if ((strcmp(myME310.buffer_cstr(1), "#MQEN: 1,0")) == 0) - { + } /////////////////////////////////// - // AT#MQEN=instance Number, state (1 enable) + // Context Activation + // AT#SGACT=cid,stat (1 enable) /////////////////////////////////// - Serial.println("mqtt enable"); - rc = myME310.mqtt_enable(1, 1); //issue command AT#MQEN=instance_number,state and wait for answer or timeout - Serial.println(myME310.buffer_cstr(1)); - rc = myME310.read_mqtt_enable(); + Serial.println("Activate context"); + myME310.context_activation(cID, 1); //issue command AT#SGACT=cid,state and wait for answer or timeout + + /////////////////////////////////// + // AT#MQEN? + /////////////////////////////////// + Serial.println("mqtt enable read"); + rc = myME310.read_mqtt_enable(); //issue command AT#MQEN? and wait for answer or timeout Serial.println(myME310.buffer_cstr(1)); - } - delay(1000); - /////////////////////////////////// - // AT#MQCFG=instance Number, hostname, port, cid, sslEN - /////////////////////////////////// - Serial.println("mqtt configure"); - rc = myME310.mqtt_configure(1, HOSTNAME , PORT, cID); //issue command AT#MQCFG=instance Number, hostname, port, cid, sslEN and wait for answer or timeout - Serial.println(myME310.buffer_cstr(1)); - delay(1000); - /////////////////////////////////// - // AT#MQCONN=instance Number, client_id, username, password - /////////////////////////////////// - if(rc == 0) - { - rc = myME310.mqtt_connect(1, CLIENT_ID, CLIENT_USERNAME, CLIENT_PASSWORD, ME310::TOUT_1MIN); //issue command AT#MQCONN=instance Number, client_id, username, password and wait for answer or timeout + resp = (char*) myME310.buffer_cstr(1); + if(resp != NULL) + { + if ((strcmp(resp, "#MQEN: 1,0")) == 0) + { + /////////////////////////////////// + // AT#MQEN=instance Number, state (1 enable) + /////////////////////////////////// + Serial.println("mqtt enable"); + rc = myME310.mqtt_enable(1, 1); //issue command AT#MQEN=instance_number,state and wait for answer or timeout + Serial.println(myME310.buffer_cstr(1)); + rc = myME310.read_mqtt_enable(); + Serial.println(myME310.buffer_cstr(1)); + } + } + delay(1000); + /////////////////////////////////// + // AT#MQCFG=instance Number, hostname, port, cid, sslEN + /////////////////////////////////// + Serial.println("mqtt configure"); + rc = myME310.mqtt_configure(1, HOSTNAME , PORT, cID); //issue command AT#MQCFG=instance Number, hostname, port, cid, sslEN and wait for answer or timeout Serial.println(myME310.buffer_cstr(1)); + delay(1000); + /////////////////////////////////// + // AT#MQCONN=instance Number, client_id, username, password + /////////////////////////////////// if(rc == 0) { - //////////////////////////////////// - // COMMAND TO SUBSCRIBE - // - // AT#MQSUB=instance_number, topic - ///////////////////////////////////// - myME310.mqtt_topic_subscribe(1,"topic"); //issue command AT#MQSUB=instance_number, topic and wait for answer or timeout + rc = myME310.mqtt_connect(1, CLIENT_ID, CLIENT_USERNAME, CLIENT_PASSWORD, ME310::TOUT_1MIN); //issue command AT#MQCONN=instance Number, client_id, username, password and wait for answer or timeout Serial.println(myME310.buffer_cstr(1)); - - ///////////////////////////////////// - // COMMAND TO PUBLISH - // - // AT#MQPUBS=instance_number, topic, retain, qos, message - //////////////////////////////////// - - myME310.mqtt_publish(1, "topic", 1, 0, "message"); //issue command AT#MQPUBS=instance_number, topic, retain, qos, message and wait for answer or timeout - Serial.println(myME310.buffer_cstr(1)); - + if(rc == 0) + { + //////////////////////////////////// + // COMMAND TO SUBSCRIBE + // + // AT#MQSUB=instance_number, topic + ///////////////////////////////////// + myME310.mqtt_topic_subscribe(1,"topic"); //issue command AT#MQSUB=instance_number, topic and wait for answer or timeout + Serial.println(myME310.buffer_cstr(1)); + + ///////////////////////////////////// + // COMMAND TO PUBLISH + // + // AT#MQPUBS=instance_number, topic, retain, qos, message + //////////////////////////////////// + + myME310.mqtt_publish(1, "topic", 1, 0, "message"); //issue command AT#MQPUBS=instance_number, topic, retain, qos, message and wait for answer or timeout + Serial.println(myME310.buffer_cstr(1)); + + } } } } - } + else + { + Serial.println((String)"Error: " + rc + " Error string: " + myME310.buffer_cstr(2)); + } +} else { Serial.println((String)"Error: " + rc + " Error string: " + myME310.buffer_cstr(2)); } - } void loop(){ @@ -195,7 +216,7 @@ void loop(){ if (count == 100) { ///////////////////////////////////// - // COMMAND TO DISCONNECT + // COMMAND TO DISCONNECT // // AT#MQDISC=instance_number //////////////////////////////////// diff --git a/examples/Ping_example/Ping_example.ino b/examples/Ping_example/Ping_example.ino index d3ecf1c..167e7c5 100644 --- a/examples/Ping_example/Ping_example.ino +++ b/examples/Ping_example/Ping_example.ino @@ -15,11 +15,11 @@ The PDP context is defined, the registration status read, the context activated and finally the ping command is sent and the response printed.\n NOTE:\n For correct operation it is necessary to set the correct APN. - - @version + + @version 1.0.0 - + @note @author @@ -31,7 +31,7 @@ #include #include -#define APN "apn" +#define APN "APN" #ifndef ARDUINO_TELIT_SAMD_CHARLIE #define ON_OFF 6 /*Select the GPIO to control ON_OFF*/ @@ -39,11 +39,11 @@ using namespace me310; /* - * If a Telit-Board Charlie is not in use, the ME310 class needs the Uart Serial instance in the constructor, that will be used to communicate with the modem.\n + * If a Telit-Board Charlie is not in use, the ME310 class needs the Uart Serial instance in the constructor, that will be used to communicate with the modem.\n * Please refer to your board configuration in variant.h file. * Example: * Uart Serial1(&sercom4, PIN_MODULE_RX, PIN_MODULE_TX, PAD_MODULE_RX, PAD_MODULE_TX, PIN_MODULE_RTS, PIN_MODULE_CTS); - * ME310 myME310 (Serial1); + * ME310 myME310 (Serial1); */ ME310 myME310; ME310::return_t rc; //Enum of return value methods @@ -78,17 +78,33 @@ void setup() { Serial.println(myME310.buffer_cstr(1)); //print second line of modem answer Serial.print("gprs network registration status :"); - rc = myME310.read_gprs_network_registration_status(); //issue command AT+CGREG=? (read mode) - Serial.println(myME310.buffer_cstr(1)); + rc = myME310.read_gprs_network_registration_status(); //issue command AT+CGREG=? (read mode) + if(rc == ME310::RETURN_VALID) { - while ((strcmp(myME310.buffer_cstr(1), "+CGREG: 0,1") != 0) && (strcmp(myME310.buffer_cstr(1), "+CGREG: 0,5") != 0)) + Serial.println(myME310.buffer_cstr(1)); + char *resp = (char* )myME310.buffer_cstr(1); + while(resp != NULL) + { + if ((strcmp(resp, "+CGREG: 0,1") != 0) && (strcmp(resp, "+CGREG: 0,5") != 0)) { delay(3000); - myME310.read_gprs_network_registration_status(); + rc = myME310.read_gprs_network_registration_status(); + if(rc != ME310::RETURN_VALID) + { + Serial.println("ERROR"); + Serial.println(myME310.return_string(rc)); + break; + } Serial.println(myME310.buffer_cstr(1)); + resp = (char* )myME310.buffer_cstr(1); } - } + else + { + break; + } + } + } Serial.println("Activate context"); myME310.context_activation(cID, 1); //issue command AT#SGACT=cid,state and wait for answer or timeout } diff --git a/examples/Socket_example/Socket_example.ino b/examples/Socket_example/Socket_example.ino index 4180c50..cb554c3 100644 --- a/examples/Socket_example/Socket_example.ino +++ b/examples/Socket_example/Socket_example.ino @@ -11,13 +11,13 @@ Sample test of using sockets via AT commands @details - In this example sketch, the use of sockets is shown through the commands offered by the ME310 library.\n + In this example sketch, the use of sockets is shown through the commands offered by the ME310 library.\n NOTE:\n For correct operation it is necessary to set the correct APN. - @version + @version 1.0.0 - + @note @author @@ -29,7 +29,7 @@ #include //#include -#define APN "apn" +#define APN "APN" #ifndef ARDUINO_TELIT_SAMD_CHARLIE #define ON_OFF 6 /*Select the GPIO to control ON_OFF*/ @@ -37,11 +37,11 @@ using namespace me310; /* - * If a Telit-Board Charlie is not in use, the ME310 class needs the Uart Serial instance in the constructor, that will be used to communicate with the modem.\n + * If a Telit-Board Charlie is not in use, the ME310 class needs the Uart Serial instance in the constructor, that will be used to communicate with the modem.\n * Please refer to your board configuration in variant.h file. * Example: * Uart Serial1(&sercom4, PIN_MODULE_RX, PIN_MODULE_TX, PAD_MODULE_RX, PAD_MODULE_TX, PIN_MODULE_RTS, PIN_MODULE_CTS); - * ME310 myME310 (Serial1); + * ME310 myME310 (Serial1); */ ME310 myME310; ME310::return_t rc; //Enum of return value methods @@ -55,8 +55,6 @@ int port = 10510; void setup() { - int rc; - pinMode(ON_OFF, OUTPUT); pinMode(LED_BUILTIN, OUTPUT); @@ -65,7 +63,7 @@ void setup() { delay(3000); myME310.module_reboot(); // issue command at#reboot - + delay(10000); Serial.println("Telit Test AT Socket command"); myME310.powerOn(); @@ -73,30 +71,51 @@ void setup() { myME310.report_mobile_equipment_error(2); //issue command AT+CMEE=2 and wait for answer or timeout myME310.read_enter_pin(); //issue command AT+pin? in read mode, check that the SIM is inserted and the module is not waiting for the PIN - if(strcmp(myME310.buffer_cstr(2), "OK") == 0) // read response in 2 array position + + char *resp = (char* )myME310.buffer_cstr(2); + if(resp != NULL) { - Serial.println("Define PDP Context"); - rc = myME310.define_pdp_context(cID, ipProt, APN); //issue command AT+CGDCONT=cid,PDP_type,APN - if(rc == ME310::RETURN_VALID) + if(strcmp(resp, "OK") == 0) // read response in 2 array position { - myME310.read_define_pdp_context(); //issue command AT+CGDCONT=? (read mode) - Serial.print("pdp context read: "); - Serial.println(myME310.buffer_cstr(1)); //print second line of modem answer - - Serial.print("gprs network registration status: "); - rc = myME310.read_gprs_network_registration_status(); //issue command AT+CGREG=? (read mode) - Serial.println(myME310.buffer_cstr(1)); + Serial.println("Define PDP Context"); + rc = myME310.define_pdp_context(cID, ipProt, APN); //issue command AT+CGDCONT=cid,PDP_type,APN if(rc == ME310::RETURN_VALID) { - while ((strcmp(myME310.buffer_cstr(1), "+CGREG: 0,1") != 0) && (strcmp(myME310.buffer_cstr(1), "+CGREG: 0,5") != 0)) + myME310.read_define_pdp_context(); //issue command AT+CGDCONT=? (read mode) + Serial.print("pdp context read: "); + Serial.println(myME310.buffer_cstr(1)); //print second line of modem answer + + Serial.print("gprs network registration status: "); + rc = myME310.read_gprs_network_registration_status(); //issue command AT+CGREG=? (read mode) + + if(rc == ME310::RETURN_VALID) + { + resp = (char* )myME310.buffer_cstr(1); + Serial.println(resp); + while(resp != NULL) { - delay(3000); - myME310.read_gprs_network_registration_status(); - Serial.println(myME310.buffer_cstr(1)); + if ((strcmp(resp, "+CGREG: 0,1") != 0) && (strcmp(resp, "+CGREG: 0,5") != 0)) + { + delay(3000); + rc = myME310.read_gprs_network_registration_status(); + if(rc != ME310::RETURN_VALID) + { + Serial.println("ERROR"); + Serial.println(myME310.return_string(rc)); + break; + } + Serial.println(myME310.buffer_cstr(1)); + resp = (char* )myME310.buffer_cstr(1); + } + else + { + break; + } } + } + Serial.println("Activate context"); + myME310.context_activation(cID, 1); //issue command AT#SGACT=cid,state and wait for answer or timeout } - Serial.println("Activate context"); - myME310.context_activation(cID, 1); //issue command AT#SGACT=cid,state and wait for answer or timeout } } } @@ -119,8 +138,8 @@ void loop() { Serial.print("Socket Status: "); r = myME310.socket_status(connID, ME310::TOUT_10SEC); //issue command AT#SS=connID and wait for answer or timeout delay(100); - Serial.println(myME310.return_string(r)); - + Serial.println(myME310.return_string(r)); + Serial.print("SEND: "); r = myME310.socket_send_data_command_mode_extended(connID, (int) sizeof(data), data, 1, ME310::TOUT_30SEC); Serial.println(myME310.return_string(r)); @@ -147,7 +166,7 @@ void loop() { } else { - Serial.println(myME310.return_string(r)); + Serial.println(myME310.return_string(r)); } r = myME310.socket_listen(connID, 0, port); } @@ -160,7 +179,7 @@ void loop() { else { Serial.println(myME310.return_string(r)); - } + } } Serial.println("The application has ended..."); exit(0); diff --git a/library.properties b/library.properties index 037119e..9136fc3 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=ME310G1 -version=2.8.0 +version=2.9.0 author=Telit maintainer=Telit sentence=Allows communication with ME310G1 Telit module. diff --git a/src/ATCommandDataParsing.cpp b/src/ATCommandDataParsing.cpp index 9b9010b..bbc9f2a 100644 --- a/src/ATCommandDataParsing.cpp +++ b/src/ATCommandDataParsing.cpp @@ -193,7 +193,7 @@ char* ATCommandDataParsing::findCommand(const char* aCommand) string tmp_str; tmp_str = aCommand; memset(_command, 0, 64); - int len; + int len = 0; std::size_t posColon = tmp_str.find_first_of("="); if(posColon != string::npos) { @@ -202,7 +202,8 @@ char* ATCommandDataParsing::findCommand(const char* aCommand) } else { - tmp_str.copy(_command, tmp_str.length(), 0); + std::size_t command_len = tmp_str.length(); + len = tmp_str.copy(_command, command_len, 0); _command[len] = '\0'; } return _command; diff --git a/src/ME310.cpp b/src/ME310.cpp index 345d1ba..e92bdaf 100644 --- a/src/ME310.cpp +++ b/src/ME310.cpp @@ -2489,7 +2489,7 @@ ME310::return_t ME310::send_short_message(int length, char *data, tout_t aTimeou if ((ret == RETURN_VALID)) { memset(mBuffer, 0, ME310_BUFFSIZE); - snprintf((char *)mBuffer, ME310_BUFFSIZE-1, data); + snprintf((char *)mBuffer, ME310_BUFFSIZE-1, "%s",data); ret = send_wait((char*)mBuffer, OK_STRING, CTRZ, aTimeout); } return ret; @@ -2512,7 +2512,7 @@ ME310::return_t ME310::send_short_message(const char *da, const char *toda, char if ((ret == RETURN_VALID)) { memset(mBuffer, 0, ME310_BUFFSIZE); - snprintf((char *)mBuffer, ME310_BUFFSIZE-1, data); + snprintf((char *)mBuffer, ME310_BUFFSIZE-1, "%s", data); ret = send_wait((char*)mBuffer, OK_STRING, CTRZ, aTimeout); } return ret; @@ -3216,7 +3216,7 @@ ME310::return_t ME310::i2c_write(int sdaPin,int sclPin, int deviceId, int regist ret = send_wait((char*)mBuffer, WAIT_DATA_STRING, aTimeout); if ((ret == RETURN_VALID)) { - char dataF[ME310_BUFFSIZE-1]; + char dataF[ME310_BUFFSIZE-1] = {}; strcat(dataF, data); strcat(dataF, "0x1A"); memset(mBuffer, 0, ME310_BUFFSIZE); @@ -3266,7 +3266,7 @@ ME310::return_t ME310::i2c_write_read(int sdaPin,int sclPin, int deviceId, int l ret = send_wait((char*)mBuffer, WAIT_DATA_STRING, aTimeout); if ((ret == RETURN_VALID)) { - char dataF[ME310_BUFFSIZE-1]; + char dataF[ME310_BUFFSIZE-1] = {}; strcat(dataF, data); strcat(dataF, "0x1A"); memset(mBuffer, 0, ME310_BUFFSIZE); @@ -4087,9 +4087,10 @@ ME310::return_t ME310::socket_send_data_command_mode(int connId, char* data, int /*IS IRA*/ read_socket_configuration_extended(); /*Control if size is > 1500 if is not IRA >3000 if is IRA*/ - while(buffer_cstr(i) != NULL) + char* tmp_buf = (char*) buffer_cstr(i); + while(tmp_buf != NULL) { - tmp_str = buffer_cstr(i); + tmp_str = tmp_buf; string searchedString = "#SCFGEXT: "; searchedString.append(std::to_string(connId)); size_t foundString = tmp_str.find(searchedString); @@ -4098,6 +4099,7 @@ ME310::return_t ME310::socket_send_data_command_mode(int connId, char* data, int break; } i++; + tmp_buf = (char*)buffer_cstr(i); } CheckIRAOption((char*)tmp_str.c_str()); memset(mBuffer, 0, ME310_BUFFSIZE); @@ -4114,7 +4116,7 @@ ME310::return_t ME310::socket_send_data_command_mode(int connId, char* data, int return ret; } } - snprintf((char *)mBuffer, ME310_SEND_BUFFSIZE - 1, data); + snprintf((char *)mBuffer, ME310_SEND_BUFFSIZE - 1, "%s", data); ret = send_wait((char*)mBuffer, OK_STRING, CTRZ, aTimeout); } return ret; @@ -4170,9 +4172,10 @@ ME310::return_t ME310::socket_receive_data_command_mode(int connId, int maxByte, } read_socket_configuration_extended(); /*Control if size is > 1500 if is not IRA >3000 if is IRA*/ - while(buffer_cstr(i) != NULL) + char* tmp_buf = (char*) buffer_cstr(i); + while(tmp_buf != NULL) { - tmp_str = buffer_cstr(i); + tmp_str = tmp_buf; string searchedString = "#SCFGEXT: "; searchedString.append(std::to_string(connId)); size_t foundString = tmp_str.find(searchedString); @@ -4181,6 +4184,7 @@ ME310::return_t ME310::socket_receive_data_command_mode(int connId, int maxByte, break; } i++; + tmp_buf = (char*) buffer_cstr(i);; } CheckIRAOption((char*)tmp_str.c_str()); snprintf((char *)mBuffer, ME310_BUFFSIZE-1, F("AT#SRECV=%d,%d,%d"), connId, maxByte, udpInfo); @@ -4448,7 +4452,6 @@ ME310::return_t ME310::ping(const char *ipaddr, tout_t aTimeout) { memset(mBuffer, 0, ME310_BUFFSIZE); snprintf((char *)mBuffer, ME310_BUFFSIZE-1, F("AT#PING=\"%s\""), ipaddr); - //return send_wait((char*)mBuffer, OK_STRING, aTimeout); return send_wait((char*)mBuffer,0, OK_STRING, aTimeout); } @@ -5385,7 +5388,7 @@ ME310::return_t ME310::ssl_security_data(int ssid, int action, int dataType, int if ((ret == RETURN_VALID)) { memset(mBuffer, 0, ME310_BUFFSIZE); - snprintf((char *)mBuffer, ME310_BUFFSIZE-1, data); + snprintf((char *)mBuffer, ME310_BUFFSIZE-1, "%s",data); ret = send_wait((char*)mBuffer, 0, OK_STRING, CTRZ, aTimeout); return ret; } @@ -5620,7 +5623,7 @@ This function sets a user defined value to the specified resource, if whiteliste ME310::return_t ME310::setResourcefloat(int type, int objID, int instanceID, int resourceID, int resourceInstance, float value, tout_t aTimeout) { (void) type; - setResourceFloat(objID, instanceID, resourceID, resourceInstance, value, aTimeout); + return setResourceFloat(objID, instanceID, resourceID, resourceInstance, value, aTimeout); } /*! \brief Implements the AT#LWM2MSET command and wait OK answer @@ -5722,7 +5725,7 @@ correspondent lwm2m agent enabled and working. */ ME310::return_t ME310::readResourcefloat(int agent,int objID,int instanceID,int resourceID, int resourceInstance, tout_t aTimeout) { - readResourceFloat(agent, objID, instanceID, resourceInstance, aTimeout); + return readResourceFloat(agent, objID, instanceID, resourceInstance, aTimeout); } /*! \brief Implements the AT#LWM2MR command and wait OK answer @@ -5971,7 +5974,8 @@ ME310::return_t ME310::m2m_read(const char *file_name, tout_t aTimeout) memset(mBuffer, 0, ME310_BUFFSIZE); PathParsing strPar((char*)file_name); m2m_list(strPar.getPath()); - int fileSize = strPar.getFileSize((char*)buffer_cstr_raw()); + char* tmp_data_raw = (char*)buffer_cstr_raw(); + int fileSize = strPar.getFileSize(tmp_data_raw); SET_BIT_MASK(_option, _M2MREAD_BIT); if(fileSize > ME310_BUFFSIZE-1) { @@ -6500,6 +6504,19 @@ ME310::return_t ME310::send_command(const char *aCommand, const char *aAnswer, t snprintf((char *)mBuffer, ME310_BUFFSIZE-1, aCommand); return send_wait((char*)mBuffer, aAnswer, aTimeout); } + + +//! \brief Send the generic AT command, termination charater without waiting for an answer +/*! \details + The command sends a generic AT command and waits for a specific answer. + * \param aCommand command string to send + * \param term termination character (CR or LF or CRLF or other) + * \param aTimeout answer timeout + */ +void ME310::send_data(const char *aCommand, const char* term, tout_t aTimeout) +{ + send(aCommand, term); +} //-------------------------------------------------------------------------------------------------------------- //! \brief Returns the string by index received from the ME310 serial connection /*! @@ -6554,6 +6571,7 @@ void ME310::send(const char *aCommand, const char *aTerm) { on_command(aCommand); //callback mSerial.write(aCommand); + delay(200); mSerial.write(aTerm); } @@ -6730,8 +6748,6 @@ return RETURN_TOUT; */ ME310::return_t ME310::wait_for(const char* aCommand, int flag, const char *aAnswer, ME310::tout_t aTimeout) { - char cmd[64]; - strcpy(cmd, aCommand); ATCommandDataParsing* dataParsing; return_t rc; const uint8_t *pBuffer; @@ -6756,7 +6772,7 @@ ME310::return_t ME310::wait_for(const char* aCommand, int flag, const char *aAns mpBuffer[bytesRead+1] = '\0'; bytesRead++; pBuffer = mpBuffer; - memcpy(tmp_str + mBuffLen, (char*)mpBuffer, bytesRead); + strcat(tmp_str, (char*)mpBuffer); mpBuffer += bytesRead; mBuffLen += bytesRead; rc = on_message((const char *)pBuffer); @@ -6783,7 +6799,7 @@ ME310::return_t ME310::wait_for(const char* aCommand, int flag, const char *aAns } } while(timeout < aTimeout); - dataParsing = new ATCommandDataParsing((char*) cmd, tmp_str, flag, _option); + dataParsing = new ATCommandDataParsing((char*)aCommand, tmp_str, flag, _option); if(dataParsing->parserIs()) { receivedDataLen = dataParsing->receivedBytes(); @@ -6792,7 +6808,6 @@ ME310::return_t ME310::wait_for(const char* aCommand, int flag, const char *aAns if(dataParsing->commandResponseResult()) { _payloadData = (uint8_t *) dataParsing->extractedPayload(); - if(str_equal(dataParsing->commandResponseString(), OK_STRING)) { rc = RETURN_VALID; @@ -6834,7 +6849,7 @@ ME310::return_t ME310::wait_for(const char* aCommand, int flag, const char *aAns * \param aTimeout answer timeout * \return return code */ -ME310::return_t ME310::wait_for_unsolicited(ME310::tout_t aTimeout) +ME310::return_t ME310::receive_data(tout_t aTimeout) { char cmd[64]; return_t rc; @@ -6886,6 +6901,16 @@ ME310::return_t ME310::wait_for_unsolicited(ME310::tout_t aTimeout) return RETURN_VALID; } +//! \brief Waits for the answer to an AT command or timeout +/*! + * \param aTimeout answer timeout + * \return return code + */ +ME310::return_t ME310::wait_for_unsolicited(tout_t aTimeout) +{ + return receive_data(aTimeout); +} + //! \brief Reads a line from serial until answer or timeout /*! * \param aAnswer string waiting for @@ -6982,21 +7007,20 @@ const char *ME310::str_start(const char *buffer, const char *string) { if(!buffer) { - return NULL; + return nullptr; } if(!string) { - return NULL; + return nullptr; } const char *rc = buffer; for(; ;buffer++,string++) { if(*buffer != *string) - return NULL; // exit if different + return nullptr; // exit if different else if(*buffer == 0) return rc; // exit if equal but = 0 } - return NULL; } //! \brief Compares 2 strings from start diff --git a/src/ME310.h b/src/ME310.h index 6c1a404..571dbd3 100644 --- a/src/ME310.h +++ b/src/ME310.h @@ -37,6 +37,7 @@ namespace me310 #define ME310_BUFFSIZE 3100 ///< Exchange buffer size #define ME310_SEND_BUFFSIZE 1500 + #define ME310_BUFFCOMMANDSIZE 64 #define F(A) A @@ -1269,6 +1270,9 @@ namespace me310 // Generic Command--------------------------------------------------------------- return_t send_command(const char *aCommand, const char *aAnswer = OK_STRING, tout_t aTimeout = TOUT_200MS); + void send_data(const char *aCommand, const char* term = TERMINATION_STRING, tout_t aTimeout = TOUT_200MS); + virtual return_t receive_data(tout_t aTimeout = TOUT_200MS); + const uint8_t *buffer(void) { return mBuffer;} //!< Returns pointer to local buffer diff --git a/src/Parser.cpp b/src/Parser.cpp index baf350d..a971159 100644 --- a/src/Parser.cpp +++ b/src/Parser.cpp @@ -228,6 +228,7 @@ namespace telitAT return posNewRow; } } + return -1; } //! \brief Implements the search for expected bytes @@ -363,27 +364,12 @@ namespace telitAT if(len != string::npos) { _commandResponse[len] = '\0'; - string tmp_cmd; + String tmp_cmd; tmp_cmd = _commandResponse; - std::size_t posNewRow = tmp_cmd.find_first_of("\n"); - if(posNewRow != string::npos) - { - std::size_t posSecondNewRow = tmp_cmd.find_first_of("\n", posNewRow+1); - if(posSecondNewRow != string::npos) - { - len = tmp_cmd.copy(_commandResponse, ((posSecondNewRow-1) - posNewRow), posNewRow+1); - _commandResponse[len] = '\0'; - return true; - } - else - { - return false; - } - } - else - { - return false; - } + tmp_cmd.trim(); + memset(_commandResponse, 0, MAX_CMD_RESPONSE); + tmp_cmd.toCharArray(_commandResponse, MAX_CMD_RESPONSE); + return true; } else { @@ -880,7 +866,8 @@ namespace telitAT std::size_t posNewRow = _rawData.find_first_of("\n"); if(posNewRow != string::npos) { - return posNewRow++; + posNewRow++; + return posNewRow; } else { @@ -997,8 +984,8 @@ namespace telitAT std::size_t posResponse = tmp_cmd.find(OK_STRING); if(posResponse != string::npos) { - strcpy(_commandResponse, OK_STRING); - return true; + strcpy(_commandResponse, OK_STRING); + return true; } posResponse = tmp_cmd.find(ERROR_STRING); if(posResponse != string::npos) @@ -1012,6 +999,10 @@ namespace telitAT strcpy(_commandResponse, CME_ERROR_STRING); return true; } + else + { + return false; + } } else { diff --git a/src/Parser.h b/src/Parser.h index 7186373..7a44f10 100644 --- a/src/Parser.h +++ b/src/Parser.h @@ -12,9 +12,9 @@ This library contains a interface that implements a parsing functions for AT command.\n Parser is an abstract class and handles common methods, like gets and parse.\n The concrete classes, derived by Parser, implement the parsing methods for the specific AT command.\n - @version + @version 1.1.1 - + @note Dependencies: string @@ -47,7 +47,7 @@ using namespace std; #define _M2MWRITE_BIT 0x04 #define _M2MREAD_BIT 0x08 - + #define SET_BIT_MASK(m, L) (m |= (1 << L)) #define UNSET_BIT_MASK(m, L) (m &= (~(1<< L))) @@ -59,7 +59,7 @@ namespace telitAT /* Classes definitions ============================================================================*/ /*! \class Parser - \brief Abstract class to parse received string + \brief Abstract class to parse received string \details Being an abstract class it is not possible to instantiate it. It is therefore mandatory to instantiate one of the classes derived from it.\n It is a interface which offers a series of methods that allow to parse a string obtained as a response following an AT command.\n @@ -132,7 +132,7 @@ namespace telitAT ----------------------------------*/ /*! \class FTPRECVParser \brief Class to parse received string, specific to parsing #FTPRECV AT command - \details + \details This is a derivated class by Parser, specific to parsing #FTPRECV AT command.\n It offers a series of methods that allow to parse a string obtained as a response following an AT#FTPRECV command. */ @@ -154,7 +154,7 @@ namespace telitAT -----------------------------------*/ /*! \class MQREADParser \brief Class to parse received string, specific to parsing #MQREAD AT command - \details + \details This is a derivated class by Parser, specific to parsing #MQREAD AT command.\n It offers a series of methods that allow to parse a string obtained as a response following an AT#MQREAD command.\n The expectedBytes() method returns always 0 value. @@ -177,7 +177,7 @@ namespace telitAT -----------------------------------*/ /*! \class M2MReadParser \brief Class to parse received string, specific to parsing #M2MREAD AT command - \details + \details This is a derivated class by Parser, specific to parsing #M2MREAD AT command.\n It offers a series of methods that allow to parse a string obtained as a response following an AT#M2MREAD command.\n The expectedBytes() method returns always 0 value. The receivedBytes() method returns the file size to read.\n @@ -204,7 +204,7 @@ namespace telitAT ----------------------------------*/ /*! \class PingParser \brief Class to parse received string, specific to parsing #PING AT command - \details + \details This is a derivated class by Parser, specific to parsing #PING AT command.\n It offers a series of methods that allow to parse a string obtained as a response following an AT#PING command.\n */ @@ -226,7 +226,7 @@ namespace telitAT ----------------------------------*/ /*! \class SMSListParser \brief Class to parse received string, specific to parsing +CMGL AT command - \details + \details This is a derivated class by Parser, specific to parsing +CMGL AT command.\n It offers a series of methods that allow to parse a string obtained as a response following an AT+CMGL command.\n */ @@ -250,7 +250,7 @@ namespace telitAT -----------------------------------*/ /*! \class GenericParser \brief Class to parse received string, specific to parsing generic AT command - \details + \details This is a derivated class by Parser, specific to parsing generic AT command.\n It offers a series of methods that allow to parse a string obtained as a response following an generic command.\n */ diff --git a/src/PathParsing.cpp b/src/PathParsing.cpp index 5159c80..e1638fe 100644 --- a/src/PathParsing.cpp +++ b/src/PathParsing.cpp @@ -94,17 +94,24 @@ namespace telitAT int PathParsing::getFileSize(char* list) { string str; - str = list; - char sizeFile[16]; - - int posFilename = str.rfind(_filename); - if(posFilename != string::npos) + if(list != NULL) { - int posComma = str.find_first_of(",", posFilename); - int posNewRow = str.find_first_of("\n", posComma); - int len = str.copy(sizeFile, posNewRow - posComma,posComma+1); - sizeFile[len] = '\0'; - return atoi(sizeFile); + str = list; + char sizeFile[16]; + + int posFilename = str.rfind(_filename); + if(posFilename != string::npos) + { + int posComma = str.find_first_of(",", posFilename); + int posNewRow = str.find_first_of("\n", posComma); + int len = str.copy(sizeFile, posNewRow - posComma,posComma+1); + sizeFile[len] = '\0'; + return atoi(sizeFile); + } + else + { + return -1; + } } else {