Advertisement
Guest User

GSM

a guest
Jan 19th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.05 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////////////////////////////////
  2. // Libraries (Ethernet and GSM)
  3. ////////////////////////////////////////////////////////////////////////////////////////////////////
  4. #include <GSM.h>
  5. #include <Ethernet.h>
  6.  
  7. ////////////////////////////////////////////////////////////////////////////////////////////////////
  8. // GSM
  9. ////////////////////////////////////////////////////////////////////////////////////////////////////
  10. #define PINNUMBER "0000" // Sim code for simcard
  11.  
  12. // initialize the library instances
  13. GSM gsmAccess; // init gsm network access
  14. GSM_SMS sms; // init sms on gsm board
  15. char senderNumber[20]; // Senders number
  16.  
  17. ////////////////////////////////////////////////////////////////////////////////////////////////////
  18. // Ethernet
  19. ////////////////////////////////////////////////////////////////////////////////////////////////////
  20. byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x5A, 0x14 }; // Ethernet MAC address
  21. IPAddress ip(192, 168, 1, 100); // Arduino IP Address
  22. IPAddress myDns(192, 168, 1, 1); // Router IP
  23. EthernetClient client; // EthernetClient set to "client"
  24. IPAddress server(192,168,1,180); // Arduino Server IP address
  25.  
  26. ////////////////////////////////////////////////////////////////////////////////////////////////////
  27. // HTTP strings
  28. ////////////////////////////////////////////////////////////////////////////////////////////////////
  29. String alarmON = "GET /?button5on"; // relay4 on
  30. String alarmOFF = "GET /?button5off"; // relay4 off
  31.  
  32.  
  33. ////////////////////////////////////////////////////////////////////////////////////////////////////
  34. // Setup
  35. ////////////////////////////////////////////////////////////////////////////////////////////////////
  36. void setup()
  37. {
  38. Serial.begin(9600); // serial to pc communication start
  39.  
  40. delay(1000); // delay allowing ethernet to get ready
  41. Ethernet.begin(mac, ip, myDns); // Launching ethernet
  42. Serial.print("My IP address: ");
  43. Serial.println(Ethernet.localIP()); // printing IP address
  44.  
  45. Serial.println("SMS Messages Receiver");
  46. boolean notConnected = true; // connection state
  47.  
  48. // while GSM isn't connected do so
  49. while (notConnected)
  50. {
  51. if (gsmAccess.begin(PINNUMBER) == GSM_READY) { //PIN correct and GSM_ready
  52. notConnected = false;
  53. } else {
  54. Serial.println("Not connected");
  55. delay(1000);
  56. }
  57. }
  58.  
  59. Serial.println("GSM initialized");
  60. Serial.println("Waiting for messages");
  61. }
  62.  
  63. void loop() {
  64. char c;
  65.  
  66.  
  67. if (sms.available()) // If there are any SMSs available run
  68. {
  69. Serial.println("Message received from:");
  70.  
  71. // Get remote number
  72. sms.remoteNumber(senderNumber, 20); // Accessing senders number via lib function
  73. Serial.println(senderNumber);
  74.  
  75. if (sms.peek() == '#') { // if first char is "#" the message is destroyed
  76. Serial.println("Discarded SMS");
  77. sms.flush();
  78. }
  79.  
  80. // Read message bytes and print them
  81. String messageR = "";
  82. while (c = sms.read()) // read message to variable is any char's are readable from sms
  83. {
  84. //Serial.print(c);
  85. messageR += c;
  86. }
  87. Serial.println("");
  88. Serial.println("Message:");
  89. Serial.println(messageR); // printing message to serial
  90. Serial.println("Senders number:");
  91. Serial.println(senderNumber); // printing senders number
  92.  
  93.  
  94.  
  95. // Return message to sender if alarm is activated or deactivated,
  96. if(messageR == "alarm off"){
  97. //Serial.println("Alarm deactivated by SMS");
  98. sms.beginSMS(senderNumber); // begin sms to senders numver
  99. sms.print("Alarm deactivated"); // text back to sender
  100. sms.endSMS(); // end and send message to sender
  101. httpRequest(alarmOFF); // set alarm off on server via http request function
  102. }
  103.  
  104. // opposite of function just above, just to send alarm on message
  105. if(messageR == "alarm on"){
  106. //Serial.println("Alarm activated by SMS");
  107. sms.beginSMS(senderNumber);
  108. sms.print("Alarm activated");
  109. sms.endSMS();
  110. httpRequest(alarmON);
  111. }
  112.  
  113. messageR = "";
  114.  
  115. // Serial.println("\nEND OF MESSAGE");
  116.  
  117. // Delete message from modem memory
  118. sms.flush(); // delete sms
  119. // Serial.println("MESSAGE DELETED");
  120. }
  121.  
  122. delay(1000); // wait for GSM module to be done
  123.  
  124. } // loop ends
  125.  
  126. ////////////////////////////////////////////////////////////////////////////////////////////////////
  127. // httpRequest() - function to call http request on server
  128. ////////////////////////////////////////////////////////////////////////////////////////////////////
  129. void httpRequest(String cmd)
  130. {
  131. client.stop(); // closing ealier clients to make sure a socket is ready
  132.  
  133. if (client.connect(server, 80)) // if there's a successful connection
  134. {
  135. client.println(cmd); // cmd variable based on the string given at http request function call
  136. client.println("Connection: close"); // closing connection
  137. client.println();
  138. Serial.println("Http request send:");
  139. Serial.println(cmd);
  140. } else {
  141. Serial.println("connection failed"); // if NO connection
  142. }
  143. } // httpRequest function end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement