202 lines
4.8 KiB
C++
202 lines
4.8 KiB
C++
#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);
|
|
}
|