Upload files to "/"

This commit is contained in:
alfianina 2024-07-16 12:33:29 +07:00
parent e3994fd179
commit 405c89cfd1
5 changed files with 499 additions and 0 deletions

11
get_data.php Normal file
View File

@ -0,0 +1,11 @@
<?php
require_once('koneksi.php');
$myArray = array();
if ($result = mysqli_query($conn, "SELECT * FROM isi_data WHERE id = 1")) {
while ($row = mysqli_fetch_object($result)) {
$myArray[] = $row;
}
mysqli_close($conn);
header('Content-Type: application/json');
echo json_encode($myArray[0]);
}

6
koneksi.php Normal file
View File

@ -0,0 +1,6 @@
<?php
define('HOST','localhost');
define('USER','root');
define('DB','lifim');
define('PASS','');
$conn = new mysqli(HOST,USER,PASS,DB) or die('Connetion error to the database');

68
lifim.sql Normal file
View File

@ -0,0 +1,68 @@
-- phpMyAdmin SQL Dump
-- version 5.2.1
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Feb 23, 2024 at 06:50 AM
-- Server version: 10.4.28-MariaDB
-- PHP Version: 8.1.17
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `lifim`
--
-- --------------------------------------------------------
--
-- Table structure for table `isi_data`
--
CREATE TABLE `isi_data` (
`id` int(11) NOT NULL,
`ultrasonic` varchar(2) NOT NULL,
`infrared` varchar(5) NOT NULL,
`valve` int(3) NOT NULL,
`suhu` int(3) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
--
-- Dumping data for table `isi_data`
--
INSERT INTO `isi_data` (`id`, `ultrasonic`, `infrared`, `valve`, `suhu`) VALUES
(1, '36', '0', 0, 25);
--
-- Indexes for dumped tables
--
--
-- Indexes for table `isi_data`
--
ALTER TABLE `isi_data`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `isi_data`
--
ALTER TABLE `isi_data`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

201
lifimapi.ino Normal file
View File

@ -0,0 +1,201 @@
#include <Arduino.h>
#include "HX711.h"
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ArduinoJson.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WebServer.h>
const char* ssid = "Asus_Rog_E"; // Ganti dengan SSID WiFi Anda
const char* password = "cekduluaja"; // Ganti dengan password WiFi Anda
const int TRIGPIN = D5;
const int ECHOPIN = D3;
const int oneWireBus = D6; //DS18B20
const int irSensorPin = D1; //Sensor IR
const int LOADCELL_DOUT_PIN = D7;
const int LOADCELL_SCK_PIN = D8;
const int relay = D2;
const float beratKalengkosong = 56.9;
const float beratKalengpenuh = 471.0;
const float tinggiTabung = 36;
const float persentaseTinggiTabung = 0.3; // 30%
const float suhumin = 20;
const float suhumax = 75;
long timer;
int jarak;
int suhu;
int statusRelay;
HX711 scale;
OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);
void setupLoadcell() {
Serial.println("Before setting up the scale:");
Serial.print("read: \t\t");
Serial.println(scale.read());
Serial.print("read average: \t\t");
Serial.println(scale.read_average(20));
Serial.print("get value: \t\t");
Serial.println(scale.get_value(5));
Serial.print("get units: \t\t");
Serial.println(scale.get_units(5), 1);
scale.set_scale(209.984);
scale.tare();
Serial.println("After setting up the scale:");
Serial.print("read: \t\t");
Serial.println(scale.read());
Serial.print("read average: \t\t");
Serial.println(scale.read_average(20));
Serial.print("get value: \t\t");
Serial.println(scale.get_value(5));
Serial.print("get units: \t\t");
Serial.println(scale.get_units(5), 1);
Serial.println("Successful Initialization of HX711");
}
void kirim_data(){
String postData = (String)"ultrasonic=" + jarak + "&infrared=" + readIrsensor()
+ "&valve=" + statusRelay + "&loadcell=" + suhu + "&suhu=" + suhu;
HTTPClient http;
http.begin(WiFiClient,"http://192.168.137.10/TestApi/update.php");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpCode = http.POST(postData);
String payload = http.getString();
Serial.println(postData);
Serial.println(payload);
http.end();
}
void setup() {
Serial.begin(115200);
digitalWrite(relay, HIGH);
WiFi.mode(WIFI_OFF);
delay(1000);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
Serial.print("Connecting");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //IP address assigned to your ESP
Serial.println("STARTING");
delay(1500);
Serial.println("PASTIKAN TIDAK ADA BENDA PADA LOADCELL");
delay(3000);
Serial.println("Starting Initialization of HX711");
delay(1000);
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
pinMode(relay, OUTPUT);
setupLoadcell();
sensors.begin();
}
void readUltrasonic() {
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGPIN, LOW);
timer = pulseIn(ECHOPIN, HIGH);
jarak = timer/58;
Serial.print("Jarak = ");
Serial.print(jarak);
Serial.print(" cm");
}
void readSuhu() {
sensors.requestTemperatures();
suhu = sensors.getTempCByIndex(0);
Serial.print(" Suhu: ");
Serial.print(suhu);
Serial.print("°C ");
}
void readLoadcell() {
Serial.print("BERAT: ");
Serial.print(scale.get_units(), 1);
}
bool readIrsensor() {
int irSensorValue = digitalRead(irSensorPin);
return (irSensorValue != HIGH);
}
void loop() {
readUltrasonic();
readSuhu();
readLoadcell();
if (jarak <= tinggiTabung * persentaseTinggiTabung) {
Serial.println(" Silakan isi sauce.");
digitalWrite(relay, HIGH);
statusRelay = 1;
} else {
if (suhu >= suhumin && suhu <= suhumax) {
if (!readIrsensor() && scale.get_units() < beratKalengkosong) {
Serial.println(" Silakan letakkan kaleng.");
digitalWrite(relay, HIGH);
statusRelay = 1;
}
else {
if (scale.get_units() >= beratKalengkosong && scale.get_units() <= beratKalengpenuh) {
Serial.println(" Mengisi kaleng.");
digitalWrite(relay, LOW);
statusRelay = 0;
}
else {
Serial.println(" Tidak Mengisi Kaleng");
digitalWrite(relay, HIGH);
statusRelay = 1;
}
}
} else {
Serial.println(" Sauce Panas");
digitalWrite(relay, HIGH);
statusRelay = 1;
}
kirim_data();
}
delay(1000);
}

