Sending SMS from Arduino + Wifi Shield

Hi there,

I guess I found a new way to try send SMS from Arduino. Found this from http://www.instructables.com/id/Send-SMS-from-Arduino-over-the-Internet-using-ENC2/?ALLSTEPS. It stated that I can change a little bit from the program to make it work with wifi shield but it seems that my program is not running when it was already uploaded to Arduino.

Here's my code:

#include <SPI.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
#include <WiFiUdp.h>

//thingspeak server
char server[] = "api.thingspeak.com";

char ssid[] = "SSID";      
char pass[] = "PASS";  

WiFiClient client;

//API key for the Thingspeak ThingHTTP already configured
const String apiKey = "API_KEY";

//the number the message should be sent to
const String sendNumber = "PHONE_NUMBER";

void setup()
{
  Serial.begin(9600);
  setupWiFi();
  
  //send the sms
  Serial.println("Sending SMS");
  
  //this function will send the sms
  //the first argument is the number to send to, formatted like this +12345678901
  //the second argument is the body of the text message, which must be within URLEncode()
  sendSMS(sendNumber, URLEncode("Hello World!"));

}

void loop()
{

}


void sendSMS(String number,String message)
{
  // Make a TCP connection to remote host
  if (client.connect(server, 80))
  {

    //should look like this...
    //api.thingspeak.com/apps/thinghttp/send_request?api_key={api key}&number={send to number}&message={text body}
    
    client.print("GET /apps/thinghttp/send_request?api_key=");
    client.print(apiKey);
    client.print("&number=");
    client.print(number);
    client.print("&message=");
    client.print(message);
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(server);
    client.println("Connection: close");
    client.println();
    
  
  }
  else
  {
    Serial.println(F("Connection failed"));
  } 

  // Check for a response from the server, and route it
  // out the serial port.
  while (client.connected())
  {
    if ( client.available() )
    {
      char c = client.read();
      Serial.print(c);
    }      
  }
  Serial.println();
  client.stop();
}


void setupWiFi()
{
  Serial.println("Setting up WiFi...");
  //start wifi connection
  WiFi.begin(ssid, pass);

  Serial.print("Connected to SSID: ");
  Serial.println(ssid);
  delay(1000);
}

String URLEncode(const char* msg)
{
  const char *hex = "0123456789abcdef";
  String encodedMsg = "";

  while (*msg!='\0'){
    if( ('a' <= *msg && *msg <= 'z')
      || ('A' <= *msg && *msg <= 'Z')
      || ('0' <= *msg && *msg <= '9') ) {
      encodedMsg += *msg;
    } 
    else {
      encodedMsg += '%';
      encodedMsg += hex[*msg >> 4];
      encodedMsg += hex[*msg & 15];
    }
    msg++;
  }
  return encodedMsg;
}

the message stated on my serial monitor is:

Setting up WiFi...

Attempting to connect to WEP network, SSID: iotlab

Sending SMS

H

any idea what is wrong here? Message seem not able to get through my phone. Appreciate for your time in helping. Thanks in advance.

Hazirah

It is usual to send a blank line here:

    client.print("Host: ");
    client.println(server);
    client.println(""); //new

Good is that you don't get the message "Connection failed".

6v6gt:
It is usual to send a blank line here:

    client.print("Host: ");

client.println(server);
    client.println(""); //new




Good is that you don't get the message "Connection failed".

hi there,

i tried removing the connection: close but it prints out HT and still not able to send out the SMS to my number. You have any idea why this is so?

Thanks anyway.

Try this an a browser with your key, number and text just as if the Arduino had sent it:

http://api.thingspeak.com/apps/thinghttp/send_request?api_key={api key}&number={send to number}&message={text body}

Don't put any spaces in your text body for this test.