So I just bought a UNO and a GPRS/GSM shield, and I'm trying to figure out how to code the arduino so that when I press a button, a text message is sent to a specific number. Arduino is all new to me, and here is the code I have so far based on a bit of researching:
#include <SoftwareSerial.h>
#include <String.h>
SoftwareSerial mySerial(7,8);
//button pin
int buttonPin = 3;
void setup()
{
mySerial.begin(19200); // the GPRS baud rate
Serial.begin(19200); // the GPRS baud rate
delay(500);
}
void loop(){
//If button is pressed, send text message
int buttonVal = digitalRead(buttonPin);
if(buttonVal == HIGH){
SendTextMessage();
delay(500);
}
//Send text message
void SendTextMessage()
{
mySerial.print("AT+CMGF=1\r"); //Because we want to send the SMS in text mode
delay(100);
mySerial.println("AT+CMGS=\"+44##########\""); //send sms message, insert your own phone number including the country code
delay(100);
mySerial.print("Test message from Arduino");//the content of the message
delay(100);
mySerial.print((char)26);//the ASCII code of the ctrl+z is 26
delay(100);
mySerial.println();
}
I have replaced my number with hashes obviously, but should this code work?
Thanks
So, what does the code actually do? The GSM chip returns a response to every AT command. It seems a shame not to read what the chip replies with. Might provide a clue.
PaulS, thanks, how can I read what the GSM chip is replying? It's a SIM900 chip.
The code is supposed to connect to the network and wait for the button press, where it then sends the text. Afterwards it will stay connected and wait for the button to be pressed again
I changed the code; I copied the code from this instructable:http://www.instructables.com/id/Mailbox-Phone-Alert/?ALLSTEPS and deleted the parts that refer to the light sensor and rearranged it so that a text is sent at the press of a button. It does not work still.
#include <SoftwareSerial.h>
#include <String.h>
SoftwareSerial mySerial(7,8);
//button pin
int buttonPin = 3;
void setup()
{
mySerial.begin(19200); // the GPRS baud rate
Serial.begin(19200); // the GPRS baud rate
delay(500);
}
void loop(){
int buttonVal = digitalRead(buttonPin);
if(buttonVal == HIGH)
SendTextMessage();
delay(500);
}
//Send a text message
void SendTextMessage()
{
mySerial.print("AT+CMGF=1\r"); //Because we want to send the SMS in text mode
delay(100);
mySerial.println("AT+CMGS=\"+############\""); //send sms message, insert your own phone number including the country code
delay(100);
mySerial.print("You've got mail!");//the content of the message
delay(100);
mySerial.print((char)26);//the ASCII code of the ctrl+z is 26
delay(100);
mySerial.println();
}