Easy IoT General

Send SMS through HTTP server

userHead Account cancelled 2021-02-07 07:51:21 5338 Views5 Replies
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
2022-02-14 08:43:42 Play Among Us Mod Apk for free to your Android. userHeadPic apkworld2020
2022-02-12 23:03:19 First review your code again, Check everything is right in your code or not? Try to change something in the code, hopefully, it will be run. If not working then search in google about this problem and search for a good article about this topic or you can also search on youtube. You can also use admin rdp for your work because the problem can happen from your HTTP server.

Thank you!
userHeadPic relaxationtune4u
2021-11-17 01:55:42 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... userHeadPic jonee1103
2021-10-16 01:05:38 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. userHeadPic janer9502
2021-02-24 18:23:33 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. userHeadPic 347945801