Advertisement
pippero

Untitled

Apr 16th, 2021
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.61 KB | None | 0 0
  1. //const int PIN_SENSORE = 5;
  2. const int PIN_BUZZER = 8;
  3. const int LED_ROSSO = 14;
  4. const int LED_BLU = 15;
  5. const int LED_VERDE = 13;
  6. const int LED_GIALLO = 16;
  7. const int PIN_RESET_SIM = 4;
  8.  
  9. unsigned long TempoInizio = 0;
  10. const int PULSANTE = 12;
  11. bool old = HIGH;
  12. bool puls = LOW;
  13.  
  14. bool SENSORE = 0;
  15.  
  16. int range = 99;
  17. int old_range = 99;
  18. unsigned long T_beep = 0;
  19.  
  20.  
  21. #define T_on 100
  22. #define T_off 500
  23. bool fase1 = 0;
  24. bool old_fase = 0;
  25. unsigned long stp = 0;//int??
  26. int sirena = 0; //variabile cicli sirena on off
  27. bool ciclo_sirena = 0; // variabile ciclo allarme sirena
  28. unsigned long t0 = 0;
  29. unsigned long dt = 0;
  30. unsigned long timeOfLastLedEvent = 0; // l'ultima volta che il LED รจ stato aggiornato
  31. int passaggi = 0;
  32.  
  33. int sensore = 0;
  34.  
  35. //-------------variabili stato led modulo sim----------
  36. int conta = 1; //variabile conteggio led modulo sim
  37. byte primoGiro = 1; //variabile tempo lampeggio
  38. bool old_sensor_led = 0; // variabile led sim
  39. bool sensor_led ; // da led a stato logico
  40. bool fronte;
  41.  
  42.  
  43. unsigned long tm; //tempo_memoria
  44. unsigned long now ;
  45.  
  46. //-------------
  47.  
  48.  
  49.  
  50.  
  51. //-------------
  52. bool stato_chiamata = 1; //variabile stato sim
  53. byte flag = 0;
  54. bool total_reset = 1;
  55. byte N_call_allarme = 0;//variabile numero ripetizione chiamate
  56. unsigned long T_pertenza = 0; // tempo avvio macchina
  57. //SIM800L
  58. // tx pin 2
  59. // rx pin 3
  60. int old_telefono = 99; // vecchio steto telefono
  61. int telefono = 0;
  62. unsigned long Tempo_chiamata_allarme = 0;
  63. unsigned long Tempo_chiamata_stato = 0;
  64. unsigned long Temp = 0;
  65. // Include the GSM library
  66. #include <GSM.h>
  67. //#include <avr/wdt.h> // includiamo la libreria watchdog
  68. //GSM_SMS sms;
  69. //GSMScanner scannerNetworks;
  70. bool allarme = 0;
  71. bool old_allarme = 1;
  72. bool presenza = 0;
  73.  
  74. // PIN Number
  75. #define PINNUMBER //""
  76.  
  77. int fase = 0;
  78. int all = 1;
  79. bool notConnected = true;
  80. // initialize the library instance
  81. GSM gsmAccess;
  82. GSMVoiceCall vcs;
  83. //gsmAccess.isAccessAlive;
  84. // Array to hold the number for the incoming call
  85. char numtel[20];
  86.  
  87.  
  88. void setup() {
  89. // initialize serial communications and wait for port to open:
  90. Serial.begin(9600);
  91. Serial.println (F(" setup"));
  92. pinMode(A3, INPUT); //led gsm
  93. pinMode(LED_VERDE, OUTPUT); //verde =sensore
  94. pinMode(LED_ROSSO, OUTPUT); //rosso = allarme attivo
  95. pinMode(LED_BLU, OUTPUT); // blu
  96. pinMode(LED_GIALLO, OUTPUT); //giallo
  97. pinMode(8, OUTPUT);// buzzer
  98. pinMode(6, OUTPUT);// rele' sirena
  99. pinMode(9, INPUT);//input sensore
  100. pinMode(PULSANTE, INPUT_PULLUP);//input sensore
  101. pinMode(PIN_RESET_SIM, OUTPUT);// rele' sirena
  102. pinMode(12, INPUT_PULLUP);
  103. // digitalWrite(LED_VERDE, LOW);
  104. while (!Serial) {
  105. ; // wait for serial port to connect. Needed for native USB port only
  106. }
  107. Serial.println("setup ok !");
  108. digitalWrite(LED_VERDE, HIGH);
  109. // This makes sure the modem correctly reports incoming events
  110. vcs.hangCall();
  111. //scannerNetworks.begin();
  112. //wdt_enable(WDTO_8S);
  113. Serial.println("attesa evento");
  114. Temp = millis();
  115. tone(PIN_BUZZER, 100);
  116. delay(100);
  117. noTone(PIN_BUZZER);
  118.  
  119. //delay(1000);
  120. }
  121. void stampa_seriale() {
  122.  
  123. //stampo stato chiamate
  124. if (telefono != old_telefono) {
  125. Serial.print(F(" case telefono N. "));
  126. Serial.println(telefono);
  127. old_telefono = telefono;
  128. }
  129. //stampo stato sirena
  130. if (fase1 != old_fase) {
  131. Serial.print(F("fase1 ")); Serial.print(fase1);
  132. Serial.print(F("sirena ")); Serial.println(sirena);
  133. old_fase = fase1;
  134. }
  135. if (range != old_range) {
  136. Serial.print(F("toni_range ")); Serial.print(range);
  137. old_range = range;
  138. }
  139.  
  140. if ( allarme != old_allarme ) {
  141. TempoInizio = millis();
  142. range = 0;
  143. if (allarme == 1) {
  144. Serial.println(F("attivo oooooooooo"));
  145. }
  146. if (allarme == 0) {
  147. Serial.println(F("disattivo oooooooooo"));
  148. }
  149. old_allarme = allarme;
  150. }
  151.  
  152. }
  153. void loop() {
  154. stampa_seriale();
  155.  
  156. if (fronte) {
  157. if (total_reset == 0 && all == 1) {// se e' attivo
  158. Serial.print(" C "); //mi scrive lonta
  159. Serial.print(conta);
  160. }
  161. if (total_reset == 1 && flag == 2) { //&& conta != 0) {
  162. if (conta != 0) {
  163. Serial.print("_"); //se modalita reset attiva e non
  164. } // sincronizzato
  165. if ( conta == 0) {
  166. Serial.print("*");//se modalita reset attiva e
  167. } // sincronizzato
  168. }
  169. if (!primoGiro) conta = (now - tm < 2000) ? conta + 1 : 0;
  170. primoGiro = 0;
  171. tm = now;
  172. }
  173.  
  174.  
  175. if (millis() - Temp > 21600000) {
  176. telefono = 5;
  177. Serial.print(F(" ''''''''''' "));
  178. Temp = millis();
  179. }
  180. now = millis();
  181. sensor_led = analogRead(A3) > 500;
  182. //Serial.println(analogRead(A3));
  183. fronte = sensor_led && !old_sensor_led;
  184. old_sensor_led = sensor_led;
  185.  
  186. //SENSORE= digitalRead(5) ;
  187. if (digitalRead(PULSANTE) == LOW && old == HIGH)
  188. {
  189. TempoInizio = millis();
  190. range = 0;
  191. old != puls;
  192. }
  193.  
  194. if (digitalRead(5) == HIGH ) {
  195. digitalWrite(LED_VERDE, HIGH); //verde
  196. Serial.print(" movimento rilevato ");
  197.  
  198. if (allarme == 1) {
  199. sensore = 1;
  200. ciclo_sirena = 1;
  201. presenza = 1;
  202. }
  203. else {
  204. presenza = 0;
  205. }
  206. }
  207. else {
  208. digitalWrite(LED_VERDE, LOW);//verde
  209. sensore = 0;
  210. }
  211.  
  212. if ( sensore && allarme == 1) {
  213. ciclo_sirena = 1;
  214. presenza = 1;
  215. }
  216. else {
  217. presenza = 0;
  218. }
  219.  
  220.  
  221. switch (allarme) {
  222. case 0:
  223. digitalWrite(LED_ROSSO, LOW);//rosso
  224. break;
  225. case 1:
  226. digitalWrite(LED_ROSSO, HIGH);//rosso
  227. break;
  228. }
  229. switch (range)
  230. {
  231. case 0: // your hand is on the sensor
  232. tone(PIN_BUZZER, 400);
  233. if ( millis() - TempoInizio > 300)
  234. {
  235. TempoInizio = millis();
  236. range = 1;
  237. }
  238. break;
  239.  
  240. case 1: // your hand is close to the sensor
  241. tone(PIN_BUZZER, 600);
  242. if ( millis() - TempoInizio > 300)
  243. {
  244. TempoInizio = millis();
  245. range = 2;
  246. }
  247. break;
  248.  
  249. case 2:
  250. tone(PIN_BUZZER, 800);
  251. if ( millis() - TempoInizio > 300)
  252. {
  253. TempoInizio = millis();
  254. range = 3;
  255. }
  256. break;
  257.  
  258. case 3:
  259. tone(PIN_BUZZER, 1000);
  260. if ( millis() - TempoInizio > 800)
  261. {
  262. TempoInizio = 0;
  263. range = 4;
  264. }
  265. break;
  266.  
  267. case 4: // your hand is nowhere near the sensor
  268. noTone(PIN_BUZZER);
  269. break;
  270. }
  271.  
  272.  
  273. if (total_reset == 0) {
  274.  
  275. switch (fase1) {
  276. case 0:
  277. digitalWrite(LED_GIALLO, LOW);//giallo
  278. noTone(PIN_BUZZER);
  279. break;
  280. case 1:
  281. digitalWrite(LED_GIALLO, HIGH);//giallo
  282. tone(PIN_BUZZER, 440);
  283. break;
  284. }
  285. //programma on off sirena
  286. if (ciclo_sirena == 1) { //
  287. if ( sirena <= 10) { //se e stato ripetuto il loop
  288. dt = millis() - t0; // qui creo lampeggio
  289. if (dt >= stp) { //con tempo on off
  290. if (fase1 == 0) {
  291. stp = T_on;
  292. fase1 = 1;
  293. } else {
  294. stp = T_off;
  295. fase1 = 0;
  296. sirena++;
  297. //digitalWrite(10, LOW);
  298. }
  299. t0 = millis();
  300. }
  301. }
  302. if (sirena == 11) {// se e' 11 ripetizione metto tempo pausa
  303. passaggi++; //variabile ripetizione sequenza sirena
  304. timeOfLastLedEvent = millis();
  305. //digitalWrite(10, LOW);
  306. Serial.println(F("PAUSA"));
  307. sirena = 12 ;
  308. }
  309. if (millis() - timeOfLastLedEvent > 5000 && sirena == 12) {
  310. Serial.println(" Ripetizione"); //aspetto tempo e poi
  311. Serial.print("passaggi "); //riparte sequenza sirena
  312. Serial.println(passaggi);
  313. sirena = 0;
  314. }
  315. if (passaggi >= 3) {// se e' la 3 ripetizione fermo il loop
  316. Serial.println("fine sirena");
  317. passaggi = 0;
  318. ciclo_sirena = 0 ;
  319. sirena = 0;
  320. }
  321. }
  322. switch (vcs.getvoiceCallStatus()) {
  323. case CALLING: {// This should never happen, as we are not placing a call
  324. digitalWrite(LED_BLU, HIGH);//blu
  325. }
  326. break;
  327. case IDLE_CALL: { // Nothing is happening
  328. digitalWrite(LED_BLU, LOW);//blu
  329. stato_chiamata = 1;
  330. break;
  331. }
  332. case RECEIVINGCALL: // Yes! Someone is calling us
  333. stato_chiamata = 0;
  334. digitalWrite(LED_BLU, HIGH);//blu
  335. Serial.println(" ");
  336. Serial.println(F("RECEIVING CALL"));
  337. // Retrieve the calling number
  338. vcs.retrieveCallingNumber(numtel, 20);
  339. if ((strcmp(numtel, "+390185358719") == 0)
  340. || (strcmp(numtel, "+393474212805") == 0))
  341. {
  342. if (strcmp(numtel, "+390185358719") == 0) { //+393479139527
  343. telefono = 1;
  344. }
  345. if (strcmp(numtel, "+393474212805") == 0) {
  346. telefono = 40;//aggiunto else if al posto di if
  347. }
  348. }
  349. else
  350. Serial.println(F(" => non riconosciuto!"));
  351. delay(150);//evita la risposta tim che e' spento
  352. vcs.hangCall();
  353. }
  354.  
  355. if (conta >= 20 || (now - tm > 20000)) {
  356. if (conta >= 20) {
  357. conta = 1;
  358. Serial.println (F(""));
  359. Serial.println("conta >= 5");
  360. }
  361. if (now - tm > 10000) {
  362. Serial.println(F("now-tm>"));
  363. }
  364. total_reset = 1;// avvio procedura reset
  365. flag = 0 ;
  366. }
  367. }
  368.  
  369. if (total_reset == 1 ) {
  370. //noTone(7);
  371. if (flag == 0 ) {//azioni da fare solo ina volta
  372. digitalWrite(LED_VERDE, LOW); //verde
  373. digitalWrite(LED_ROSSO, LOW); //rosso
  374. digitalWrite(LED_BLU, LOW); //blu
  375. digitalWrite(LED_GIALLO, LOW); //giallo
  376. allarme = 0;//variabile stato allarme disattivata
  377. T_pertenza = millis();
  378. notConnected = true;
  379. Serial.println(F(" reset SISTEMA"));
  380. //digitalWrite(LED_VERDE, HIGH); //led esterno verde
  381. digitalWrite(PIN_RESET_SIM, HIGH);
  382. flag = 1;
  383. }
  384. if (sensor_led == 1 ) {//lampeggia come led sim durante il reset
  385. digitalWrite(LED_ROSSO, !digitalRead(LED_ROSSO)); //giallo
  386. }
  387. if (millis() - T_pertenza > 1000 && flag == 1) {
  388. //if (flag == 1 ) {
  389. Serial.println(F(" RIAVVIO"));
  390. digitalWrite(PIN_RESET_SIM, LOW);
  391. digitalWrite(LED_VERDE, HIGH); //led esterno
  392. Serial.println(F("reset ON"));
  393. flag = 2;
  394. }
  395. // tempo di avvio da reset
  396. if (millis() - T_pertenza > 30000 && flag == 2) { //dopo 20 secondi
  397. // if (flag == 2 ) { //se
  398. Serial.println(F( " ")); //non si e'
  399. Serial.println(F("connessione gsm "));
  400. digitalWrite(LED_BLU, HIGH);
  401. if ( conta != 0) { // stabilizzato il
  402. Serial.println(F( " reset conta"));//lampeggio
  403. flag = 0;//nuova procedura reset //lento
  404. }
  405. if ( conta == 0) { //invece se il lampeggio
  406. // Serial.println (F("\r\n" "seriale""));//e' stabile
  407. Serial.println (F("seriale"));
  408.  
  409. //GSM_SMS sms;
  410. //GSM gsmAccess;
  411. //GSMVoiceCall vcs;
  412. //gsmAccess.begin() ;
  413. //notConnected = true;
  414. Serial.println(F(" bbb "));
  415. // wdt_reset();
  416. flag = 3;
  417. }
  418. //}
  419. }
  420.  
  421. if (flag == 3 ) { //oltre il tempo si resetta
  422. //if (millis() - T_pertenza > 40000) {
  423.  
  424. //Serial.println(F(" reset inizializzazione sim "));
  425. //flag = 0;
  426. //}
  427.  
  428. while (notConnected) { //attendo la connessione
  429. Serial.println(F("."));
  430. if (gsmAccess.begin() == GSM_READY) {
  431. notConnected = false;
  432. Serial.println(F(" GSM_READY avvio"));
  433. tone(PIN_BUZZER, 1200);
  434. delay(250);
  435. //tone(PIN_BUZZER, 1600);
  436. //delay(250);
  437. //tone(PIN_BUZZER, 2000);
  438. digitalWrite(LED_GIALLO, HIGH); //led esterno
  439. delay(1000);
  440. noTone(PIN_BUZZER);
  441. allarme = 1;//variabile stato allarme attivata
  442. conta = 1;
  443. primoGiro = 1;
  444. now = millis();
  445. tm = millis();
  446. vcs.hangCall();
  447. total_reset = 0;
  448. flag = 0;
  449.  
  450. } else {
  451. Serial.println(F("Not connected"));
  452. notConnected = false;
  453. delay(1000);
  454. total_reset = 1;
  455. flag = 0;
  456. }
  457. }
  458. range = 99;
  459. digitalWrite(LED_VERDE, LOW); //giallo
  460. digitalWrite(LED_ROSSO, LOW); //giallo
  461. digitalWrite(LED_BLU, LOW); //giallo
  462. digitalWrite(LED_GIALLO, LOW); //giallo
  463. }
  464. // wdt_reset();
  465.  
  466. }
  467.  
  468. if ( presenza == 1 && all == 1) {
  469. Serial.println(F(""));
  470. Serial.println(F("chiamate per allarme"));
  471. vcs.hangCall(); //serve per far cadere la chiamata
  472. all = 0;
  473. fase = 1;
  474. }
  475. if ( total_reset == 0 && all == 0 && stato_chiamata == 1) { //__________ da qui____________
  476.  
  477. //telefono = 0;
  478. switch (fase) {
  479.  
  480. case 1: {
  481. N_call_allarme++;// incremento chiamate
  482. Serial.println(F(" 1 chiamo daniele"));
  483. vcs.voiceCall("+393474212805", 500);
  484. Serial.println(F("codice inviato a sim900l chiamo"));
  485. Tempo_chiamata_allarme = millis();
  486. fase = 2;
  487.  
  488. }
  489. break;
  490. case 2: {
  491. if (millis() - Tempo_chiamata_allarme > 20000) {
  492. Serial.println(F(" 2 fine chiamata daniele"));
  493. vcs.hangCall(); //serve per far cadere la chiamata
  494. Serial.println(F("codice inviato a sim900l riaggancio"));
  495. fase = 3;
  496. Tempo_chiamata_allarme = millis();
  497. }
  498. }
  499. break;
  500. case 3: {
  501. if (millis() - Tempo_chiamata_allarme > 1500) {
  502. Serial.println(F(" 3 chiamo olindo"));
  503. //vcs.voiceCall("+393479139527", 5000);//+393474212805
  504. Serial.println(F("codice inviato a sim900l chiamo"));
  505. Tempo_chiamata_allarme = millis();
  506. fase = 4;
  507. //}
  508. }
  509. }
  510. break;
  511. case 4: {
  512. if (millis() - Tempo_chiamata_allarme > 7000) {
  513. Tempo_chiamata_allarme = millis();
  514. Serial.println(F("4 fine chiamata olindo"));
  515. vcs.hangCall(); //serve per far cadere la chiamata
  516. Serial.println(F("codice inviato a sim900l riaggancio"));
  517. fase = 5;
  518. }
  519. }
  520. break;
  521. case 5: {
  522. if (millis() - Tempo_chiamata_allarme > 1000) {//valuto il numero di
  523. Serial.print(F("N_call_allarme N. "));//volte che ho
  524. Serial.println(N_call_allarme);//ripetuto il swich case
  525. if (N_call_allarme <= 3) { // fase
  526. Serial.println(F("ripeto fasi"));
  527. fase = 1;
  528. }
  529. else {
  530. fase = 0; // se e' superiore esco dallo
  531. N_call_allarme = 0;//swich case
  532. all = 1;
  533. Serial.println(F("fine fasi"));
  534. }
  535. }
  536. }
  537. break;
  538. }
  539. }
  540. if ( total_reset == 0 && all == 1 && stato_chiamata == 1) {
  541. //if (fase != 0) {
  542. //presenza = 0 ;
  543. // }
  544. switch (telefono) {
  545. case 1: {
  546. allarme = !allarme;
  547. Serial.println(F("cambio stato allarme da casa "));
  548. Serial.print(F("nuovo stato = "));
  549. Serial.println(allarme);
  550. //vcs.hangCall(); //serve per far cadere la chiamata
  551. Serial.println(F("riaggancio casa"));
  552. Tempo_chiamata_stato = millis();
  553.  
  554. if (allarme == 1) {
  555. telefono = 2;
  556. }
  557. if (allarme == 0) {
  558. telefono = 3;
  559. // }
  560. }
  561. }
  562. break;
  563. case 40: {
  564. Serial.println(F("cambio stato allarme dal cell "));
  565. Serial.print(F("vecchio stato = "));
  566. Serial.println(allarme);
  567. Tempo_chiamata_stato = millis();
  568. //vcs.hangCall();
  569. telefono = 4;
  570. }
  571. break;
  572. case 4: {
  573. if (millis() - Tempo_chiamata_stato > 2000) {
  574. vcs.hangCall();
  575. Serial.println(F("riaggancio cell"));
  576. allarme = !allarme;
  577. Serial.print(F("nuovo stato = "));
  578. Serial.println(allarme);
  579. //vcs.hangCall(); //serve per far cadere la chiamata
  580.  
  581. Tempo_chiamata_stato = millis();
  582. if (allarme == 1) {
  583. telefono = 5;
  584. }
  585. if (allarme == 0) {
  586. telefono = 6;
  587. }
  588. }
  589. }
  590. break;
  591. case 2: {
  592. if (millis() - Tempo_chiamata_stato > 2000) {
  593. Serial.println(F("chiamata casa attivato "));
  594. vcs.voiceCall("+390185358719", 500);//393479139527
  595. Serial.println(F("codice inviato a sim900l"));
  596. Tempo_chiamata_stato = millis();
  597. telefono = 20;
  598. }
  599. }
  600. break;
  601. case 3: {
  602. if (millis() - Tempo_chiamata_stato > 2000) {
  603. Serial.println(F("chiamata casa disattivato"));
  604. Tempo_chiamata_stato = millis();
  605. vcs.voiceCall("+390185358719", 1000);
  606. Serial.println(F("codice inviato a sim900l"));
  607. telefono = 21;
  608. }
  609. }
  610. break;
  611. case 20: {
  612. if (millis() - Tempo_chiamata_stato > 9000) {
  613. Tempo_chiamata_stato = millis();
  614. Serial.println(F("fine chiamata casa allarme attivato"));
  615. vcs.hangCall(); //serve per far cadere la chiamata
  616. Serial.println(F("chiamata stop"));
  617. telefono = 10;
  618. }
  619. }
  620. break;
  621. case 21: {
  622. if (millis() - Tempo_chiamata_stato > 5000) {
  623. Tempo_chiamata_stato = millis();
  624. Serial.println(F("chiamata casa allarme disattivato "));
  625. vcs.hangCall(); //serve per far cadere la chiamata
  626. Serial.println(F("chiamata stop"));
  627. telefono = 10;
  628. }
  629. }
  630. break;
  631. case 5: {
  632. if (millis() - Tempo_chiamata_stato > 2000) {
  633. Serial.println(F("chiamata cell attivato "));
  634. vcs.voiceCall("+393474212805", 500);
  635. Serial.println(F("codice inviato a sim900l"));
  636. Tempo_chiamata_stato = millis();
  637. telefono = 30;
  638. }
  639. }
  640. break;
  641. case 6: {
  642. if (millis() - Tempo_chiamata_stato > 2000) {
  643. Serial.println(F(""));
  644. Serial.println(F("chiamata cell allarme disattivato"));
  645. Tempo_chiamata_stato = millis();
  646. vcs.voiceCall("+393474212805", 500);
  647. Serial.println(F("codice inviato a sim900l"));
  648. telefono = 31;
  649. }
  650. }
  651. break;
  652. case 30: {//lunghezza squillo allarme attivato
  653. if (millis() - Tempo_chiamata_stato > 9500) {
  654. Tempo_chiamata_stato = millis();
  655. Serial.println(F(""));
  656. Serial.println(F("fine chiamata cell allarme attivato"));
  657. telefono = 10;
  658. }
  659. }
  660. break;
  661. case 31: {//lunghezza squillo allarme disattivato
  662. if (millis() - Tempo_chiamata_stato > 23000) {
  663. Tempo_chiamata_stato = millis();
  664. Serial.println(F(""));
  665. Serial.println(F("fine chiamata cell allarme disattivato"));
  666. telefono = 10;
  667. }
  668. }
  669. break;
  670. case 10: {
  671. vcs.hangCall(); //serve per far cadere la chiamata
  672. Serial.println(F("chiamata stop"));
  673. telefono = 0;
  674. //Tempo_chiamta_stato = 0; presenza = 0;
  675. }
  676. break;
  677. }
  678. // Check the status of the voice call
  679. }
  680. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement