Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/nugrohoesbb/webserver_iot

Pengiriman data dari ESP8266/ESP32 ke web server
https://github.com/nugrohoesbb/webserver_iot

esp32 esp8266 iot webserver

Last synced: about 4 hours ago
JSON representation

Pengiriman data dari ESP8266/ESP32 ke web server

Awesome Lists containing this project

README

        

🛠️ Setup folder setelah download repositori

1. Paste file zip setelah download file dari repositori ke folder xampp > htdocs


2. Unzip file tersebut


3. Ubah nama folder yang di dapat setelah dilakukan unzip yang semula nama foldernya "webserver_iot" menjadi "iot"


4. Setelah nama foldernya diubah sehingga alur foldernya menjadi xampp > htdocs > iot



🛠️ Arduino IDE Code (GET METHOD)

1. Ubah SSID dan Password

```php
const char* ssid = "ENTER A VALUE";
const char* password = "ENTER A VALUE";
```

2. Isi IP untuk get data dan melihat status pengiriman data

```php
if ((WiFi.status() == WL_CONNECTED)) {
// C:\xampp\htdocs\iot\webapi\api\create.php
address ="http://(yourIP)/iot/webapi/api/create.php?suhu=";
address += String(suhu);
address += "&kelembaban=";
address += String(kelembaban) ;

http.begin(client,address); //Specify request destination
int httpCode = http.GET();//Send the request

if (httpCode > 0) { //Check the returning code
payload = http.getString(); //Get the request response payload
payload.trim();
if( payload.length() > 0 ){
Serial.println(payload + "\n");
}
} else {
Serial.print("Connected failed ");
}

http.end(); //Close connection
}else{
Serial.print("Not connected to wifi ");
Serial.println(ssid);
}
```

🛠️ Arduino IDE Code (POST METHOD)

1. Ubah SSID dan Password

```php
const char* ssid = "ENTER A VALUE";
const char* password = "ENTER A VALUE";
```

2. Isi IP untuk post data dan melihat status pengiriman data

```php
if ((WiFi.status() == WL_CONNECTED)) {
WiFiClient client;
HTTPClient http;

StaticJsonDocument<200> doc;
String url, nodemcuData;

// C:\xampp\htdocs\iot\webapi\api\create.php
url ="http://(yourIP)/iot/webapi/api/create.php";

doc["suhu"] = String(suhu);
doc["kelembaban"] = String(kelembaban);

http.begin(client,url);
http.addHeader("Content-Type", "application/json");

serializeJson(doc, nodemcuData);
Serial.print("POST data >> ");
Serial.println(nodemcuData);

int httpCode = http.POST(nodemcuData);//Send the request
String payload;
if (httpCode > 0) { //Check the returning code
payload = http.getString(); //Get the request response payload
payload.trim();
if( payload.length() > 0 ){
Serial.println(payload + "\n");
}
}

http.end(); //Close connection
}else{
Serial.print("Not connected to wifi ");Serial.println(ssid);
}
```

🛠️ PHP Code (database.php)


ubah bagian database.php sesuai kebutuhan

```php
conn = null;
try{
$this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->database_name, $this->username, $this->password);
$this->conn->exec("set names utf8");
}catch(PDOException $exception){
echo "Database could not be connected: " . $exception->getMessage();
}
return $this->conn;
}
}
?>
```

🛠️ PHP Code (create.php)


Import database.php and mc_log.php

```php
include_once '../config/database.php';
include_once '../class/mc_log.php';
```

Melakukan pengecekan dan publish data ke database

```php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// The request is using the POST method
$data = json_decode(file_get_contents("php://input"));
$item->suhu = $data->suhu;
$item->kelembaban = $data->kelembaban;
} elseif ($_SERVER['REQUEST_METHOD'] === 'GET'){
// The request is using the GET method
$item->suhu = isset($_GET['suhu']) ? $_GET['suhu'] : die('wrong structure!');
$item->kelembaban = isset($_GET['kelembaban']) ? $_GET['kelembaban'] : die('wrong structure!');
}else {
die('wrong request method');
}

if($item->createLogData()){
echo 'Data created successfully.';
} else{
echo 'Data could not be created.';
}
```