我的sim800l仅在串行监视器打开的情况下连接到笔记本电脑时才能工作

时间:2019-04-13 18:43:15

标签: c++ c arduino

我有此代码,它在收到单词SEND时通过SMS发送DHT11值,但所有此设置仅在将arduino nano连接到笔记本电脑且串行监视器打开时才起作用,否则,如果我连接到移动电源或墙壁充电器它不给我值。即使当它连接到笔记本电脑时,如果我不打开串行监视器也不会发送。

这是代码

#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.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();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  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);
}

2 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。 我摆脱它的唯一方法是定义所有Seri​​alMonitor指令,例如:

Serial.begin(x);
Serial.println(x);

仅在运行与笔记本电脑连接的程序时才有条件地进行编译。因此,您可以创建一个#define,如:

#define DEBUG 1
#ifdef DEBUG
#define startupMonitor(x)     Serial.begin(x)
#define monitorPrint(x)       Serial.print(x)
#else
#define startupMonitor(x)     
#define monitorPrint(x)
#endif

当我将程序部署到Arduino进行断开测试时,我将#define注释掉。这将删除可执行代码中对Arduino串行监视器的串行端口的所有引用,至少对我来说,断开连接后SIM800L可以工作。

我不知道为什么会这样,但是只有两个使用串行端口通信的设备,即SIM和带有笔记本电脑的Arduino本身,所以我卸下了Arduino,毕竟断开连接后,您不需要串行监视;)

结论:使用条件编译将生产代码与开发代码分开。

答案 1 :(得分:0)

GSM 模块连接网络后,按下板上的重置按钮即可重置 Arduino。它对我有用。