Advertisement
pippero

Untitled

Apr 16th, 2021
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.53 KB | None | 0 0
  1. #define PIN_SENSORE 5
  2. #define PIN_BUZZER 8
  3. #define LED_ROSSO 14
  4. #define LED_BLU 15
  5. #define LED_VERDE 13
  6. #define LED_GIALLO 16
  7. #define PIN_RESET_SIM 4
  8. #define PULSANTE 12
  9.  
  10. unsigned long TempoInizio = 0;
  11.  
  12. bool old = HIGH;
  13. bool puls = LOW;
  14.  
  15.  
  16.  
  17. int range = 99;
  18. int old_range = 99;
  19. unsigned long T_beep = 0;
  20.  
  21.  
  22. unsigned long T_on= 100;
  23. unsigned long T_off =500;
  24. bool fase1 = 0;
  25. bool old_fase = 0;
  26. unsigned long stp = 0;//int??
  27. int sirena = 0; //variabile cicli sirena on off
  28. bool ciclo_sirena = 0; // variabile ciclo allarme sirena
  29. unsigned long t0 = 0;
  30. unsigned long dt = 0;
  31. unsigned long timeOfLastLedEvent = 0; // l'ultima volta che il LED รจ stato aggiornato
  32. int passaggi = 0;
  33.  
  34. int sensore = 0;
  35.  
  36. //-------------variabili stato led modulo sim----------
  37. int conta = 1; //variabile conteggio led modulo sim
  38. byte primoGiro = 1; //variabile tempo lampeggio
  39. bool old_sensor_led = 0; // variabile led sim
  40. bool sensor_led ; // da led a stato logico
  41. bool fronte;
  42.  
  43.  
  44. unsigned long tm; //tempo_memoria
  45. unsigned long now ;
  46.  
  47. //-------------
  48.  
  49.  
  50.  
  51.  
  52. //-------------
  53. bool stato_chiamata = 1; //variabile stato sim
  54. byte flag = 0;
  55. bool total_reset = 1;
  56. byte N_call_allarme = 0;//variabile numero ripetizione chiamate
  57. unsigned long T_pertenza = 0; // tempo avvio macchina
  58. //SIM800L
  59. // tx pin 2
  60. // rx pin 3
  61. int old_telefono = 99; // vecchio steto telefono
  62. int telefono = 0;
  63. unsigned long Tempo_chiamata_allarme = 0;
  64. unsigned long Tempo_chiamata_stato = 0;
  65. unsigned long Temp = 0;
  66. // Include the GSM library
  67. #include <GSM.h>
  68. //#include <avr/wdt.h> // includiamo la libreria watchdog
  69. //GSM_SMS sms;
  70. //GSMScanner scannerNetworks;
  71. bool allarme = 0;
  72. bool old_allarme = 1;
  73. bool presenza = 0;
  74.  
  75. // PIN Number
  76. #define PINNUMBER //""
  77.  
  78. int fase = 0;
  79. int all = 1;
  80. bool notConnected = true;
  81. // initialize the library instance
  82. GSM gsmAccess;
  83. GSMVoiceCall vcs;
  84. //gsmAccess.isAccessAlive;
  85. // Array to hold the number for the incoming call
  86. char numtel[20];
  87.  
  88.  
  89. void setup() {
  90. // initialize serial communications and wait for port to open:
  91. Serial.begin(9600);
  92. Serial.println (F(" setup"));
  93. pinMode(A3, INPUT); //led gsm
  94. pinMode(LED_VERDE, OUTPUT); //verde =sensore
  95. pinMode(LED_ROSSO, OUTPUT); //rosso = allarme attivo
  96. pinMode(LED_BLU, OUTPUT); // blu
  97. pinMode(LED_GIALLO, OUTPUT); //giallo
  98. pinMode(PIN_BUZZER, OUTPUT);// buzzer
  99. pinMode(6, OUTPUT);// rele' sirena
  100. pinMode(PIN_SENSORE, INPUT);//input sensore
  101. pinMode(PULSANTE, INPUT_PULLUP);//input sensore
  102. pinMode(PIN_RESET_SIM, OUTPUT);// rele' sirena
  103. pinMode(12, INPUT_PULLUP);
  104. // digitalWrite(LED_VERDE, LOW);
  105. while (!Serial) {
  106. ; // wait for serial port to connect. Needed for native USB port only
  107. }
  108. Serial.println("setup ok !");
  109. digitalWrite(LED_VERDE, HIGH);
  110. // This makes sure the modem correctly reports incoming events
  111. vcs.hangCall();
  112. //scannerNetworks.begin();
  113. //wdt_enable(WDTO_8S);
  114. Serial.println("attesa evento");
  115. Temp = millis();
  116. tone(PIN_BUZZER, 100);
  117. delay(100);
  118. noTone(PIN_BUZZER);
  119.  
  120. //delay(1000);
  121. }
  122. void stampa_seriale() {
  123.  
  124. //stampo stato chiamate
  125. if (telefono != old_telefono) {
  126. Serial.print(F(" case telefono N. "));
  127. Serial.println(telefono);
  128. old_telefono = telefono;
  129. }
  130. //stampo stato sirena
  131. if (fase1 != old_fase) {
  132. Serial.print(F("fase1 ")); Serial.print(fase1);
  133. Serial.print(F("sirena ")); Serial.println(sirena);
  134. old_fase = fase1;
  135. }
  136. if (range != old_range) {
  137. Serial.print(F("toni_range ")); Serial.print(range);
  138. old_range = range;
  139. }
  140.  
  141. if ( allarme != old_allarme ) {
  142. TempoInizio = millis();
  143. range = 0;
  144. if (allarme == 1) {
  145. Serial.println(F("attivo oooooooooo"));
  146. }
  147. if (allarme == 0) {
  148. Serial.println(F("disattivo oooooooooo"));
  149. }
  150. old_allarme = allarme;
  151. }
  152.  
  153. }
  154. void loop() {
  155. stampa_seriale();
  156.  
  157. if (fronte) {
  158. if (total_reset == 0 && all == 1) {// se e' attivo
  159. Serial.print(" C "); //mi scrive lonta
  160. Serial.print(conta);
  161. }
  162. if (total_reset == 1 && flag == 2) { //&& conta != 0) {
  163. if (conta != 0) {
  164. Serial.print("_"); //se modalita reset attiva e non
  165. } // sincronizzato
  166. if ( conta == 0) {
  167. Serial.print("*");//se modalita reset attiva e
  168. } // sincronizzato
  169. }
  170. if (!primoGiro) conta = (now - tm < 2000) ? conta + 1 : 0;
  171. primoGiro = 0;
  172. tm = now;
  173. }
  174.  
  175.  
  176. if (millis() - Temp > 21600000) {
  177. telefono = 5;
  178. Serial.print(F(" ''''''''''' "));
  179. Temp = millis();
  180. }
  181. now = millis();
  182. sensor_led = analogRead(A3) > 500;
  183. fronte = sensor_led && !old_sensor_led;
  184. old_sensor_led = sensor_led;
  185.  
  186.  
  187. if (digitalRead(PULSANTE) == LOW && old == HIGH)
  188. {
  189. TempoInizio = millis();
  190. range = 0;
  191. old != puls;
  192. }
  193.  
  194. if (PIN_SENSORE == 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