Easy IoT is a simplest IoT Solution presented by DFRobot
By marjangvg
#44093
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:
Code: Select all
/*
  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 for the Arduino, and a laptop charger (19v, 2A - it supports voltage from 5v-26v).
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.

So, something breaks after the sms is sent.

Thanks in advance for your help
By 347945801
#44208
This module can only receive very short messages. If it is too long, it will fail. You need to check the length of the original data sent by your server.
By rebeccabondvqt
#44912
I must say that it's super weird that when the SMS sending code is enabled, the response is not sent. Are you sure that everything is correct in your code? Maybe try changing something in the code, and run it one more time. If this doesn't work, maybe try contacting an expert who could further assist you and help with the issue. By the way, what HTTP SERVER are you using? Also, do you use an RDP? I would be very thankful if you could recommend a cheap rdp . Looking forward to hearing from you! Thanks!
By marianaonevesme68
#44976
I have had some issues with my phone number lately. I was getting random text messages, which at one point got me very annoyed. So I decided to purchase another number from https://quackr.io.But TBT, it didn't solve the problem. I am thinking that there is a problem with my phone. So I wondered if someone who is more experienced in this field would like to give me a hand. In case someone has any recommendations for me, feel free to text me. Guys, I will appreciate any help you can provide.
By janer9502
#45384
I must say that it's super weird that when the SMS sending code is enabled, the response is not sent. Are you sure that everything is correct in your code? Maybe try changing something in the code, and run it one more time. If this doesn't work, maybe try contacting an expert who could further assist you and help with the issue.
By jonee1103
#45666
The use of HTTP Server for SMS Service is not always Supported or Efficient. The Connection my lost or Server May Down due to over load like blue youtube app get Down while HD Streaming and buffer the Video from Server the SMS also sends in Packet and connection lost cause message lost so it's not as Simple to just connect HTTP Server with SMS and Ready to Go...