sim800l send data to server

Sim800l send data to server | sim800l gprs arduino

Sim800l is a great module that could be used for sending texts, making calls, and send data to the server over the internet. This module could be used with Arduino and controlled via serial communication using the AT commands. The place where people struggle with this module is stabilizing the network. This module requires a stable supply of 3.7 to 4.2 volts. Also, it can draw up to 2A current. I’ve tried different power sources and I strongly recommend using a lipo cell of 3.7v 2000mAH.

Sim800l connections with Arduino

vcc -> 3.7v of the battery

gnd -> gnd

tx -> rx

rx -> tx through voltage divider

See the following video for a proper understanding of the code and the functionality of the process.

Creating the database and table

Now we have to create a database where we can store the data transmitted by the sim800l gprs functionality. Login to your cpanel > MySQL Database Wizard then create a database, in this case, I’m naming it “sample”.

After that you have to create a used to access the database. Set a username and a password.

It’s time to create a table inside the newly created database. Go to cpanel > phpMyAdmin then choose the database and go to the SQL tab. Paste the following code there:

 

CREATE TABLE gprs (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    val VARCHAR(30) NOT NULL,
    reading_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)

We need three parameters. The first one is used to count the entries, the second one is where we are going to store the data and the third one is the time record. Click on the “Go” button and it will create the table.

Now, we have to create a PHP file on the root directory of the website. Let’s name the file “get_method.php” and paste the following code there.

<?php
$servername = "localhost";

  // REPLACE with your Database name
$dbname = ""; //your database name inside the quotation
  // REPLACE with Database user
$username = ""; // your database username
  // REPLACE with Database user password
$password = ""; //your database user password
$val = $_GET['val'];

echo 'From Server-> Received: '.$val;

$conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO gprs(val) VALUES ('$val')";

if ($conn->query($sql) === TRUE) {
  return "New record created successfully";
}
else {
  return "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();


?>

Arduino code for sim800l send data to server

It’s time to prepare the Arduino code.

 

#include <SoftwareSerial.h>

String apn = "internet";                    //APN
String apn_u = "";                     //APN-Username
String apn_p = "";                     //APN-Password
String url = "http://sihabthetechguy.info/get_method.php";  //URL of Server

SoftwareSerial mySerial(8, 7); // sim800l_TX_pin, sim800l_RX_pin
int counter = 0;
void setup()
{
  Serial.begin(9600);
  Serial.println("SIM800 AT CMD Test");
  mySerial.begin(9600);
  delay(5000);
  while (mySerial.available()) {
    Serial.write(mySerial.read());
  }
  delay(2000);
  configure_gprs();
}

void loop() {
  counter++;
  String data = String(counter); //write your data here
  send_to_server(data);
  delay(2000);
}
void send_to_server( String data) {
  String sendURL = url+"?val="+data;
  Serial.println(" --- Start GPRS & HTTP --- ");
  send_to_serial("AT+SAPBR=1,1");
  send_to_serial("AT+SAPBR=2,1");
  send_to_serial("AT+HTTPINIT");
  send_to_serial("AT+HTTPPARA=CID,1");
  send_to_serial("AT+HTTPPARA=URL," + sendURL);
  send_to_serial("AT+HTTPPARA=CONTENT,application/x-www-form-urlencoded");
  send_to_serial("AT+HTTPDATA=192,5000");
  send_to_serial(data);
  send_to_serial("AT+HTTPACTION=1");
  send_to_serial("AT+HTTPREAD");
  send_to_serial("AT+HTTPTERM");
  send_to_serial("AT+SAPBR=0,1");
}

void configure_gprs() {
  Serial.println(" --- CONFIG GPRS --- ");
  send_to_serial("AT+SAPBR=3,1,Contype,GPRS");
  send_to_serial("AT+SAPBR=3,1,APN," + apn);
  if (apn_u != "") {
    send_to_serial("AT+SAPBR=3,1,USER," + apn_u);
  }
  if (apn_p != "") {
    send_to_serial("AT+SAPBR=3,1,PWD," + apn_p);
  }
}

void send_to_serial(String command) {
  Serial.println("Send ->: " + command);
  mySerial.println(command);
  long wtimer = millis();
  while (wtimer + 3000 > millis()) {
    while (mySerial.available()) {
      Serial.write(mySerial.read());
    }
  }
  Serial.println();
}

Upload the code to the arduino and then open up the terminal to see the progress. Go to cpanel > phpMyAdmin > gprs table and you will an entry with a value “1”. I’m sending value that will be incremented automatically and send to the server. You can send any kind of data depending on which kind of variable you’re defining inside the table.