insert data into database mysql with SIM900 GPRS, HTTP request CODE

Hello,i have a project and i will insert data to mysql, this is the link i used to insert data into database "http://localhost/workfile/dht.php?temperature=20" working on chrome with out problem.
now i went to run it with SIM900 GPRS please someone help me if you have the code and schematic diagram

1 Like

I don't know of a way to insert data into a database directly, you'll have to either write an API or use a system that has an API. From the looks of your link you've written your own API.... I'm going to guess this is the first project of this type you've attempted, I don't want to sound harsh but I feel you have a lot to learn, but saying that, everyone starts somewhere... My advice, test each step in the chain before bringing in the next step...! You won't be able to access 'local host' from the gsm card as the gsm card will be connected to your mobile service provider which will be external to your PC on local network.

I see no reason why you can't do it this way, it's just a little more complex. You'll probably need to use port forwarding on your router to allow external traffic into your network, you'll also have to configure your PC to accept incoming connections... (Make sure you know exactly what you're doing here as you can potentially be opening your entire PC to the world!!!) Probably also wise to give your PC an internal static IP. Might also be easier to use a Dynamic DNS service (NoIP for instance) then you don't have to worry about your ISP changing your external IP address...

Anyway that's a little out of the scope of this, but Arduino wise see the code below, it works as it's what i'm using (I'm in the middle of a similar project), the comments should make it a easier to understand.

Declatations will be something like :

SoftwareSerial gsmSerial(7, 8); // SIM900 Tx & Rx is connected to Arduino #7 & #8
String postURL="mytestapi.example.com/forumtest.php"; // URL or IP the webserver API is located. Note lack of WWW. !
String postString=""; // Initilise the variable for the API POST request

First thing you'll want to do is handshake with the GSM board...

void SIM900Handshake() //Handshake with SIM900 module & get ready for use.
{
gsmSerial.println("AT"); //Handshaking with SIM900
delay(200);
gsmSerial.println("AT+CPIN?"); //Check SIM Pin OK
delay(200);
gsmSerial.println("ATE1"); //SIM900 Diagnostic echo off = 0 Diagnostic echo on = 1
delay(200);
gsmSerial.println("AT+CSQ"); //Signal quality test, value range is 0-31 , 31 is the best
delay(200);
gsmSerial.println("AT+CCID"); //Read SIM information to confirm whether the SIM is plugged
delay(200);
gsmSerial.println("AT+CREG?"); //Check whether it has registered in the network
delay(200);
gsmSerial.println("AT+CGATT=1"); //Attach GPRS (data comunications)
delay(200);
}

Then you can generate the HTTP POST request, something like this:
NB: APN, USER & PWD will be mobile network dependant, mine is UK o2, yours likely different!!
postURL & postString are variables (see declatations above). You'll also have to set the postURL to
the URL you want to use & the set postString to the data you want to send.
If in doubt use Postman (www.postman.com) to test & generate your POST requests.

void GeneratePayload()
{
gsmSerial.println("AT+SAPBR=3,1,"Contype","GPRS"");
delay(1000);
gsmSerial.println("AT+SAPBR=3,1,"APN","mobile.o2.co.uk""); // APN
delay(1000);
gsmSerial.println("AT+SAPBR=3,1,"USER","o2web""); // User
delay(1000);
gsmSerial.println("AT+SAPBR=3,1,"PWD","password""); // Password
delay(1000);
gsmSerial.println("AT+SAPBR=1,1"); // Enable the GPRS
delay(1000);
gsmSerial.println("AT+SAPBR=2,1"); // Query connection is setup properly, reply IP address then ok.
delay(1000);
gsmSerial.println("AT+HTTPINIT"); // Initialise http connection
delay(1000);
gsmSerial.println("AT+HTTPPARA="CID",1"); // Set up the HTTP bearer profile identifier.
delay(1000);
gsmSerial.println("AT+HTTPPARA="URL","" + postURL + """); // Server address url
delay(2000);
gsmSerial.println("AT+HTTPPARA="CONTENT","application/json""); // Set returning content header
delay(2000);
gsmSerial.println("AT+HTTPDATA=" + String(postString.length()) + ",100000"); // Get & set poststring (message body) length & timeout 10 seconds
delay(2000);
gsmSerial.println(postString); // Send poststring (message body) to SIM900
delay(3000);
gsmSerial.println("AT+HTTPACTION=1"); // Start the HTTP POST session
delay(3000);
gsmSerial.println("AT+HTTPREAD=20,20"); // Read server reply (200, all ok)
delay(1000);
updateSerial();
gsmSerial.println("AT+HTTPTERM"); // Terminate the session.
delay(1000);
updateSerial;
}

Oh and i'm using 'updateSerial' for diagnostics purposes (ie keeping an eye on whats happening) so for completeness here it is:

void updateSerial()
{
delay(500);
while (Serial.available())
{
gsmSerial.write(Serial.read()); //Forward what Serial received to Software Serial Port
}
while (gsmSerial.available())
{
Serial.write(gsmSerial.read()); //Forward what Software Serial received to Serial Port
}
}

1 Like