184 lines
4.5 KiB
C++
184 lines
4.5 KiB
C++
#include <Arduino.h>
|
|
#if defined(ESP32)
|
|
#include <WiFi.h>
|
|
#elif defined(ESP8266)
|
|
#include <ESP8266WiFi.h>
|
|
#endif
|
|
#include <Firebase_ESP_Client.h>
|
|
#include <LiquidCrystal_I2C.h>
|
|
#include <DHT.h>
|
|
#define DHT11_PIN 5
|
|
#define pinSensor 35
|
|
|
|
|
|
LiquidCrystal_I2C lcd(0x27,20,4);
|
|
DHT dht11(DHT11_PIN, DHT11);
|
|
unsigned int pH_Value = 0;
|
|
float Voltage = 0.0;
|
|
float voltage;
|
|
float pHValue;
|
|
int adcPH;
|
|
int air_sedang = 200;
|
|
int air_tinggi = 400;
|
|
int nilai = 0;
|
|
const int relayPIN = 17;
|
|
|
|
#include "addons/TokenHelper.h"
|
|
#include "addons/RTDBHelper.h"
|
|
|
|
#define WIFI_SSID "ARCADIA TECH"
|
|
#define WIFI_PASSWORD "PUNYAACENK"
|
|
#define API_KEY "AIzaSyBbM6_m1NNvhq92OIVYyrAiZ9fPW-EoPgA"
|
|
#define DATABASE_URL "https://hidroponik-d039f-default-rtdb.asia-southeast1.firebasedatabase.app/"
|
|
|
|
FirebaseData fbdo;
|
|
FirebaseAuth auth;
|
|
FirebaseConfig config;
|
|
unsigned long sendDataPrevMillis = 0;
|
|
int count = 0;
|
|
bool signupOK = false;
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
pinMode(5,INPUT);
|
|
pinMode(relayPIN, OUTPUT);
|
|
dht11.begin();
|
|
lcd.init();
|
|
lcd.backlight();
|
|
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
|
Serial.print("Connecting to Wi-Fi");
|
|
while (WiFi.status() != WL_CONNECTED)
|
|
{
|
|
Serial.print(".");
|
|
delay(300);
|
|
}
|
|
Serial.println();
|
|
Serial.print("Connected with IP: ");
|
|
Serial.println(WiFi.localIP());
|
|
Serial.println();
|
|
|
|
config.api_key = API_KEY;
|
|
config.database_url = DATABASE_URL;
|
|
|
|
if (Firebase.signUp(&config, &auth, "", "")){
|
|
Serial.println("ok");
|
|
signupOK = true;
|
|
}
|
|
else{
|
|
Serial.printf("%s\n", config.signer.signupError.message.c_str());
|
|
}
|
|
config.token_status_callback = tokenStatusCallback;
|
|
Firebase.begin(&config, &auth);
|
|
Firebase.reconnectWiFi(true);
|
|
}
|
|
int bacaSensor (){
|
|
nilai = analogRead(pinSensor);
|
|
return nilai;
|
|
}
|
|
|
|
|
|
void loop() {
|
|
pH_Value = 0;
|
|
for (int test_cycle = 1 ; test_cycle <= 10; test_cycle++)
|
|
{
|
|
pH_Value = pH_Value + analogRead(33);
|
|
delay (10);
|
|
}
|
|
|
|
int ketinggian = bacaSensor();
|
|
Serial.print("Ketinggian Air :" );
|
|
Serial.println(ketinggian);
|
|
delay(2000);
|
|
|
|
|
|
float humi = dht11.readHumidity();
|
|
float tempC = dht11.readTemperature();
|
|
float analog = (float) pH_Value/10.0;
|
|
float calibration_value = 21.34 - 0.1;
|
|
float ph_act = -5.70 * Voltage + calibration_value;
|
|
delay(500);
|
|
Voltage = analog * (3.3 /4095.0);
|
|
|
|
String kirimdata = ("#") + String (humi) + (",")+ String (tempC)+(",")+String (ph_act);
|
|
Serial.println(kirimdata);
|
|
|
|
lcd.clear();
|
|
|
|
if (isnan(humi) || isnan(tempC))
|
|
{
|
|
lcd.setCursor(0, 0);
|
|
lcd.print("Failed");
|
|
}
|
|
else
|
|
{
|
|
lcd.setCursor(0, 0);
|
|
lcd.print("PH : ");
|
|
lcd.print(ph_act);
|
|
lcd.print("ppm");
|
|
|
|
lcd.setCursor(0, 1);
|
|
lcd.print("SUHU: ");
|
|
lcd.print(dht11.readTemperature());
|
|
lcd.write((char)223);
|
|
lcd.print("C");
|
|
|
|
lcd.setCursor(0, 2);
|
|
lcd.print("HUMI: ");
|
|
lcd.print(dht11.readHumidity());
|
|
lcd.print("%");
|
|
|
|
if(ketinggian <= 1000)
|
|
{
|
|
digitalWrite(relayPIN, HIGH);
|
|
lcd.setCursor(0, 3);
|
|
lcd.print("AIR : HABIS ");
|
|
}
|
|
else if(ketinggian > 1000)
|
|
{
|
|
digitalWrite(relayPIN, LOW);
|
|
lcd.setCursor(0, 3);
|
|
lcd.print("AIR : PENUH ");
|
|
}
|
|
|
|
|
|
|
|
}
|
|
if (Firebase.ready() && signupOK && (millis() - sendDataPrevMillis > 15000 || sendDataPrevMillis == 0)){
|
|
sendDataPrevMillis = millis();
|
|
// Write an Int number on the database path test/int
|
|
if (Firebase.RTDB.setInt(&fbdo, "test/Suhu", tempC)){
|
|
Serial.println("PASSED");
|
|
Serial.println("PATH: " + fbdo.dataPath());
|
|
Serial.println("TYPE: " + fbdo.dataType());
|
|
}
|
|
else {
|
|
Serial.println("FAILED");
|
|
Serial.println("REASON: " + fbdo.errorReason());
|
|
}
|
|
count++;
|
|
|
|
// Write an Float number on the database path test/float
|
|
if (Firebase.RTDB.setFloat(&fbdo, "test/Humidity", humi)){
|
|
Serial.println("PASSED");
|
|
Serial.println("PATH: " + fbdo.dataPath());
|
|
Serial.println("TYPE: " + fbdo.dataType());
|
|
}
|
|
else {
|
|
Serial.println("FAILED");
|
|
Serial.println("REASON: " + fbdo.errorReason());
|
|
}
|
|
|
|
if (Firebase.RTDB.setFloat(&fbdo, "test/PH", ph_act)){
|
|
Serial.println("PASSED");
|
|
Serial.println("PATH: " + fbdo.dataPath());
|
|
Serial.println("TYPE: " + fbdo.dataType());
|
|
}
|
|
else {
|
|
Serial.println("FAILED");
|
|
Serial.println("REASON: " + fbdo.errorReason());
|
|
}
|
|
|
|
}
|
|
|
|
}
|