I've been playing with the GSM shield and the sms functions. As some of you may remember a while ago I was having problems with my mega and the shield. paulellison helped me out considerably with identifying problems with the mega and the shield using different pins and also we found the power drain could be quite high during some functions. Anyway, I have been playing with some ideas, using incoming sms messages to trigger events and so far I have got the Uno I am using to receive a test and, depending on the first character of the text it will reply to the sender with an appropriate text message and, if the message starts with a '#' it will discard the text and let the sender know that this is what has happened.
This is going to be the starting code for something I am going to try to work on during our Arduino Day so I give you the basic code now and will let you see how it works tomorrow!
/* SMS receiver This sketch, for the Arduino GSM shield, waits for a SMS message and displays it through the Serial port. Then returns message to sender letting them know it has been received on Arduino Circuit: * GSM shield attached to and Arduino * SIM card that can receive SMS messages created 27 Mar 2015 by Mark Venn based on code by Javier Zorzano / TD & Tom Igoe */ // include the GSM library #include <GSM.h> // PIN Number for the SIM #define PINNUMBER "" // initialize the library instances GSM gsmAccess; GSM_SMS sms; // Array to hold the number a SMS is retreived from char senderNumber[20]; void setup() { // initialize serial communications and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo only } Serial.println("SMS Messages Receiver / Responder"); // connection state boolean notConnected = true; // Start GSM connection while (notConnected) { if (gsmAccess.begin(PINNUMBER) == 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 there are any SMSs available() if (sms.available()) { Serial.println("Message received from:"); // Get remote number sms.remoteNumber(senderNumber, 20); Serial.println(senderNumber); // An example of message disposal // Any messages starting with # should be discarded if (sms.peek() == '#') { Serial.println("Discarded SMS"); sms.flush(); Serial.println("\nSENDING REPLY"); sms.beginSMS(senderNumber); sms.print("Message discarded. (Began with #"); sms.endSMS(); Serial.println("\nCOMPLETE"); } else if (sms.peek() == '@') { Serial.println("\nSENDING REPLY"); sms.beginSMS(senderNumber); sms.print("Message began @"); sms.endSMS(); Serial.println("\nCOMPLETE"); } else if (sms.peek() == ';') { Serial.println("\nSENDING REPLY"); sms.beginSMS(senderNumber); sms.print("Message began ;"); sms.endSMS(); Serial.println("\nCOMPLETE"); } else if (sms.peek() == '!') { Serial.println("\nSENDING REPLY"); sms.beginSMS(senderNumber); sms.print("Message began !"); sms.endSMS(); Serial.println("\nCOMPLETE"); } else // Read message bytes and print them while (c = sms.read()) Serial.print(c); Serial.println("\nEND OF MESSAGE"); // return message to sms sender sms.beginSMS(senderNumber); sms.print("Message Received!"); sms.endSMS(); Serial.println("\nCOMPLETE!\n"); // Delete message from modem memory sms.flush(); Serial.println("MESSAGE DELETED"); } delay(1000); }
I am thinking of trying case statements instead of the dreaded if else if else mess, this was just to try to get something working ready for tomorrow! We shall see how it goes! Going to have a good day I think, odion is going over with me, he is really getting in to Arduino and using python to do clever stuff depending on what is spat out of the serial port. Will get him to write something up sometime.
Top Comments