hi everyone, i got error code 400 while sending data string barcode from esp32 to mySQL database.
but when the sensor reading is not correct, the data can be sent to the database
my code
/*
Rui Santos
Complete project details at Complete project details at https://RandomNerdTutorials.com/esp32-http-get-post-arduino/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
#include <WiFi.h>
#include <HTTPClient.h>
#include <HardwareSerial.h>
#include <ESP32Servo.h>
Servo servo1;
Servo servo2;
Servo servo3;
HardwareSerial MySerial(2);
const char* ssid = "RidwanSoleh";
const char* password = "ridwananaksoleh";
String kodebarcode ="";
String typea = "A";
String typeb = "B";
String typec = "C";
String kategori = typea ;
String perusahaan = "PT_Cinta_Sejati";
int jumlah = 1;
//Your Domain name with URL path or IP address with path
String serverName = "http://192.168.0.196:80/sobsite/update_fcn.php";
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 5 seconds (5000)
unsigned long timerDelay = 5000;
void setup() {
Serial.begin(115200);
MySerial.begin(115200, SERIAL_8N1, 16, 17);
servo1.attach(4);
servo2.attach(2);
servo3.attach(5);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}
void loop() {
// Pembacaan GPS
//Send an HTTP POST request every 10 minutes
if ((millis() - lastTime) > timerDelay) {
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
HTTPClient http;
String serverPath = serverName + "?kodebarcode=" + kodebarcode + "&kategori=" + kategori + "&perusahaan=" + perusahaan + "&jumlah=" + jumlah;
Serial.println(serverPath);
// Your Domain name with URL path or IP address with path
http.begin(serverPath.c_str());
// Send HTTP GET request
int httpResponseCode = http.GET();
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println(payload);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
while (MySerial.available() > 0) {
kodebarcode = MySerial.readString();
Serial.println(kodebarcode);
Serial.println(kodebarcode.substring(0, 2));
if (kodebarcode.substring(0, 2) == "A0") {
Serial.println(typea);
}
else if (kodebarcode.substring(0, 2) == "B0"){
Serial.println(typeb);}
else if (kodebarcode.substring(0, 2) == "C0"){
Serial.println(typec);}
else{Serial.println("Tidak Terdeteksi");}
}
servo1.write(0);
if(kodebarcode.substring(0, 2) == "A0"){
servo1.write(90);}
else{servo1.write(0);}
servo2.write(0);
if(kodebarcode.substring(0, 2) == "B0"){
servo2.write (90);}
else{servo2.write(0);}
servo3.write(0);
if(kodebarcode.substring(0, 2) == "C0"){
servo3.write(90);}
else{servo3.write(0);}
}