So I'm trying to send data from ESP8266 to a local MSSQL database. I'm using XAMPP and I have a working php script that inserts data into the database, but I can't get the arduino code to send anything.
This is my arduino code:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
void connecttowifi();
void sendDataToDB();
String Data ="TEST"; //test data
char ssid[] = "ssid"; //wifi ssid
char password[] = "password"; //wifi password
WiFiClient client;
char server[] = "192.168.1.6";
void setup()
{
Serial.begin(115200);
delay(10);
connecttowifi();
}
void connecttowifi()
{
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
delay(1000);
}
void sendDataToDB(){
if(client.connect(server, 80))
{
Serial.println("Connected...");
Serial.println("Making HTTP request...");
client.print("GET /insert.php?data="); //php code for database
client.print(Data); //data
client.print(" HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.println();
Serial.println("HTTP request sent");
delay(3000);
if(client.available())
{
Serial.println("HTTP request received");
while(client.available())
{
char c = client.read();
Serial.print(c);
}
}
else
{
Serial.println("No response received");
}
client.stop();
}
else
{
Serial.println("connection failure");
}
}
void loop() {
sendDataToDB();
Serial.print("Data sent: ");
Serial.println(Data);
}
and this is the error I'm getting:
Connected...
Making HTTP request...
HTTP request sent
HTTP request received
HTTP/1.1 400 Bad Request
Date: Sat, 26 May 2018 03:04:19 GMT
Server: Apache/2.4.33 (Win32) OpenSSL/1.0.2n PHP/7.1.16
Vary: accept-language,accept-charset
Accept-Ranges: bytes
Connection: close
Content-Type: text/html; charset=utf-8
Content-Language: en
Expires: Sat, 26 May 2018 03:04:19 GMT
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Bad request!</title>
<link rev="made" href="mailto:postmaster@localhost" />
<style type="text/css"><!--/*--><![CDATA[/*><!--*/
body { color: #000000; background-color: #FFFFFF; }
a:link { color: #0000CC; }
p, address {margin-left: 3em;}
span {font-size: smaller;}
/*]]>*/--></style>
</head>
<body>
<h1>Bad request!</h1>
<p>
Your browser (or proxy) sent a request that
this server could not understand.
</p>
<p>
If you think this is a server error, please contact
the <a href="mailto:postmaster@localhost">webmaster</a>.
</p>
<h2>Error 400</h2>
<address>
<a href="/">localhost</a>
<span>Apache/2.4.33 (Win32) OpenSSL/1.0.2n PHP/7.1.16</span>
</address>
</body>
</html>
Data sent: TEST
I'm not sure what I'm doing wrong since all the tutorials I've seen online seem to have the same or similar codes.
My firewall is off and I could successfully insert data into my database if I type "192.168.1.6/insert.php?data=whatever" on any browser.
Any help would be greatly appreciated.