Advertisement
Guest User

Untitled

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