Search code examples
arduinogsmgprssim900

Sending sensor data to a MySQL server with Arduino and Sim900a


I am trying to send sensor data to a MySQL server of mine but can't achieve that. I followed several similar examples here in other questions but it isn't working. The Arduino code is given below. The idea is to measure the voltage and current across a load and send it to a MySQL server. TIA.

#include <SoftwareSerial.h>
SoftwareSerial gprsSerial(7, 8);

//variables that imitates actual voltage and current data
float a=random(300.0);
float b=random(2.00);
char  c[10]="B110";    

void setup() {
  gprsSerial.begin(19200);
  Serial.begin(19200);
  Serial.println("Config SIM900A...");
  delay(2000);`enter code here`
  Serial.println("Done!...");
  gprsSerial.flush();
  Serial.flush();
  // attach or detach from GPRS service 
  gprsSerial.println("AT+CGATT?");
  delay(100);
  toSerial();
  // bearer settings
  gprsSerial.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");
  delay(2000);
  toSerial();
  // bearer settings
  gprsSerial.println("AT+SAPBR=3,1,\"APN\",\"my carrier apn here\"");
  delay(2000);
  toSerial();
  // bearer settings
  gprsSerial.println("AT+SAPBR=1,1");
  delay(2000);
  toSerial();
}

void loop() {
  // initialize http service
  gprsSerial.println("AT+HTTPINIT");
  delay(2000);
  toSerial();
  // set http param value
  gprsSerial.println("AT+HTTPPARA=\"URL\",\"http://mocdl.net/api/data/create?voltage="" + a + ""&current="" + b + "" &load_id="" + c + ""\"");
  delay(2000);
  toSerial();
  // set http action type 0 = GET, 1 = POST, 2 = HEAD
  gprsSerial.println("AT+HTTPACTION=0");
  delay(6000);
  toSerial();
  // read server response
  gprsSerial.println("AT+HTTPREAD");
  delay(1000);
  toSerial();
  gprsSerial.println("");
  gprsSerial.println("AT+HTTPTERM");
  toSerial();
  delay(300);
  gprsSerial.println("");
  delay(10000);
}

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

Solution

  • As Panciz said you should set you APN (i think you already know that).

    You should also make a small change in your program.

    gprsSerial.println("AT+HTTPPARA=\"URL\",\"http://mocdl.net/api/data/create?voltage="" + a + ""&current="" + b + "" &load_id="" + c + ""\"");
    

    The above line wont work as you expected.

    So edit the http request line as follows :

     gprsSerial.println("AT+HTTPPARA=\"URL\",\"http://mocdl.net/api/data/create?voltage=%f&current=%f&load_id=%f\"",a,b,c);