Created
November 23, 2019 21:16
-
-
Save doorbash/4ed71b81aac754902519c3ee6f471185 to your computer and use it in GitHub Desktop.
arduino uno + 433 mhz transmitter + air200
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define MESSAGE_TOTAL_LENGTH 301 | |
#define BIT_PERIOD 350 | |
#define DATA_PIN 4 | |
#include <SoftwareSerial.h> | |
const char *STR_REC_UNREAD = "\"REC UNREAD\""; | |
const char *STR_PHONE_NUMBER = "\"+98XXXXXXXXXX\""; | |
int data[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0, 0}; | |
int size = sizeof(data) / sizeof(int); | |
SoftwareSerial mSerial(2, 3); // RX, TX | |
char message[MESSAGE_TOTAL_LENGTH]; | |
int messageLen = 0; | |
bool readyToDeleteAllMessages = false; | |
void setup() { | |
Serial.begin(9600); | |
mSerial.begin(2400); | |
pinMode(DATA_PIN, OUTPUT); | |
} | |
void loop() { | |
if (mSerial.available() > 0) { | |
char r = (char)mSerial.read(); | |
if ((r == '\r' || r == '\n')) { | |
if (messageLen > 0) { | |
message[messageLen] = 0; | |
Serial.println("new message"); | |
Serial.println(message); | |
parseMessage(); | |
messageLen = 0; | |
} | |
} else if (messageLen < (MESSAGE_TOTAL_LENGTH - 1)) { | |
message[messageLen] = r; | |
messageLen++; | |
} else messageLen = 0; | |
} | |
} | |
void parseMessage() { | |
if (messageStartsWith("+CMTI:")) { | |
char *buf = &message[12]; | |
Serial.print("OK message number is: "); | |
Serial.println(buf); | |
int msgNumber = atoi(buf); | |
Serial.println(msgNumber); | |
delay(1000); | |
mSerial.print("AT+CMGR="); | |
mSerial.print(msgNumber); | |
mSerial.print("\r\n"); | |
mSerial.flush(); | |
} else if (messageStartsWith("+CMGR:")) { | |
if (cmgrIsUnread() && cmgrCheckPhoneNumber()) { | |
Serial.println("Yeah"); | |
Serial.println("Opening door..."); | |
for (int cnt = 0; cnt < 6; cnt++) { | |
for (int i = 0; i < size; i++) { | |
digitalWrite(DATA_PIN, data[i]); | |
delayMicroseconds(BIT_PERIOD); | |
} | |
digitalWrite(DATA_PIN, LOW); | |
delay(12); | |
} | |
} else { | |
Serial.println("Nope"); | |
} | |
readyToDeleteAllMessages = true; | |
} else if (messageStartsWith("OK") && readyToDeleteAllMessages) { | |
mSerial.println("AT+CMGD=1 ,4\r"); | |
readyToDeleteAllMessages = false; | |
} | |
} | |
bool messageStartsWith(const char *str) { | |
if (messageLen < strlen(str)) return false; | |
for (int i = 0; i < strlen(str); i++) { | |
if (str[i] != message[i]) return false; | |
} | |
return true; | |
} | |
bool cmgrIsUnread() { | |
for (int i = 0; i < strlen(STR_REC_UNREAD); i++) { | |
if (STR_REC_UNREAD[i] != message[i + 7]) return false; | |
} | |
Serial.println("message is unread"); | |
return true; | |
} | |
bool cmgrCheckPhoneNumber() { | |
int index = strchr(message, ',') - message; | |
Serial.print("index is "); | |
Serial.println(index); | |
for (int i = 0; i < strlen(STR_PHONE_NUMBER); i++) { | |
if (STR_PHONE_NUMBER[i] != message[i + index + 1]) return false; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
AT+CMGF=1 //0->pdu 1->text MODE
AT+IPR=2400 // baud rate