PlatformIO Community

The program in my Arduino IDE application cannot connect to the server. Where I use ESP8266. (If (!client.connect(host,80)) )

The program in my Arduino IDE application cannot connect to the server. Where I use ESP8266. When uploading program code to esp8266, the program always works on the If (!client.connect(host,80)) section. Below is a link to the program code that I mean. Hope someone can help me.
Link

https://drive.google.com/drive/folders/1aLVmw3mYiSdZHf9bYOPrZwAa03FbZX69?usp=sharing

Are you sure you’re in the same network as the ESP8266 tries to connect to and the IP is correct? Try using the HTTPClient object direct without doing a connect on the WiFiClient first, like the Arduino/BasicHttpClient.ino at master · esp8266/Arduino · GitHub does too.

My laptop as a local server and esp is already connected to the same network, maxgerhardt :pray:. I’m confused about using the program code that you share “Arduino/BasicHttpClient.ino on master · esp8266/Arduino · GitHub”. What do I try the program code for? :pray:

I mean something more direct like

#include <Arduino.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <DHT.h>      //Memasukkan library DHT11
#define DHTPIN 4      // Menentukan pin untuk data
#define DHTTYPE DHT11 // Mendefinisikan tipe DHT yang dipakai

const char *ssid = "Samsung";
const char *password = "<password here>";
IPAddress host(192, 168, 43, 225);
DHT dht(DHTPIN, DHTTYPE); // Pengenalan sensor DHT
int led = 0;
int led_test = 5;

void setup()
{
  Serial.begin(9600); // Membuka jalur komunikasi serial
  // dengan boudrate 9600
  dht.begin(); // Membuka jalur komunikasi DHT11
  pinMode(led, OUTPUT);
  pinMode(led_test, OUTPUT);

  WiFi.hostname("NodeMCU");
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED)
  {
    digitalWrite(led_test, LOW);
    Serial.print("Sedang Mencari");
    delay(500);
  }
  digitalWrite(led_test, HIGH);
  Serial.println("Koneksi Berhasil");
  Serial.println(WiFi.localIP());
}
void loop()
{
  float suhu = dht.readTemperature();    // Memasukkan nilai temperatur ke variabel t
  float Kelembapan = dht.readHumidity(); // Memasukkan nilai kelembapan ke variabel h
  Serial.print("Kelembapan: ");
  Serial.print(Kelembapan); // Menampilkan nilai (kelembapan)
  Serial.print(" %\t");
  Serial.print("Suhu: ");
  Serial.print(suhu); // Menampilkan nilai (suhu)
  Serial.println(" *C");
  WiFiClient client;
  HTTPClient http;
  // converted space to %20 (0x20 = space), URL encoding
  String Link = "http://192.168.43.225/14S18034_Kode%20Program/kirimdata.php?sensor=" + String(suhu) + "&sensorus=" + String(Kelembapan);
  if(http.begin(client, Link)) {
    int httpcode = http.GET();
    Serial.println("HTTP GET request: Return code " + String(httpcode));
    http.end();
  } else {
    Serial.println("HTTP GET request to \"" + String(Link) + "\" failed.");
  }
  delay(500); // Memberikan jeda selama 2000ms untuk pembacaan sensor
  if (suhu > 30) {
    digitalWrite(led, HIGH);
  } else {
    digitalWrite(led, LOW);
  }
}

Also, your PHP scripts are really unsafe. You can do SQL injection in them…

	$nilai = $_GET['sensor'];
	$nilai2 = $_GET['sensor2'];
	
	mysqli_query($konek, "update sensor set nilai_sensor ='$nilai'");
	mysqli_query($konek, "update sensor set nilai_kelembapan='$nilai2'");

hello maxgerhardt, until now my program code still failed to connect to the server. Previously I’ve updated my code. I have changed my code according to the program code you typed. The following link contains the program code and screenshot of my serial monitor. Previously, if anyone was confused in the comments section of the program code, I used Indonesian and English in the program code. I’m using Arduino IDE, Xampp, and sublime text applications. Thank you so much.

https://drive.google.com/drive/folders/1PthFY_LZ2JrtYSCJiaYCdpUbPqyfjJWR?usp=sharing

I don’t see any screenshot inside this folder you shared? Can you post the serial monitor output here?

I am sorry, now I have posted :pray:

Can you change the %20 back to a space character again? Has the IP changed?

in my program code now, I have maked the link variable like this

Link = “http://192.168.43.225/bisa/kirimdata.php?sensor=” + String(suhu);

not 14S18034_Kode Program anymore, Now. :pray:

Can you locally access the webserver with some data?

curl -v http://192.168.43.225/bisa/kirimdata.php?sensor=1.0

The command you sent is, whether to enter cmd or to the arduino IDE application?

Well it seems you don’t know about curl. It’s a command line tool for executing web queries, to test if your server returns at least 200 OK for the request. How did you test your PHP script?

You can download curl.exe from curl for Windows and use that in the commandline.

I tested my program code in a way when I created the program code, then with the help of the XAMPP web server. I can do PHP testing in the browser. Sorry, I can’t open curl.exe, even though I’ve clicked it many times

It’s not a GUI program, you need to open a cmd.exe then cd <folder where you downloaded it> and then curl -v http://192.168.43.225/bisa/kirimdata.php?sensor=1.0. What does that return?

ss
That’s the result :pray:

So your code is wrong isn’t it, it should be

$nilai = $_GET["sensor"];

not

$nilai = $GET["sensor"];

sorry, I’ve fixed it. That’s because of the impact when I try to redo my code from scratch. And I’ve uploaded the program to the ESP8266 but the connection still fails to the server
Capture

<?php  
	$konek = mysqli_connect("localhost", "root", "", "bisa"); 
	$nilai = $_GET["sensor"];

	mysqli_query($konek, "UPDATE sensor SET nilai_sensor='$nilai'");
?>

What does the previous curl command return now?

Add

build_flags = -DCORE_DEBUG_LEVEL=5

to the platformio.ini and reupload the firmware. What does it say now?