I've recently started working for a charity, and was asked if we could send information by text as a lot of our supporters aren't online.
I remembered I had a GSM Playground shield tucked away in the shed and set about making a web to SMS gateway so I can automate texts using a web server.
I have an SQL database that houses a table containing phone number, content, target datetime, and sent status. A simple webpage shows the phone number and the contents (using line breaks as separators) of the next message to send when asked for it. The arduino polls this page every three seconds.
/*
Reads and sends SMS as instructed via WEB
by CowJam
*/
#include <GSM.h>
#include <SPI.h>
#include <Ethernet.h>
//network stuff
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "SERVERADDRESS.COM"; // use your web server
String tel = "";
String msg = "";
char cTel[20] = "";
char cMsg[99] = "";
byte dumpLines = 0;
char c = "";
EthernetClient client;
// variables used for timing
unsigned long previous_timer;
byte timer100msec;
int val;
char string[30];
void setup()
{
// initialization of serial line
gsm.InitSerLine(57600);
// turn on GSM module
gsm.TurnOn();
// periodic timer initialization
timer100msec = 0;
previous_timer = millis();
// we are not registered so far => disable button
gsm.DisableUserButton();
Ethernet.begin(mac);
delay(1000);
}
void sendMessage() {
gsm.DisableUserButton();
gsm.TurnOffLED();
//Grab the web data
if (client.connect(server, 80)) {
client.println("GET /sms.php?A=pop HTTP/1.1");
client.println("Host: YOURSERVER.COM");
client.println("Connection: close");
client.println();
}
//process the data
while (client.connected()) {
while (dumpLines < 8) { //dump unwanted lines, there are 8 at the top.
while (client.read() != '\n') { }
dumpLines++;
}
c = client.read();
while (c != '\n') { //the next line is the phone number
if((c>=48) && (c<=57)) { tel = tel + c; }
c = client.read();
}
c = client.read();
while (c != '\n') { //this is the message to send
msg = msg + c;
c = client.read();
}
client.flush();
}
tel.toCharArray(cTel, 20);
msg.toCharArray(cMsg, 60);
client.flush();
client.stop();
if (tel.length() > 9) {
gsm.SendSMS(cTel, cMsg);
}
dumpLines = 0;
tel = "";
msg = "";
gsm.EnableUserButton();
gsm.TurnOnLED();
}
void loop()
{
if ((unsigned long)(millis() - previous_timer) >= 100) {
previous_timer = millis();
//*******************************
//****** EVERY 100msec. *********
//*******************************
if (gsm.IsUserButtonEnable() && gsm.IsUserButtonPushed()) {
//When the button is pressed it sends the current temperature to the number specified
//so you can confirm it's working
gsm.DisableUserButton();
gsm.TurnOffLED();
// read temperature
val = gsm.GetTemp();
if (val > -1000) {
// prepare a string which will be send by the SMS:
// "Temperature: 25 C"
sprintf(string, "Temperature: %i C", val/10);
gsm.SendSMS("1324", string);
}
}
//*******************************
//****** EVERY 1 sec. ***********
// %10 means every 1000 msec. = 1sec.
//*******************************
if ((timer100msec+3) % 10 == 0) {
// is the GSM module registered?
gsm.CheckRegistration();
if (gsm.IsRegistered()) {
// GSM modul is still registered
gsm.EnableUserButton();
gsm.TurnOnLED();
}
else {
// not registered - so disable button
gsm.DisableUserButton();
gsm.TurnOffLED();
}
}
//*******************************
//****** EVERY 3 sec. ***********
// %30 means every 3000 msec. = 3sec.
//*******************************
if ((timer100msec+4) % 30 == 0) {
sendMessage();
}
timer100msec = (timer100msec + 1) % 100;
}
}