Skip to content

Instantly share code, notes, and snippets.

@jdevoo
Created September 24, 2012 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdevoo/3775189 to your computer and use it in GitHub Desktop.
Save jdevoo/3775189 to your computer and use it in GitHub Desktop.
SMS_Gateway
#include <Ethernet.h>
#include <EthernetDHCP.h>
#include <GSM.h>
#include <ctype.h>
long last_poll = 0;
long poll_interval = 0;
byte server[] = { 192, 168, 1, 100 };
GSM telit;
void setup()
{
byte mac[] = { 0xDA, 0xAD, 0xBE, 0xAA, 0xFE, 0x4D };
EthernetDHCP.begin(mac);
telit.InitSerLine(115200);
telit.TurnOn();
while (!telit.IsRegistered()) {
telit.CheckRegistration();
delay(1000);
}
}
void loop()
{
Client client(server, 8080);
byte pos;
char sender[20];
char text[140];
unsigned int res;
boolean await_reply = false;
if ((millis() - last_poll) > poll_interval) {
last_poll = millis();
poll_interval = 10000;
if (telit.IsRegistered() && (pos = telit.IsSMSPresent(SMS_ALL))) {
telit.GetSMS(pos, sender, text, 140);
client.connect();
await_reply = postToServer(client, sender, text);
telit.DeleteSMS(pos);
}
}
if (await_reply) {
res = serverResponse(client);
client.stop();
await_reply = false;
}
EthernetDHCP.maintain();
}
boolean postToServer(Client client, char *phone_no, char *sms_text)
{
char *m = urlEncode(sms_text);
boolean await_reply = false;
if (client.connected()) {
client.print("GET /frontlinesms/?key=S6JL4ZSM&s=");
client.print(phone_no);
client.print("&m=");
client.print(m);
client.println(" HTTP/1.1");
client.println("Host: change.this.host");
client.println("User-Agent: Arduino");
client.println("Accept: text/html");
client.println();
await_reply = true;
}
free(m);
return await_reply;
}
unsigned int serverResponse(Client client)
{
while (client.available()) {
char c = client.read();
}
return 200;
}
char *urlEncode(char *str) {
char *pstr = str, *buf = (char *) malloc(strlen(str) * 3 + 1), *pbuf = buf;
while (*pstr) {
if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~')
*pbuf++ = *pstr;
else if (*pstr == ' ')
*pbuf++ = '+';
else
*pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15);
pstr++;
}
*pbuf = '\0';
return buf;
}
char to_hex(char code) {
static char hex[] = "0123456789abcdef";
return hex[code & 15];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment