Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //const int PIN_SENSORE = 5;
- const int PIN_BUZZER = 8;
- const int LED_ROSSO = 14;
- const int LED_BLU = 15;
- const int LED_VERDE = 13;
- const int LED_GIALLO = 16;
- const int PIN_RESET_SIM = 4;
- bool SENSORE = 0;
- int toni = 99;
- int old_toni= 99;
- unsigned long T_beep = 0;
- #define T_on 100
- #define T_off 500
- bool fase1 = 0;
- bool old_fase = 0;
- unsigned long stp = 0;//int??
- int sirena = 0; //variabile cicli sirena on off
- bool ciclo_sirena = 0; // variabile ciclo allarme sirena
- unsigned long t0 = 0;
- unsigned long dt = 0;
- unsigned long timeOfLastLedEvent = 0; // l'ultima volta che il LED è stato aggiornato
- int passaggi = 0;
- int sensore = 0;
- //-------------variabili stato led modulo sim----------
- int conta = 1; //variabile conteggio led modulo sim
- byte primoGiro = 1; //variabile tempo lampeggio
- bool old_sensor_led = 0; // variabile led sim
- bool sensor_led ; // da led a stato logico
- bool fronte;
- unsigned long tm; //tempo_memoria
- unsigned long now ;
- //-------------
- //-------------
- bool stato_chiamata = 1; //variabile stato sim
- byte flag = 0;
- bool total_reset = 1;
- byte N_call_allarme = 0;//variabile numero ripetizione chiamate
- unsigned long T_pertenza = 0; // tempo avvio macchina
- //SIM800L
- // tx pin 2
- // rx pin 3
- int old_telefono = 99; // vecchio steto telefono
- int telefono = 0;
- unsigned long Tempo_chiamata_allarme = 0;
- unsigned long Tempo_chiamata_stato = 0;
- unsigned long Temp = 0;
- // Include the GSM library
- #include <GSM.h>
- //#include <avr/wdt.h> // includiamo la libreria watchdog
- //GSM_SMS sms;
- //GSMScanner scannerNetworks;
- bool allarme = 0;
- bool old_allarme = 1;
- bool presenza = 0;
- // PIN Number
- #define PINNUMBER //""
- int fase = 0;
- int all = 1;
- bool notConnected = true;
- // initialize the library instance
- GSM gsmAccess;
- GSMVoiceCall vcs;
- //gsmAccess.isAccessAlive;
- // Array to hold the number for the incoming call
- char numtel[20];
- void setup() {
- // initialize serial communications and wait for port to open:
- Serial.begin(9600);
- Serial.println (F(" setup"));
- pinMode(A3, INPUT); //led gsm
- pinMode(LED_VERDE, OUTPUT); //verde =sensore
- pinMode(LED_ROSSO, OUTPUT); //rosso = allarme attivo
- pinMode(LED_BLU, OUTPUT); // blu
- pinMode(LED_GIALLO, OUTPUT); //giallo
- pinMode(PIN_BUZZER, OUTPUT);// buzzer
- pinMode(6, OUTPUT);// rele' sirena
- pinMode(9, INPUT);//input sensore
- pinMode(12, INPUT_PULLUP);//input sensore
- pinMode(PIN_RESET_SIM, OUTPUT);// rele' sirena
- pinMode(12, OUTPUT);
- // digitalWrite(LED_VERDE, LOW);
- while (!Serial) {
- ; // wait for serial port to connect. Needed for native USB port only
- }
- Serial.println("setup ok !");
- digitalWrite(LED_VERDE, HIGH);
- // This makes sure the modem correctly reports incoming events
- vcs.hangCall();
- //scannerNetworks.begin();
- //wdt_enable(WDTO_8S);
- Serial.println("attesa evento");
- Temp = millis();
- tone(PIN_BUZZER, 100);
- delay(100);
- noTone(PIN_BUZZER);
- //delay(1000);
- }
- void stampa_seriale() {
- //stampo stato chiamate
- if (telefono != old_telefono) {
- Serial.print(F(" case telefono N. "));
- Serial.println(telefono);
- old_telefono = telefono;
- }
- //stampo stato sirena
- if (fase1 != old_fase) {
- Serial.print(F("fase1 ")); Serial.print(fase1);
- Serial.print(F("sirena ")); Serial.println(sirena);
- old_fase = fase1;
- }
- if (toni != old_toni) {
- Serial.print(F("toni ")); Serial.print(toni);
- old_toni=toni;
- }
- if ( allarme != old_allarme) {
- toni = 0;
- if (allarme == 1) {
- Serial.println(F("attivo oooooooooo"));
- }
- if (allarme == 0) {
- Serial.println(F("disattivo oooooooooo"));
- }
- old_allarme = allarme;
- }
- }
- void loop() {
- stampa_seriale();
- if (fronte) {
- if (total_reset == 0 && all == 1) {// se e' attivo
- Serial.print(" C "); //mi scrive lonta
- Serial.print(conta);
- }
- if (total_reset == 1 && flag == 2) { //&& conta != 0) {
- if (conta != 0) {
- Serial.print("_"); //se modalita reset attiva e non
- } // sincronizzato
- if ( conta == 0) {
- Serial.print("*");//se modalita reset attiva e
- } // sincronizzato
- }
- if (!primoGiro) conta = (now - tm < 2000) ? conta + 1 : 0;
- primoGiro = 0;
- tm = now;
- }
- if (millis() - Temp > 21600000) {
- telefono = 5;
- Serial.print(F(" ''''''''''' "));
- Temp = millis();
- }
- now = millis();
- sensor_led = analogRead(A3) > 500;
- //Serial.println(analogRead(A3));
- fronte = sensor_led && !old_sensor_led;
- old_sensor_led = sensor_led;
- //SENSORE= digitalRead(5) ;
- //if (digitalRead(12) == HIGH ) {
- //toni = 0;
- //}
- if (digitalRead(5) == HIGH ) {
- digitalWrite(LED_VERDE, HIGH); //verde
- Serial.print(" movimento rilevato ");
- if (allarme == 1) {
- sensore = 1;
- ciclo_sirena = 1;
- presenza = 1;
- }
- else {
- presenza = 0;
- }
- }
- else {
- digitalWrite(LED_VERDE, LOW);//verde
- sensore = 0;
- }
- if ( sensore && allarme == 1) {
- ciclo_sirena = 1;
- presenza = 1;
- }
- else {
- presenza = 0;
- }
- switch (allarme) {
- case 0:
- digitalWrite(LED_ROSSO, LOW);//rosso
- break;
- case 1:
- digitalWrite(LED_ROSSO, HIGH);//rosso
- break;
- }
- switch (toni) {
- case 0: { // your hand is on the sensor
- T_beep = millis();
- toni = 1;
- }
- break;
- case 1: { // your hand is close to the sensor
- tone(PIN_BUZZER, 500);
- if ( millis() - T_beep > 1000) {
- T_beep = millis();
- toni = 2;
- }
- }
- break;
- case 2: {
- tone(PIN_BUZZER, 800);
- if ( millis() - T_beep > 1500) {
- T_beep = millis();
- toni = 3;
- }
- }
- break;
- case 3:
- tone(PIN_BUZZER, 1700);
- if ( millis() - T_beep> 1500) {
- toni = 4;
- break;
- case 4: // your hand is nowhere near the sensor
- noTone(PIN_BUZZER);
- break;
- }
- }
- if (total_reset == 0) {
- switch (fase1) {
- case 0:
- digitalWrite(LED_GIALLO, LOW);//giallo
- noTone(PIN_BUZZER);
- break;
- case 1:
- digitalWrite(LED_GIALLO, HIGH);//giallo
- tone(PIN_BUZZER, 440);
- break;
- }
- //programma on off sirena
- if (ciclo_sirena == 1) { //
- if ( sirena <= 10) { //se e stato ripetuto il loop
- dt = millis() - t0; // qui creo lampeggio
- if (dt >= stp) { //con tempo on off
- if (fase1 == 0) {
- stp = T_on;
- fase1 = 1;
- } else {
- stp = T_off;
- fase1 = 0;
- sirena++;
- //digitalWrite(10, LOW);
- }
- t0 = millis();
- }
- }
- if (sirena == 11) {// se e' 11 ripetizione metto tempo pausa
- passaggi++; //variabile ripetizione sequenza sirena
- timeOfLastLedEvent = millis();
- //digitalWrite(10, LOW);
- Serial.println(F("PAUSA"));
- sirena = 12 ;
- }
- if (millis() - timeOfLastLedEvent > 5000 && sirena == 12) {
- Serial.println(" Ripetizione"); //aspetto tempo e poi
- Serial.print("passaggi "); //riparte sequenza sirena
- Serial.println(passaggi);
- sirena = 0;
- }
- if (passaggi >= 3) {// se e' la 3 ripetizione fermo il loop
- Serial.println("fine sirena");
- passaggi = 0;
- ciclo_sirena = 0 ;
- sirena = 0;
- }
- }
- switch (vcs.getvoiceCallStatus()) {
- case CALLING: {// This should never happen, as we are not placing a call
- digitalWrite(LED_BLU, HIGH);//blu
- }
- break;
- case IDLE_CALL: { // Nothing is happening
- digitalWrite(LED_BLU, LOW);//blu
- stato_chiamata = 1;
- break;
- }
- case RECEIVINGCALL: // Yes! Someone is calling us
- stato_chiamata = 0;
- digitalWrite(LED_BLU, HIGH);//blu
- Serial.println(" ");
- Serial.println(F("RECEIVING CALL"));
- // Retrieve the calling number
- vcs.retrieveCallingNumber(numtel, 20);
- if ((strcmp(numtel, "+39018xxxxxxx") == 0)
- || (strcmp(numtel, "+39347xxxxxx") == 0))
- {
- Serial.print("Number:");
- Serial.println(numtel);
- if (strcmp(numtel, "+39018xxxxxxx") == 0) {
- telefono = 1;
- }
- if (strcmp(numtel, "+39347xxxxxxxxx") == 0) {
- telefono = 40;//aggiunto else if al posto di if
- }
- }
- else
- Serial.println(F(" => non riconosciuto!"));
- delay(150);//evita la risposta tim che e' spento
- vcs.hangCall();
- }
- if (conta >= 20 || (now - tm > 20000)) {
- if (conta >= 20) {
- conta = 1;
- Serial.println (F(""));
- Serial.println("conta >= 5");
- }
- if (now - tm > 10000) {
- Serial.println(F("now-tm>"));
- }
- total_reset = 1;// avvio procedura reset
- flag = 0 ;
- }
- }
- if (total_reset == 1 ) {
- //noTone(7);
- if (flag == 0 ) {//azioni da fare solo ina volta
- digitalWrite(LED_VERDE, LOW); //verde
- digitalWrite(LED_ROSSO, LOW); //rosso
- digitalWrite(LED_BLU, LOW); //blu
- digitalWrite(LED_GIALLO, LOW); //giallo
- allarme = 0;//variabile stato allarme disattivata
- T_pertenza = millis();
- notConnected = true;
- Serial.println(F(" reset SISTEMA"));
- //digitalWrite(LED_VERDE, HIGH); //led esterno verde
- digitalWrite(PIN_RESET_SIM, HIGH);
- flag = 1;
- }
- if (sensor_led == 1 ) {//lampeggia come led sim durante il reset
- digitalWrite(LED_ROSSO, !digitalRead(LED_ROSSO)); //giallo
- }
- if (millis() - T_pertenza > 1000 && flag == 1) {
- //if (flag == 1 ) {
- Serial.println(F(" RIAVVIO"));
- digitalWrite(PIN_RESET_SIM, LOW);
- digitalWrite(LED_VERDE, HIGH); //led esterno
- Serial.println(F("reset ON"));
- flag = 2;
- }
- // tempo di avvio da reset
- if (millis() - T_pertenza > 30000 && flag == 2) { //dopo 20 secondi
- // if (flag == 2 ) { //se
- Serial.println(F( " ")); //non si e'
- Serial.println(F("connessione gsm "));
- digitalWrite(LED_BLU, HIGH);
- if ( conta != 0) { // stabilizzato il
- Serial.println(F( " reset conta"));//lampeggio
- flag = 0;//nuova procedura reset //lento
- }
- if ( conta == 0) { //invece se il lampeggio
- // Serial.println (F("\r\n" "seriale""));//e' stabile
- Serial.println (F("seriale"));
- //GSM_SMS sms;
- //GSM gsmAccess;
- //GSMVoiceCall vcs;
- //gsmAccess.begin() ;
- //notConnected = true;
- Serial.println(F(" bbb "));
- // wdt_reset();
- flag = 3;
- }
- //}
- }
- if (flag == 3 ) { //oltre il tempo si resetta
- //if (millis() - T_pertenza > 40000) {
- //Serial.println(F(" reset inizializzazione sim "));
- //flag = 0;
- //}
- while (notConnected) { //attendo la connessione
- Serial.println(F("."));
- if (gsmAccess.begin() == GSM_READY) {
- notConnected = false;
- Serial.println(F(" GSM_READY avvio"));
- tone(PIN_BUZZER, 1200);
- delay(250);
- //tone(PIN_BUZZER, 1600);
- //delay(250);
- //tone(PIN_BUZZER, 2000);
- digitalWrite(LED_GIALLO, HIGH); //led esterno
- delay(1000);
- noTone(PIN_BUZZER);
- allarme = 1;//variabile stato allarme attivata
- conta = 1;
- primoGiro = 1;
- now = millis();
- tm = millis();
- vcs.hangCall();
- total_reset = 0;
- flag = 0;
- } else {
- Serial.println(F("Not connected"));
- notConnected = false;
- delay(1000);
- total_reset = 1;
- flag = 0;
- }
- }
- toni = 99;
- digitalWrite(LED_VERDE, LOW); //giallo
- digitalWrite(LED_ROSSO, LOW); //giallo
- digitalWrite(LED_BLU, LOW); //giallo
- digitalWrite(LED_GIALLO, LOW); //giallo
- }
- // wdt_reset();
- }
- if ( presenza == 1 && all == 1) {
- Serial.println(F(""));
- Serial.println(F("chiamate per allarme"));
- vcs.hangCall(); //serve per far cadere la chiamata
- all = 0;
- fase = 1;
- }
- if ( total_reset == 0 && all == 0 && stato_chiamata == 1) { //__________ da qui____________
- //telefono = 0;
- switch (fase) {
- case 1: {
- N_call_allarme++;// incremento chiamate
- Serial.println(F(" 1 chiamo daniele"));
- vcs.voiceCall("+39347xxxxxxx", 500);
- Serial.println(F("codice inviato a sim900l chiamo"));
- Tempo_chiamata_allarme = millis();
- fase = 2;
- }
- break;
- case 2: {
- if (millis() - Tempo_chiamata_allarme > 20000) {
- Serial.println(F(" 2 fine chiamata daniele"));
- vcs.hangCall(); //serve per far cadere la chiamata
- Serial.println(F("codice inviato a sim900l riaggancio"));
- fase = 3;
- Tempo_chiamata_allarme = millis();
- }
- }
- break;
- case 3: {
- if (millis() - Tempo_chiamata_allarme > 1500) {
- Serial.println(F(" 3 chiamo olindo"));
- //vcs.voiceCall("+39347xxxxxxx", 5000);
- Serial.println(F("codice inviato a sim900l chiamo"));
- Tempo_chiamata_allarme = millis();
- fase = 4;
- //}
- }
- }
- break;
- case 4: {
- if (millis() - Tempo_chiamata_allarme > 7000) {
- Tempo_chiamata_allarme = millis();
- Serial.println(F("4 fine chiamata olindo"));
- vcs.hangCall(); //serve per far cadere la chiamata
- Serial.println(F("codice inviato a sim900l riaggancio"));
- fase = 5;
- }
- }
- break;
- case 5: {
- if (millis() - Tempo_chiamata_allarme > 1000) {//valuto il numero di
- Serial.print(F("N_call_allarme N. "));//volte che ho
- Serial.println(N_call_allarme);//ripetuto il swich case
- if (N_call_allarme <= 3) { // fase
- Serial.println(F("ripeto fasi"));
- fase = 1;
- }
- else {
- fase = 0; // se e' superiore esco dallo
- N_call_allarme = 0;//swich case
- all = 1;
- Serial.println(F("fine fasi"));
- }
- }
- }
- break;
- }
- }
- if ( total_reset == 0 && all == 1 && stato_chiamata == 1) {
- //if (fase != 0) {
- //presenza = 0 ;
- // }
- switch (telefono) {
- case 1: {
- allarme = !allarme;
- Serial.println(F("cambio stato allarme da casa "));
- Serial.print(F("nuovo stato = "));
- Serial.println(allarme);
- //vcs.hangCall(); //serve per far cadere la chiamata
- Serial.println(F("riaggancio casa"));
- Tempo_chiamata_stato = millis();
- if (allarme == 1) {
- telefono = 2;
- }
- if (allarme == 0) {
- telefono = 3;
- // }
- }
- }
- break;
- case 40: {
- Serial.println(F("cambio stato allarme dal cell "));
- Serial.print(F("vecchio stato = "));
- Serial.println(allarme);
- Tempo_chiamata_stato = millis();
- //vcs.hangCall();
- telefono = 4;
- }
- break;
- case 4: {
- if (millis() - Tempo_chiamata_stato > 2000) {
- vcs.hangCall();
- Serial.println(F("riaggancio cell"));
- allarme = !allarme;
- Serial.print(F("nuovo stato = "));
- Serial.println(allarme);
- //vcs.hangCall(); //serve per far cadere la chiamata
- Tempo_chiamata_stato = millis();
- if (allarme == 1) {
- telefono = 5;
- }
- if (allarme == 0) {
- telefono = 6;
- }
- }
- }
- break;
- case 2: {
- if (millis() - Tempo_chiamata_stato > 2000) {
- Serial.println(F("chiamata casa attivato "));
- vcs.voiceCall("+39018xxxxxx", 500);
- Serial.println(F("codice inviato a sim900l"));
- Tempo_chiamata_stato = millis();
- telefono = 20;
- }
- }
- break;
- case 3: {
- if (millis() - Tempo_chiamata_stato > 2000) {
- Serial.println(F("chiamata casa disattivato"));
- Tempo_chiamata_stato = millis();
- vcs.voiceCall("+390185358719", 1000);
- Serial.println(F("codice inviato a sim900l"));
- telefono = 21;
- }
- }
- break;
- case 20: {
- if (millis() - Tempo_chiamata_stato > 9000) {
- Tempo_chiamata_stato = millis();
- Serial.println(F("fine chiamata casa allarme attivato"));
- vcs.hangCall(); //serve per far cadere la chiamata
- Serial.println(F("chiamata stop"));
- telefono = 10;
- }
- }
- break;
- case 21: {
- if (millis() - Tempo_chiamata_stato > 5000) {
- Tempo_chiamata_stato = millis();
- Serial.println(F("chiamata casa allarme disattivato "));
- vcs.hangCall(); //serve per far cadere la chiamata
- Serial.println(F("chiamata stop"));
- telefono = 10;
- }
- }
- break;
- case 5: {
- if (millis() - Tempo_chiamata_stato > 2000) {
- Serial.println(F("chiamata cell attivato "));
- vcs.voiceCall("+39347xxxxxxx", 500);
- Serial.println(F("codice inviato a sim900l"));
- Tempo_chiamata_stato = millis();
- telefono = 30;
- }
- }
- break;
- case 6: {
- if (millis() - Tempo_chiamata_stato > 2000) {
- Serial.println(F(""));
- Serial.println(F("chiamata cell allarme disattivato"));
- Tempo_chiamata_stato = millis();
- vcs.voiceCall("+39347xxxxxxx", 500);
- Serial.println(F("codice inviato a sim900l"));
- telefono = 31;
- }
- }
- break;
- case 30: {//lunghezza squillo allarme attivato
- if (millis() - Tempo_chiamata_stato > 9500) {
- Tempo_chiamata_stato = millis();
- Serial.println(F(""));
- Serial.println(F("fine chiamata cell allarme attivato"));
- telefono = 10;
- }
- }
- break;
- case 31: {//lunghezza squillo allarme disattivato
- if (millis() - Tempo_chiamata_stato > 23000) {
- Tempo_chiamata_stato = millis();
- Serial.println(F(""));
- Serial.println(F("fine chiamata cell allarme disattivato"));
- telefono = 10;
- }
- }
- break;
- case 10: {
- vcs.hangCall(); //serve per far cadere la chiamata
- Serial.println(F("chiamata stop"));
- telefono = 0;
- //Tempo_chiamta_stato = 0; presenza = 0;
- }
- break;
- }
- // Check the status of the voice call
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement