//------ESP32 Arduino IDE--------
#include <WiFi.h>
#include <HTTPClient.h>
WiFiClient Client;
int i;
String dataIn;
String arrData[5];
boolean parsing=false;
const char* host = WiFi.localIP().toString().c_str();
const char* ssid = "***";
const char* password = "***********";
HTTPClient http;
void setup() {
Serial.begin(9600);
WiFi.begin(ssid, password);
while(WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(500);
}
Serial.println("WiFi Connected");
}
int value = 0;
void loop() {
if (Serial.available()>0)
{
char inChar = (char)Serial.read();
dataIn += inChar;
if (inChar == '\n') (parsing = true);
}
if (parsing)
{
parsingData();
parsing=false;
dataIn="";
delay(1000);
++value;
Serial.print("Connecting to ");
Serial.print(WiFi.localIP().toString());
WiFiClient client;
if(!client.connect(host, 80)){
Serial.println("Connection Failed");
return;
}
String url = "/multisensor/kirimdata.php?suhu=";
url += arrData[0];
url += "&kekeruhan=";
url += arrData[1].toFloat();
url += "&amonia=";
url += arrData[2].toFloat();
url += "&ph=";
url += arrData[3].toFloat();
url += "&kondisi=";
url += arrData[4].toFloat();
Serial.print("Requesting URL: ");
Serial.println(url);
client.print(String ("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + WiFi.localIP().toString() + "\r\n" +
"Connection : close\r\n" +
"\r\n");
unsigned long timeout = millis();
while(client.available() == 0) {
if (millis() - timeout > 1000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
while(client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("Closing connection");
}
}
void parsingData()
{
int j=0;
Serial.print("data masuk: ");
Serial.println(dataIn);
Serial.print("\n");
arrData[j]="";
for(i=0;i<dataIn.length();i++)
{
//pemisah delimeter #
if ((dataIn[i] == '#'))
{
//increment variabel j, mengubah index array penampung
j++;
arrData[j]=""; //inisialisasai variabel array data [j]
}
else
{
//proses menampung data saaat sudah dicek
arrData[j] = arrData[j] + dataIn[i];
}
}
Serial.print("suhu = ");
Serial.print(arrData[0]);
Serial.print("\n");
Serial.print("kekeruhan = ");
Serial.print(arrData[1].toFloat());
Serial.print("\n");
Serial.print("amonia = ");
Serial.print(arrData[2].toFloat());
Serial.print("\n");
Serial.print("ph = ");
Serial.print(arrData[3].toFloat());
Serial.print("\n");
Serial.print("kondisi = ");
Serial.print(arrData[4].toFloat());
Serial.print("\n\n");
}
That's i am writing on Arduino IDE for my ESP32 and the connection to server always fail and last night it shown in the serial monitor like this
"Your browser sent a request that this site can't understand" I tried to change port several times and checked on cmd there's only one port available for apache and there's only one PID shown out of two PIDs.
While i am checking on cmd the IPvAddress seems to be different from Wifi.LocalIP() while being printed.
I keep going back and forth finding out what's wrong with the server and why it can't connect and input the number in database. I am new to mysql and stuff and if you notice what i did wrong please tell me how to fix the problem. Thank you
This is my php code for inputing data to database
<?php
//koneksi ke database
$konek = mysqli_connect("localhost", "root", "", "tugasakhir");
//baca data yang dikirim oleh nodemcu
$suhu = $_GET['suhu'];
$kekeruhan = $_GET['kekeruhan'];
$amonia = $_GET['amonia'];
$ph = $_GET['ph'];
$kondisi = $_GET['kondisi'];
//update data di database (tabel sensor)
mysqli_query($konek, "ALTER TABLE keranjangsensor AUTO_INCREMENT=1");
$simpan = mysqli_query($konek, "insert into keranjangsensor(suhu, kekeruhan, amonia, ph, kondisi) values ('$suhu', '$kekeruhan', '$amonia' , '$ph', '$kondisi')");
if (!$simpan)
{
die('Invalid query: '.mysqli_error($konek));
}
?>
source https://stackoverflow.com/questions/72774330/i-cant-connect-to-port-xampp-for-apache-and-mysql-causing-404-not-found-i-am-us
No comments:
Post a Comment