使用arduino用sim900发送短信
send sms with sim900 using arduino
#include <Password.h>
#include <Keypad.h>
#include <Servo.h>
#include "SIM900.h"
#include <SoftwareSerial.h>
#include "sms.h"
Servo myservo;
Password password = Password( "1234" ); //password to unlock box, can be changed
SMSGSM sms;
int numdata;
boolean started=false;
char smsbuffer[160];
char n[20];
const byte ROWS = 4;
const byte COLS = 4;
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 5, 4, 3, 2 };
int x=0;
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup() //if i add sms(); function it workssss
{
Serial.begin(9600); //Start a Serial COM
Serial.println(F("ARDUINO SECURITY SYSTEM V1.0"));
Serial.print(F("Checking GSM COM..."));
if (gsm.begin(9600)) //Start the GSM COM
{
(sms.SendSMS("+XXXXX","Your Home Security system is powered up"));
Serial.println(F("Good To GO!!"));
}
else
{
Serial.println(F("Could not connect to GSM modem"));
}
Serial.write(254);
Serial.write(0x01);
delay(200);
pinMode(11, OUTPUT); //green light
pinMode(12, OUTPUT); //red light
myservo.attach(13); //servo on digital pin 9 //servo
keypad.addEventListener(keypadEvent);//add an event listener for this keypad
}
void loop(){
keypad.getKey();
myservo.write(0);
}
//take care of some special events
void keypadEvent(KeypadEvent eKey){
switch (keypad.getState()){
case PRESSED:
Serial.print("Enter : ");
Serial.println(eKey);
delay(10);
Serial.write(254);
switch (eKey){
case 'A': checkPassword(); delay(1); break;
case 'C': checkPassword(); delay(1); break;
case 'D': checkPassword(); delay(1); break;
case 'B': password.reset(); delay(1); break;
case '*': checkPassword(); break;
case '#': password.reset(); break;
default: password.append(eKey); delay(1);
}
}
}
void checkPassword(){
if (password.evaluate()){ //if password is right open box
Serial.println("Accepted");
Serial.write(254);delay(50);
//Add code to run if it works
myservo.write(5); //160deg
digitalWrite(11, HIGH);//turn on
delay(2000); //wait 5 seconds
digitalWrite(11, LOW);// turn off
}
else
{
Serial.println("Denied"); //if passwords wrong keep box locked
Serial.write(254);delay(10);
x++;
if(x==3)
//add code to run if it did not work
{
myservo.write(0);
digitalWrite(12, HIGH);
delay(500);
digitalWrite(12, LOW);
if (gsm.begin(9600))
{
(sms.SendSMS("+XXXXX","Your Home Security system is being bridged"));
Serial.println("USER WARNED");
}
}
}
}
;
}
图中相同的代码在我放置线条时似乎不起作用
if (gsm.begin(9600)) //Start the GSM COM
{
(sms.SendSMS("+8613668914901","Your Home Security system is being bridged"));
但是这行代码在 void setup 函数中工作得很好。
我该如何解决这个问题?在 void 设置中,草图工作正常,但是当我也将代码放入函数 CheckPassword 中时,它不发送短信。
我还尝试创建一个函数让我们说 void SMS 并在 checkPassword 函数中调用它,但它没有解决问题,顺便说一句,在 void 设置中调用相同的函数时工作正常。
您应该只在 setup
函数中初始化 gsm
一次。
在您的代码中,您尝试在 checkPassword
方法中再次初始化它,这显然是行不通的。
因此,您应该从 checkPassword
函数中删除行 gsm.begin(9600)
。
更新 1:
在你的方案中,你为键盘保留了 pins 9, 8, 7, 6, 5, 4, 3, 2 .然而,与此同时,您为 GSM 模块保留引脚 2、3(参见 GSM.cpp
):
#define _GSM_TXPIN_ 2
#define _GSM_RXPIN_ 3
将相同的引脚用于多种用途通常会导致(如果未正确完成)未定义的行为,这在最好的情况下意味着您的草图没有按照预期的方式进行,而在最坏的情况下它可能会损坏您的组件。
您已经在 Serial 库中使用引脚 0, 1,但是根据您的代码,引脚 如果您想将现有引脚重新定位到您的组件,10、11、13 应该仍然可用。
另请注意 GSM 库中的以下 warnings:
[3] My shield doesn't work. Why?
Check this steps and then ask for support on the issues' page on google
code.
1) SIM900 and SIM908 require about 1 A during the hardest tasks.
You should have an external power source that can provide about
1 A at 8-12 V
2) If the SIM90X blinks (1 Hz) for some seconds and then turn off,
probably it's a communication's problem. Check the switch/jumpers
for Serial communication.
3) Arduino Uno has 2 KB of RAM. Library takes about 80% (we are working
to reduce it), if you use more than 20% left, Arduino can restart
or print on serial strange strings.
4) Check the jumper of communication, power source (battery or externel) and charge.
#include <Password.h>
#include <Keypad.h>
#include <Servo.h>
#include "SIM900.h"
#include <SoftwareSerial.h>
#include "sms.h"
Servo myservo;
Password password = Password( "1234" ); //password to unlock box, can be changed
SMSGSM sms;
int numdata;
boolean started=false;
char smsbuffer[160];
char n[20];
const byte ROWS = 4;
const byte COLS = 4;
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 5, 4, 3, 2 };
int x=0;
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup() //if i add sms(); function it workssss
{
Serial.begin(9600); //Start a Serial COM
Serial.println(F("ARDUINO SECURITY SYSTEM V1.0"));
Serial.print(F("Checking GSM COM..."));
if (gsm.begin(9600)) //Start the GSM COM
{
(sms.SendSMS("+XXXXX","Your Home Security system is powered up"));
Serial.println(F("Good To GO!!"));
}
else
{
Serial.println(F("Could not connect to GSM modem"));
}
Serial.write(254);
Serial.write(0x01);
delay(200);
pinMode(11, OUTPUT); //green light
pinMode(12, OUTPUT); //red light
myservo.attach(13); //servo on digital pin 9 //servo
keypad.addEventListener(keypadEvent);//add an event listener for this keypad
}
void loop(){
keypad.getKey();
myservo.write(0);
}
//take care of some special events
void keypadEvent(KeypadEvent eKey){
switch (keypad.getState()){
case PRESSED:
Serial.print("Enter : ");
Serial.println(eKey);
delay(10);
Serial.write(254);
switch (eKey){
case 'A': checkPassword(); delay(1); break;
case 'C': checkPassword(); delay(1); break;
case 'D': checkPassword(); delay(1); break;
case 'B': password.reset(); delay(1); break;
case '*': checkPassword(); break;
case '#': password.reset(); break;
default: password.append(eKey); delay(1);
}
}
}
void checkPassword(){
if (password.evaluate()){ //if password is right open box
Serial.println("Accepted");
Serial.write(254);delay(50);
//Add code to run if it works
myservo.write(5); //160deg
digitalWrite(11, HIGH);//turn on
delay(2000); //wait 5 seconds
digitalWrite(11, LOW);// turn off
}
else
{
Serial.println("Denied"); //if passwords wrong keep box locked
Serial.write(254);delay(10);
x++;
if(x==3)
//add code to run if it did not work
{
myservo.write(0);
digitalWrite(12, HIGH);
delay(500);
digitalWrite(12, LOW);
if (gsm.begin(9600))
{
(sms.SendSMS("+XXXXX","Your Home Security system is being bridged"));
Serial.println("USER WARNED");
}
}
}
}
;
}
图中相同的代码在我放置线条时似乎不起作用
if (gsm.begin(9600)) //Start the GSM COM
{
(sms.SendSMS("+8613668914901","Your Home Security system is being bridged"));
但是这行代码在 void setup 函数中工作得很好。
我该如何解决这个问题?在 void 设置中,草图工作正常,但是当我也将代码放入函数 CheckPassword 中时,它不发送短信。
我还尝试创建一个函数让我们说 void SMS 并在 checkPassword 函数中调用它,但它没有解决问题,顺便说一句,在 void 设置中调用相同的函数时工作正常。
您应该只在 setup
函数中初始化 gsm
一次。
在您的代码中,您尝试在 checkPassword
方法中再次初始化它,这显然是行不通的。
因此,您应该从 checkPassword
函数中删除行 gsm.begin(9600)
。
更新 1:
在你的方案中,你为键盘保留了 pins 9, 8, 7, 6, 5, 4, 3, 2 .然而,与此同时,您为 GSM 模块保留引脚 2、3(参见 GSM.cpp
):
#define _GSM_TXPIN_ 2
#define _GSM_RXPIN_ 3
将相同的引脚用于多种用途通常会导致(如果未正确完成)未定义的行为,这在最好的情况下意味着您的草图没有按照预期的方式进行,而在最坏的情况下它可能会损坏您的组件。
您已经在 Serial 库中使用引脚 0, 1,但是根据您的代码,引脚 如果您想将现有引脚重新定位到您的组件,10、11、13 应该仍然可用。
另请注意 GSM 库中的以下 warnings:
[3] My shield doesn't work. Why?
Check this steps and then ask for support on the issues' page on google code.
1) SIM900 and SIM908 require about 1 A during the hardest tasks. You should have an external power source that can provide about 1 A at 8-12 V
2) If the SIM90X blinks (1 Hz) for some seconds and then turn off, probably it's a communication's problem. Check the switch/jumpers for Serial communication.
3) Arduino Uno has 2 KB of RAM. Library takes about 80% (we are working to reduce it), if you use more than 20% left, Arduino can restart or print on serial strange strings.
4) Check the jumper of communication, power source (battery or externel) and charge.