Archive

Archive for September, 2009

WiFi SMS Gateway

September 5th, 2009 10 comments

After a long search on the internet, trying to find a cheap SMS Gateway, I got the idea to create one myself.
As I already had a GM862 module laying around, and I’ve got the WiShield WiFi module for the Arduino too, I thought it would be easy – but it wasn’t!

The hardware setup is pretty easy, except the powering for the GM862, as it requires about 3.8V. That was made with a Linear LT1528 3A low dropout voltage regulator.
After alot of testing and measuring, I finally made a working circuit, and afterwards an Eagle Schematic.

WiFi SMS Gateway - Schematic

WiFi SMS Gateway - Schematic

� 

In the following picture you will see the hardware setup, which consists of the Arduino with the WiShield on the top, the GM862 on two breadboards, and alot of wires!

WiFi SMS Gateway - Hardware

WiFi SMS Gateway - Hardware

� 

But the real problems appeared when I started programming! I had alot of troubles with the serial connection between the Arduino and the GM862 module, which apparently wasn’t caused by the hardware setup!

� 
After alot of debugging I figured it out, and a simple SMS sending code was working 🙂
Then after a couple of hours I’ve added the WiFi parsing code which sends an SMS to the requested number, with the requested message, read from the GET parameters!

You can grab the code here, and try it out:

/*
 * WiFi SMS Gateway sketch by Thomas Jespersen - http://elec.tkjweb.dk/blog
 * --- Remeber to change the PIN, if any, in the setup() routine! ---
 */

#include <WiServer.h>

#define WIRELESS_MODE_INFRA	1
#define WIRELESS_MODE_ADHOC	2

// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {192,168,1,111};	// IP address of WiShield
unsigned char gateway_ip[] = {192,168,2,254};	// router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0};	// subnet mask for the local network
const prog_char ssid[] PROGMEM = {"SSID"};		// max 32 bytes


unsigned char security_type = 0;	// 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2

// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"Passphrase"};	// max 64 characters

// WEP 128-bit keys
// sample HEX keys
prog_uchar wep_keys[] PROGMEM = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,	// Key 0
				  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	// Key 1
				  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	// Key 2
				  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00	// Key 3
				};

// setup the wireless mode
// infrastructure - connect to AP
// adhoc - connect to another WiFi device
unsigned char wireless_mode = WIRELESS_MODE_INFRA;

unsigned char ssid_len;
unsigned char security_passphrase_len;
// End of wireless configuration parameters ----------------------------------------

//unsigned byte TimeBeforeConnection;

unsigned int lastSMStime = 0;

   char number[7];
   char* message;

void sendSMS() {
  if (millis()/1000 > lastSMStime) {
    Serial.print("AT+CMGF=1\r\n");
    delay(500);
    Serial.print("AT+CMGS=");
    delay(250);
    for (int i=0; i < 8; i++) {
      Serial.print(number[i]);
    }
    //Serial.print(number);
    // Replace with a valid phone number
    Serial.print("\r\n");
    delay(2500);
    Serial.print(message);
    delay(500);

    // End the SMS with a control-z
    Serial.print(0x1A,BYTE);
    lastSMStime = (millis()/1000) + 2;
  }
}

// This is our page serving function that generates web pages
boolean sendMyPage(char* URL) {

    // Check if the requested URL matches "/"
    if (strcmp(URL, "/") == 0) {
        // Use WiServer's print and println functions to write out the page content
        WiServer.print("<html>");
        WiServer.print("<head><title>TKJ's SMS Gateway</title>");
        WiServer.print("<h1>This is TKJ's SMS Gateway</h1><br>");
        WiServer.print("</html>");

        // URL was recognized
        return true;
    }

    // Check if the requested URL starts with "/?"
    if (URL[0] == '/' && URL[1] == '?') {
        for (int i=0; i<8; i++) {
          number[i] = '0';
        }
        message = "";

        for (int i=0; i < 8; i++) {
          number[i] = URL[i+2];
        }
        for (int i=0; i < strlen(URL); i++) {
          message[i] = URL[i+10];
        }

        // Use WiServer's print and println functions to write out the page content
        WiServer.print("<html>");
        WiServer.print("<head><title>TKJ's SMS Gateway</title>");
        WiServer.print("<h1>The SMS is now sent!</h1><br><p>");
        //WiServer.print(message);
        WiServer.print("</p>");
        WiServer.print("</html>");

        sendSMS();

        // URL was recognized
        return true;
    }

    // URL not found
    return false;
}

void setup() {
  // Turn GSM Module ON
  pinMode(3, OUTPUT);
  digitalWrite(3, HIGH);
  delay(500);
  digitalWrite(3, LOW);
  delay(500);
  digitalWrite(3, HIGH);
  delay(1000);

  // Type Mobile SIM Pin Code
  Serial.begin(9600);
  // Remember to change the PIN, if any
  Serial.print("AT+CPIN=0000\r\n");

  // Initialize WiServer and have it use the sendMyPage function to serve pages
  WiServer.init(sendMyPage);

  // Enable Serial output and ask WiServer to generate log messages (optional)
  WiServer.enableVerboseMode(false);

}

void loop(){

  // Run WiServer
  WiServer.server_task();
  delay(10);
}


Categories: Arduino Tags: , , , , ,