Arduino – Sending data to server using GSM Sim900A module

Arduino – Sending data to server using GSM Sim900A module
September 14, 2019 No Comments IoT, Programming ismithpasha

Git/Source Code: https://github.com/ismithpasha/gsmSIM900A

Connections of GSM Sim900A

1 to: No connection needed
2 to: GND of Arduino near 5V
3 to: Digital Pin 0 of Arduino (RX)
4 to: Digital Pin 1 of Arduino (TX)
5 to: GND (Digital GND near Pin 13)
6 to: 5V of Arduino

Arduino Code:

#include <SoftwareSerial.h>
int RST_PIN = 12;
SoftwareSerial gprsSerial(7, 8);

void setup(){
  Serial.begin(9600);
  Serial.print("Start "); 
}

void loop(){
  
  sendToServer();
}

void sendToServer() {
  
gprsSerial.begin(9600);
 
  Serial.println("Config SIM900...");
  delay(1000);
  Serial.println("Done!...");
  gprsSerial.flush();
   Serial.println("1!...");
  Serial.flush();

  // attach or detach from GPRS service 
  gprsSerial.println("AT+CGATT?");
  delay(100);
  toSerial();
Serial.println("2!...");

  // bearer settings
  gprsSerial.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");
  delay(500);
  toSerial();
Serial.println("3!...");
  // bearer settings
  gprsSerial.println("AT+SAPBR=3,1,\"APN\",\"INTERNET\"");
  delay(500);
  toSerial();
Serial.println("4!...");
  // bearer settings
  gprsSerial.println("AT+SAPBR=1,1");
  delay(500);
  toSerial();
  Serial.println("5!...");

  gprsSerial.println("AT+HTTPINIT");
   delay(1000); 
   toSerial(); 
   String url = "http://mywebsite.com/savedata.php?data=Hello World";
     Serial.println(url);
   // set http param value
   gprsSerial.println("AT+HTTPPARA=\"URL\",\""+url+"\"");
   delay(2000);
   toSerial();

   // set http action type 0 = GET, 1 = POST, 2 = HEAD
   gprsSerial.println("AT+HTTPACTION=0");
   delay(5000);
   toSerial();

   // read server response
   gprsSerial.println("AT+HTTPREAD"); 
   delay(1000);
   toSerial();

   gprsSerial.println("");
   gprsSerial.println("AT+HTTPTERM");
   toSerial();
   delay(500);

   gprsSerial.println("");
   delay(1000);
}

void toSerial()
{
  while(gprsSerial.available()!=0)
  {
    Serial.write(gprsSerial.read());
  }
}

void GSMRest() {
  digitalWrite(RST_PIN, LOW);
  delay(100);
  digitalWrite(RST_PIN, HIGH);
}

Server side Code (PHP):

savedata.php
<?php
include('functions.php'); 
 include('connection.php');
if(isset($_GET['data']))
{
  $data = validateFormData($_GET['data']);    
  
 $query = "INSERT INTO `testdata` (`id`, `details`, `time`, `statys`) VALUES (NULL, '" .  $data . "', CURRENT_TIMESTAMP, 'Y')";
       $result1 = mysqli_query($conn, $query);
        
        if($result1 == false)
        {  
         echo 'Failed';
            
        }
	   else
        {
           echo 'Success';
        }
    }
?>
functions.php
<?php
function validateFormData($formData) {
    $formData= trim(stripcslashes(htmlspecialchars( strip_tags(str_replace( array( '(',')' ), '', $formData) ), ENT_QUOTES ) ) );
    return $formData;
}
?>
connection.php
<?php
$server     ="localhost";
$username   ="root";
$password   ="";
$db         ="iotdb";
$conn = mysqli_connect($server, $username, $password, $db);
mysqli_query($conn,"SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'");
if(!$conn)
{
    die("connection failed: " . mysqli_connect_error());
}
?>
Tags
About The Author

Leave a reply

Your email address will not be published. Required fields are marked *