Advertisement
pippero

Untitled

Apr 16th, 2021
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.56 KB | None | 0 0
  1. //const int 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. unsigned long TempoInizio = 0;
  10.  
  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. unsigned long T_on= 100;
  22. unsigned long 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. fronte = sensor_led && !old_sensor_led;
  183. old_sensor_led = sensor_led;
  184.  
  185. //SENSORE= digitalRead(5) ;
  186. if (digitalRead(PULSANTE) == LOW && old == HIGH)
  187. {
  188. TempoInizio = millis();
  189. range = 0;
  190. old != puls;
  191. }
  192.  
  193. if (digitalRead(5) == HIGH ) {
  194. digitalWrite(LED_VERDE, HIGH); //verde
  195. Serial.print(" movimento rilevato ");
  196.  
  197. if (allarme == 1) {
  198. sensore = 1;
  199. ciclo_sirena = 1;
  200. presenza = 1;
  201. }
  202. else {
  203. presenza = 0;
  204. }
  205. }
  206. else {
  207. digitalWrite(LED_VERDE, LOW);//verde
  208. sensore = 0;
  209. }
  210.  
  211. if ( sensore && allarme == 1) {
  212. ciclo_sirena = 1;
  213. presenza = 1;
  214. }
  215. else {
  216. presenza = 0;
  217. }
  218.  
  219.  
  220. switch (allarme) {
  221. case 0:
  222. digitalWrite(LED_ROSSO, LOW);//rosso
  223. break;
  224. case 1:
  225. digitalWrite(LED_ROSSO, HIGH);//rosso
  226. break;
  227. }
  228. switch (range)
  229. {
  230. case 0: // your hand is on the sensor
  231. tone(PIN_BUZZER, 400);
  232. if ( millis() - TempoInizio > 300)
  233. {
  234. TempoInizio = millis();
  235. range = 1;
  236. }
  237. break;
  238.  
  239. case 1: // your hand is close to the sensor
  240. tone(PIN_BUZZER, 600);
  241. if ( millis() - TempoInizio > 300)
  242. {
  243. TempoInizio = millis();
  244. range = 2;
  245. }
  246. break;
  247.  
  248. case 2:
  249. tone(PIN_BUZZER, 800);
  250. if ( millis() - TempoInizio > 300)
  251. {
  252. TempoInizio = millis();
  253. range = 3;
  254. }
  255. break;
  256.  
  257. case 3:
  258. tone(PIN_BUZZER, 1000);
  259. if ( millis() - TempoInizio > 800)
  260. {
  261. TempoInizio = 0;
  262. range = 4;
  263. }
  264. break;
  265.  
  266. case 4: // your hand is nowhere near the sensor
  267. noTone(PIN_BUZZER);
  268. break;
  269. }
  270.  
  271.  
  272. if (total_reset == 0) {
  273.  
  274. switch (fase1) {
  275. case 0:
  276. digitalWrite(LED_GIALLO, LOW);//giallo
  277. noTone(PIN_BUZZER);
  278. break;
  279. case 1:
  280. digitalWrite(LED_GIALLO, HIGH);//giallo
  281. tone(PIN_BUZZER, 440);
  282. break;
  283. }
  284. //programma on off sirena
  285. if (ciclo_sirena == 1) { //
  286. if ( sirena <= 10) { //se e stato ripetuto il loop
  287. dt = millis() - t0; // qui creo lampeggio
  288. if (dt >= stp) { //con tempo on off
  289. if (fase1 == 0) {
  290. stp = T_on;
  291. fase1 = 1;
  292. } else {
  293. stp = T_off;
  294. fase1 = 0;
  295. sirena++;
  296. //digitalWrite(10, LOW);
  297. }
  298. t0 = millis();
  299. }
  300. }
  301. if (sirena == 11) {// se e' 11 ripetizione metto tempo pausa
  302. passaggi++; //variabile ripetizione sequenza sirena
  303. timeOfLastLedEvent = millis();
  304. //digitalWrite(10, LOW);
  305. Serial.println(F("PAUSA"));
  306. sirena = 12 ;
  307. }
  308. if (millis() - timeOfLastLedEvent > 5000 && sirena == 12) {
  309. Serial.println(" Ripetizione"); //aspetto tempo e poi
  310. Serial.print("passaggi "); //riparte sequenza sirena
  311. Serial.println(passaggi);
  312. sirena = 0;
  313. }
  314. if (passaggi >= 3) {// se e' la 3 ripetizione fermo il loop
  315. Serial.println("fine sirena");
  316. passaggi = 0;
  317. ciclo_sirena = 0 ;
  318. sirena = 0;
  319. }
  320. }
  321. switch (vcs.getvoiceCallStatus()) {
  322. case CALLING: {// This should never happen, as we are not placing a call
  323. digitalWrite(LED_BLU, HIGH);//blu
  324. }
  325. break;
  326. case IDLE_CALL: { // Nothing is happening
  327. digitalWrite(LED_BLU, LOW);//blu
  328. stato_chiamata = 1;
  329. break;
  330. }
  331. case RECEIVINGCALL: // Yes! Someone is calling us
  332. stato_chiamata = 0;
  333. digitalWrite(LED_BLU, HIGH);//blu
  334. Serial.println(" ");
  335. Serial.println(F("RECEIVING CALL"));
  336. // Retrieve the calling number
  337. vcs.retrieveCallingNumber(numtel, 20);
  338. if ((strcmp(numtel, "+390185358719") == 0)
  339. || (strcmp(numtel, "+393474212805") == 0))
  340. {
  341. if (strcmp(numtel, "+390185358719") == 0) { //+393479139527
  342. telefono = 1;
  343. }
  344. if (strcmp(numtel, "+393474212805") == 0) {
  345. telefono = 40;//aggiunto else if al posto di if
  346. }
  347. }
  348. else
  349. Serial.println(F(" => non riconosciuto!"));
  350. delay(150);//evita la risposta tim che e' spento
  351. vcs.hangCall();
  352. }
  353.  
  354. if (conta >= 20 || (now - tm > 20000)) {
  355. if (conta >= 20) {
  356. conta = 1;
  357. Serial.println (F(""));
  358. Serial.println("conta >= 5");
  359. }
  360. if (now - tm > 10000) {
  361. Serial.println(F("now-tm>"));
  362. }
  363. total_reset = 1;// avvio procedura reset
  364. flag = 0 ;
  365. }
  366. }
  367.  
  368. if (total_reset == 1 ) {
  369. //noTone(7);
  370. if (flag == 0 ) {//azioni da fare solo ina volta
  371. digitalWrite(LED_VERDE, LOW); //verde
  372. digitalWrite(LED_ROSSO, LOW); //rosso
  373. digitalWrite(LED_BLU, LOW); //blu
  374. digitalWrite(LED_GIALLO, LOW); //giallo
  375. allarme = 0;//variabile stato allarme disattivata
  376. T_pertenza = millis();
  377. notConnected = true;
  378. Serial.println(F(" reset SISTEMA"));
  379. //digitalWrite(LED_VERDE, HIGH); //led esterno verde
  380. digitalWrite(PIN_RESET_SIM, HIGH);
  381. flag = 1;
  382. }
  383. if (sensor_led == 1 ) {//lampeggia come led sim durante il reset
  384. digitalWrite(LED_ROSSO, !digitalRead(LED_ROSSO)); //giallo
  385. }
  386. if (millis() - T_pertenza > 1000 && flag == 1) {
  387. //if (flag == 1 ) {
  388. Serial.println(F(" RIAVVIO"));
  389. digitalWrite(PIN_RESET_SIM, LOW);
  390. digitalWrite(LED_VERDE, HIGH); //led esterno
  391. Serial.println(F("reset ON"));
  392. flag = 2;
  393. }
  394. // tempo di avvio da reset
  395. if (millis() - T_pertenza > 30000 && flag == 2) { //dopo 20 secondi
  396. // if (flag == 2 ) { //se
  397. Serial.println(F( " ")); //non si e'
  398. Serial.println(F("connessione gsm "));
  399. digitalWrite(LED_BLU, HIGH);
  400. if ( conta != 0) { // stabilizzato il
  401. Serial.println(F( " reset conta"));//lampeggio
  402. flag = 0;//nuova procedura reset //lento
  403. }
  404. if ( conta == 0) { //invece se il lampeggio
  405. // Serial.println (F("\r\n" "seriale""));//e' stabile
  406. Serial.println (F("seriale"));
  407.  
  408. //GSM_SMS sms;
  409. //GSM gsmAccess;
  410. //GSMVoiceCall vcs;
  411. //gsmAccess.begin() ;
  412. //notConnected = true;
  413. Serial.println(F(" bbb "));
  414. // wdt_reset();
  415. flag = 3;
  416. }
  417. //}
  418. }
  419.  
  420. if (flag == 3 ) { //oltre il tempo si resetta
  421. //if (millis() - T_pertenza > 40000) {
  422.  
  423. //Serial.println(F(" reset inizializzazione sim "));
  424. //flag = 0;
  425. //}
  426.  
  427. while (notConnected) { //attendo la connessione
  428. Serial.println(F("."));
  429. if (gsmAccess.begin() == GSM_READY) {
  430. notConnected = false;
  431. Serial.println(F(" GSM_READY avvio"));
  432. tone(PIN_BUZZER, 1200);
  433. delay(250);
  434. //tone(PIN_BUZZER, 1600);
  435. //delay(250);
  436. //tone(PIN_BUZZER, 2000);
  437. digitalWrite(LED_GIALLO, HIGH); //led esterno
  438. delay(1000);
  439. noTone(PIN_BUZZER);
  440. allarme = 1;//variabile stato allarme attivata
  441. conta = 1;
  442. primoGiro = 1;
  443. now = millis();
  444. tm = millis();
  445. vcs.hangCall();
  446. total_reset = 0;
  447. flag = 0;
  448.  
  449. } else {
  450. Serial.println(F("Not connected"));
  451. notConnected = false;
  452. delay(1000);
  453. total_reset = 1;
  454. flag = 0;
  455. }
  456. }
  457. range = 99;
  458. digitalWrite(LED_VERDE, LOW); //giallo
  459. digitalWrite(LED_ROSSO, LOW); //giallo
  460. digitalWrite(LED_BLU, LOW); //giallo
  461. digitalWrite(LED_GIALLO, LOW); //giallo
  462. }
  463. // wdt_reset();
  464.  
  465. }
  466.  
  467. if ( presenza == 1 && all == 1) {
  468. Serial.println(F(""));
  469. Serial.println(F("chiamate per allarme"));
  470. vcs.hangCall(); //serve per far cadere la chiamata
  471. all = 0;
  472. fase = 1;
  473. }
  474. if ( total_reset == 0 && all == 0 && stato_chiamata == 1) { //__________ da qui____________
  475.  
  476. //telefono = 0;
  477. switch (fase) {
  478.  
  479. case 1: {
  480. N_call_allarme++;// incremento chiamate
  481. Serial.println(F(" 1 chiamo daniele"));
  482. vcs.voiceCall("+393474212805", 500);
  483. Serial.println(F("codice inviato a sim900l chiamo"));
  484. Tempo_chiamata_allarme = millis();
  485. fase = 2;
  486.  
  487. }
  488. break;
  489. case 2: {
  490. if (millis() - Tempo_chiamata_allarme > 20000) {
  491. Serial.println(F(" 2 fine chiamata daniele"));
  492. vcs.hangCall(); //serve per far cadere la chiamata
  493. Serial.println(F("codice inviato a sim900l riaggancio"));
  494. fase = 3;
  495. Tempo_chiamata_allarme = millis();
  496. }
  497. }
  498. break;
  499. case 3: {
  500. if (millis() - Tempo_chiamata_allarme > 1500) {
  501. Serial.println(F(" 3 chiamo olindo"));
  502. //vcs.voiceCall("+393479139527", 5000);//+393474212805
  503. Serial.println(F("codice inviato a sim900l chiamo"));
  504. Tempo_chiamata_allarme = millis();
  505. fase = 4;
  506. //}
  507. }
  508. }
  509. break;
  510. case 4: {
  511. if (millis() - Tempo_chiamata_allarme > 7000) {
  512. Tempo_chiamata_allarme = millis();
  513. Serial.println(F("4 fine chiamata olindo"));
  514. vcs.hangCall(); //serve per far cadere la chiamata
  515. Serial.println(F("codice inviato a sim900l riaggancio"));
  516. fase = 5;
  517. }
  518. }
  519. break;
  520. case 5: {
  521. if (millis() - Tempo_chiamata_allarme > 1000) {//valuto il numero di
  522. Serial.print(F("N_call_allarme N. "));//volte che ho
  523. Serial.println(N_call_allarme);//ripetuto il swich case
  524. if (N_call_allarme <= 3) { // fase
  525. Serial.println(F("ripeto fasi"));
  526. fase = 1;
  527. }
  528. else {
  529. fase = 0; // se e' superiore esco dallo
  530. N_call_allarme = 0;//swich case
  531. all = 1;
  532. Serial.println(F("fine fasi"));
  533. }
  534. }
  535. }
  536. break;
  537. }
  538. }
  539. if ( total_reset == 0 && all == 1 && stato_chiamata == 1) {
  540. //if (fase != 0) {
  541. //presenza = 0 ;
  542. // }
  543. switch (telefono) {
  544. case 1: {
  545. allarme = !allarme;
  546. Serial.println(F("cambio stato allarme da casa "));
  547. Serial.print(F("nuovo stato = "));
  548. Serial.println(allarme);
  549. //vcs.hangCall(); //serve per far cadere la chiamata
  550. Serial.println(F("riaggancio casa"));
  551. Tempo_chiamata_stato = millis();
  552.  
  553. if (allarme == 1) {
  554. telefono = 2;
  555. }
  556. if (allarme == 0) {
  557. telefono = 3;
  558. // }
  559. }
  560. }
  561. break;
  562. case 40: {
  563. Serial.println(F("cambio stato allarme dal cell "));
  564. Serial.print(F("vecchio stato = "));
  565. Serial.println(allarme);
  566. Tempo_chiamata_stato = millis();
  567. //vcs.hangCall();
  568. telefono = 4;
  569. }
  570. break;
  571. case 4: {
  572. if (millis() - Tempo_chiamata_stato > 2000) {
  573. vcs.hangCall();
  574. Serial.println(F("riaggancio cell"));
  575. allarme = !allarme;
  576. Serial.print(F("nuovo stato = "));
  577. Serial.println(allarme);
  578. //vcs.hangCall(); //serve per far cadere la chiamata
  579.  
  580. Tempo_chiamata_stato = millis();
  581. if (allarme == 1) {
  582. telefono = 5;
  583. }
  584. if (allarme == 0) {
  585. telefono = 6;
  586. }
  587. }
  588. }
  589. break;
  590. case 2: {
  591. if (millis() - Tempo_chiamata_stato > 2000) {
  592. Serial.println(F("chiamata casa attivato "));
  593. vcs.voiceCall("+390185358719", 500);//393479139527
  594. Serial.println(F("codice inviato a sim900l"));
  595. Tempo_chiamata_stato = millis();
  596. telefono = 20;
  597. }
  598. }
  599. break;
  600. case 3: {
  601. if (millis() - Tempo_chiamata_stato > 2000) {
  602. Serial.println(F("chiamata casa disattivato"));
  603. Tempo_chiamata_stato = millis();
  604. vcs.voiceCall("+390185358719", 1000);
  605. Serial.println(F("codice inviato a sim900l"));
  606. telefono = 21;
  607. }
  608. }
  609. break;
  610. case 20: {
  611. if (millis() - Tempo_chiamata_stato > 9000) {
  612. Tempo_chiamata_stato = millis();
  613. Serial.println(F("fine chiamata casa allarme attivato"));
  614. vcs.hangCall(); //serve per far cadere la chiamata
  615. Serial.println(F("chiamata stop"));
  616. telefono = 10;
  617. }
  618. }
  619. break;
  620. case 21: {
  621. if (millis() - Tempo_chiamata_stato > 5000) {
  622. Tempo_chiamata_stato = millis();
  623. Serial.println(F("chiamata casa allarme disattivato "));
  624. vcs.hangCall(); //serve per far cadere la chiamata
  625. Serial.println(F("chiamata stop"));
  626. telefono = 10;
  627. }
  628. }
  629. break;
  630. case 5: {
  631. if (millis() - Tempo_chiamata_stato > 2000) {
  632. Serial.println(F("chiamata cell attivato "));
  633. vcs.voiceCall("+393474212805", 500);
  634. Serial.println(F("codice inviato a sim900l"));
  635. Tempo_chiamata_stato = millis();
  636. telefono = 30;
  637. }
  638. }
  639. break;
  640. case 6: {
  641. if (millis() - Tempo_chiamata_stato > 2000) {
  642. Serial.println(F(""));
  643. Serial.println(F("chiamata cell allarme disattivato"));
  644. Tempo_chiamata_stato = millis();
  645. vcs.voiceCall("+393474212805", 500);
  646. Serial.println(F("codice inviato a sim900l"));
  647. telefono = 31;
  648. }
  649. }
  650. break;
  651. case 30: {//lunghezza squillo allarme attivato
  652. if (millis() - Tempo_chiamata_stato > 9500) {
  653. Tempo_chiamata_stato = millis();
  654. Serial.println(F(""));
  655. Serial.println(F("fine chiamata cell allarme attivato"));
  656. telefono = 10;
  657. }
  658. }
  659. break;
  660. case 31: {//lunghezza squillo allarme disattivato
  661. if (millis() - Tempo_chiamata_stato > 23000) {
  662. Tempo_chiamata_stato = millis();
  663. Serial.println(F(""));
  664. Serial.println(F("fine chiamata cell allarme disattivato"));
  665. telefono = 10;
  666. }
  667. }
  668. break;
  669. case 10: {
  670. vcs.hangCall(); //serve per far cadere la chiamata
  671. Serial.println(F("chiamata stop"));
  672. telefono = 0;
  673. //Tempo_chiamta_stato = 0; presenza = 0;
  674. }
  675. break;
  676. }
  677. // Check the status of the voice call
  678. }
  679. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement