کد:
#include <SoftwareSerial.h>
SoftwareSerial GPRS(10, 11);
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 16, 2);
enum _parseState {
PS_DETECT_MSG_TYPE,
PS_IGNORING_COMMAND_ECHO,
PS_READ_CMTI_STORAGE_TYPE,
PS_READ_CMTI_ID,
PS_READ_CMGR_STATUS,
PS_READ_CMGR_NUMBER,
PS_READ_CMGR_SOMETHING,
PS_READ_CMGR_DATE,
PS_READ_CMGR_CONTENT,
PHONE
};
String ADMIN_PHONE="";
String SMS_PHONE_NUMBER="";
String TEMP_PHONE="";
String x="";
String y="";
int AV;
int BV;
int CV;
int DV;
byte state = PS_DETECT_MSG_TYPE;
char buffer[80];
byte pos = 0;
int lastReceivedSMSId = 0;
boolean validSender = false;
void resetBuffer() {
memset(buffer, 0, sizeof(buffer));
pos = 0;
}
void setup()
{
GPRS.begin(9600);
Serial.begin(9600);
//----lcd----//
lcd.init();
lcd.backlight();
//----lcd----//
//--------relay-------//
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
//--------relay-------//
Loading();
GSM_CMGF_CMGD();
lcd_Relay_v();
}
void loop()
{
while(GPRS.available()) {
parseATText(GPRS.read());
}
}
void parseATText(byte b) {
buffer[pos++] = b;
if ( pos >= sizeof(buffer) )
resetBuffer();
switch (state) {
case PS_DETECT_MSG_TYPE:
{
if ( b == '\n' )
resetBuffer();
else {
if ( pos == 3 && strcmp(buffer, "AT+") == 0 ) {
state = PS_IGNORING_COMMAND_ECHO;
}
else if ( pos == 6 ) {
if ( strcmp(buffer, "+CMTI:") == 0 ) {
Serial.println("Received CMTI");
state = PS_READ_CMTI_STORAGE_TYPE;
}
else if ( strcmp(buffer, "+CMGR:") == 0 ) {
Serial.println("Received CMGR");
state = PS_READ_CMGR_STATUS;
}
resetBuffer();
}
}
}
break;
case PS_IGNORING_COMMAND_ECHO:
{
if ( b == '\n' ) {
state = PS_DETECT_MSG_TYPE;
resetBuffer();
}
}
break;
case PS_READ_CMTI_STORAGE_TYPE:
{
if ( b == ',' ) {
Serial.print("SMS storage is ");
Serial.println(buffer);
state = PS_READ_CMTI_ID;
resetBuffer();
}
}
break;
case PS_READ_CMTI_ID:
{
if ( b == '\n' ) {
lastReceivedSMSId = atoi(buffer);
Serial.print("SMS id is ");
Serial.println(lastReceivedSMSId);
GPRS.print("AT+CMGR=");
GPRS.println(lastReceivedSMSId);
state = PS_DETECT_MSG_TYPE;
resetBuffer();
}
}
break;
case PS_READ_CMGR_STATUS:
{
if ( b == ',' ) {
Serial.print("CMGR status: ");
Serial.println(buffer);
state = PS_READ_CMGR_NUMBER;
resetBuffer();
}
}
break;
case PS_READ_CMGR_NUMBER:
{
if ( b == ',' ) {
Serial.print("CMGR number: ");
Serial.println(buffer);
TEMP_PHONE=buffer;
SMS_PHONE_NUMBER=buffer;
validSender = true;
state = PS_READ_CMGR_SOMETHING;
resetBuffer();
}
}
break;
case PS_READ_CMGR_SOMETHING:
{
if ( b == ',' ) {
Serial.print("CMGR something: ");
Serial.println(buffer);
state = PS_READ_CMGR_DATE;
resetBuffer();
}
}
break;
case PS_READ_CMGR_DATE:
{
if ( b == '\n' ) {
Serial.print("CMGR date: ");
Serial.println(buffer);
state = PS_READ_CMGR_CONTENT;
resetBuffer();
}
}
break;
case PS_READ_CMGR_CONTENT:
{
if ( b == '\n' ) {
Serial.print("CMGR content: ");
Serial.print(buffer);
parseSMSContent();
GPRS.print("AT+CMGD=");
GPRS.println(lastReceivedSMSId);
state = PS_DETECT_MSG_TYPE;
resetBuffer();
}
}
break;
}
}
void parseSMSContent() {
char* ptr = buffer;
lcd_NEW_Message();
while ( strlen(ptr) >=2 ) {
if (ptr[0] == 'T') {
if ( ptr[1] == '1' )
ADMIN_PHONE = TEMP_PHONE;
GPRS.println("AT+CMGS="+ ADMIN_PHONE);
delay(1000);
GPRS.println("Admin phone is");
GPRS.println(ADMIN_PHONE);
GPRS.println("Najmi ICT");
delay(100);
GPRS.println((char)26);// ASCII code of CTRL+Z
delay(1000);
}
if (ptr[0] == 'A') {
if (ptr[1] == '1')
{
if (ADMIN_PHONE == SMS_PHONE_NUMBER)
{
x= " A ";
y= " ON";
SEND_SMS();
digitalWrite(3, LOW);
AV=1;
}
}
else if ( ADMIN_PHONE == SMS_PHONE_NUMBER )
{
if (ptr[1] == '0')
{
x= " A ";
y= " OFF";
SEND_SMS();
digitalWrite(3, HIGH);
AV=0;
}
}
}
if (ptr[0] == 'B') {
if (ptr[1] == '1')
{
if (ADMIN_PHONE == SMS_PHONE_NUMBER)
{
x= " B ";
y= " ON";
SEND_SMS();
digitalWrite(4, LOW);
BV=1;
}
}
else if ( ADMIN_PHONE == SMS_PHONE_NUMBER )
{
if (ptr[1] == '0')
{
x= " B ";
y= " OFF";
SEND_SMS();
digitalWrite(4, HIGH);
AV=0;
}
}
}
if (ptr[0] == 'C')
{
if (ptr[1] == '1')
{
if (ADMIN_PHONE == SMS_PHONE_NUMBER)
{
x= " C ";
y= " ON";
SEND_SMS();
digitalWrite(5, LOW);
CV=1;
}
}
else if ( ADMIN_PHONE == SMS_PHONE_NUMBER )
{
if (ptr[1] == '0')
{
x= " C ";
y= " OFF";
SEND_SMS();
digitalWrite(5, HIGH);
AV=0;
}
}
}
if (ptr[0] == 'D')
{
if (ptr[1] == '1')
{
if (ADMIN_PHONE == SMS_PHONE_NUMBER)
{
x= " D ";
y= " ON";
SEND_SMS();
digitalWrite(6, LOW);
DV=1;
}
}
else if ( ADMIN_PHONE == SMS_PHONE_NUMBER )
{
if (ptr[1] == '0')
{
x= " D ";
y= " OFF";
SEND_SMS();
digitalWrite(6, HIGH);
AV=0;
}
}
}
lcd_Relay_v();
ptr += 2;
}
}
void SEND_SMS(){
GPRS.println("AT+CMGS="+ADMIN_PHONE);
delay(1000);
GPRS.println("Relay"+x+""+y+"");
GPRS.println("Najmi ICT");
delay(100);
GPRS.println((char)26);// ASCII code of CTRL+Z
delay(1000);
}
void lcd_NEW_Message(){
lcd.clear();
lcd.setCursor( 0 , 0 );
lcd.print(" New Message ");
lcd.setCursor( 0 , 1 );
lcd.print(" * Najmi ICT *");
}
void lcd_Relay_v(){
lcd.clear();
lcd.setCursor( 0 , 0 );
lcd.print("A=");
lcd.setCursor( 2 , 0 );
lcd.print(AV);
lcd.setCursor( 5 , 0 );
lcd.print("B=");
lcd.setCursor( 7 , 0 );
lcd.print(BV);
lcd.setCursor( 0 , 1 );
lcd.print("C=");
lcd.setCursor( 2 , 1 );
lcd.print(CV);
lcd.setCursor( 5 , 1 );
lcd.print("D=");
lcd.setCursor( 7 , 1 );
lcd.print(DV);
}
void Loading(){
lcd.setCursor( 0 , 0 );
lcd.print("> Loading: <");
for (int j = 0; j <= 15; j++)
{
lcd.setCursor( j , 1 );
lcd.print((char)255);
delay(1500);
}
lcd.clear();
lcd.setCursor( 0 , 0 );
lcd.print(".:::: Ready ::::.");
lcd.setCursor( 0 , 1 );
lcd.print(" Made by Najmi");
}
void GSM_CMGF_CMGD(){
GPRS.println("AT+CMGf=1");
delay(1000);
for (int i = 1; i <= 15; i++) {
GPRS.print("AT+CMGD=");
GPRS.println(i);
delay(200);
while(GPRS.available())
Serial.write(GPRS.read());
}
}