PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/bht/Arduino/libraries/GSM_Shield/examples/GSM_Shield_Test/GSM_Shield_Test.pde

https://bitbucket.org/DaveRobertson/beechhall
Processing | 250 lines | 191 code | 43 blank | 16 comment | 22 complexity | 6764c7054a64ce2ce0830b69717646aa MD5 | raw file
  1. /* GSM Shield example
  2. created 2011
  3. by Boris Landoni
  4. This example code is in the public domain.
  5. http://www.open-electronics.org
  6. http://www.futurashop.it
  7. */
  8. #include <SoftwareSerial.h>
  9. #include <GSM_Shield.h>
  10. //**************************************************************************
  11. char number[]="+39123456789"; //Destination number
  12. char text[]="hello world"; //SMS to send
  13. byte type_sms=SMS_UNREAD; //Type of SMS
  14. byte del_sms=0; //0: No deleting sms - 1: Deleting SMS
  15. //**************************************************************************
  16. GSM gsm;
  17. char sms_rx[122]; //Received text SMS
  18. //int inByte=0; //Number of byte received on serial port
  19. char number_incoming[20];
  20. int call;
  21. int error;
  22. void setup()
  23. {
  24. Serial.begin(9600);
  25. Serial.println("system startup");
  26. gsm.TurnOn(9600); //module power on
  27. gsm.InitParam(PARAM_SET_1);//configure the module
  28. gsm.Echo(0); //enable AT echo
  29. }
  30. void loop()
  31. {
  32. char inSerial[5];
  33. int i=0;
  34. delay(2000);
  35. Check_Call(); //Check if there is an incoming call
  36. Check_SMS(); //Check if there is SMS
  37. //Check data serial com
  38. if (Serial.available() > 0)
  39. {
  40. while (Serial.available() > 0) {
  41. inSerial[i]=(Serial.read()); //read data
  42. i++;
  43. }
  44. inSerial[i]='\0';
  45. Check_Protocol(inSerial);
  46. }
  47. }
  48. void Check_Protocol(String inStr)
  49. {
  50. Serial.print("Command: ");
  51. Serial.println(inStr);
  52. Serial.println("Check_Protocol");
  53. switch (inStr[0])
  54. {
  55. case 'a' : //Answer
  56. if (gsm.CallStatus()==CALL_INCOM_VOICE){
  57. gsm.PickUp();
  58. Serial.println("Answer");
  59. }
  60. else
  61. {
  62. Serial.println("No incoming call");
  63. }
  64. break;
  65. case 'c': // C //Call
  66. if (inStr.length()<2) //To call variable 'number' comand c
  67. {
  68. Serial.print("Calling ");
  69. Serial.println(number);
  70. gsm.Call(number);
  71. }
  72. if (inStr.length()==2) //To call number in phone book position comand cx where x is the SIM position
  73. {
  74. error=gsm.GetPhoneNumber(inStr[1],number);
  75. if (error!=0)
  76. {
  77. Serial.print("Calling ");
  78. Serial.println(number);
  79. gsm.Call(number);
  80. }
  81. else
  82. {
  83. Serial.print("No number in pos ");
  84. Serial.println(inStr[1]);
  85. }
  86. }
  87. break;
  88. case 'h': //H //HangUp if there is an incoming call
  89. if (gsm.CallStatus()!=CALL_NONE)
  90. {
  91. Serial.println("Hang");
  92. gsm.HangUp();
  93. }
  94. else
  95. {
  96. Serial.println("No incoming call");
  97. }
  98. break;
  99. case 's': //S //Send SMS
  100. Serial.print("Send SMS to ");
  101. Serial.println(number);
  102. error=gsm.SendSMS(number,text);
  103. if (error==0) //Check status
  104. {
  105. Serial.println("SMS ERROR \n");
  106. }
  107. else
  108. {
  109. Serial.println("SMS OK \n");
  110. }
  111. break;
  112. case 'p': //Read-Write Phone Book
  113. if (inStr.length()==3)
  114. {
  115. switch (inStr[1])
  116. {
  117. case 'd': //Delete number in specified position pd2
  118. error=gsm.DelPhoneNumber(inStr[2]);
  119. if (error!=0)
  120. {
  121. Serial.print("Phone number position ");
  122. Serial.print(inStr[2]);
  123. Serial.println(" deleted");
  124. }
  125. break;
  126. case 'g': //Read from Phone Book position pg2
  127. error=gsm.GetPhoneNumber(inStr[2],number);
  128. if (error!=0) //Find number in specified position
  129. {
  130. Serial.print("Phone Book position ");
  131. Serial.print(inStr[2]);
  132. Serial.print(": ");
  133. Serial.println(number);
  134. }
  135. else //Not find number in specified position
  136. {
  137. Serial.print("No Phone number in position ");
  138. Serial.println(inStr[2]);
  139. }
  140. break;
  141. case 'w': //Write from Phone Book Position pw2
  142. error=gsm.WritePhoneNumber(inStr[2],number);
  143. if (error!=0)
  144. {
  145. Serial.print("Number ");
  146. Serial.print(number);
  147. Serial.print(" writed in Phone Book position ");
  148. Serial.println(inStr[2]);
  149. }
  150. else Serial.println("Writing error");
  151. break;
  152. }
  153. }
  154. break;
  155. }
  156. delay(1500);
  157. return;
  158. }
  159. void Check_Call() //Check status call if this is available
  160. {
  161. call=gsm.CallStatus();
  162. switch (call)
  163. {
  164. case CALL_NONE:
  165. Serial.println("no call");
  166. break;
  167. case CALL_INCOM_VOICE:
  168. gsm.CallStatusWithAuth(number_incoming,0,0);
  169. Serial.print("incoming voice call from ");
  170. Serial.println(number_incoming);
  171. break;
  172. case CALL_ACTIVE_VOICE:
  173. Serial.println("active voice call");
  174. break;
  175. case CALL_NO_RESPONSE:
  176. Serial.println("no response");
  177. break;
  178. }
  179. return;
  180. }
  181. void Check_SMS() //Check if there is an sms 'type_sms'
  182. {
  183. char pos_sms_rx; //Received SMS position
  184. pos_sms_rx=gsm.IsSMSPresent(type_sms);
  185. if (pos_sms_rx!=0)
  186. {
  187. //Read text/number/position of sms
  188. gsm.GetSMS(pos_sms_rx,number_incoming,sms_rx,120);
  189. Serial.print("Received SMS from ");
  190. Serial.print(number_incoming);
  191. Serial.print("(sim position: ");
  192. Serial.print(word(pos_sms_rx));
  193. Serial.println(")");
  194. Serial.println(sms_rx);
  195. if (del_sms==1) //If 'del_sms' is 1, i delete sms
  196. {
  197. error=gsm.DeleteSMS(pos_sms_rx);
  198. if (error==1)Serial.println("SMS deleted");
  199. else Serial.println("SMS not deleted");
  200. }
  201. }
  202. return;
  203. }