Arduino发送短信回复发送号码

时间:2019-09-26 15:21:45

标签: arduino sms sim800

我有一个Arduino SIM800代码,可以接收SMS并发送回信。

#include<SoftwareSerial.h>

SoftwareSerial mySerial(9, 10);//Connect to pin 9&10 on GSM module 

const byte numChars = 128; //character size
char receivedChars[numChars]; // Store data read from SMS
boolean newData = false; //flag to check if new data has arrived

void setup() {
 Serial.begin(9600);
 mySerial.begin(9600);
 Serial.println("<Arduino is ready>");
 mySerial.println("AT");
 Serial.println("AT done");
 mySerial.println("AT+CMGF=1"); //text mode
 Serial.println("Text Mode");
 mySerial.println("AT+CNMI=1,2,0,0,0");
 Serial.println("How to handle");
 mySerial.println("AT+CSQ"); //Check signal strength
   while (Serial.available()) 
  {
    mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
  }
  while(mySerial.available()) 
  {
    Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
  }
}

void loop() {
  //code for checking sms
  scanSMS();
  //Print if a new data arrived
  showNewData();
}

void scanSMS() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '#'; //Start and end markers. Eg. SMS body will be sent as #status# 
    char endMarker = '#';
    char rc;
    while (Serial.available()) 
    {
      mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
    }

    while (mySerial.available() > 0 && newData == false) {
      //Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port

      rc = mySerial.read();

      if (recvInProgress == true) {
        if (rc != endMarker) {
          receivedChars[ndx] = rc;
          ndx++;
          if (ndx >= numChars) {
            ndx = numChars - 1;
          }
        }
        else {
          receivedChars[ndx] = '\0'; // terminate the string
          recvInProgress = false;
          ndx = 0;
          newData = true;
          }
        }
      else if (rc == startMarker) {
        recvInProgress = true;
      }
    }
}

/*PRINT IF THERE IS A NEW DATA*/
void showNewData() {
 if (newData == true) {
  String charToString=receivedChars;
  if(charToString=="status"){
   SendMessage(receivedChars);
   Serial.println("AT+CNUM=?");
   updateSerial();
  }
 newData = false;
 }
}


/*SEND BACK WHATEVER SMS IS RECEIVED*/
void SendMessage(String receivedChars)
{
  String mobilenum = Serial.println("AT+CNUM=?");
  Serial.print("here");
  mySerial.println("AT"); //Handshaking with SIM900
  updateSerial();
  mySerial.println("AT+CMGF=1");
  updateSerial();
  mySerial.println("AT+CMGS=\"+mobileno\"\r"); // Replace x with mobile number
  updateSerial();
  mySerial.println("Current Status!");// The SMS text you want to send
  updateSerial();
  mySerial.print("Humidity : ");// The SMS text you want to send
  updateSerial();
  mySerial.print(hum);
  mySerial.println("%");
  mySerial.print("Temperature : ");// The SMS text you want to send
  updateSerial();
  mySerial.print(temp);
  mySerial.println(" C");
  updateSerial();
  //mySerial.print(receivedChars);
  //updateSerial();
   mySerial.println((char)26);// ASCII code of CTRL+Z
}


void updateSerial()
{
  delay(500);
  while (Serial.available()) 
  {
    mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
  }
  while(mySerial.available()) 
  {
    Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
  }
}

如果回复电话号码是硬编码的,我可以发送回复。但是,我希望Arduino发送对发送初始SMS的手机号码的答复。我无法做到这一点。实际上,我不确定如何从邮件中提取发件人的电话号码。我们非常感谢您的帮助。

0 个答案:

没有答案