[SIM800] Fixed begin method
This commit is contained in:
parent
1155851481
commit
f371c2d213
3 changed files with 49 additions and 28 deletions
|
@ -5,8 +5,14 @@
|
||||||
// include the library
|
// include the library
|
||||||
#include <RadioLib.h>
|
#include <RadioLib.h>
|
||||||
|
|
||||||
// SIM800 module is in slot A on the shield
|
// SIM800 has the following connections:
|
||||||
SIM800 gsm = RadioShield.ModuleA;
|
// 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() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
|
@ -14,7 +20,6 @@ void setup() {
|
||||||
// initialize SIM800 with default settings
|
// initialize SIM800 with default settings
|
||||||
Serial.print(F("[SIM800] Initializing ... "));
|
Serial.print(F("[SIM800] Initializing ... "));
|
||||||
// baudrate: 9600 baud
|
// baudrate: 9600 baud
|
||||||
// PIN: "1234"
|
|
||||||
int state = gsm.begin(9600);
|
int state = gsm.begin(9600);
|
||||||
if (state == ERR_NONE) {
|
if (state == ERR_NONE) {
|
||||||
Serial.println(F("success!"));
|
Serial.println(F("success!"));
|
||||||
|
@ -28,7 +33,7 @@ void setup() {
|
||||||
void loop() {
|
void loop() {
|
||||||
// send SMS to number 0123456789
|
// send SMS to number 0123456789
|
||||||
Serial.print(F("[SIM800] Sending SMS ... "));
|
Serial.print(F("[SIM800] Sending SMS ... "));
|
||||||
int state = gsm.sendSMS("0123456789", "Hello World!");
|
int state = gsm.sendSMS("774313955", "Hello World!");
|
||||||
if (state == ERR_NONE) {
|
if (state == ERR_NONE) {
|
||||||
Serial.println(F("success!"));
|
Serial.println(F("success!"));
|
||||||
} else {
|
} else {
|
||||||
|
@ -37,5 +42,6 @@ void loop() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// wait 10 seconds before sending again
|
// wait 10 seconds before sending again
|
||||||
delay(10000);
|
//delay(10000);
|
||||||
|
while(true);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ SIM800::SIM800(Module* module) {
|
||||||
_mod = module;
|
_mod = module;
|
||||||
}
|
}
|
||||||
|
|
||||||
int16_t SIM800::begin(long speed, const char* pin) {
|
int16_t SIM800::begin(long speed) {
|
||||||
// set module properties
|
// set module properties
|
||||||
_mod->AtLineFeed = "\r\n";
|
_mod->AtLineFeed = "\r\n";
|
||||||
_mod->baudrate = speed;
|
_mod->baudrate = speed;
|
||||||
|
@ -29,20 +29,6 @@ int16_t SIM800::begin(long speed, const char* pin) {
|
||||||
return(ERR_AT_FAILED);
|
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);
|
return(ERR_NONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,5 +41,34 @@ void SIM800::shutdown() {
|
||||||
}
|
}
|
||||||
|
|
||||||
int16_t SIM800::sendSMS(const char* num, const char* msg) {
|
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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ class SIM800 {
|
||||||
SIM800(Module* module);
|
SIM800(Module* module);
|
||||||
|
|
||||||
// basic methods
|
// basic methods
|
||||||
int16_t begin(long speed, const char* pin = "1234");
|
int16_t begin(long speed);
|
||||||
void shutdown();
|
void shutdown();
|
||||||
int16_t sendSMS(const char* num, const char* msg);
|
int16_t sendSMS(const char* num, const char* msg);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue