sim800 AT Get command write Mysql - Solved

Hi to all

i have the code bellow that works fine until the line GET command that gives me an error in serial monitor and no data is written to the Mysql

#include <SoftwareSerial.h>

SoftwareSerial mySerial(8, 9); // RX, TX
const byte simStart = 12;

int humidityData = 69;
int temperatureData = 78;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  mySerial.begin(9600);
  pinMode(12, INPUT_PULLUP);
}

void loop() {
  if (mySerial.available()) {
    Serial.write(mySerial.read());
  }
  if (Serial.available()) {
    mySerial.write(Serial.read());
  }
  if (digitalRead(simStart) == LOW) {

    GSMconnection();


  }
  else if (digitalRead(simStart) == HIGH) {


  }
}


void GSMconnection()
{
  mySerial.begin(9600);
  Serial.begin(9600);

  Serial.println("Connecting");
  delay(2000);
  Serial.println("Done!...");
  mySerial.flush();
  Serial.flush();


  // See if the SIM800 is ready
  mySerial.println("AT");
  delay(1000);
  ReadSerial();

  // SIM card inserted and unlocked?
  mySerial.println("AT+CPIN?");
  delay(1000);
  ReadSerial();

  // Is the SIM card registered?
  mySerial.println("AT+CREG?");
  delay(1000);
  ReadSerial();

  // Is GPRS attached?
  mySerial.println("AT+CGATT?");
  delay(1000);
  ReadSerial();

  // Check signal strength
  mySerial.println("AT+CSQ ");
  delay(1000);
  ReadSerial();

  // Set connection type to GPRS
  mySerial.println("AT+SAPBR=3,1,\"Contype\",\"GPRS\"");
  delay(2000);
  ReadSerial();

  // Set the APN
  mySerial.println("AT+SAPBR=3,1,\"APN\",\"APN_name_Here\"");
  delay(2000);
  ReadSerial();

  // Enable GPRS
  mySerial.println("AT+SAPBR=1,1");
  delay(10000);
  ReadSerial();

  // Check to see if connection is correct and get your IP address
  mySerial.println("AT+SAPBR=2,1");
  delay(2000);
  ReadSerial();

  Sending_To_phpmyadmindatabase();

}

void Sending_To_phpmyadmindatabase()   //CONNECTING WITH MYSQL
{
  // initialize http service
  mySerial.println("AT+HTTPINIT");
  delay(2000);
  ReadSerial();


  mySerial.println("AT+HTTPPARA=\"CID\",1");
  delay(2000);
  ReadSerial();

  // set http param value
  mySerial.println("AT + HTTPPARA = \"URL\",\"www.myWeb.address/test.php\"");
  delay(4000);
  ReadSerial();

  // set http action type 0 = GET, 1 = POST, 2 = HEAD
  mySerial.println("AT+HTTPACTION=0");
  delay(6000);
  ReadSerial();


  // Make a HTTP request:
  Serial.print("GET /test.php?humidity=");
  mySerial.print("GET /test.php?humidity=");     //YOUR URL
  Serial.println(humidityData);
  mySerial.print(humidityData);
  mySerial.print("&temperature=");
  Serial.println("&temperature=");
  mySerial.print(temperatureData);
  Serial.println(temperatureData);
  mySerial.print(" ");      //SPACE BEFORE HTTP/1.1
  mySerial.print("HTTP/1.1");
  mySerial.println();
  delay(1000);
  mySerial.println("AT+HTTPTERM");
  delay(1000);
  ReadSerial();
  //close connection
  mySerial.println("AT+SAPBR=0,1");
  delay(2000);
  ReadSerial();
  mySerial.println();


}

void ReadSerial()
{
  while (mySerial.available() != 0)
  {
    Serial.write(mySerial.read());
  }
}

here is the serial monitor print :

