From 0cca185d1bbe5877aa95d44b0914e90482b327a5 Mon Sep 17 00:00:00 2001 From: Antonio Date: Sun, 17 May 2020 13:21:20 +0200 Subject: [PATCH 1/3] Add support for ESP32 HardwareSerial --- src/Sim800.cpp | 22 ++++++++++++++++------ src/Sim800.h | 13 +++++++------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/Sim800.cpp b/src/Sim800.cpp index ab2ac14..7fa9304 100644 --- a/src/Sim800.cpp +++ b/src/Sim800.cpp @@ -32,6 +32,9 @@ const char SLEEP_MODE_2[] PROGMEM = "AT+CSCLK=2\r\n"; const char SLEEP_MODE_1[] PROGMEM = "AT+CSCLK=1\r\n"; const char SLEEP_MODE_0[] PROGMEM = "AT+CSCLK=0\r\n"; const char AT_OK[] PROGMEM = "OK"; +const char AT[] PROGMEM = "AT\r\n"; +const char POWER_DOWN[] PROGMEM = "AT+CPOWD=0\r\n"; + int SIM800::preInit(void) { @@ -47,6 +50,8 @@ int SIM800::preInit(void) purgeSerial(); serialSIM800.flush(); + sendATTest(); + return TRUE; } @@ -106,7 +111,7 @@ void SIM800::sendCmd(const char *cmd, unsigned int delayBeforeSend) int SIM800::sendATTest(void) { - int ret = sendCmdAndWaitForResp("AT\r\n", "OK", DEFAULT_TIMEOUT); + int ret = sendCmdAndWaitForResp_P(AT, AT_OK, DEFAULT_TIMEOUT); return ret; } @@ -189,29 +194,34 @@ void SIM800::purgeSerial() void SIM800::write(const char *data) { - serialSIM800.listen(); + //serialSIM800.listen(); serialSIM800.write(data); } void SIM800::write(const char *data, unsigned int size) { - serialSIM800.listen(); + //serialSIM800.listen(); + serialSIM800.write(data, size); +} + +void SIM800::write(const uint8_t *data, unsigned int size){ serialSIM800.write(data, size); } -void SIM800::sleep(bool force) +int SIM800::sleep(bool force) { if (force) { - sendCmdAndWaitForResp_P(SLEEP_MODE_1, AT_OK, 2000); + return sendCmdAndWaitForResp_P(SLEEP_MODE_1, AT_OK, 2000); } else { - sendCmdAndWaitForResp_P(SLEEP_MODE_2, AT_OK, 2000); + return sendCmdAndWaitForResp_P(SLEEP_MODE_2, AT_OK, 2000); } } void SIM800::wakeUp() { preInit(); + sendCmdAndWaitForResp_P(SLEEP_MODE_0, AT_OK, 2000); } diff --git a/src/Sim800.h b/src/Sim800.h index f699526..0d0006a 100644 --- a/src/Sim800.h +++ b/src/Sim800.h @@ -30,14 +30,14 @@ #define __SIM800_H__ #include -#include +#include #define TRUE 1 #define FALSE 0 #define DEFAULT_TIMEOUT 5000 // Comment or uncomment this to debug the library -// #define DEBUG true +#define DEBUG true /** SIM800 class. * Used for SIM800 communication. attention that SIM800 module communicate with MCU in serial protocol @@ -55,9 +55,9 @@ class SIM800 SIM800(unsigned int baudRate, unsigned int rxPin, unsigned int txPin, - unsigned int rstPin) : serialSIM800(txPin, rxPin) + unsigned int rstPin) : serialSIM800(1) { - serialSIM800.begin(baudRate); + serialSIM800.begin(baudRate, SERIAL_8N1, txPin, rxPin); resetPin = rstPin; }; @@ -134,12 +134,13 @@ class SIM800 void write(const char *data); void write(const char *data, unsigned int size); + void write(const uint8_t *data, unsigned int size); - void sleep(bool force = FALSE); + int sleep(bool force = FALSE); void wakeUp(); protected: - SoftwareSerial serialSIM800; + HardwareSerial serialSIM800; unsigned int resetPin; }; From 433548cb5c62e6f568ac0b0611d752502dd3fb38 Mon Sep 17 00:00:00 2001 From: Antonio Date: Sun, 17 May 2020 13:35:49 +0200 Subject: [PATCH 2/3] Undo changes --- src/Sim800.cpp | 6 +----- src/Sim800.h | 2 +- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Sim800.cpp b/src/Sim800.cpp index 7fa9304..c987c0a 100644 --- a/src/Sim800.cpp +++ b/src/Sim800.cpp @@ -32,8 +32,6 @@ const char SLEEP_MODE_2[] PROGMEM = "AT+CSCLK=2\r\n"; const char SLEEP_MODE_1[] PROGMEM = "AT+CSCLK=1\r\n"; const char SLEEP_MODE_0[] PROGMEM = "AT+CSCLK=0\r\n"; const char AT_OK[] PROGMEM = "OK"; -const char AT[] PROGMEM = "AT\r\n"; -const char POWER_DOWN[] PROGMEM = "AT+CPOWD=0\r\n"; int SIM800::preInit(void) @@ -50,8 +48,6 @@ int SIM800::preInit(void) purgeSerial(); serialSIM800.flush(); - sendATTest(); - return TRUE; } @@ -208,7 +204,7 @@ void SIM800::write(const uint8_t *data, unsigned int size){ serialSIM800.write(data, size); } -int SIM800::sleep(bool force) +void SIM800::sleep(bool force) { if (force) { diff --git a/src/Sim800.h b/src/Sim800.h index 0d0006a..fee07c7 100644 --- a/src/Sim800.h +++ b/src/Sim800.h @@ -136,7 +136,7 @@ class SIM800 void write(const char *data, unsigned int size); void write(const uint8_t *data, unsigned int size); - int sleep(bool force = FALSE); + void sleep(bool force = FALSE); void wakeUp(); protected: From d23f3055a180d03f0f9b63dc74a3d0d3f9c9c1a3 Mon Sep 17 00:00:00 2001 From: Antonio Date: Mon, 18 May 2020 11:51:09 +0200 Subject: [PATCH 3/3] Fix compilation errors --- src/Sim800.cpp | 14 +++++--------- src/Sim800.h | 1 - 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/Sim800.cpp b/src/Sim800.cpp index c987c0a..efdd12c 100644 --- a/src/Sim800.cpp +++ b/src/Sim800.cpp @@ -32,7 +32,7 @@ const char SLEEP_MODE_2[] PROGMEM = "AT+CSCLK=2\r\n"; const char SLEEP_MODE_1[] PROGMEM = "AT+CSCLK=1\r\n"; const char SLEEP_MODE_0[] PROGMEM = "AT+CSCLK=0\r\n"; const char AT_OK[] PROGMEM = "OK"; - +const char AT[] PROGMEM = "AT\r\n"; int SIM800::preInit(void) { @@ -98,7 +98,7 @@ void SIM800::cleanBuffer(char *buffer, int count) void SIM800::sendCmd(const char *cmd, unsigned int delayBeforeSend) { - serialSIM800.listen(); + //serialSIM800.listen(); serialSIM800.flush(); delay(delayBeforeSend); write(cmd); @@ -197,22 +197,18 @@ void SIM800::write(const char *data) void SIM800::write(const char *data, unsigned int size) { //serialSIM800.listen(); - serialSIM800.write(data, size); -} - -void SIM800::write(const uint8_t *data, unsigned int size){ - serialSIM800.write(data, size); + serialSIM800.write((const uint8_t*)data, size); } void SIM800::sleep(bool force) { if (force) { - return sendCmdAndWaitForResp_P(SLEEP_MODE_1, AT_OK, 2000); + sendCmdAndWaitForResp_P(SLEEP_MODE_1, AT_OK, 2000); } else { - return sendCmdAndWaitForResp_P(SLEEP_MODE_2, AT_OK, 2000); + sendCmdAndWaitForResp_P(SLEEP_MODE_2, AT_OK, 2000); } } diff --git a/src/Sim800.h b/src/Sim800.h index fee07c7..9480f73 100644 --- a/src/Sim800.h +++ b/src/Sim800.h @@ -134,7 +134,6 @@ class SIM800 void write(const char *data); void write(const char *data, unsigned int size); - void write(const uint8_t *data, unsigned int size); void sleep(bool force = FALSE); void wakeUp();