SIM900无法与Arduino一起使用

时间:2017-09-11 12:36:28

标签: arduino sim900

最近我买了一个SIM900A模块和一个Arduino Mega 2560运行我的旧代码发送短信和电话,但现在有一个问题,因为我无法发送短信或电话。我的代码附在下面。

#include <SoftwareSerial.h>

SoftwareSerial gsm(2, 3);

void setup() {
  gsm.begin(9600); // Setting the baud rate of GSM Module
  Serial.begin(9600); // Setting the baud rate of Serial Monitor (Arduino)
  delay(100);
}

void loop() {
  if (Serial.available() > 0)
    switch (Serial.read()) {
      case 's':
        Send();
        break;
      case 'r':
        Receive();
        break;
      case 'S':
        Send();
        break;
      case 'R':
        Receive();
        break;
    }
  if (gsm.available() > 0)
    Serial.write(gsm.read());
}

void Send() {
  Serial.print("Sending");
  gsm.println("AT+CMGF=1");
  delay(1000);
  gsm.println("AT+CMGS=\"+xxxxxxxxxxxx\"\r"); // Replace x with mobile number
  delay(1000);
  gsm.println("Hello I am GSM modem!!!");// The SMS text you want to send
  delay(100);
  gsm.println((char)26); // ASCII code of CTRL+Z
  delay(1000);
}

void Receive() {
  Serial.print("Receiving");
  gsm.println("AT+CNMI=2,2,0,0,0"); // AT Command to receive a live SMS
  delay(1000);
}

0 个答案:

没有答案