Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- // Libraries (Ethernet and GSM)
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- #include <GSM.h>
- #include <Ethernet.h>
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- // GSM
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- #define PINNUMBER "0000" // Sim code for simcard
- // initialize the library instances
- GSM gsmAccess; // init gsm network access
- GSM_SMS sms; // init sms on gsm board
- char senderNumber[20]; // Senders number
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- // Ethernet
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x5A, 0x14 }; // Ethernet MAC address
- IPAddress ip(192, 168, 1, 100); // Arduino IP Address
- IPAddress myDns(192, 168, 1, 1); // Router IP
- EthernetClient client; // EthernetClient set to "client"
- IPAddress server(192,168,1,180); // Arduino Server IP address
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- // HTTP strings
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- String alarmON = "GET /?button5on"; // relay4 on
- String alarmOFF = "GET /?button5off"; // relay4 off
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- // Setup
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- void setup()
- {
- Serial.begin(9600); // serial to pc communication start
- delay(1000); // delay allowing ethernet to get ready
- Ethernet.begin(mac, ip, myDns); // Launching ethernet
- Serial.print("My IP address: ");
- Serial.println(Ethernet.localIP()); // printing IP address
- Serial.println("SMS Messages Receiver");
- boolean notConnected = true; // connection state
- // while GSM isn't connected do so
- while (notConnected)
- {
- if (gsmAccess.begin(PINNUMBER) == GSM_READY) { //PIN correct and GSM_ready
- notConnected = false;
- } else {
- Serial.println("Not connected");
- delay(1000);
- }
- }
- Serial.println("GSM initialized");
- Serial.println("Waiting for messages");
- }
- void loop() {
- char c;
- if (sms.available()) // If there are any SMSs available run
- {
- Serial.println("Message received from:");
- // Get remote number
- sms.remoteNumber(senderNumber, 20); // Accessing senders number via lib function
- Serial.println(senderNumber);
- if (sms.peek() == '#') { // if first char is "#" the message is destroyed
- Serial.println("Discarded SMS");
- sms.flush();
- }
- // Read message bytes and print them
- String messageR = "";
- while (c = sms.read()) // read message to variable is any char's are readable from sms
- {
- //Serial.print(c);
- messageR += c;
- }
- Serial.println("");
- Serial.println("Message:");
- Serial.println(messageR); // printing message to serial
- Serial.println("Senders number:");
- Serial.println(senderNumber); // printing senders number
- // Return message to sender if alarm is activated or deactivated,
- if(messageR == "alarm off"){
- //Serial.println("Alarm deactivated by SMS");
- sms.beginSMS(senderNumber); // begin sms to senders numver
- sms.print("Alarm deactivated"); // text back to sender
- sms.endSMS(); // end and send message to sender
- httpRequest(alarmOFF); // set alarm off on server via http request function
- }
- // opposite of function just above, just to send alarm on message
- if(messageR == "alarm on"){
- //Serial.println("Alarm activated by SMS");
- sms.beginSMS(senderNumber);
- sms.print("Alarm activated");
- sms.endSMS();
- httpRequest(alarmON);
- }
- messageR = "";
- // Serial.println("\nEND OF MESSAGE");
- // Delete message from modem memory
- sms.flush(); // delete sms
- // Serial.println("MESSAGE DELETED");
- }
- delay(1000); // wait for GSM module to be done
- } // loop ends
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- // httpRequest() - function to call http request on server
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- void httpRequest(String cmd)
- {
- client.stop(); // closing ealier clients to make sure a socket is ready
- if (client.connect(server, 80)) // if there's a successful connection
- {
- client.println(cmd); // cmd variable based on the string given at http request function call
- client.println("Connection: close"); // closing connection
- client.println();
- Serial.println("Http request send:");
- Serial.println(cmd);
- } else {
- Serial.println("connection failed"); // if NO connection
- }
- } // httpRequest function end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement