Skip to content

Commit

Permalink
Add receive-sms example
Browse files Browse the repository at this point in the history
Fix #1
  • Loading branch information
yeganemehr committed Sep 8, 2023
1 parent c3ee176 commit c8199cd
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 4 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/build-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }}
79 changes: 78 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Arduino.h>
Expand Down Expand Up @@ -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 <Arduino.h>
#include <SoftwareSerial.h>
#include <ATConnection.hpp>
#include <Sim800.hpp>
#include <Sim800/Events.hpp>
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\<void\> \*Sim800::init()**:
Expand Down Expand Up @@ -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\<SMS \*\> \*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.

Expand Down
5 changes: 5 additions & 0 deletions examples/receive-sms/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
10 changes: 10 additions & 0 deletions examples/receive-sms/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
17 changes: 17 additions & 0 deletions examples/receive-sms/platformio.ini
Original file line number Diff line number Diff line change
@@ -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://../../
65 changes: 65 additions & 0 deletions examples/receive-sms/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <Arduino.h>
#include <SoftwareSerial.h>
#include <ATConnection.hpp>
#include <Sim800.hpp>
#include <Sim800/Events.hpp>

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();
}
9 changes: 9 additions & 0 deletions examples/send-sms/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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://../../

0 comments on commit c8199cd

Please sign in to comment.