我有一个连接了sim800l和dht11的arduino nano,下面的代码检测传入的短信并用dht11值回复该号码,现在我添加了电话,因此当有人拨打该号码时,它会发送ATH挂断,但无法发送短信,因为我不知道如何检测号码并将短信发送给它。这是代码
#include<SoftwareSerial.h>
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
SoftwareSerial mySerial(10, 11);
int index = 0;
float h;
float temp;
char incomingByte;
String incomingData;
bool atCommand = true;
String message = "";
String number = "";
void setup()
{
Serial.begin(9600);
mySerial.begin(9600);
dht.begin();
while (!mySerial.available()) {
mySerial.println("AT");
delay(1000);
Serial.println("connecting....");
}
mySerial.println("AT+CMGF=1"); //Set SMS Text Mode
delay(1000);
mySerial.println("AT+CNMI=1,2,0,0,0"); //procedure, how to receive messages from the network
delay(1000);
//mySerial.println("AT+CMGL=\"REC UNREAD\""); // Read unread messages
mySerial.println("AT+CBAND=DCS_MODE");
Serial.println("Ready to receive Commands..");
}
void loop()
{
if (mySerial.find("RING")){
mySerial.println("ATH");
SendTextMessage();
Serial.println("send");
}
if (mySerial.available()) {
delay(100);
// Serial buffer
while (mySerial.available()) {
incomingByte = mySerial.read();
incomingData += incomingByte;
}
delay(10);
if (atCommand == false) {
receivedMessage(incomingData);
}
else {
atCommand = false;
}
//delete messages to save memory
if (incomingData.indexOf("OK") == -1) {
mySerial.println("AT+CMGDA=\"DEL ALL\"");
delay(1000);
atCommand = true;
}
incomingData = "";
}
if (message.indexOf("SEND") > -1) { //
SendTextMessage();
Serial.println("send");
}
}
void SendTextMessage()
{
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float temp = dht.readTemperature();
mySerial.println("AT+CMGF=1"); //To send SMS in Text Mode
delay(1000);
mySerial.println("AT+CMGS=\"" + number + "\"\r"); // change to the phone number you using
delay(2000);
mySerial.print("Humidity: ");
mySerial.print(h);
mySerial.print(" % ");
mySerial.print("Temperature: ");
mySerial.print(temp);
mySerial.println(" *C ");
delay(200);
mySerial.println((char)26);//the stopping character
delay(1000);
}
void receivedMessage(String inputString) {
//Get The number of the sender
index = inputString.indexOf('"') + 1;
inputString = inputString.substring(index);
index = inputString.indexOf('"');
number = inputString.substring(0, index);
Serial.println("Number: " + number);
//Get The Message of the sender
index = inputString.indexOf("\n") + 1;
message = inputString.substring(index);
message.trim();
Serial.println("Message: " + message);
message.toUpperCase(); // uppercase the message received
Serial.println("receive");
delay(50);
}