使用Arduino + Sparkfun SM5100B + Cellular Shield。
我需要以下代码来循环播放一组移动号码,每个号码延迟一分钟。如果Arduino收到肯定/'是'消息,我需要这个来打破循环并结束它。如果收到“否”或否定消息,我需要它继续循环。
现在,我已经成功编译了,但是当它在循环中正确延迟并在串行监视器中打印正确的消息时,它实际上只发送测试消息到第一个数字以及AT命令发送到在这段代码中,我已经包含了接收消息代码的下一个号码(在第一个号码接收的文本消息中),因为我想在我尝试打破它之前先让循环工作。
//THis code is sending both messages to first number
#include <GSM.h>
#define PINNUMBER ""
const int buttonPin = 4;
const int ledPin1 = 13;
const int ledPin2 = 12;
int buttonState = 0;
// initialize the library instance
GSM gsmAccess; // include a 'true' parameter for debug enabled
GSM_SMS sms;
// char array of the telephone number to send SMS
const char* number1 = "xxxxxxxxxxxxx"; // enter any two mobile numbers here in international format
const char* number2 = "xxxxxxxxxxxxx";
// char array of the message
char txtMsg[200]="test";
// connection state
boolean notConnected = true;
void setup()
{
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(buttonPin, INPUT);
// initialize serial communications
Serial.begin(9600);
Serial.println("SMS Messages Sender");
// Start GSM shield
// If your SIM has PIN, pass it as a parameter of begin() in quotes
while(notConnected)
{
if(gsmAccess.begin(PINNUMBER)==GSM_READY)
notConnected = false;
else
{
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("GSM initialized");
}
void loop()
{
buttonState = digitalRead(buttonPin);
{
sendSMS();
}
}
void sendSMS()
{
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
if(buttonState == HIGH) {
buttonState = 1;
};
if (buttonState == 1) {
digitalWrite(ledPin2, HIGH);
delay(1000);
Serial.print("Message to mobile number: ");
Serial.println(number1);
// sms text
Serial.println("SENDING");
Serial.println();
Serial.println("Message:");
Serial.println(txtMsg);
// send the message
sms.beginSMS(number1);
sms.print(txtMsg);
Serial.println("\nFirst Message Sent\n");
digitalWrite(ledPin2, LOW);
delay (60000);
digitalWrite(ledPin2, HIGH);
delay(1000);
Serial.print("Message to mobile number: ");
Serial.println(number2);
// sms text
Serial.println("SENDING");
Serial.println();
Serial.println("Message:");
Serial.println(txtMsg);
// send the message
sms.beginSMS(number2);
sms.print(txtMsg);
sms.endSMS();
Serial.println("\nSecond Message Sent\n");
digitalWrite(ledPin2, LOW);
}
}
答案 0 :(得分:1)
更改GSM_sms;
到
GSM_smsone;
GSM_smstwo;
使用
smsone.beginSMS(数量1);
smsone.print(txtMsg);
smsone.endSMS();
smstwo.beginSMS(数字2);
smstwo.print(txtMsg);
smstwo.endSMS();
发送短信。
#include <GSM.h>
#define PINNUMBER ""
const int buttonPin = 4;
const int ledPin1 = 13;
const int ledPin2 = 12;
int buttonState = 0;
// initialize the library instance
GSM gsmAccess; // include a 'true' parameter for debug enabled
GSM_SMS smsone;
GSM_SMS smstwo;
// char array of the telephone number to send SMS
const char* number1 = "xxxxxxxxxxxxx"; // enter any two mobile numbers here in international format
const char* number2 = "xxxxxxxxxxxxx";
// char array of the message
char txtMsg[200] = "test";
// connection state
boolean notConnected = true;
void setup()
{
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(buttonPin, INPUT);
// initialize serial communications
Serial.begin(9600);
Serial.println("SMS Messages Sender");
// Start GSM shield
// If your SIM has PIN, pass it as a parameter of begin() in quotes
while (notConnected)
{
if (gsmAccess.begin(PINNUMBER) == GSM_READY)
notConnected = false;
else
{
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("GSM initialized");
}
void loop(){
buttonState = digitalRead(buttonPin);
{
sendSMS();
}
}
void sendSMS()
{
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
if (buttonState == HIGH) {
buttonState = 1;
};
if (buttonState == 1) {
digitalWrite(ledPin2, HIGH);
delay(1000);
Serial.print("Message to mobile number: ");
Serial.println(number1);
// sms text
Serial.println("SENDING");
Serial.println();
Serial.println("Message:");
Serial.println(txtMsg);
// send the first message
smsone.beginSMS(number1);
smsone.print(txtMsg);
smsone.endSMS();
Serial.println("\nFirst Message Sent\n");
digitalWrite(ledPin2, LOW);
delay (60000);
digitalWrite(ledPin2, HIGH);
delay(1000);
Serial.print("Message to mobile number: ");
Serial.println(number2);
// sms text
Serial.println("SENDING");
Serial.println();
Serial.println("Message:");
Serial.println(txtMsg);
// send the second message
smstwo.beginSMS(number2);
smstwo.print(txtMsg);
smstwo.endSMS();
Serial.println("\nSecond Message Sent\n");
digitalWrite(ledPin2, LOW);
}
}