[SIM800] Fixed begin method

This commit is contained in:
jgromes 2019-09-19 08:04:24 +02:00
parent 1155851481
commit f371c2d213
3 changed files with 49 additions and 28 deletions

View file

@ -5,8 +5,14 @@
// include the library
#include <RadioLib.h>
// 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);
}

View file

@ -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);
}

View file

@ -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;
};