Click here to Skip to main content
15,883,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello i'm from itenas. how do I connect esp8266 to the database. I have followed many tutorials but still can't. please tell me the code

What I have tried:

C#
#include <ESP8266WiFi.h>
#include <DHT.h>
 
#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
 
/* Parameters:
* t = temperature
* h = humidity
* i = ID
*/
const char* server = "fiksid.xyz"; // ganti dengan alamat servermu
const char* ssid = "f"; // ganti dengan SSID modem/router wifimu
const char* password = "87654321"; // ganti dengan password utk konek ke wifimu
const char* SensorID = "ESP001"; // ini sekedar identitas sensormu
 
WiFiClient client;
 
void setup() {
  Serial.begin(115200);
  delay(10);
  dht.begin();
 
  pinMode(BUILTIN_LED, OUTPUT);
  digitalWrite(BUILTIN_LED, HIGH);
 
  WiFi.begin(ssid, password);
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.print(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi Connected");
}
 
void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (isnan(h) || isnan(t)) {
    Serial.println("Gagal membaca sensor DHT");
    return;
  }
 
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C. Humidity: ");
  Serial.println(h);
 
  if (client.connect(server,8090)) {
    Serial.print("Posting data...");
    digitalWrite(BUILTIN_LED, LOW);
    Serial.println("Temperature: " + String(t) + ", Humidity: " + String(h));
 
    client.println("GET fiksid.xyz/connect.php? t=" + String(t) + "&&h=" + String(h) + "&&i=" +SensorID+ " HTTP/1.1");
    client.println("HOST: fiksid.xyz");
    client.println("Connection: close");
    client.println();
 
    /*
    while (client.connected() &amp;&amp; !client.available()) delay(1);
    while (client.connected() || client.available()) {
      char c = client.read();
      Serial.print(c);
    }
    */
    client.stop();
    Serial.println();
 
    digitalWrite(BUILTIN_LED, HIGH);
  }
 
  delay(5000); // Beri delay 5 detik sebelum polling berikutnya
}


<pre lang="PHP">
<?php
$db_amb = mysqli_connect("localhost", "user", "password", "namadb");
if (!$db_amb) die("Gagal terkoneksi ke DB Utama. Error : " . mysqli_connect_error()); 
 
$t = $_REQUEST['t'];
$h = $_REQUEST['h'];
$IP = $_SERVER['REMOTE_ADDR'];
$i = $_REQUEST['i'];
if (!empty($t) && !empty($h) && !empty($i) && !empty($IP)) {
  $s = "insert into log_sensor set IP='$IP', SensorID='$i', Temp='$t', Humid='$h', Waktu=now()";
  $r = mysqli_query($db_amb, $s);
  echo "OK";
}
else echo "ERR";
?>
Posted
Comments
CHill60 5-May-21 7:03am    
What database are you using and what happens when your run your code. If there is an error message please be precise about what it says
MUHAMAD FIKRI SIDDIQ 6-May-21 8:47am    
I use mysql for for database

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900