Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. #include <GSM.h>
  2.  
  3. // PIN Number
  4. #define PINNUMBER ""
  5.  
  6. // initialize the library instance
  7. GSM gsmAccess; // include a 'true' parameter for debug enabled
  8. GSMVoiceCall vcs;
  9. uint8_t passPnt = 0;
  10. uint8_t passLen = 4;
  11. uint8_t passCode[] = {1,2,3,4};
  12. uint8_t passBuffer[4];
  13. char numtel[20]; // buffer for the incoming call
  14. uint16_t codeTimeOutInMillis = 2000;
  15. unsigned long lastCodeMillis = 0;
  16. bool codeTimeOutStarted = false;
  17.  
  18. void setup()
  19. {
  20. // initialize serial communications
  21. Serial.begin(9600);
  22. Serial.println("Receive Voice Call");
  23.  
  24. // connection state
  25. boolean notConnected = true;
  26.  
  27. // Start GSM shield
  28. // If your SIM has PIN, pass it as a parameter of begin() in quotes
  29. while(notConnected)
  30. {
  31. if(gsmAccess.begin(PINNUMBER)==GSM_READY)
  32. notConnected = false;
  33. else
  34. {
  35. Serial.println("Not connected");
  36. delay(1000);
  37. }
  38. }
  39.  
  40. //Enable dtmf
  41. theGSM3ShieldV1ModemCore.genericCommand_rqc("AT+QTONEDET=1", true); // Send command
  42. bool resp;
  43. delay(1000); // Wait for response
  44. theGSM3ShieldV1ModemCore.genericParse_rsp(resp);
  45. if(resp){
  46. Serial.println("DTMF Enabled");
  47. }else{
  48. Serial.println("DTMF NOT Enabled");
  49. }
  50. //Enable tone only
  51. theGSM3ShieldV1ModemCore.genericCommand_rqc("AT+QULDLSPH=0,0", true); // Send command
  52. delay(1000); // Wait for response
  53. theGSM3ShieldV1ModemCore.genericParse_rsp(resp);
  54. if(resp){
  55. Serial.println("ToneOnly Enabled");
  56. }else{
  57. Serial.println("ToneOnly NOT Enabled");
  58. }
  59.  
  60. // This makes sure the modem notifies correctly incoming events
  61. vcs.hangCall();
  62.  
  63. Serial.println("Waiting Call");
  64. }
  65.  
  66. void loop()
  67. {
  68. // Check the status of the voice call
  69. switch (vcs.getvoiceCallStatus())
  70. {
  71. case IDLE_CALL: // Nothing is happening
  72.  
  73. break;
  74.  
  75. case CALLING: // This should never happen, as we are not placing a call
  76.  
  77. Serial.println("CALLING");
  78. break;
  79.  
  80. case RECEIVINGCALL: // Yes! Someone is calling us
  81.  
  82. Serial.println("RECEIVING CALL");
  83.  
  84. // Retrieve the calling number
  85. vcs.retrieveCallingNumber(numtel, 20);
  86.  
  87. // Print the calling number
  88. Serial.print("Number:");
  89. Serial.println(numtel);
  90.  
  91. // Answer the call, establish the call
  92. vcs.answerCall();
  93. break;
  94.  
  95. case TALKING: // In this case the call would be established
  96.  
  97. Serial.println("TALKING. Enter line to interrupt.");
  98. uint8_t charToneSum = 0;
  99. const char* strToneSearch = "QTONEDET: ";
  100. uint8_t len = strlen(strToneSearch);
  101.  
  102. while(Serial.read()!='\n'){
  103. int toneNum = detectTone(&charToneSum , len, strToneSearch);
  104. if (isCodeTimedout()) badPass();
  105. else{
  106. if(toneNum != -1){
  107. resetCodeTimeout();
  108. Serial.print("Tone detected:");Serial.println(toneNum);
  109. if(passPnt < 4){
  110. passBuffer[passPnt] = toneNum;
  111. passPnt++;
  112. }
  113. if(passPnt>= 4){
  114. passPnt = 0;
  115. if(checkPass()) {
  116. Serial.println("Correct pass");
  117. correctPass();
  118. }
  119. else {
  120. Serial.println("Wrong pass");
  121. badPass();
  122. }
  123. }
  124. }
  125. }
  126. }
  127.  
  128. vcs.hangCall();
  129. Serial.println("HANG. Waiting Call.");
  130. break;
  131. }
  132. delay(1000);
  133. }
  134.  
  135. int detectTone(uint8_t* sum , uint8_t len, const char* strSearch){
  136. char c = theGSM3ShieldV1ModemCore.theBuffer().read();
  137. *sum = (c==strSearch[*sum]) ? *sum+1 : 0;
  138. if(*sum == len){
  139. char cc[2];
  140. cc[0] = (char)theGSM3ShieldV1ModemCore.theBuffer().read();
  141. cc[1] = (char)theGSM3ShieldV1ModemCore.theBuffer().read();
  142. int n;
  143. n = atoi(cc);
  144. *sum = 0;
  145. return n - 48;
  146. }
  147. return -1;
  148. }
  149.  
  150. bool isCodeTimedout(){
  151. if(codeTimeOutStarted){
  152. if( (unsigned long) millis() - lastCodeMillis > codeTimeOutInMillis){
  153. codeTimeOutStarted = false;
  154. return true;
  155. }
  156. }
  157. return false;
  158. }
  159.  
  160. void resetCodeTimeout(){
  161. lastCodeMillis = millis();
  162. codeTimeOutStarted = true;
  163. }
  164.  
  165. bool checkPass(){
  166. for(int i = 0; i < passLen; i++){
  167. if(passCode[i] != passBuffer[i]) return false;
  168. }
  169. return true;
  170. }
  171.  
  172. void correctPass(){
  173. passPnt = 0;
  174. theGSM3ShieldV1ModemCore.genericCommand_rqc("AT+QWDTMF=7, 0, \"G, 500, 500\"", true);
  175. }
  176.  
  177. void badPass(){
  178. passPnt = 0;
  179. theGSM3ShieldV1ModemCore.genericCommand_rqc("AT+QWDTMF=7, 0, \"G, 300, 300, G, 300, 300\"", true);
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement