Advertisement
pippero

Untitled

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