I am getting "400 Bad Request" when sending data to mySQL database using php
Here is my code:
#include <EthernetENC.h>
#include <SPI.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
//0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED //0x8C,0xAA,0xB5,0xA2,0x14,0xB0
0x8C, 0xAA, 0xB5, 0xA2, 0x14, 0xB0
};
IPAddress ip(116, 75, 59, 24);
IPAddress myDns(192, 168, 0, 1);
char serv[] = "http://understaffed-partie.000webhostapp.com";
String tagID = "123ABC";
String RTCtime2 = "2020-12-27 11:58:32";
String GateNO = "gate2";
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
//EthernetServer server(80);
EthernetClient client;
unsigned long beginMicros, endMicros;
unsigned long byteCount = 0;
bool printWebData = true; // set to false for better speed measurement
void setup() {
// You can use Ethernet.init(pin) to configure the CS pin
//Ethernet.init(10); // Most Arduino shields
//Ethernet.init(5); // MKR ETH shield
//Ethernet.init(0); // Teensy 2.0
//Ethernet.init(20); // Teensy++ 2.0
//Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
Ethernet.init(5); // ESP32 with Adafruit Featherwing Ethernet
// Open serial communications and wait for port to open:
Serial.begin(115200);
Serial.println("Initialize Ethernet with DHCP:");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip, myDns);
} else {
Serial.print(" DHCP assigned IP ");
Serial.println(Ethernet.localIP());
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.print("connecting to ");
Serial.print(serv);
Serial.println("...");
if (client.connect(serv, 80)) {
Serial.print("connected to ");
Serial.println(client.remoteIP());
// Make a HTTP request:
client.println(String("GET http://understaffed-partie.000webhostapp.com/connect.php?") +
("tagID=") + tagID + ("&RTCtimestamp=") + RTCtime2 + ("&GateNO=") + GateNO +
("&i=1") +
" HTTP/1.1\r\n" +
"Host: " + serv + "\r\n" +
"Connection: close\r\n\r\n");
Serial.println(String("GET http://understaffed-partie.000webhostapp.com/connect.php?") +
("tagID=") + tagID + ("&RTCtimestamp=") + RTCtime2 + ("&GateNO=") + GateNO +
("&i=1") +
" HTTP/1.1\r\n" +
"Host: " + serv + "\r\n" +
"Connection: Keep-Alive\r\n\r\n");
//client.println("Connection: close");
client.println();
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
}
void loop() {
Ethernet.maintain();
// if there are incoming bytes available
// from the server, read them and print them:
int len = client.available();
if (len > 0) {
byte buffer[80];
if (len > 80) len = 80;
client.read(buffer, len);
if (printWebData) {
Serial.write(buffer, len); // show in the serial monitor (slows some boards)
}
byteCount = byteCount + len;
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
endMicros = micros();
Serial.println();
Serial.println("disconnecting.");
client.stop();
Serial.print("Received ");
Serial.print(byteCount);
Serial.print(" bytes in ");
float seconds = (float)(endMicros - beginMicros) / 1000000.0;
Serial.print(seconds, 4);
float rate = (float)byteCount / seconds / 1000.0;
Serial.print(", rate = ");
Serial.print(rate);
Serial.print(" kbytes/second");
Serial.println();
// do nothing forevermore:
while (true) {
delay(1);
}
}
}
I have attached the screenshot of the output below.
Here are the things that I tried:
- I used "http" instead of "https"
- Wherever I needed to add space I added "%20". Also tried sending the string without "%20"
Any other suggestion ?