From afa21bf66831d06c36833e1592e53a3588b5fd7a Mon Sep 17 00:00:00 2001 From: Fabio Pintus Date: Thu, 24 Nov 2022 16:17:06 +0100 Subject: [PATCH] mqtt and ftp examples update --- CHANGELOG | 8 ++ README.md | 4 + examples/FTP_example/FTP_example.ino | 6 +- .../Hello_World_example.ino | 37 +++++ .../LWM2M_first_example.ino | 3 +- examples/ME310_AT_Test/ME310_AT_Test.ino | 44 +++--- examples/MQTT_example/MQTT_example.ino | 90 ++++++++---- .../Sleep_mode_example/Sleep_mode_example.ino | 133 ++++++++++++++++++ library.properties | 2 +- src/ME310.cpp | 16 ++- src/ME310.h | 5 +- 11 files changed, 284 insertions(+), 64 deletions(-) create mode 100644 examples/Hello_World_example/Hello_World_example.ino create mode 100644 examples/Sleep_mode_example/Sleep_mode_example.ino diff --git a/CHANGELOG b/CHANGELOG index e119b25..74f3309 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,13 @@ ME310 0.0.0 - ????.??.?? +ME310 2.12.0 - 2022.11.23 +* Fixes in MQTT and FTP examples + + +ME310 2.11.0 - 2022.11.22 +* Added hello world example +* Added sleep example +* Added debug flag in ME310 class ME310 2.10.0 - 2022.01.31 * Added several lwm2m utility functions diff --git a/README.md b/README.md index 40a8439..85d40b0 100644 --- a/README.md +++ b/README.md @@ -28,14 +28,18 @@ The following examples are available: - **[CLIP_example](examples/CLIP_example/CLIP_example.ino)** : _Shows how to use Calling Line Identifier protocol_ - **[FTP_example](examples/FTP_example/FTP_example.ino)** : _Connects to an FTP server and performs basic operations_ - **[GNSS_example](examples/GNSS_example/GNSS_example.ino)** : _Simple GNSS example, it enables the GNSS receiver and provides raw location data_ + - **[Hello_World_example](examples/Hello_World_example/Hello_World_example.ino)** : _Basic Hello World example_ - **[LWM2M_example](examples/LWM2M_example/)** : _uses LwM2M protocol and the accelerometer to send data to the OneEdge portal_ - **[LWM2M_example_2G](examples/LWM2M_example/LWM2M_example_2G/LWM2M_example_2G.ino)** : _example with 2G network_ - **[LWM2M_example_4G](examples/LWM2M_example/LWM2M_example_4G/LWM2M_example_4G.ino)** : _example with 4G network_ + - **[LWM2M_first_example](examples/LWM2M_first_example/)** : _uses LwM2M to create a simple connection to OneEdge portal and pushes accelerometer data showing basic resources operations_ + - **[LWM2M_Get_Object_example](examples/LWM2M_Get_Object_example/)** : _uses LwM2M GET OBJ functionality to access a whole object with single calls_ - **[M2M_example](examples/M2M_example/M2M_example.ino)** : _Communicates with the modem using the M2M commands to manage the filesystem_ - **[ME310_AT_Test](examples/ME310_AT_Test/ME310_AT_Test.ino)** : _Communicates with the modem and provides info about it (ICCID, IMEI etc.)_ - **[MQTT_example](examples/MQTT_example/MQTT_example.ino)** : _Communicate with a MQTT broker_ - **[Ping_example](examples/Ping_example/Ping_example.ino)** : _Simple example that enables the connectivity and pings a server_ - **[Socket_example](examples/Socket_example/Socket_example.ino)** : _Enables connectivity and uses a TCP socket example communicating with a demo server_ + - **[Sleep_mode_example](examples/Sleep_mode_example/Sleep_mode_example.ino)** : _Shows how to configure sleep modes on the board_ - **[TransparentBridge](examples/TransparentBridge/TransparentBridge.ino)** : _Enable the modem and creates a bridge between Arduino serial and modem AT interface, allowing user to send AT commands manually_ - **[GenericCommand_example](examples/GenericCommand_example/GenericCommand_example.ino)** : _Shows how to send AT commands to the modem manually and manage the response (useful for complex AT commands or chains of commands)_ diff --git a/examples/FTP_example/FTP_example.ino b/examples/FTP_example/FTP_example.ino index 2b84abd..dc90a2d 100644 --- a/examples/FTP_example/FTP_example.ino +++ b/examples/FTP_example/FTP_example.ino @@ -17,7 +17,7 @@ @version - 1.0.0 + 1.0.1 @note @@ -141,10 +141,6 @@ void loop() { Serial.println(myME310.return_string(rc)); if(rc == ME310::RETURN_VALID) { - Serial.println("Ftp list in current directory: "); - myME310.ftp_list(ME310::TOUT_10SEC); - Serial.println(myME310.buffer_cstr_raw()); - Serial.print("Ftp Change Working Directory: "); rc = myME310.ftp_change_working_directory("/CristinaDe", ME310::TOUT_10SEC); //issue command AT#FTPCWD=path directory and wait for answer or timeout Serial.println(myME310.return_string(rc)); //returns a string with return_t codes diff --git a/examples/Hello_World_example/Hello_World_example.ino b/examples/Hello_World_example/Hello_World_example.ino new file mode 100644 index 0000000..6535e3a --- /dev/null +++ b/examples/Hello_World_example/Hello_World_example.ino @@ -0,0 +1,37 @@ +/*Copyright (C) 2020 Telit Communications S.p.A. Italy - All Rights Reserved.*/ +/* See LICENSE file in the project root for full license information. */ + +/** + @file + + @brief + Sample test of the use the Arduino. + + @details + In this example sketch, it is shown how to print Hello World.\n + + @version + 1.0.0 + + @note + + @author + Cristina Desogus + + @date + 04/20/2022 + */ + +void setup() +{ + Serial.begin(115200); + delay(5000); + + Serial.println("Hello World"); + +} + +void loop() +{ + exit(0); +} diff --git a/examples/LWM2M_first_example/LWM2M_first_example.ino b/examples/LWM2M_first_example/LWM2M_first_example.ino index 2b0b2a0..eafc3fa 100644 --- a/examples/LWM2M_first_example/LWM2M_first_example.ino +++ b/examples/LWM2M_first_example/LWM2M_first_example.ino @@ -408,7 +408,8 @@ This function checks whether or not a particular agent exists. bool checkExistAgent(String LWM2MEXISTCommand) { bool ret = false; - myRc = myME310.send_command(LWM2MEXISTCommand.c_str(), OK_ANSWER); //issue command AT#LWM2MEXIST=agentInstance, objectNumber, objecttInstanceNumber. + const char *answer = OK_ANSWER; + myRc = myME310.send_command(LWM2MEXISTCommand.c_str(), answer); //issue command AT#LWM2MEXIST=agentInstance, objectNumber, objecttInstanceNumber. String resp = myME310.buffer_cstr(1); if(resp.endsWith("OK")) { diff --git a/examples/ME310_AT_Test/ME310_AT_Test.ino b/examples/ME310_AT_Test/ME310_AT_Test.ino index 61e8fca..0cb4172 100644 --- a/examples/ME310_AT_Test/ME310_AT_Test.ino +++ b/examples/ME310_AT_Test/ME310_AT_Test.ino @@ -14,9 +14,9 @@ The library contains a single class that implements a C++ interface to all ME310 AT Commands. It makes it easy to build Arduino applications that use the full power of ME310 module - @version + @version 1.0.0 - + @note @author @@ -25,8 +25,8 @@ @date 28/10/2020 */ - - #include + +#include #ifndef ARDUINO_TELIT_SAMD_CHARLIE #define ON_OFF 6 /*Select the GPIO to control ON_OFF*/ @@ -34,11 +34,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,9 +55,9 @@ void setup() { delay(1000); Serial.println("SERCOMM Telit Test AT"); - + myME310.powerOn(); - + Serial.println("ME310 ON"); Serial.println(); @@ -65,7 +65,7 @@ void setup() { 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 Serial.print(ME310::return_string(rc)); // print return value - Serial.println(" answer from ME310 MODULE"); + Serial.println(" answer from ME310 MODULE"); if(rc != ME310::RETURN_VALID) // exit on error return; @@ -75,21 +75,21 @@ void setup() { Serial.println(ME310::return_string(rc)); // print return value if(rc != ME310::RETURN_VALID) // exit on error return; - + Serial.println(); - Serial.println("Display Config Profile : "); + Serial.println("Display Config Profile : "); rc = myME310.display_config_profile(); // issue command and wait for answer or timeout if(rc == ME310::RETURN_VALID) // print all rows returned from ME310 except command echo (index = 0) print_buffer(myME310); else return; // exit on error - + Serial.println(); - Serial.print("Query SIM Status : "); + Serial.print("Query SIM Status : "); myME310.query_sim_status(); Serial.println(myME310.buffer_cstr(1)); Serial.println(); - Serial.println("Read Query SIM Status : "); + Serial.println("Read Query SIM Status : "); rc = myME310.read_query_sim_status(); if(rc == ME310::RETURN_VALID) // print all rows returned from ME310 except command echo (index = 0) { @@ -100,7 +100,7 @@ void setup() { if(strstr(resp,"#QSS: 0,1")) { 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) { @@ -113,10 +113,10 @@ void setup() { Serial.println("PIN required"); } } - else return; + else return; Serial.println(); - Serial.print("Print ICCID : "); + Serial.print("Print ICCID : "); rc = myME310.read_iccid(); if(rc == ME310::RETURN_VALID) { @@ -124,7 +124,7 @@ void setup() { if(resp != NULL) { const char *pLabel = strstr(resp,"+CCID: "); - if(pLabel) + if(pLabel) { Serial.print("["); Serial.write(pLabel+7,19); @@ -145,7 +145,7 @@ void setup() { 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 return; } else @@ -161,7 +161,7 @@ void setup() { if(rc == ME310::RETURN_VALID) Serial.println(myME310.buffer_cstr(1)); else return; - + Serial.print("Model Identification : "); rc = myME310.model_identification(); if(rc == ME310::RETURN_VALID) @@ -178,7 +178,7 @@ void setup() { rc = myME310.serial_number(); if(rc == ME310::RETURN_VALID) Serial.println(myME310.buffer_cstr(1)); - else return; + else return; Serial.print("Request Manufacturer Ident. : "); rc = myME310.request_manufacturer_identification(); @@ -237,7 +237,7 @@ void print_buffer(ME310 &aME310, const char *term) for(int index = 1;;index++) { const char * tmp = aME310.buffer_cstr(index); - if(tmp) + if(tmp) { if(term) { diff --git a/examples/MQTT_example/MQTT_example.ino b/examples/MQTT_example/MQTT_example.ino index 4006d3b..d43811e 100644 --- a/examples/MQTT_example/MQTT_example.ino +++ b/examples/MQTT_example/MQTT_example.ino @@ -19,7 +19,7 @@ @version - 1.0.0 + 1.1.0 @note @@ -34,7 +34,7 @@ #include #include -#define APN "\"APN\"" +#define APN "APN" #define HOSTNAME "api-dev.devicewise.com" #define PORT 1883 @@ -61,6 +61,7 @@ int cID = 1; //PDP Context Identifier char ipProt[]= "IP"; //Packet Data Protocol type int count = 0; +bool isConnect = false; void setup() { @@ -117,6 +118,10 @@ void setup() { resp = (char*)myME310.buffer_cstr(1); Serial.println(resp); } + else + { + break; + } } } /////////////////////////////////// @@ -151,24 +156,27 @@ void setup() { /////////////////////////////////// // AT#MQCFG=instance Number, hostname, port, cid, sslEN /////////////////////////////////// - Serial.println("mqtt configure"); + Serial.print("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) + if(rc == ME310::RETURN_VALID) { + Serial.print("mqtt connect: "); 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)); - if(rc == 0) + if(rc == ME310::RETURN_VALID) { + isConnect = true; //////////////////////////////////// // COMMAND TO SUBSCRIBE // // AT#MQSUB=instance_number, topic ///////////////////////////////////// + Serial.print("MQTT Topic Subscribe: "); 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)); @@ -177,7 +185,7 @@ void setup() { // // AT#MQPUBS=instance_number, topic, retain, qos, message //////////////////////////////////// - + Serial.print("MQTT Publish: "); 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)); @@ -189,7 +197,7 @@ void setup() { { Serial.println((String)"Error: " + rc + " Error string: " + myME310.buffer_cstr(2)); } -} + } else { Serial.println((String)"Error: " + rc + " Error string: " + myME310.buffer_cstr(2)); @@ -197,30 +205,52 @@ void setup() { } void loop(){ + if(isConnect) + { ///////////////////////////////////// - // COMMAND TO CHECK IF SOME MESSAGES HAVE ARRIVED - // - // AT#MQREAD? - //////////////////////////////////// - myME310.read_mqtt_read(); //issue command AT#MQREAD? and wait for answer or timeout - Serial.println(myME310.buffer_cstr(1)); - - ///////////////////////////////////// - // COMMAND TO READ A MESSAGE - // - // AT#MQREAD=instance_number, id_message - //////////////////////////////////// - myME310.mqtt_read(1,1); //issue command AT#MQREAD=instance_number, id_message and wait for answer or timeout - Serial.println(myME310.buffer_cstr_raw()); //print response in raw mode - count++; - if (count == 100) + // COMMAND TO CHECK IF SOME MESSAGES HAVE ARRIVED + // + // AT#MQREAD? + //////////////////////////////////// + Serial.print("MQTT read value: "); + myME310.read_mqtt_read(); //issue command AT#MQREAD? and wait for answer or timeout + Serial.println(myME310.buffer_cstr(1)); + char *resp = (char* )myME310.buffer_cstr(1); + if(resp != NULL) + { + if ((strcmp(resp, "#MQREAD: 1,1")) == 0) + { + ///////////////////////////////////// + // COMMAND TO READ A MESSAGE + // + // AT#MQREAD=instance_number, id_message + //////////////////////////////////// + Serial.print("MQTT read: "); + myME310.mqtt_read(1,1); //issue command AT#MQREAD=instance_number, id_message and wait for answer or timeout + Serial.println(myME310.buffer_cstr_raw()); //print response in raw mode + myME310.mqtt_disconnect(1); //AT#MQDISC=instance_number + exit(0); + } + else + { + count++; + } + } + + if (count == 5) + { + ///////////////////////////////////// + // COMMAND TO DISCONNECT + // + // AT#MQDISC=instance_number + //////////////////////////////////// + myME310.mqtt_disconnect(1); //AT#MQDISC=instance_number + exit(0); + } + } + else { - ///////////////////////////////////// - // COMMAND TO DISCONNECT - // - // AT#MQDISC=instance_number - //////////////////////////////////// - myME310.mqtt_disconnect(1); //AT#MQDISC=instance_number - exit(0); + Serial.println("Connection error"); + exit(0); } } diff --git a/examples/Sleep_mode_example/Sleep_mode_example.ino b/examples/Sleep_mode_example/Sleep_mode_example.ino new file mode 100644 index 0000000..843f237 --- /dev/null +++ b/examples/Sleep_mode_example/Sleep_mode_example.ino @@ -0,0 +1,133 @@ +/*Copyright (C) 2020 Telit Communications S.p.A. Italy - All Rights Reserved.*/ +/* See LICENSE file in the project root for full license information. */ + +/** + @file + ME310.cpp + string.h + stdio.h + + @brief + Sample test of the use of AT commands via ME310 library + + @details + In this example sketch, it is shown how to get the board to sleep mode using the AT + CFUN = 5 command via the ME310 library.\n + + @version + 1.0.0 + + @note + + @author + Cristina Desogus + + @date + 04/20/2022 + */ + + +#include +#include + +/*When NMEA_DEBUG is 0 Unsolicited NMEA is disable*/ +#define NMEA_DEBUG 0 + +#ifndef ARDUINO_TELIT_SAMD_CHARLIE +#define ON_OFF 6 /*Select the GPIO to control ON_OFF*/ +#endif + +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 + * 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; +ME310::return_t rc; + +int val; + +void setup() +{ + Serial.begin(115200); + myME310.begin(115200); + + delay(4000); + myME310.powerOn(); + Serial.println("ME310 is ON"); + + Serial.println("Application start"); + delay(1000); + Serial.print("Set DTR PIN: "); + rc = myME310.gpio_control(5,0,9,1); //this command sets GPIO5 as DTR and save the configuration + Serial.println(myME310.return_string(rc)); + + /*After the AT#GPIO=5,0,9,1 command, the module must be rebooted.*/ + + myME310.module_reboot(); //issue command AT#REBOOT + delay(5000); + + pinMode(PIN_MODULE_DTR,OUTPUT); //this command sets the DTR PIN in OUTPUT mode + digitalWrite(PIN_MODULE_DTR, LOW); //this command sets the DTR PIN to LOW level +} + +void loop() { + + Serial.print("Value of DTR is: "); + val = digitalRead(PIN_MODULE_DTR); //this command reads the DTR PIN value, it is only for control + Serial.println(val); + + Serial.print("CFUN=5 result: "); + rc = myME310.set_phone_functionality(5); //issues AT#CFUN=5 command and wait for answer or timeout + Serial.println(myME310.return_string(rc)); + + myME310.attention(); //issue AT command, for control + Serial.print("Control AT command result: "); + if(myME310.buffer_cstr(1) != NULL) + { + Serial.println(myME310.buffer_cstr(1)); + } + + delay(3000); + Serial.println("Move DTR to OFF"); + digitalWrite(PIN_MODULE_DTR, HIGH); //this command sets the DTR PIN to HIGH level + + Serial.print("Value of DTR: "); + val = digitalRead(PIN_MODULE_DTR); + Serial.println(val); + + myME310.attention(); + if(myME310.buffer_cstr(1) == NULL) + { + Serial.println("Module is in sleep mode"); + } + else + { + Serial.println("ERROR module is not in sleep mode"); + } + + delay(1000); + Serial.println("Move DTR to ON"); + digitalWrite(PIN_MODULE_DTR, LOW); //this command sets the DTR PIN to LOW level, it moves the DTR PIN to wake-up the module + + Serial.print("Value of DTR: "); + val = digitalRead(PIN_MODULE_DTR); + Serial.println(val); + delay(3000); + + myME310.attention(); + if(myME310.buffer_cstr(1) != NULL) + { + Serial.println("Module is awakened"); + } + else + { + Serial.println("ERROR Module is not awakened"); + } + exit(0); + +} \ No newline at end of file diff --git a/library.properties b/library.properties index e7f53d2..762be0c 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=ME310G1 -version=2.10.0 +version=2.12.0 author=Telit maintainer=Telit sentence=Allows communication with ME310G1 Telit module. diff --git a/src/ME310.cpp b/src/ME310.cpp index eca94f4..9bd12f6 100644 --- a/src/ME310.cpp +++ b/src/ME310.cpp @@ -17,7 +17,7 @@ It makes it easy to build Arduino applications that use the full power of ME310 module @version - 2.10.0 + 2.11.0 @note @@ -66,9 +66,10 @@ ME310::~ME310() /*! \brief Begin method \param baudRate baud rate of Uart for serial communication */ -void ME310::begin(unsigned long baudRate) +void ME310::begin(unsigned long baudRate, bool debug) { mBaudrate = baudRate; + _debug = debug; mSerial.begin(baudRate); } @@ -5848,7 +5849,7 @@ This function sets a user defined value to the specified resource, if whiteliste ME310::return_t ME310::setResourceBool(int type, int objID, int instanceID, int resourceID, int resourceInstance, int value, tout_t aTimeout) { (void)type; - return setResourceBool(objID, instanceID, resourceID, resourceInstance, value); + return setResourceBool(objID, instanceID, resourceID, resourceInstance, value, aTimeout); } /*! \brief Implements the AT#LWM2MSET command and wait OK answer @@ -7498,6 +7499,11 @@ const char *ME310::buffer_cstr_raw() void ME310::send(const char *aCommand, const char *aTerm) { on_command(aCommand); //callback + if(_debug) + { + Serial.print(aCommand); + Serial.println(aTerm); + } mSerial.write(aCommand); delay(200); mSerial.write(aTerm); @@ -7511,6 +7517,10 @@ void ME310::send(const char *aCommand, const char *aTerm) void ME310::send(const uint8_t* data, int len) { on_command((char*)data); //callback + if(_debug) + { + Serial.println((char*)data); + } mSerial.write(data, len); } diff --git a/src/ME310.h b/src/ME310.h index 7f2d185..fe2eb18 100644 --- a/src/ME310.h +++ b/src/ME310.h @@ -13,7 +13,7 @@ It makes it easy to build Arduino applications that use the full power of ME310 module @version - 2.10.0 + 2.11.0 @note Dependencies: @@ -122,7 +122,7 @@ namespace me310 void powerOn(unsigned int onoff_gpio); #endif - void begin(unsigned long baudRate); + void begin(unsigned long baudRate, bool debug = false); void end(); // Command Line Prefixes ------------------------------------------------------- @@ -1518,6 +1518,7 @@ namespace me310 uint32_t _option = 0; bool _isIRARx, _isIRATx; + bool _debug; static const char CTRZ[1];