Hello, i'm trying to send data from an Arduino UNO to a MySQL database in my web. I'm connecting to the internet with the GSM Shield using a SIM card from my country ( not the one included with the shield from Telefonica) and it connects perfectly.
In this sketch i'm using a switch button to input data, the idea is to send every 10 seconds the state of the button (0 or 1) to the server and store it in a database.
Right now the sketch connects to the server, sends data and this data is stored in the database, but there are two problems:
1)The information stored in the database is always 0, no matter what state the button has. Maybe there is something wrong in the HTTP request that i dont see.
2)The connection with the server is succesful the first time, then it fails to connect everytime ( every 10 seconds).
This is the .ino file in the Arduino:
// libraries
#include <GSM.h>
// PIN Number
#define PINNUMBER "number"
// APN data
#define GPRS_APN "apn.name" // replace your GPRS APN
#define GPRS_LOGIN "user" // replace with your GPRS login
#define GPRS_PASSWORD "psw" // replace with your GPRS password
// initialize the library instance
GSMClient client;
GPRS gprs;
GSM gsmAccess;
// URL, path & port
char server[] = "mywebsite.com";
char path[] = "/data_process.php";
int port = 80; // port 80 is the default for HTTP
//Example Button
int Boton = 4;
//The variable that will have the data as a string to send it, i read sending an int with POST may cause trouble
String txtBoton="";
void setup(){
// initialize serial communications and wait for port to open:
Serial.begin(9600);
//Establish pin 4 as an INPUT
pinMode(Boton, INPUT);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("Starting Arduino Web Client.");
// connection state
boolean notConnected = true;
// After starting the modem with GSM.begin()
// attach the shield to the GPRS network with the APN, login and password
while(notConnected)
{
if((gsmAccess.begin(PINNUMBER)==GSM_READY) &
(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD)==GPRS_READY)){
notConnected = false;
} else
{
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("connecting...");
}
void loop(){
int estadoBoton = digitalRead(Boton);
//print the state of the button in the Monitor Serial
Serial.println(estadoBoton);
//convert the button state in string to send it
//Estado_Boton is the variable that the POST method will search for
txtBoton= "Estado_Boton="+(String(estadoBoton));
//
// if you get a connection, report back via serial:
if (client.connect(server, port))
{
Serial.println("conected and sending data");
// Make a HTTP request:
client.print("POST ");
client.print(path);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
//PHP page needs to know how many characters the data has
client.println(txtBoton.length());
client.print("\n\n");
//finally the data that is being send to the server
client.print(txtBoton);
//Just to know what i am sending
Serial.print("Variable: ");
Serial.println(txtBoton);
Serial.print("Variable Length: ");
Serial.print(txtBoton.length());
}
else
{
// if you didn't get a connection to the server:
Serial.println("the connection failed");
Serial.print("");
}
delay(10000); // delay to send data every 10 seconds
}
This is the data_process.php file
<?php
$conex = mysql_connect("localhost", "root", "");
if(!$conex){
die("Atención, no fue posible conectarse con el servidor
".mysql_error());
}
$db = mysql_select_db("prueba_arduino", $conex);
if(!$db){
die("Atención, no fue posible conectarse con la base de datos
".mysql_error());
}
$Estado_Boton = $_POST["Estado_Boton"];
$sql = "INSERT INTO tabla_arduino (Estado_Boton) VALUES ('$Estado_Boton') ";
$res = mysql_query($sql, $conex);
?>
The MySQL database prueba_arduino has a table called tabla_arduino, in this table there are 2 columns.
First one is ID which is the primary key, tinyint, length/set=3, unsigned, NotNull, Autoincrement.
The second one is Estado_Boton which is a tinyint, length/set=3, default=Null, Unsigned.
This is what the Monitor Serial shows:
Starting Arduino Web Client.
connecting...
1
conected and sending data
Variable: Estado_Boton=1
Variable Length: 14
1
the connection failed
Obviously only the first one was entered into the database but with 0 as it's value.
The connection then continues to fail every 10 seconds and never connects again.
I've tried changing txtBoton= "Estado_Boton="+(String(estadoBoton)); to txtBoton= "Estado_Boton=4"; in the .ino file with no success, it continued to be 0 the value stored in the database.
If anyone could help I would be very grateful.
Thanks in advance, Leo.