TKK_E32211895/full.ino

138 lines
4.0 KiB
C++

#include <WiFi.h>
#include <HTTPClient.h>
#include <LiquidCrystal_I2C.h>
#include "HX711.h"
// Koneksi Wi-Fi
const char* ssid = "Dimas";
const char* password = "12345678";
// Endpoint REST API
const char* serverName = "http://192.168.156.183/simget";
// Pin untuk HX711 Load Cell
const int LOADCELL_DOUT_PIN = 19;
const int LOADCELL_SCK_PIN = 18;
// Pin untuk Soil Moisture Sensor
const int SOIL_MOISTURE_PIN = 35;
// Inisialisasi HX711
HX711 scale;
float calibration_factor = 197.5; // Change this value to match your setup
LiquidCrystal_I2C lcd(0x27, 16, 2); // Alamat I2C untuk LCD 16x2
void setup() {
Serial.begin(115200);
// Koneksi ke Wi-Fi
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("Connected to WiFi");
// Inisialisasi HX711
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale();
scale.tare(); // Reset the scale to 0
long zero_factor = scale.read_average(); // Get a baseline reading
Serial.print("Zero factor: "); // This can be used to remove the need to tare the scale.
Serial.println(zero_factor);
lcd.init();
// turn on LCD backlight
lcd.backlight();
lcd.begin(16, 2); // Inisialisasi LCD 16x2
lcd.backlight(); // Nyalakan backlight
lcd.setCursor(0, 0);
lcd.print("Moisture:");
lcd.setCursor(0, 1);
lcd.print("LOADCELL: gram");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// Dapatkan tipe dari /api/tipe.php
http.begin(String(serverName) + "/api/tipe.php");
Serial.println(String(serverName) + "/api/tipe.php");
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String tipe = http.getString();
Serial.println("Tipe: " + tipe);
if (tipe == "1" || tipe == "2") {
// Baca data dari Load Cell
long berat = scale.get_units(10); // Baca berat dari sensor load cell
String path = (tipe == "1") ? "/api/berat_basah.php" : "/api/berat_sample.php";
lcd.setCursor(10, 1);
lcd.print(" "); // Hapus nilai sebelumnya
lcd.setCursor(10, 1);
lcd.print(berat);
lcd.print(" gram");
scale.set_scale(calibration_factor);
if (Serial.available()) {
char temp = Serial.read();
if (temp == '+' || temp == 'a') {
calibration_factor += 10; // Adjust by small steps
}
else if (temp == '-' || temp == 'z') {
calibration_factor -= 10; // Adjust by small steps
}
}
// Kirim data Load Cell ke REST API
http.begin(String(serverName) + path + "?berat=" + String(berat));
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println("Response: " + response);
} else {
Serial.println("Error sending Load Cell data: " + String(httpResponseCode));
}
}
} else {
Serial.println("Error getting tipe: " + String(httpResponseCode));
}
http.end();
// Baca data dari sensor Soil Moisture
int kelembapan = analogRead(SOIL_MOISTURE_PIN);
// 4095 0%
// 0 100%
kelembapan = (kelembapan - 4095) * -1;
float persenKelembapan = map(kelembapan, 0, 4095, 0, 100);
Serial.println(persenKelembapan);
lcd.setCursor(10, 0);
lcd.print(" "); // Hapus nilai sebelumnya
lcd.setCursor(10, 0);
lcd.print(persenKelembapan);
lcd.print("%");
// Kirim data Soil Moisture ke REST API
http.begin(String(serverName) + "/api/kelembapan.php?kelembapan=" + String(persenKelembapan));
httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println("Response: " + response);
} else {
Serial.println("Error sending Soil Moisture data: " + String(httpResponseCode));
}
http.end();
}
// Tunggu sebelum mengirim data lagi
delay(1000); // 1 menit
}