(i have changed only my personal info (apn real name, web address etc)

Connecting
Done!...
AT

OK
AT+CPIN?

+CPIN: READY

OK
AT+CREG?

+CREG: 0,5

OK
AT+CGATT?

+CGATT: 1

OK
AT+CSQ 

+CSQ: 19,0

OK
AT+SAPBR=3,1,"Contype","GPRS"

OK
AT+SAPBR=3,1,"APN","APN_name_Here"

OK
AT+SAPBR=1,1
⸮⸮
OK
AT+SAPBR=2,1

+SAPBR: 1,1,"10.140.225.41"

OK
AT+HTTPINIT

OK
⸮⸮⸮⸮AT+HTTPPARA="CID",1

OK
AT + HTTPPARA = "URL","www.myWeb.address/test.php"

AT+HTTPACTION=0

OK
⸮⸮
+HTTPACTION: 0,200,1
⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮connected
GET /mpersefoni001.php?humidity=69
&temperature=
78
GET /test.php?humidity=69&temperature=78  HTTP/1.1

ERROR

AT+SAPBR=0,1
⸮⸮
OK

Here is the php script with test.php name in my server (it is not my code as i dont know anything about php coding , but i can ensure you that works fine when i run it in my browser and put manual the data. it updates my Mysql table

example : http://www.myWeb.address/test.php?humidity=65&temperature=41 )

<?php
class dht11{
 public $link='';
 function __construct($temperature, $humidity){
  $this->connect();
  $this->storeInDB($temperature, $humidity);
 }
 
 function connect(){
  $this->link = mysqli_connect('localhost','My_user','My_Password') or die('Cannot connect to the DB');
  mysqli_select_db($this->link,'<Your Database Name>') or die('Cannot select the DB');
 }
 
 function storeInDB($temperature, $humidity){
  $query = "insert into <Your table name> set humidity='".$humidity."', temperature='".$temperature."'";
  $result = mysqli_query($this->link,$query) or die('Errant query:  '.$query);
 }
 
}
if($_GET['temperature'] != '' and  $_GET['humidity'] != ''){
 $dht11=new dht11($_GET['temperature'],$_GET['humidity']);
}


?>

seems you are printing your data with new lines and twice. the GET line needs to be in one line

GET /mpersefoni001.php?humidity=69
&temperature=
78
GET /test.php?humidity=69&temperature=78  HTTP/1.1

note that this is sub-optimal

  mySerial.print(" ");      //SPACE BEFORE HTTP/1.1
  mySerial.print("HTTP/1.1");

why don't you domySerial.print(" HTTP/1.1");with the space before HTTP/1.1 directly embedded in the text ?

EDIT: ah sorry, did not see the code. You have both the module and your Serial output there and they are not exactly the same... I'll keep reading

I think to remember you don't need to put GET in the request

I would suggest you fine tune you AT commands sequence. try with static text first

AT+HTTPINIT
AT+HTTPPARA="CID",1
AT+HTTPPARA="URL","www.myWeb.address/test.php?humidity=69&temperature=78"
AT+HTTPACTION=0

then read the result

AT+HTTPREAD

(loop reading what's coming)

and terminate the session

AT+HTTPTERM

Thank you for the interesting and for answering to me

J-M-L:
I think to remember you don't need to put GET in the request

I would suggest you fine tune you AT commands sequence. try with static text first

AT+HTTPINIT

AT+HTTPPARA="CID",1
AT+HTTPPARA="URL","www.myWeb.address/test.php?humidity=69&temperature=78"
AT+HTTPACTION=0




then read the result


AT+HTTPREAD



(loop reading what's coming) 

and terminate the session


AT+HTTPTERM

i need the Get in the reguest as the

int humidityData = 69;
int temperatureData = 78;

will not be fixed values in the future... it will be reading from the sensors
i just place them in this code with fixed values to make it simple to work as a start... have less to worry

You’ll be able to print the values when building the AT+HTTPPARA="URL", command, that’s just for testing

AT+HTTPACTION=0 is your GET you don’t need to send it yourself (from top of my memory)

yes if i place that line in my code :

  mySerial.println("AT + HTTPPARA = \"URL\",\"www.myWeb.address/test.php?humidity=69&temperature=78\"");

  delay(4000);
  toSerial();

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

i can update my mysql databed table with the values "69" and "78"

so i have to figure out how will make the "AT + HTTPPARA" command in my code to read the variables

is any way to place in the AT command to read from a variable?

in the AT : mySerial.println("AT + HTTPPARA = \"URL\",\"www.myWeb.address/test.php\"");

is it possible to make it like this ? : mySerial.println("AT + HTTPPARA = \"URL\",\"www.myWeb.address/test.php?humidity=<variable1>&temperature=<variable2>\"");

Just do separate prints
The module doesn’t care if they come in one go or not, it’s a asynchronous communication protocol

unfortunately you can brake an AT command in 2 separate prints

i tried to brake this command that works fine as it is :

mySerial.println("AT + HTTPPARA = \"URL\",\"www.myWeb.address/test.php?humidity=90&temperature=70\"");

to something like this :

mySerial.println("AT + HTTPPARA = \"URL\",\"www.myWeb.address/test.php");
mySerial.print("?humidity=");
mySerial.print("31");
mySerial.print("&temperature=");
mySerial.print("71\r\n");

But always get an ERROR answer from sim800L (i have tried it with different serial speeds to software serial connection, from 300 - 115200 )

the same and with this example :

mySerial.println("AT + HTTPPARA = \"URL\",\"www.myWeb.address/test.php?humidity="");
mySerial.print("31");
mySerial.print("&temperature=");
mySerial.print("71\r\n");

so AT commands to the module should be printed in one line in the code in order to accept them

ok i found the solution :slight_smile:

first of all we declare the variables we want to update to the Mysql as a "String" type

String humidityData;
String temperatureData;

then somewhere in the code we change the values of the variable depending of the sensor,switch reading

and the At command in order to be accepted from the module should be like this :

  mySerial.println(("AT + HTTPPARA = \"URL\",\"www.myWeb.address/test.php?humidity=")+(humidityData)+("&temperature=")+(temperatureData)+ ("\""));

thanks for the help make me clear some issues and find the solution

Come on...

caslor:
unfortunately you can brake an AT command in 2 separate prints

sorry but that's conceptually an engineering nonsense and should have you think twice.

The receiver of the command (the AT command line parser listening to the Serial input in your sim800 module) does not care how you build up the stream of data. It just sits there until it gets a proper AT command that it can recognize and then executes it. So why would it be impossible to break it apart on the sender side? There is no reason justifying this.

BUT what matters is the correctness of what you send.

if you want to break upmySerial.println("AT+HTTPPARA=\"URL\",\"www.myWeb.address/test.php?humidity=90&temperature=70\"");
you can't start with a println() because you introduce a CR+LF that the AT parser does not understand

you just do

mySerial.print("AT+HTTPPARA=\"URL\",\"www.myWeb.address/test.php?humidity=");
mySerial.print(90); // of course this could be a variable
mySerial.print("&temperature=");
mySerial.print(70); // of course this could be a variable
mySerial.println("\""); // THIS IS WHERE THE 'ln' comes in

Then the output on the Serial line is 100% identical and thus the behavior of the AT command parser will be identical.

Don't use the String class if you don't need to, you'll come to regret it later when memory will start to run low.

Thank you!!

it is amazing every time how much important maybe such a small change in one word

yeah, computer science is all about precision :slight_smile: