M2M : GSM/GPRS to WiFi Bridge
 Page :   [ 1 ]    [ 2 ]    [ 3 ]    [ 4 ]    [ 5 ]  

Here's a simple example sketch running on the ESP8266.  It just blinks one of the leds to indicate the phone number of whoever called the unit. It totally blocks code execution while blinking but it's just for demonstration!

In a real world scenario it might take that number and then perform an action such as making a request (via WiFi) to a server to compare that number to a list of "allowed" numbers and perhaps open a door for the person that called.

 

#include <SoftwareSerial.h>

#define RESET 16
#define LED1 13
#define LED2 12

SoftwareSerial GSMSerial(4, 5, false, 512);

const char end_c[2] = {0x1a, '\0'};

unsigned long previousMillis = 0;
unsigned long previousRegCheckMillis = 0;

volatile int failedRegCount = 0;
volatile int isRegistered = 0;



void GSMreset(){

  isRegistered = 0;
  failedRegCount = 0;

  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);

  digitalWrite(RESET, HIGH);
  delay(2000);
  digitalWrite(RESET, LOW);
  delay(1000);

  GSMcommand("AT", "OK", 1000, 1);
  GSMcommand("AT", "OK", 1000, 1);
  GSMcommand("AT", "OK", 1000, 1); 
  
  GSMcommand("ATE0", "OK", 5000, 3);
  GSMcommand("AT+CLIP=1", "OK", 5000, 3);  
  GSMcommand("AT+CREG?", "+CREG: 1,1\r", 1500, 30);
  
}



String GSMread() {

  String reply = "";
  
  if (GSMSerial.available())  {

    reply = GSMSerial.readStringUntil('\n');
    //Serial.println(reply);

  }

  return reply;

}



int GSMwait(String response,int timeout) {

  unsigned long start = millis();
  String reply = "";

  do {

      reply = GSMread();

      if ((millis() - start) > timeout){
        break;
      }      

      yield();
  
  } while ((reply.indexOf(response) == -1) && (!(reply.indexOf("CME ERROR:") > -1)));
  
  if (reply.indexOf(response) > -1){
      return 1;
  }
  return 0;
}



int GSMcommand(String command, String response, int timeout, int repetitions) {

  GSMSerial.flush();

  while (repetitions-- > 0) {

    GSMSerial.println(command);
    
    if (GSMwait(response, timeout)) {
      return 1;
    }

    yield();
    delay(100);
  
  }
  
  return 0;

}



void blinkNumber(String number){

  // this completely blocks but i just for testing and is informative

  digitalWrite(LED2, HIGH);
  delay(2000);
  digitalWrite(LED2, LOW);
  delay(2000);

  int len  = number.length();

  for (int i = 0; i < len; i++){

    char x = number.charAt(i);

    for (int j = 0; j < (x - '0'); j++){

      digitalWrite(LED2, HIGH);
      delay(250);
      digitalWrite(LED2, LOW);
      delay(250);
      
    }

    delay(1000);
    
  }
  
}



void handleIncoming(String incoming){
   
  if (incoming.indexOf("+CLIP") > -1){

    GSMcommand("ATA", "OK", 1000, 1);
    delay(500);

    GSMcommand("AT+ATH", "OK", 1000, 1);
    GSMcommand("AT+CHUP", "OK", 1000, 1);

    int commaIndex = incoming.indexOf(',');

    if (commaIndex > -1){

      String part1 = incoming.substring(0, commaIndex);

      int quoteIndex = part1.indexOf('"');

      if (quoteIndex > -1){
      
        String part2 = part1.substring(quoteIndex +1, part1.length() - 1);
        blinkNumber(part2);
          
      }
        
    }
      
  }
    
}


void setup() {

  pinMode(RESET, OUTPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);

  digitalWrite(RESET, LOW);
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);

  Serial.begin(57600);
  while (!Serial) {
    ;
  }
  
  GSMSerial.begin(57600);
  while (!GSMSerial) {
    ;
  }
  
  GSMreset();  

}



void loop() {

  yield();
  unsigned long currentMillis = millis();

  if((currentMillis - previousRegCheckMillis > 30000) || !previousRegCheckMillis) {

    previousRegCheckMillis = currentMillis;   

    int reg =   GSMcommand("AT+CREG?", "+CREG: 1,1\r", 1500, 3);

    if (reg){

      digitalWrite(LED1, HIGH);
      failedRegCount = 0;
      isRegistered = 1;

    } else {

      digitalWrite(LED1, LOW);
      failedRegCount++;
      isRegistered = 0;

    }

    if (failedRegCount == 5){
      GSMreset();
    } else {
      GSMcommand("AT", "OK", 1000, 1);
      GSMcommand("AT", "OK", 1000, 1);
      GSMcommand("AT", "OK", 1000, 1);
    }

  }
 
  if (GSMSerial.available())  {

    String incoming = GSMSerial.readStringUntil('\n');
    Serial.println(incoming);
    handleIncoming(incoming);

  }
  
  while (Serial.available() > 0) {
    GSMSerial.write(Serial.read());
  }

}

(Page 5 of 5)