From c8199cd907825d39effe6ba822e60df28a8d218a Mon Sep 17 00:00:00 2001 From: "Amir H. Yeganemehr" Date: Fri, 8 Sep 2023 23:14:57 +0330 Subject: [PATCH] Add receive-sms example Fix #1 --- .github/workflows/build-examples.yml | 7 +- README.md | 79 +++++++++++++++++++- examples/receive-sms/.gitignore | 5 ++ examples/receive-sms/.vscode/extensions.json | 10 +++ examples/receive-sms/platformio.ini | 17 +++++ examples/receive-sms/src/main.cpp | 65 ++++++++++++++++ examples/send-sms/platformio.ini | 9 +++ 7 files changed, 188 insertions(+), 4 deletions(-) create mode 100644 examples/receive-sms/.gitignore create mode 100644 examples/receive-sms/.vscode/extensions.json create mode 100644 examples/receive-sms/platformio.ini create mode 100644 examples/receive-sms/src/main.cpp diff --git a/.github/workflows/build-examples.yml b/.github/workflows/build-examples.yml index 4f1d51a..c47a884 100644 --- a/.github/workflows/build-examples.yml +++ b/.github/workflows/build-examples.yml @@ -7,7 +7,8 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - example: [send-sms] + example: [send-sms, receive-sms] + env_name: [nodemcuv2, wemos_d1_mini32] steps: - name: Checkout uses: actions/checkout@v3 @@ -22,6 +23,6 @@ jobs: python -m pip install --upgrade pip pip install --upgrade platformio - - name: Build ${{ matrix.example }} + - name: Build ${{ matrix.example }} on ${{ matrix.env_name }} working-directory: examples/${{ matrix.example }}/ - run: pio run + run: pio run -e ${{ matrix.env_name }} diff --git a/README.md b/README.md index 3d965b5..2aaeca6 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ This library built on top of [yeganemehr/arduino-at](https://github.com/yeganeme It's tested against Sim800c module & ESP8266 microprocessor but technically it can works with other boards and modules too. # Quickstart -Lets send an SMS: +## Sending an SMS: ```c++ #include @@ -61,6 +61,79 @@ void loop() ``` You can build and upload this code to your board quickly with [Ready-To-Use Example](examples/send-sms) +## Receing an SMS: + +```c++ +#include +#include +#include +#include +#include + +SoftwareSerial swSerial(D3, D4); // RX & TX pins +ATConnection at(&swSerial); +Sim800 sim800(&at); + +void setup() +{ + Serial.begin(9600); + swSerial.begin(9600); + + sim800.setup(); + sim800.on(Sim800IncomingSMSEvent::type, [](const Event *e) { + auto env = (Sim800IncomingSMSEvent *)e; + Serial.printf("New SMS received (memory: %s, index: %hhu)\n", env->getMemory(), env->getIndex()); + + Serial.println("Retrieve SMS from sim800"); + sim800.getSMS(env->getIndex()) + ->onSuccess([](const SMS *sms) { + Serial.println("Retrieve SMS from sim800 [done]"); + if (sms->getType() == SMS::Type::TEXT_SMS) { + auto textSMS = (TextSMS *)sms; + Serial.println("Type: TEXT_SMS"); + Serial.printf("Sender: %s\n", textSMS->getPeer()); + Serial.printf("Text: \"%s\"\n", textSMS->getText()); + } else if (sms->getType() == SMS::Type::STATUS_SMS) { + Serial.println("Type: STATUS_SMS"); + } else { + Serial.println("Type: Unsupported"); + } + }) + ->onFail([](const std::exception &reason) { + Serial.println("Retrive SMS from sim800 [failed]"); + }) + ->freeOnFinish(); + }); + + Serial.println("Initialize Sim800 connection"); + sim800.init() + ->onSuccess([]() { + Serial.println("Initialize Sim800 connection [done]"); + Serial.println("Put sim800 into sms text mode"); + sim800.putInTextMode() + ->onSuccess([]() { + Serial.println("Put sim800 into sms text mode [done]"); + }) + ->onFail([](const std::exception &reason) { + Serial.println("Put sim800 into sms text mode [failed]"); + }) + ->freeOnFinish(); + }) + ->onFail([](const std::exception &reason) { + Serial.println("Initialize Sim800 connection [failed]"); + }) + ->freeOnFinish(); +} + +void loop() +{ + at.communicate(); +} +``` +You can build and upload this code to your board quickly with [Ready-To-Use Example](examples/receive-sms) + + + ## Methods & Helpers **Promise\ \*Sim800::init()**: @@ -140,6 +213,10 @@ Sends `AT+CMGS` command to the modem and sends your plain ascii `text` to to `ph Its safer to format your `phone` into international format (+YYXXXXXXXX where YY is country code). If your message sent successfully, it's index in modems memory will accessible as promise's result. +**Promise\ \*getSMS(uint8_t index, bool changeStatus = true)**: +Retreive an SMS from primary SMS memory and return it as a `SMS` object. +if you pass `true` to `changeStatus` your unread message marked as read. + # TODO: * Add `SMS` & `TextSMS` classes to documention. diff --git a/examples/receive-sms/.gitignore b/examples/receive-sms/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/examples/receive-sms/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/examples/receive-sms/.vscode/extensions.json b/examples/receive-sms/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/examples/receive-sms/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/examples/receive-sms/platformio.ini b/examples/receive-sms/platformio.ini new file mode 100644 index 0000000..a15c418 --- /dev/null +++ b/examples/receive-sms/platformio.ini @@ -0,0 +1,17 @@ +[env:nodemcuv2] +build_type = debug +platform = espressif8266 +board = nodemcuv2 +framework = arduino +lib_deps = + plerup/EspSoftwareSerial@^8.1.0 + arduino-sim800=symlink://../../ + +[env:wemos_d1_mini32] +build_type = debug +platform = espressif32 +board = wemos_d1_mini32 +framework = arduino +lib_deps = + plerup/EspSoftwareSerial@^8.1.0 + arduino-sim800=symlink://../../ \ No newline at end of file diff --git a/examples/receive-sms/src/main.cpp b/examples/receive-sms/src/main.cpp new file mode 100644 index 0000000..dbdfa87 --- /dev/null +++ b/examples/receive-sms/src/main.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include + +SoftwareSerial swSerial(D3, D4); // RX & TX pins +ATConnection at(&swSerial); +Sim800 sim800(&at); + +void setup() +{ + Serial.begin(9600); + swSerial.begin(9600); + + sim800.setup(); + sim800.on(Sim800IncomingSMSEvent::type, [](const Event *e) { + auto env = (Sim800IncomingSMSEvent *)e; + Serial.printf("New SMS received (memory: %s, index: %hhu)\n", env->getMemory(), env->getIndex()); + + Serial.println("Retrieve SMS from sim800"); + sim800.getSMS(env->getIndex()) + ->onSuccess([](const SMS *sms) { + Serial.println("Retrieve SMS from sim800 [done]"); + if (sms->getType() == SMS::Type::TEXT_SMS) { + auto textSMS = (TextSMS *)sms; + Serial.println("Type: TEXT_SMS"); + Serial.printf("Sender: %s\n", textSMS->getPeer()); + Serial.printf("Text: \"%s\"\n", textSMS->getText()); + } else if (sms->getType() == SMS::Type::STATUS_SMS) { + Serial.println("Type: STATUS_SMS"); + } else { + Serial.println("Type: Unsupported"); + } + }) + ->onFail([](const std::exception &reason) { + Serial.println("Retrive SMS from sim800 [failed]"); + }) + ->freeOnFinish(); + }); + + Serial.println("Initialize Sim800 connection"); + sim800.init() + ->onSuccess([]() { + Serial.println("Initialize Sim800 connection [done]"); + Serial.println("Put sim800 into sms text mode"); + sim800.putInTextMode() + ->onSuccess([]() { + Serial.println("Put sim800 into sms text mode [done]"); + }) + ->onFail([](const std::exception &reason) { + Serial.println("Put sim800 into sms text mode [failed]"); + }) + ->freeOnFinish(); + }) + ->onFail([](const std::exception &reason) { + Serial.println("Initialize Sim800 connection [failed]"); + }) + ->freeOnFinish(); +} + +void loop() +{ + at.communicate(); +} \ No newline at end of file diff --git a/examples/send-sms/platformio.ini b/examples/send-sms/platformio.ini index 3ac0cbe..a15c418 100644 --- a/examples/send-sms/platformio.ini +++ b/examples/send-sms/platformio.ini @@ -6,3 +6,12 @@ framework = arduino lib_deps = plerup/EspSoftwareSerial@^8.1.0 arduino-sim800=symlink://../../ + +[env:wemos_d1_mini32] +build_type = debug +platform = espressif32 +board = wemos_d1_mini32 +framework = arduino +lib_deps = + plerup/EspSoftwareSerial@^8.1.0 + arduino-sim800=symlink://../../ \ No newline at end of file