کد:
//Sim800l(8,7)(RX,TX)
// pin OUTPUT to Relay(A0, A1, A2, A3 , 2, 3, 4, 5)
#include <gprs.h>
#include <softwareserial.h>
#define TIMEOUT 5000
#define Ron LOW
#define Roff HIGH
byte Relay[] = {A0, A1, A2, A3, 2, 3, 4, 5};
byte StatRelay[8];
char buffNumber[20];
String replyNumber = "09120000000";
GPRS gprs;
void setup() {
for (int i = 0; i < 8; i++) {
pinMode (Relay[i] , OUTPUT);
digitalWrite (Relay[i], Roff);
StatRelay[i] = Roff;
}
Serial.begin(9600);
while (!Serial);
Serial.println("Starting SIM800 SMS Command Processor>>> Automatically Read SMS");
gprs.preInit();
delay(1000);
while (0 != gprs.init()) {
delay(1000);
Serial.print("init error\r\n");
}
//Set SMS mode to ASCII
if (0 != gprs.sendCmdAndWaitForResp("AT+CMGF=1\r\n", "OK", TIMEOUT)) {
ERROR("ERROR:CNMI");
return;
}
//Start listening to New SMS Message Indications
if (0 != gprs.sendCmdAndWaitForResp("AT+CNMI=1,2,0,0,0\r\n", "OK", TIMEOUT)) {
ERROR("ERROR:CNMI");
return;
}
int pjg = replyNumber.length() + 1;
buffNumber[pjg];
replyNumber.toCharArray(buffNumber, pjg);
//Serial.print("Send reply to number = > ");
//Serial.println(buffNumber);
//Serial.println("Initialization Done");
//Serial.println("=============================================================");
}
//Variable to hold last line of serial output from SIM800
char currentLine[500] = "";
int currentLineIndex = 0;
//Boolean to be set to true if message notificaion was found and next
//line of serial output is the actual SMS message content
bool nextLineIsMessage = false;
void loop() {
// Change status Relay ON / OFF
for (int i = 0; i < 8; i++) {
digitalWrite(Relay[i], StatRelay[i]);
}
//If there is serial output from SIM800
if (gprs.serialSIM800.available()) {
char lastCharRead = gprs.serialSIM800.read();
//Read each character from serial output until \r or \n is reached (which denotes end of line)
if (lastCharRead == '\r' || lastCharRead == '\n') {
String lastLine = String(currentLine);
lastLine.toUpperCase(); // Uppercase the Received Message FOR Non-case-sensitivity
//If last line read +CMT, New SMS Message Indications was received.
//Hence, next line is the message content.
if (lastLine.startsWith("+CMT:")) {
Serial.println(lastLine);
nextLineIsMessage = true;
} else if (lastLine.length() > 0) {
if (nextLineIsMessage) {
Serial.println(lastLine);
//Relay 1 Controller
//Read message content and set status according to SMS content Then reply SMS
if (lastLine.indexOf("R1 OFF") >= 0) {
StatRelay[0] = Roff;
//Serial.print("Reply To ");
//Serial.print(buffNumber);
//Serial.println("==>> Relay 1 Status OFF");
gprs.sendSMS (buffNumber, "Relay 1 Status OFF");
} else if (lastLine.indexOf("R1 ON") >= 0) {
StatRelay[0] = Ron;
//Serial.print("Reply To ");
//Serial.print(buffNumber);
//Serial.println("==>> Relay 1 Status ON");
gprs.sendSMS (buffNumber, "Relay 1 Status ON");
}
//Relay 2 Controller
//Read message content and set status according to SMS content Then reply SMS
if (lastLine.indexOf("R2 OFF") >= 0) {
StatRelay[1] = Roff;
//Serial.print("Reply To ");
//Serial.print(buffNumber);
//Serial.println("==>> Relay 2 Status OFF");
gprs.sendSMS (buffNumber, "Relay 2 Status OFF");
} else if (lastLine.indexOf("R2 ON") >= 0) {
StatRelay[1] = Ron;
//Serial.print("Reply To ");
//Serial.print(buffNumber);
//Serial.println("==>> Relay 2 Status ON");
gprs.sendSMS (buffNumber, "Relay 2 Status ON");
}
//Relay 3 Controller
//Read message content and set status according to SMS content Then reply SMS
if (lastLine.indexOf("R3 OFF") >= 0) {
StatRelay[2] = Roff;
//Serial.print("Reply To ");
//Serial.print(buffNumber);
//Serial.println("==>> Relay 3 Status OFF");
gprs.sendSMS (buffNumber, "Relay 3 Status OFF");
} else if (lastLine.indexOf("R3 ON") >= 0) {
StatRelay[2] = Ron;
//Serial.print("Reply To ");
//Serial.print(buffNumber);
//Serial.println("==>> Relay 3 Status ON");
gprs.sendSMS (buffNumber, "Relay 3 Status ON");
}
//Relay 4 Controller
//Read message content and set status according to SMS content Then reply SMS
if (lastLine.indexOf("R4 OFF") >= 0) {
StatRelay[3] = Roff;
//Serial.print("Reply To ");
//Serial.print(buffNumber);
//Serial.println("==>> Relay 4 Status OFF");
gprs.sendSMS (buffNumber, "Relay 4 Status OFF");
} else if (lastLine.indexOf("R4 ON") >= 0) {
StatRelay[3] = Ron;
//Serial.print("Reply To ");
//Serial.print(buffNumber);
//Serial.println("==>> Relay 4 Status ON");
gprs.sendSMS (buffNumber, "Relay 4 Status ON");
}
//Relay 5 Controller
//Read message content and set status according to SMS content Then reply SMS
if (lastLine.indexOf("R5 OFF") >= 0) {
StatRelay[4] = Roff;
//Serial.print("Reply To ");
//Serial.print(buffNumber);
//Serial.println("==>> Relay 5 Status OFF");
gprs.sendSMS (buffNumber, "Relay 5 Status OFF");
} else if (lastLine.indexOf("R5 ON") >= 0) {
StatRelay[4] = Ron;
//Serial.print("Reply To ");
//Serial.print(buffNumber);
//Serial.println("==>> Relay 5 Status ON");
gprs.sendSMS (buffNumber, "Relay 5 Status ON");
}
//Relay 6 Controller
//Read message content and set status according to SMS content Then reply SMS
if (lastLine.indexOf("R6 OFF") >= 0) {
StatRelay[5] = Roff;
//Serial.print("Reply To ");
//Serial.print(buffNumber);
//Serial.println("==>> Relay 6 Status OFF");
gprs.sendSMS (buffNumber, "Relay 6 Status OFF");
} else if (lastLine.indexOf("R6 ON") >= 0) {
StatRelay[5] = Ron;
//Serial.print("Reply To ");
//Serial.print(buffNumber);
//Serial.println("==>> Relay 6 Status ON");
gprs.sendSMS (buffNumber, "Relay 6 Status ON");
}
//Relay 7 Controller
//Read message content and set status according to SMS content Then reply SMS
if (lastLine.indexOf("R7 OFF") >= 0) {
StatRelay[6] = Roff;
//Serial.print("Reply To ");
//Serial.print(buffNumber);
//Serial.println("==>> Relay 7 Status OFF");
gprs.sendSMS (buffNumber, "Relay 7 Status OFF");
} else if (lastLine.indexOf("R7 ON") >= 0) {
StatRelay[6] = Ron;
//Serial.print("Reply To ");
//Serial.print(buffNumber);
//Serial.println("==>> Relay 7 Status ON");
gprs.sendSMS (buffNumber, "Relay 7 Status ON");
}
//Relay 8 Controller
//Read message content and set status according to SMS content Then reply SMS
if (lastLine.indexOf("R8 OFF") >= 0) {
StatRelay[7] = Roff;
//Serial.print("Reply To ");
//Serial.print(buffNumber);
//Serial.println("==>> Relay 8 Status OFF");
gprs.sendSMS (buffNumber, "Relay 8 Status OFF");
} else if (lastLine.indexOf("R8 ON") >= 0) {
StatRelay[7] = Ron;
//Serial.print("Reply To ");
//Serial.print(buffNumber);
//Serial.println("==>> Relay 8 Status ON");
gprs.sendSMS (buffNumber, "Relay 8 Status ON");
}
nextLineIsMessage = false;
}
}
//Clear char array for next line of read
for ( int i = 0; i < sizeof(currentLine); ++i ) {
currentLine[i] = (char)0;
}
currentLineIndex = 0;
} else {
currentLine[currentLineIndex++] = lastCharRead;
}
}
}