Send SMS through HTTP server

Hello everyone,

I'm working on a GSM module project, where users need to be able to send SMS through a web platform and using the gsm module.

I'm using Arduino Mega 2560 + Ethernet Shield W5100 + SIM808 module

Here is the code:

/*
  Web Server 
*/

#include <SPI.h>
#include <Ethernet.h>

//SMS Include
#include <DFRobot_sim808.h>
#include <SoftwareSerial.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xA2, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 23);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

//SMS VARIABLES
#define MESSAGE_LENGTH 160
char message[MESSAGE_LENGTH];
char phone[16];
char datetime[24];
char phone_to[] = "xxxxxxx";
#define PIN_TX    12
#define PIN_RX    13 
SoftwareSerial mySerial(PIN_TX,PIN_RX);
DFRobot_SIM808 sim808(&mySerial);//Connect RX,TX,PWR,


void setup() {
  // Open serial communications and wait for port to open:
  mySerial.begin(9600);
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection and the server:
  //Ethernet.begin(mac, ip);
  Ethernet.begin(mac);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());

  //******** Initialize sim808 module *************
  while(!sim808.init())
  {
      Serial.print("Sim808 init error\r\n");
      delay(1000);
  }

  delay(3000);
  if( sim808.attachGPS()) {
      Serial.println("Open the GPS power success");
      //return true;
  }
  else {
      Serial.println("Open the GPS power failure");
      //return false;
  }
}

void writeResponse(EthernetClient client) {
  // send a standard http response header
  Serial.println("SMS was sent - this is in writeResponse");
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println("Connection: close");  // the connection will be closed after completion of the response

  client.println();
  client.println("SENT");
}

void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");

    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    String req_str = "";
    int data_length = -1;
    boolean skip = false;

    //int empty_line_count = 0;
    while (client.connected()) 
    {
      if (client.available()) {
        char c = client.read();
        //Serial.write(c);
        req_str += c;

        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply       
        if (c == '\n' && currentLineIsBlank && req_str.startsWith("GET")) {
          writeResponse(client);
          break;
        }
        if (c == '\n' && currentLineIsBlank && req_str.startsWith("POST") && !skip) {

          if(sendSms()) {
            Serial.println("SMS was sent - this is in loop");
            writeResponse(client);
          }
          break;
        }   
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    //Serial.println(req_str);
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }
}

bool sendSms() {
  char msg[] = "test";
  sim808.sendSMS(phone_to,msg);
  //************* Turn off the GPS power ************
  sim808.detachGPS();
  Serial.println("SMS was sent - this is in function");
  return true;
}

The SMS is received, but the response in the writeResponse function is never received by the client. The serial write works ok btw.

The serial output looks like this:

SMS was sent - this is in function
SMS was sent - this is in loop
SMS was sent - this is in writeResponse

client disconnected

I'm using 5V 1A power supply that is powering both the arduino and SIM808 GSM modules. The arduino is connected with usb to PC too.

If I disable the SMS sending in sendSms() function, the response is sent OK.
But with the SMS sending code enabled, the response is not sent.

Thanks in advance for your help :slight_smile:

Try passing the the EthernetClient variable by reference (or pointer) to the writeResponse() routine.

pylon:
Try passing the the EthernetClient variable by reference (or pointer) to the writeResponse() routine.

Thanks for the response, I tested it but the result is the same. writeResponse() works ok when I comment out the sms sending line - so something breaks after the SMS is sent.

Can someone help me with this please?

I'm using Arduino Mega 2560 + Ethernet Shield W5100 + SIM808 module

Wild guessing: you're not using an original Arduino Ethernet Shield. Try to use other pins than 12 and 13 for the serial connection. Maybe your shield connect them to the SPI pins so any voltage change there influences the SPI bus.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.