Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.88 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()) {
  118. Serial.println("Key timeout");
  119. keyBufferPnt = 0;
  120. }
  121. else{
  122. if(toneNum != -1){
  123. resetCodeTimeout();
  124. Serial.print("Tone detected:");Serial.println(toneNum);
  125. if(keyBufferPnt < keyBufferLen){
  126. keyBuffer[keyBufferPnt] = toneNum;
  127. keyBufferPnt++;
  128. }
  129. if(keyBufferPnt>= keyBufferLen){
  130. if(checkPass())correctPass();
  131. else badPass();
  132. }
  133. }
  134. }
  135. }
  136.  
  137. vcs.hangCall();
  138. Serial.println("HANG. Waiting Call.");
  139. break;
  140. }
  141. delay(1000);
  142. }
  143.  
  144. int detectTone(uint8_t* sum , uint8_t len, const char* strSearch){
  145. char c = theGSM3ShieldV1ModemCore.theBuffer().read();
  146. *sum = (c==strSearch[*sum]) ? *sum+1 : 0;
  147. if(*sum == len){
  148. char cc[2];
  149. cc[0] = (char)theGSM3ShieldV1ModemCore.theBuffer().read();
  150. cc[1] = (char)theGSM3ShieldV1ModemCore.theBuffer().read();
  151. int n;
  152. n = atoi(cc);
  153. *sum = 0;
  154. return n - 48;
  155. }
  156. return -1;
  157. }
  158.  
  159. bool isCodeTimedout(){
  160. if(codeTimeOutStarted){
  161. if( (unsigned long) millis() - lastCodeMillis > codeTimeOutInMillis){
  162. codeTimeOutStarted = false;
  163. return true;
  164. }
  165. }
  166. return false;
  167. }
  168.  
  169. void resetCodeTimeout(){
  170. lastCodeMillis = millis();
  171. codeTimeOutStarted = true;
  172. }
  173.  
  174. bool checkPass(){
  175. codeTimeOutStarted = false;
  176. for(int i = 0; i < passLen; i++){
  177. if(passCode[i] != keyBuffer[i]) return false;
  178. }
  179. return true;
  180. }
  181.  
  182. void correctPass(){
  183. Serial.println("Correct pass");
  184. keyBufferPnt = 0;
  185. theGSM3ShieldV1ModemCore.genericCommand_rqc("AT+QWDTMF=7, 0, \"G, 500, 500\"", true);
  186. runCommand(keyBuffer[4]);
  187. }
  188.  
  189. void badPass(){
  190. Serial.println("Wrong pass");
  191. keyBufferPnt = 0;
  192. theGSM3ShieldV1ModemCore.genericCommand_rqc("AT+QWDTMF=7, 0, \"G, 300, 300, G, 300, 300\"", true);
  193. }
  194.  
  195. void runCommand(int commandCode){
  196. switch (commandCode) {
  197. case ARM_COMMAND:
  198. armCom();
  199. break;
  200. case DISARM_COMMAND:
  201. disarmCom();
  202. break;
  203. case STATUS_COMMAND:
  204. statusCom();
  205. break;
  206. default:
  207. Serial.println("Uknown command");
  208. break;
  209. }
  210. }
  211.  
  212. void armCom(){
  213. isArmed = true;
  214. Serial.println("Armed");
  215. }
  216.  
  217. void disarmCom(){
  218. isArmed = false;
  219. Serial.println("Disarmed");
  220. }
  221.  
  222. void statusCom(){
  223. if(isArmed) Serial.println("Is armed");
  224. else Serial.println("Is disarmed");
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement