From 455fd277c9d4c0afcb05c1c9070a5f842c875d39 Mon Sep 17 00:00:00 2001 From: Fabio Pintus Date: Mon, 15 Nov 2021 09:29:02 +0100 Subject: [PATCH] Add generic Command example and functions --- CHANGELOG | 3 + README.md | 1 + .../GenericCommand_example.ino | 84 +++++++++++++++++++ library.properties | 2 +- src/ME310.cpp | 18 +++- src/ME310.h | 9 +- 6 files changed, 113 insertions(+), 4 deletions(-) create mode 100644 examples/GenericCommand_example/GenericCommand_example.ino diff --git a/CHANGELOG b/CHANGELOG index 32e07d5..7435440 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,8 @@ ME310 0.0.0 - ????.??.?? +ME310 2.7.0 - 2021.11.15 +* Added generic AT command utilities and example sketch + ME310 2.6.0 - 2021.10.14 * File utilities improvements * sketches code improvements diff --git a/README.md b/README.md index 777464d..40a8439 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ The following examples are available: - **[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_ - **[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)_ ## Support diff --git a/examples/GenericCommand_example/GenericCommand_example.ino b/examples/GenericCommand_example/GenericCommand_example.ino new file mode 100644 index 0000000..1e1a051 --- /dev/null +++ b/examples/GenericCommand_example/GenericCommand_example.ino @@ -0,0 +1,84 @@ +/*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 Generic Command method via ME310 library + + @details + In this example sketch, the use of method offered by the ME310 library for using AT commands is shown.\n + Through this method, it is possible to send a generic AT command or a list of AT commands.\n + + + @version + 1.0.0 + + @note + + @author + Cristina Desogus + + @date + 08/11/2021 + */ +#include +#include + +#ifndef ARDUINO_TELIT_SAMD_CHARLIE +#define ON_OFF 6 /*Select the GPIO to control ON_OFF*/ +#endif + +using namespace me310; + +ME310 myME310; +ME310::return_t myRc; +void setup() { + int i = 0; + const char OKanswer[] = "OK"; + Serial.begin(115200); + myME310.begin(115200); + delay(1000); + Serial.println("Generic AT Command test"); + delay(1000); + myME310.powerOn(); + Serial.println("ME310 is ON"); + + /* + ATCommand is the command string which contains the AT commands to send. + The single command must be separated from the ;. + The "AT" string must not be repeated. + For example: "AT#GPIO=1,1,1;#GPIO=2,1,1;#GPIO=3,1,1" + */ + const char ATCommand[] = "AT#SWPKGV;#CGMM"; + Serial.println((String)"Send: " + ATCommand); + myRc = myME310.send_command(ATCommand, OKanswer); //issue a list of command indicated in the string and wait for anwser or timeout + Serial.println((String) "Result value: " + myME310.return_string(myRc)); //print the anwser value + while(myME310.buffer_cstr(i) != NULL) + { + Serial.println(myME310.buffer_cstr(i)); //print line of modem answer + i++; + } + + const char readCommand[] = "at+cops?"; + Serial.println((String)"Send: " + readCommand); + myRc = myME310.send_command(readCommand, OKanswer); //issue a command indicated in the string and wait for anwser or timeout + Serial.println((String) "Result value: " + myME310.return_string(myRc)); + Serial.println(myME310.buffer_cstr(1)); + + const char testCommand[] = "at+cmee=?"; + Serial.println((String)"Send: " + testCommand); + myRc = myME310.send_command(testCommand, OKanswer); + Serial.println((String) "Result value: " + myME310.return_string(myRc)); + Serial.println(myME310.buffer_cstr(1)); +} + +void loop() { + // put your main code here, to run repeatedly: + Serial.println("The application has ended..."); + exit(0); +} diff --git a/library.properties b/library.properties index 121c59f..571d2a8 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=ME310G1 -version=2.6.0 +version=2.7.0 author=Telit maintainer=Telit sentence=Allows communication with ME310G1 Telit module. diff --git a/src/ME310.cpp b/src/ME310.cpp index 006271d..85298d2 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.6.0 + 2.7.0 @note @@ -6368,6 +6368,22 @@ ME310::return_t ME310::set_trace(int mode, char* configuration_string, tout_t aT snprintf((char *)mBuffer, ME310_BUFFSIZE-1, F("AT#TRACE=%d,%s"), mode, configuration_string); return send_wait((char*)mBuffer, OK_STRING, aTimeout); } + +//! \brief Send the generic AT command and waits for a specific answer +/*! \details + The command sends a generic AT command and waits for a specific answer. + * \param aCommand command string to send + * \param aAnswer answer string to wait for + * \param aTimeout answer timeout + * \return return code + */ +ME310::return_t ME310::send_command(const char *aCommand, const char *aAnswer, tout_t aTimeout) +{ + memset(mBuffer, 0, ME310_BUFFSIZE); + snprintf((char *)mBuffer, ME310_BUFFSIZE-1, aCommand); + return send_wait((char*)mBuffer, aAnswer, aTimeout); +} + //-------------------------------------------------------------------------------------------------------------- //! \brief Returns the string by index received from the ME310 serial connection /*! diff --git a/src/ME310.h b/src/ME310.h index 9563542..8418b14 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.6.0 + 2.7.0 @note Dependencies: @@ -1243,7 +1243,12 @@ namespace me310 return_t set_trace(int mode, char* configuration_string, tout_t aTimeout = TOUT_100MS); _READ_TEST(set_trace, "AT#TRACE", TOUT_100MS) - // ----------------------------------------------------------------------------- + + // Generic Command--------------------------------------------------------------- + + return_t send_command(const char *aCommand, const char *aAnswer = OK_STRING, tout_t aTimeout = TOUT_200MS); + + const uint8_t *buffer(void) { return mBuffer;} //!< Returns pointer to local buffer size_t length(void) { return mBuffLen;} //!< Returns length of local buffer const char * buffer_cstr(int aIndex = 0);