我正在尝试从arduino UNO向手机发送sms消息,事实是我从Android应用程序收到消息但是当我尝试直接将其发送到sms消息时arduino崩溃。 / p>
这是我到目前为止尝试过的代码:
#include "SIM900.h"
#include <SoftwareSerial.h>
SoftwareSerial portOne(4,3);
SoftwareSerial portTwo(6,5);
#include "sms.h"
SMSGSM sms;
int numdata;
boolean started=false;
char smsbuffer[160];
char n[10]= "87423474";
void setup()
{
Serial.begin(9600);
while(!Serial){
;
}
portOne.begin(9600);
portTwo.begin(9600);
Serial.println("***** Prueba de envio de SMS y Bluetooth *****");
//Start configuration of shield with baudrate.
//For http uses is raccomanded to use 4800 or slower.
if (gsm.begin(2400)){
Serial.println("\nstatus=READY");
started=true;
}
else Serial.println("\nstatus=IDLE");
}
void loop()
{
portTwo.listen();
Serial.println("Entra a loop1");
if ( portTwo.available() )
{
Serial.println("Entro a loop 2");
int x;
x=Serial.write(portTwo.read());
char ConvirtiendoAChar[15];
String string;
string=String(x);
string.toCharArray(ConvirtiendoAChar,15);
Serial.println("Convirtio a char");
portOne.listen();
if (portOne.isListening()){
Serial.println("Otro debug");
if(sms.SendSMS(n,ConvirtiendoAChar))
{
Serial.println("\nEl mensaje se envió correctamente");
}}
}
Serial.println("No entro a loop");
delay(2000);
}
我不知道我做错了什么,或者是否有更好的方法。
答案 0 :(得分:0)
不使用sms.h
,您可以通过直接向SIM900模块发送AT命令来轻松完成任务。即使sms.h
也是如此,但它封装了这些方法。模块和标题之间可能存在一些不匹配。
#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
void setup()
{
SIM900.begin(19200);
SIM900power();
// give time to log on to network.
delay(20000);
}
// software equivalent of pressing the GSM shield "power" button
void SIM900power()
{
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
delay(5000);
}
void sendSMS()
{
// AT command to send SMS message
SIM900.print("AT+CMGF=1\r");
delay(100);
// recipient's mobile number, in international format
SIM900.println("AT + CMGS = \"+0123456789\"");
delay(100);
SIM900.println("Hello, world. This is a text message from an Arduino Uno."); // message to send
delay(100);
SIM900.println((char)26);
// End AT command with a ^Z, ASCII code 26
delay(100);
SIM900.println();
// give module time to send SMS
delay(5000);
// turn off module
SIM900power();
}
void loop()
{
sendSMS();
do {} while (1);
}