我想用DHT11值回复短信

时间:2019-03-28 18:45:29

标签: c# arduino

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

这是我的代码,可以说我发送一条短信,说从我的号码1234发送,代码检测到该号码,并在使用此行将信息发送到我的号码时

mySerial.println(“ AT + CMGS =” +数字);

我在串行监视器中得到它

正在连接...。 准备接收命令。 数: 好

消息:确定 接收 号码:+1234 讯息:SEND 接收 发送 发送 编号:AT + CMGF = 1

好 AT + CMGS = + 1234

错误 湿度:64.00 讯息:确定 AT + CMGS = + 1234

错误 湿度:64.00 接收

没有消息,但是如果我将此代码更改为mySerial.println(“ AT + CMGS = \” + 1234 \“ \ r”);

它可以工作,但是我不希望它是一个固定的数字,我想回复收到的任何数字

谢谢

1 个答案:

答案 0 :(得分:0)

固定/工作代码为:AT + CMGS =“ + 1234” \ r(数字前后带有引号,回车符为\ r),其中草图中的代码为AT + CMGS = + 1234 (不带引号,也不回车)。 试试:

mySerial.println("AT+CMGS=\"" + number + "\"\r");