Im trying to update some data in a database using arduino. I got a database online using www.000webhost.com . This is my PHP code:
<?php
//Database login variables
$dbusername = "id5096752_root";
$dbpassword = "***";
$host = "localhost";
$mydb = "id5096752_disspill";
//Connect to database
$con = new mysqli ($host, $dbusername, $dbpassword);
if($con)
{
echo "Connection succesful";
}
else
{
echo "Connection error";
}
mysqli_select_db($con,$mydb) or die
("Database selection error");
//Query
$SQL = "UPDATE contenedores SET ocupado='$_GET[ocupado]', nombrePastilla='$_GET[nombrePastilla]', cantidadPastillas = '$_GET[cantidadPastillas]' WHERE numeroContenedor = '$_GET[numeroContenedor]'";
//Execute query
mysqli_query($con,$SQL);
?>
I already manually tested it with my browser and it works. Im trying to code it into arduino. Here is the important parts of the code:
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x19 };
char bdServer[] = "disspill.000webhostapp.com";
...
Ethernet.begin(mac);
client.stop();
Serial.println("Connecting with BD");
if(client.connect(bdServer, 80))
{
Serial.println("Connected");
client.print("GET /phpcommands/updateData.php?numeroContenedor=1&ocupado=si&nombrePastilla=paracetamol&cantidadPastillas=7");
client.println(" HTTP:/1.0");
client.println("Host: disspill.000webhostapp.com");
client.println("Connection: close");
client.println();
client.stop();
}
else
{
Serial.println("Error");
}
Aparently it does connect to the server, but the data doesnt update. Does somebody know what is wrong?
Moderator: Code tags added
Sorry, here is the complete code:
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x19 }; // MAC for the ethernet shield
IPAddress ip(192,168,15,50); //Local ip address
char bdServer[] = "disspill.000webhostapp.com"; //Database address
EthernetServer server(80);
String readString=String(30);
EthernetClient client;
void setup()
{
Serial.begin(9600);
Ethernet.begin(mac,ip);
server.begin();
Serial.println("Ethernet ready");
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
delay(1000);
}
void loop()
{
EthernetClient client = server.available();
if(client)
{
while(client.connected())
{
if(client.available())
{
char c = client.read();
if(readString.length()<30)
{
readString.concat(c);
}
if(c=='\n')
{
int CMD = readString.indexOf("CMD=");
if(readString.substring(CMD+4,CMD+7)=="upd")
{
Serial.println("update database");
Ethernet.begin(mac);
delay(1000);
client.stop();
Serial.println("Connecting with BD");
if(client.connect(bdServer, 80))
{
Serial.println("Connected");
client.print("GET /phpcommands/updateData.php?");
client.print("numeroContenedor=1");
client.print("&ocupado=si");
client.print("&nombrePastilla=paracetamol");
client.print("&cantidadPastillas=7");
client.println(" HTTP:/1.1");
client.println("Host: disspill.000webhostapp.com");
client.println("Connection: close");
client.println();
client.stop();
}
else
{
Serial.println("Error");
}
}
client.stop();
readString="";
}
}
}
}
}
Moderator: Code tags added
I hope someone wants to spend their valuable time debugging code.
system
March 19, 2018, 11:26am
5
client.print("GET /phpcommands/updateData.php?");
client.print("numeroContenedor=1");
client.print("&ocupado=si");
client.print("&nombrePastilla=paracetamol");
client.print("&cantidadPastillas=7");
client.println(" HTTP:/1.1");
client.println("Host: disspill.000webhostapp.com");
client.println("Connection: close");
client.println();
client.stop();
Connect to the server. Make a GET request. Tell the server to f**k off, because you can't be bothered to read its response. Wonder why the code doesn't work.
Perhaps if you actually bothered to read the server reply, a clue-by-four would present itself.