Hallo,
ich habe hier einen Zustands automat der einen Befehl senden soll und dann 5 sekunden auf eine Antwort warten bevor er den nächsten sendet, dass klappt hin und wieder mal, ich führe exakt das gleiche script mehrmals aus und bekomme unterschiedliche cases nur beantwortet. Weiß jemand woran das liegt?
Code:
#include <SoftwareSerial.h>
#define SIM800_TX 8
#define SIM800_RX 9
SoftwareSerial mySerial(SIM800_TX, SIM800_RX);
int start = 0;
int zustand = 0;
unsigned long Millis;
unsigned long Intervall;
enum ZKom : byte { SENDEN, EMPFANGEN };
ZKom zustandKom = SENDEN;
#define ZEILENTRENNZEICHEN 13
char* receiveBuffer() {
static char lineBuffer[40];
static byte counter = 0;
char c;
if (mySerial.available() == 0) return NULL;
if (counter == 0) memset(lineBuffer, 0, sizeof(lineBuffer));
c = mySerial.read();
if (c == ZEILENTRENNZEICHEN)
{
counter = 0;
return lineBuffer;
}
else if (c >= 32)
{
lineBuffer[counter] = c;
if (counter < sizeof(lineBuffer) - 2) counter++;
}
return NULL;
}
void setup() {
Serial.begin(9600);
while (!Serial);
mySerial.begin(9600);
while (!mySerial);
Serial.println("Bereit");
}
void loop() {
if (millis() - Millis >= Intervall) {
zustandKom = SENDEN;
}
if (zustandKom == SENDEN) {
if (start == 0) {
zustand = Serial.read();
if (zustand > 48 ) {
start = 1;
Serial.println(zustand);
}
}
switch (zustand) {
case 49:
Serial.println("Case 1");
while (!mySerial);
mySerial.println("AT");
zustand = 50;
zustandKom = EMPFANGEN;
Millis = millis();
Intervall = 5000;
break;
case 50:
Serial.println("Case 2");
while (!mySerial);
mySerial.println("AT+SAPBR=3,1,Contype,GPRS");
zustand = 51;
zustandKom = EMPFANGEN;
Millis = millis();
Intervall = 5000;
break;
case 51:
Serial.println("Case 3");
while (!mySerial);
mySerial.println("AT+SAPBR=3,1,APN,web.vodafone.de");
zustand = 52;
zustandKom = EMPFANGEN;
Millis = millis();
Intervall = 5000;
break;
case 52:
Serial.println("Case 4");
while (!mySerial);
mySerial.println("AT+SAPBR=3,1,USER,vodafone");
zustand = 53;
zustandKom = EMPFANGEN;
Millis = millis();
Intervall = 5000;
break;
case 53:
Serial.println("Case 5");
while (!mySerial);
mySerial.println("AT+SAPBR=3,1,PWD,vodafone");
zustand = 54;
zustandKom = EMPFANGEN;
Millis = millis();
Intervall = 5000;
break;
case 54:
Serial.println("Case 6");
while (!mySerial);
mySerial.println("AT+SAPBR=1,1");
zustand = 55;
zustandKom = EMPFANGEN;
Millis = millis();
Intervall = 5000;
break;
case 55:
Serial.println("Case 7");
while (!mySerial);
mySerial.println("AT+SAPBR=2,1");
zustand = 56;
zustandKom = EMPFANGEN;
Millis = millis();
Intervall = 5000;
break;
case 56:
Serial.println("Verbindung fertig");
start = 0;
break;
}
}
if (zustandKom == EMPFANGEN) {
char* text = receiveBuffer();
if (text != NULL) {
if (strcmp(text, "OK") == 0 ) {
Serial.println(text);
//zustandKom = SENDEN;
} else if (strcmp(text, "ERROR") == 0 ) {
Serial.println(text);
//zustandKom = SENDEN;
} else {
Millis = millis();
Intervall = 3000;
Serial.println(text);
}
}
}
}
Serieller Monitor 1:
Bereit
49
Case 1
Case 2
AT+SAPBR=3,1,Contype,GPRS
OK
Call Ready
SMS Ready
Case 3
Case 4
AT+SAPBR=3,1,USER,vodafone
OK
SMS Ready
Case 5
Case 6
AT+SAPBR=1,1
ERROR
SMS Ready
Case 7
Verbindung fertig
Serieller Monitor2:
Bereit
49
20:44:51.584 -> Case 1
20:44:51.584 -> AT
20:44:51.584 ->
20:44:51.584 -> OK
20:44:52.285 -> +CPIN: READY
20:44:53.889 -> Call Ready
20:44:54.589 -> SMS Ready
Case 2
Case 3
AT+SAPBR=3,1,APN,web.vodafone.de
20:45:02.737 ->
20:45:02.737 -> OK
SMS Ready
Case 4
Case 5
AT+SAPBR=3,1,PWD,vodafone
20:45:11.215 ->
20:45:11.215 -> OK
20:45:11.548 -> SMS Ready
Case 6
Case 7
AT+SAPBR=2,1
20:45:19.636 ->
20:45:19.636 -> +SAPBR: 1,3,"0.0.0.0"
20:45:19.724 ->
20:45:19.724 -> OK
20:45:20.027 -> SMS Ready
Verbindung fertig