213
lifimfirebase.ino Normal file
View File

@ -0,0 +1,213 @@
//MEMAKAI RELAY ACTIVE LOW, JADI KALAU HIGH RELAY MATI
#include <Arduino.h>
#include "HX711.h"
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WebServer.h>
#include <Firebase_ESP_Client.h>
#include <WiFiUdp.h>
#include <NTPClient.h>
// Provide the token generation process info.
#include "addons/TokenHelper.h"
// Provide the RTDB payload printing info and other helper functions.
#include "addons/RTDBHelper.h"
const char* ssid = "Asus_Rog_E"; // Ganti dengan SSID WiFi Anda
const char* password = "cekduluaja"; // Ganti dengan password WiFi Anda
#define API_KEY "AIzaSyDq2Xkt6qNszEzq_pCzEkO7QfjvGRALlQU"
#define USER_EMAIL "fikriahdiars@gmail.com"
#define USER_PASSWORD "Lifim!234"
#define DATABASE_URL "https://lifim-3e7e2-default-rtdb.asia-southeast1.firebasedatabase.app/"
// Define Firebase objects
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
// Variable to save USER UID
String uid;
// Database main path (to be updated in setup with the user UID)
String databasePath;
// Database child nodes
String UltrasonicPath = "/ultrasonic";
String irPath = "/infrared";
String valvePath = "/valve";
String suhuPath = "/suhu";
// Parent Node (to be updated in every loop)
String parentPath;
FirebaseJson json;
// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "id.pool.ntp.org");
// Variable to save current epoch time
int timestamp;
const int TRIGPIN = D8;
const int ECHOPIN = D7;
const int oneWireBus = D6; //DS18B20
const int irSensorPin = D1; //Sensor IR
const int relay = D2;
const float tinggiTabung = 35;
const float persentaseTinggiTabung = 0.3; // 30%
const float suhumin = 20;
const float suhumax = 75;
long timer;
int jarak;
int suhu;
int statusRelay;
OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);
// Timer variables (send new readings every three minutes)
unsigned long sendDataPrevMillis = 0;
unsigned long timerDelay = 3000;
unsigned long getTime() {
timeClient.update();
unsigned long now = timeClient.getEpochTime();
return now;
}
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
int i = 0;
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(1000);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println();
// Assign the api key (required)
config.api_key = API_KEY;
// Assign the user sign in credentials
auth.user.email = USER_EMAIL;
auth.user.password = USER_PASSWORD;
// Assign the RTDB URL (required)
config.database_url = DATABASE_URL;
Firebase.reconnectWiFi(true);
fbdo.setResponseSize(4096);
// Assign the callback function for the long running token generation task */
config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h
// Assign the maximum retry of token generation
config.max_token_generation_retry = 5;
// Initialize the library with the Firebase authen and config
Firebase.begin(&config, &auth);
// Getting the user UID might take a few seconds
Serial.println("Getting User UID");
while ((auth.token.uid) == "") {
Serial.print('.');
delay(1000);
}
// Print user UID
uid = auth.token.uid.c_str();
Serial.print("User UID: ");
Serial.println(uid);
// Update database path
databasePath = "/UsersData/" + uid + "/readings";
Serial.println("STARTING");
delay(1500);
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
pinMode(relay, OUTPUT);
digitalWrite(relay, HIGH);
sensors.begin();
}
void readUltrasonic() {
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGPIN, LOW);
timer = pulseIn(ECHOPIN, HIGH);
jarak = timer/58;
// jarak = tinggiTabung - jarak;
Serial.print("Jarak = ");
Serial.print(jarak);
Serial.print(" cm");
}
void readSuhu() {
sensors.requestTemperatures();
suhu = sensors.getTempCByIndex(0);
Serial.print(" Suhu: ");
Serial.print(suhu);
Serial.print("°C ");
}
bool readIrsensor() {
int irSensorValue = digitalRead(irSensorPin);
return (irSensorValue != HIGH);
}
void loop() {
if (Firebase.ready() && (millis() - sendDataPrevMillis > timerDelay || sendDataPrevMillis == 0)){
sendDataPrevMillis = millis();
readUltrasonic();
readSuhu();
if (jarak <= tinggiTabung * persentaseTinggiTabung) { //jika jarak kurang dari 30% refill sauce
Serial.println(" Silakan refill sauce.");
digitalWrite(relay, HIGH);
statusRelay = 0;
} else {
if (suhu >= suhumin && suhu <= suhumax) {
if (readIrsensor()) { //Jika kaleng terdeteksi mengisi kaleng
Serial.println(" Mengisi kaleng.");
digitalWrite(relay, LOW);
delay(5000); // DELAY ISI KALENG SAMPAI PENUH
digitalWrite(relay, HIGH);
delay (3000); //DELAY TUNGGU KALENG PINDAH / GESER
digitalWrite(relay, HIGH);
statusRelay = 1;
} else {
Serial.println(" Tidak Mengisi Kaleng"); //Jika kaleng tidak terdeteksi
digitalWrite(relay, HIGH);
statusRelay = 0;
}
} else {
Serial.println(" Sauce Panas"); //Jika suhu melebihi suhumax
digitalWrite(relay, HIGH);
statusRelay = 0;
}
parentPath= databasePath + "/" + String(timestamp);
json.set(UltrasonicPath.c_str(), String(jarak));
json.set(irPath.c_str(), String(readIrsensor()));
json.set(valvePath.c_str(), String(statusRelay));
json.set(suhuPath.c_str(), String(suhu));
Serial.printf("Set json... %s\n", Firebase.RTDB.setJSON(&fbdo, parentPath.c_str(), &json) ? "ok" : fbdo.errorReason().c_str());
}
}
}