1

我们在使用 SIM900 接收短信时遇到问题。根据我们的程序,我们发送的短信必须采用某种格式才能被接受。例如,发送#m1到 Arduino 必须打开绿色 LED,并且程序必须忽略任何其他文本。但是现在我们的程序接受所有文本,不管文本的内容是什么,并打开或关闭一个无关紧要的红色 LED。

你们可以看看代码并找出问题所在。

#include <SoftwareSerial.h>
SoftwareSerial SIM900(2, 3);

char inchar;
int motor = 4;
int heater = 12;
int sensor = 11;
int counter1;
int heat_degreePin = A0;
int counter;
int buttonState = 0;
String message = "";

void setup()
{
    Serial.begin(19200);
    pinMode(motor, OUTPUT);
    pinMode(sensor, INPUT);
    pinMode(heater, OUTPUT);
    analogReference(INTERNAL);
    digitalWrite(heater, LOW);
    digitalWrite(motor, LOW);
    int counter1 = 0;
    int counter = 0;
    SIM900power();
    SIM900.begin(19200);
    delay(20000);
    SIM900.print("AT+CMGF=1\r");
    delay(100);
    SIM900.print("AT+CNMI=2,2,0,0,0\r");
    delay(100);
}

void loop()
{
    int voltage_value = analogRead(heat_degreePin);
    float heat_degree = heat_degree = voltage_value / 9.31;
    voltage_value = analogRead(heat_degreePin);
    heat_degree = voltage_value / 9.31;
    buttonState = digitalRead(sensor);

    if (heat_degree < 36 && buttonState == LOW && counter != 1) {
        digitalWrite(heater, HIGH);
        delay(50);
        digitalWrite(motor, LOW);
        delay(50);
        message = "D1: heat low: heater on ,moist ok: motor off";
        delay(50);
        SendTextMessage();
        counter = 1;
    }

    else if (heat_degree >= 36 && buttonState == LOW && counter != 2) {
        digitalWrite(heater, LOW);
        delay(50);
        digitalWrite(motor, LOW);
        delay(50);
        message = "D2: heat ok: heater off, moist ok: motor off";
        delay(50);
        SendTextMessage();
        counter = 2;
    }

    else if (heat_degree < 36 && buttonState == HIGH && counter != 3) {
        digitalWrite(heater, HIGH);
        delay(50);
        digitalWrite(motor, HIGH);
        delay(50);
        message = "D3: heat low: heater on, moist low: motor on";
        delay(50);
        SendTextMessage();
        counter = 3;
    }

    else if (heat_degree >= 36 && buttonState == HIGH && counter != 4) {
        digitalWrite(heater, LOW);
        delay(50);
        digitalWrite(motor, HIGH);
        delay(50);
        message = "D4: heat ok: heater off, moist low: motor on";
        delay(50);
        SendTextMessage();
        counter = 4;
    }

    else {
    }

    if (SIM900.available() > 0) {
        inchar = SIM900.read();

        if (inchar == '#') {
            delay(10);
            inchar = SIM900.read();

            if (inchar == 'm') {
                delay(10);
                inchar = SIM900.read();

                if (inchar == '0') {
                    digitalWrite(motor, LOW);
                    delay(10);
                    SIM900.print("AT+CMGF=1\r");
                    delay(100);
                    SIM900.println("AT + CMGS = \"+90505xxxxxxx\"\r");
                    delay(100);
                    SIM900.println("motor off");
                    delay(100);
                    SIM900.println((char)26);
                    delay(100);
                    SIM900.println();
                    delay(5000);
                    SIM900power();
                }
                else if (inchar == '1') {
                    digitalWrite(motor, HIGH);
                    SIM900.print("AT+CMGF=1\r");
                    delay(100);
                    SIM900.println("AT + CMGS = \"+90505xxxxxxx\"\r");
                    delay(100);
                    SIM900.println("motor on");
                    delay(100);
                    SIM900.println((char)26);
                    delay(100);
                    SIM900.println();
                    delay(5000);
                    SIM900power();
                }

                SIM900.println("AT+CMGD=1,4");
            }
        }
    }
}

void SIM900power()
{
    digitalWrite(8, HIGH);
    delay(1000);
    digitalWrite(8, LOW);
    delay(1000);
}

void SendTextMessage()
{
    SIM900.print("AT+CMGF=1\r");
    delay(500);
    SIM900.print("AT+CMGS=\"+90505xxxxxxx\"\r");
    delay(500);
    SIM900.println(message);
    SIM900.print("\r");
    delay(500);
    SIM900.println((char)26);
    delay(100);
    SIM900.println();
    delay(5000);
    SIM900power();
    delay(3000);
}
4

0 回答 0