I am unable to get the following code working. Or atleast don't know how to handle the Char 's' & 'r'.
Any kind of help is welcome.
serialSIM800.listen();
//while (!serialSIM800.available()) {}
while (serialSIM800.available() > 0)
{
Serial.println("Listening to serialSIM800");
if (Serial.available() > 0)
switch (Serial.read())
{
case 's':
SendMessage();
break;
case 'r':
RecieveMessage();
break;
case '@':
TestNode();
break;
}
if (serialSIM800.available()) {
Serial.write(serialSIM800.read());
}
}
void SendMessage()
{
//Send SMS
//Following SMS will be Used as SMSText Value
//Alert: Your Check Out is delayed more than 30min. Please Check Out Immediately. Warden
//Alert: Your Check In is delayed more than 30min. Please Check In Immediately. Warden
Serial.println("Commencig SMS Send");
serialSIM800.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1000); // Delay of 1000 milli seconds or 1 second
Serial.println(MobileNumber);
serialSIM800.println("AT+CMGS=\"" + MobileNumber + "\"\r\n"); //Send new SMS command and message number
delay(1000);
Serial.println(SMSText);
serialSIM800.println(SMSText); //Send SMS content
delay(100);
serialSIM800.println((char)26); //Send Ctrl+Z / ESC to denote SMS message is complete
delay(1000);
Serial.println("SMS Sent");
}
void RecieveMessage()
{
//To Recieve SMS from SIM800
Serial.println("Readinng SMS Start");
serialSIM800.println("AT+CNMI=2,2,0,0,0"); // AT Command to receive a live SMS
delay(1000);
Serial.println("Reading SMS Ended");
}
void serialEvent() {
MobileNumber;
SMSText;
strValue;
while (Serial.available()) {
// get the new byte:
char ch = (char)Serial.read();
// add it to the inputString:
strValue += ch;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (ch == '\n') {
if (strValue.startsWith("+91") && IsNumeric(strValue))
{
MobileNumber = strValue; //Mobile Number Detected
//strValue.toCharArray(MobileNumber, strValue.length());
}
else if (IsNumeric(strValue))
{
tBaud = strValue.toInt(); //Baud Speed Detected
}
else if (strValue.length() > 20)
{
SMSText = strValue; //SMS Text Detected
//strValue.toCharArray(SMSText, strValue.length());
}
else
{
//index = 0;
strValue = "";
};
stringComplete = true;
}
}
}
boolean IsNumeric(String str) {
if (str.length()) {
//Serial.println(str);
for (char i = 0; i < str.length(); i++) {
if ( !(isDigit(str.charAt(i)) || str.charAt(i) == '.' )) {
return false;
}
}
return true;
}
else {
return false;
}
}
//Following SMS will be Used as SMSText Value
//Alert: Your Check Out is delayed more than 30min. Please Check Out Immediately. Warden
//Alert: Your Check In is delayed more than 30min. Please Check In Immediately. Warden
void TestNode()
{
//Read SIM800 output (if available) and print it in Arduino IDE Serial Monitor
if (serialSIM800.available()) {
Serial.write(serialSIM800.read());
}
//Read Arduino IDE Serial Monitor inputs (if available) and send them to SIM800
if (Serial.available()) {
serialSIM800.write(Serial.read());
}
}