SWITCH LED BY GSM

SWITCH GSM

#include <EEPROM.h>

int addr = 0;

byte value;

#include <SoftwareSerial.h>

char inchar; // Will hold the incoming character from the GSM shield

// Tx of A6 linked to pin 7

const int pinRxNano = 7;

// Rx of A6 linked to pin 8

const int pinTxNano = 8;

const String phoneNumber= “+97433187707”;

//char phone_no[]=”+97433187707″;

const String messageInit = “A6 ready”;

SoftwareSerial A6GSM(pinRxNano, pinTxNano);

int numring=0;

int comring=3;

int onoff=0; // 0 = off, 1 = on

void Response(){

int a = 0;

Serial.println();

while(1){

if(A6GSM.available()){

String dataFromA6 =A6GSM.readString();

dataFromA6.trim();

Serial.println(dataFromA6);

a++;

if(dataFromA6.indexOf(“OK”)>0){break;}

}

if(a>500){a=0;break;}

delay(200);

}

}

void startAT(){

int nbAT = 0;

while(1){

if (nbAT==0){A6GSM.println(“AT”);Serial.print(“.”);}

if(A6GSM.available()){

String dataFromA6 =A6GSM.readString();

dataFromA6.trim();

Serial.println(dataFromA6);

if(dataFromA6.indexOf(“OK”)>0){break;}

}

delay(200);

nbAT++;

if(nbAT++>10){nbAT=0;}

}

}

void envoieSMS(String numeroEnvoi, String messageEnvoi) {

delay(200);

Serial.println(“Selection format message SMS”);

A6GSM.println(“AT+CMGF=1”);

Response();

delay(200);

Serial.println(“Envoi message SMS”);

A6GSM.print(“AT+CMGS=\””);

A6GSM.print(numeroEnvoi);

A6GSM.println(“\””);

delay(200);

A6GSM.print(messageEnvoi);

delay(200);

A6GSM.println (char(26));

Response();

}

void setup()

{

pinMode(2, OUTPUT);

//digitalWrite(2, LOW);

A6GSM.begin(9600);

Serial.begin(9600);

delay(200);

Serial.println(“Init”);

//AT

startAT();

//envoieSMS(phoneNumber,messageInit);

A6GSM.println(“AT”);

delay(1000);

A6GSM.print(“ATD”);

A6GSM.println(phoneNumber);

delay(10000);

A6GSM.println(“ATH”);

}

void doSomething()

{

if (onoff==0)

{

onoff=1;

EEPROM.write(addr, 1);

}

else if (onoff==1)

{

onoff=0;

EEPROM.write(addr, 0);

}

}

void loop()

{

if(A6GSM.available() >0)

{

inchar=A6GSM.read();

if (inchar==’R’)

{

delay(10);

inchar=A6GSM.read();

if (inchar==’I’)

{

delay(10);

inchar=A6GSM.read();

if (inchar==’N’)

{

delay(10);

inchar=A6GSM.read();

if (inchar==’G’)

{

delay(10);

numring++;

Serial.println(“ring!”);

if (numring==comring)

{

numring=0; // reset ring counter

doSomething();

delay(5000);

}

}

}

}

}

}

else {

value = EEPROM.read(addr);

if (value==1){

digitalWrite(2, HIGH);

}

value = EEPROM.read(addr);

if (value==0){

digitalWrite(2, LOW);

}

}

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s