From f371c2d21358a0f5ced92c849f87063d2d48f773 Mon Sep 17 00:00:00 2001 From: jgromes Date: Thu, 19 Sep 2019 08:04:24 +0200 Subject: [PATCH] [SIM800] Fixed begin method --- .../SIM800_SMS_Send/SIM800_SMS_Send.ino | 16 ++++-- src/modules/SIM800.cpp | 55 ++++++++++++------- src/modules/SIM800.h | 6 +- 3 files changed, 49 insertions(+), 28 deletions(-) diff --git a/examples/SIM800/SIM800_SMS_Send/SIM800_SMS_Send.ino b/examples/SIM800/SIM800_SMS_Send/SIM800_SMS_Send.ino index 14431d4a..a5a65b1b 100644 --- a/examples/SIM800/SIM800_SMS_Send/SIM800_SMS_Send.ino +++ b/examples/SIM800/SIM800_SMS_Send/SIM800_SMS_Send.ino @@ -5,8 +5,14 @@ // include the library #include -// SIM800 module is in slot A on the shield -SIM800 gsm = RadioShield.ModuleA; +// SIM800 has the following connections: +// TX pin: 9 +// RX pin: 8 +SIM800 gsm = new Module(9, 8); + +// or using RadioShield +// https://github.com/jgromes/RadioShield +//SIM800 gsm = RadioShield.ModuleA; void setup() { Serial.begin(9600); @@ -14,7 +20,6 @@ void setup() { // initialize SIM800 with default settings Serial.print(F("[SIM800] Initializing ... ")); // baudrate: 9600 baud - // PIN: "1234" int state = gsm.begin(9600); if (state == ERR_NONE) { Serial.println(F("success!")); @@ -28,7 +33,7 @@ void setup() { void loop() { // send SMS to number 0123456789 Serial.print(F("[SIM800] Sending SMS ... ")); - int state = gsm.sendSMS("0123456789", "Hello World!"); + int state = gsm.sendSMS("774313955", "Hello World!"); if (state == ERR_NONE) { Serial.println(F("success!")); } else { @@ -37,5 +42,6 @@ void loop() { } // wait 10 seconds before sending again - delay(10000); + //delay(10000); + while(true); } diff --git a/src/modules/SIM800.cpp b/src/modules/SIM800.cpp index 6b2a8dde..00af99be 100644 --- a/src/modules/SIM800.cpp +++ b/src/modules/SIM800.cpp @@ -4,45 +4,31 @@ SIM800::SIM800(Module* module) { _mod = module; } -int16_t SIM800::begin(long speed, const char* pin) { +int16_t SIM800::begin(long speed) { // set module properties _mod->AtLineFeed = "\r\n"; _mod->baudrate = speed; _mod->init(USE_UART, INT_0); - + // empty UART buffer (garbage data) _mod->ATemptyBuffer(); - + // power on pinMode(_mod->getInt0(), OUTPUT); digitalWrite(_mod->getInt0(), LOW); delay(1000); pinMode(_mod->getInt0(), INPUT); - + // test AT setup if(!_mod->ATsendCommand("AT")) { return(ERR_AT_FAILED); } - + // set phone functionality if(!_mod->ATsendCommand("AT+CFUN=1")) { return(ERR_AT_FAILED); } - - // set SMS message format - if(!_mod->ATsendCommand("AT+CMFG=1")) { - return(ERR_AT_FAILED); - } - - // set PIN code - char cmd[14]; - strcat(cmd, "AT+CPIN=\""); - strcat(cmd, pin); - strcat(cmd, "\""); - if(!_mod->ATsendCommand(cmd)) { - return(ERR_AT_FAILED); - } - + return(ERR_NONE); } @@ -55,5 +41,34 @@ void SIM800::shutdown() { } int16_t SIM800::sendSMS(const char* num, const char* msg) { + // set SMS message format to text mode + if(!_mod->ATsendCommand("AT+CMGF=1")) { + return(ERR_AT_FAILED); + } + // build SMS command and text + size_t cmdLen = 9 + strlen(num) + 3; + char* cmd = new char[cmdLen]; + strcpy(cmd, "AT+CMGS=\""); + strcat(cmd, num); + strcat(cmd, "\"\r"); + + size_t textLen = strlen(msg) + 2; + char* text = new char[textLen]; + strcpy(text, msg); + text[textLen - 2] = 0x1A; + text[textLen - 1] = '\0'; + + // send the command + _mod->ModuleSerial->print(cmd); + + delay(50); + + // send the text + _mod->ModuleSerial->print(text); + + delete[] cmd; + delete[] text; + + return(ERR_NONE); } diff --git a/src/modules/SIM800.h b/src/modules/SIM800.h index 261ff2b2..2ae44cc5 100644 --- a/src/modules/SIM800.h +++ b/src/modules/SIM800.h @@ -7,12 +7,12 @@ class SIM800 { public: // constructor SIM800(Module* module); - + // basic methods - int16_t begin(long speed, const char* pin = "1234"); + int16_t begin(long speed); void shutdown(); int16_t sendSMS(const char* num, const char* msg); - + private: Module* _mod; };