Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SoftwareSerial.h>
- #define debug Serial
- char input[1024]; // variable to store the incoming characters
- int rdy = 0, rdyStart = 0;
- //SoftwareSerial debug(4, 5); //RX, TX
- SoftwareSerial sms(4, 5); //RX, TX
- const int Relay = 8;
- const int SMSReset = 7;
- void sendCommand(const char* cmd, int prnt = 1){
- sms.println(cmd);
- delay(300);
- char buf[128];
- int x=0;
- while(sms.available()){
- buf[x++] = sms.read();
- }
- if(prnt && x){
- buf[x++] = 0;
- debug.write(buf);
- }
- }
- void echoState(int state){
- sendCommand(strcat("ATE", String(state).c_str()), 0);
- }
- void initSMS(){
- debug.println("SMS Init...");
- echoState(0);
- sendCommand("AT"); //Check AT Status
- //Get device information
- sendCommand("AT+CMEE=2");
- sendCommand("AT+CGMI");
- sendCommand("AT+CGMM");
- sendCommand("AT+CGMR");
- sendCommand("AT+CGSN");
- sendCommand("AT+CPAS");
- sendCommand("AT+CSQ");
- echoState(1);
- sendCommand("AT+CMGF=1"); // set SMS mode to text
- sendCommand("AT+CNMI=2,2,0,0,0"); // just to get a notification when SMS arrives &direct out SMS upon receipt to the GSM serial out
- }
- void setup() {
- debug.begin(9600);
- debug.println("Device Initializing...");
- pinMode(Relay, OUTPUT);
- pinMode(SMSReset, OUTPUT);
- memset(input, 0, sizeof(input)); //clear input buffer
- digitalWrite(Relay, LOW);
- digitalWrite(SMSReset, LOW);
- delay(500);
- // wake up the GSM shield
- sms.begin(9600);
- digitalWrite(SMSReset, HIGH);
- delay(500);
- sms.println(""); //Needed for starting AT communication on serial
- rdyStart = millis() / 1000;
- }
- void executeCommand(){
- if(!strcmp(input, "$a0")){
- debug.println("Relay OFF");
- digitalWrite(Relay, LOW);
- } else if(!strcmp(input, "$a1")) {
- debug.println("Relay ON");
- digitalWrite(Relay, HIGH);
- } else {
- debug.println(strcat("Unknown Command: ", input));
- }
- sms.println("AT+CMGD=1,4"); // delete all SMS
- delay(1000);
- }
- void loop(){
- //If a character comes in from the GSM...
- int i = 0;
- while(sms.available()){
- input[i++] = sms.read();
- delay(50);
- }
- { //Debug listener scope
- char buf[128];
- int x=0;
- while(debug.available()){
- buf[x++] = debug.read();
- if(buf[x-1] == '~') buf[x-1] = 26;
- }
- if(x){
- buf[x++] = 0;
- sms.write(buf);
- }
- }
- if(i){ //Output SMS data
- input[i++] = 0; //null terminate our string
- debug.println("OUTPUT:");
- debug.println(input);
- debug.println("--------------------");
- //executeCommand(); //Execute SMS command
- i = 0;
- }
- if(!rdy && millis()/1000 - rdyStart >= 3){
- rdy = 1;
- initSMS();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement