Upload files to "arduino cloud"

This commit is contained in:
alfan_al_ikhsan 2024-07-25 08:18:47 +07:00
commit 65053c42ac
1 changed files with 214 additions and 0 deletions

View File

@ -0,0 +1,214 @@
/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled"
https://create.arduino.cc/cloud/things/fefef12a-9d1d-4f50-b0a4-8cd6e1d3693f
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
float ph;
int ldr;
int soil;
bool relay;
bool relay1;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
#include <LiquidCrystal_I2C.h>
#define SOIL_PIN A0 // Pin untuk sensor SOIL (input)
#define relayPin 2 // Pin untuk relay (output soil)
#define wet 210
#define dry 1023
#define LDR_PIN A1 // Pin untuk sensor LDR (input)
#define relay1Pin 4 // Pin untuk relay (output)
#define dark 1023
#define bright 0
#define analogInPin A2 //sambungkan kabel hitam (output) ke pin A2
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Deklarasi fungsi
void onRelayChange();
void onRelay1Change();
void onSoilChange();
void onLdrChange();
void onPhChange();
int sensorValue = 0; //ADC value from sensor
float outputValue = 0.0; //pH value after conversion
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
lcd.init();
lcd.backlight();
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
pinMode(relayPin,OUTPUT);
digitalWrite(relayPin,HIGH);
pinMode(relay1Pin, OUTPUT); // Mengatur pin relay sebagai output
digitalWrite(relay1Pin, HIGH);
// Tampilkan pesan selamat datang
lcd.setCursor(0, 0);
lcd.print("Selamat Datang");
lcd.setCursor(0, 1);
lcd.print("Project Ikhsan");
delay(1000);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information youll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
// Your code here
onRelayChange();
onRelay1Change();
onSoilChange();
onLdrChange();
onPhChange();
}
void onSoilChange() {
// Baca nilai mentah dari sensor
int rawSoil = analogRead(SOIL_PIN);
soil = map(rawSoil, dry, wet, 0, 100);
// Pastikan nilai soil berada dalam rentang yang benar
if (soil > 100) soil = 100;
if (soil < 0) soil = 0;
// Penyiraman otomatis jika kelembaban tanah di bawah 20%
if (soil <= 50) {
digitalWrite(relayPin, HIGH); // Aktifkan relay untuk menyiram tanaman
} else {
digitalWrite(relayPin, LOW); // Matikan relay setelah penyiraman selesai
}
// Display on serial monitor
Serial.print("Raw Soil Value: ");
Serial.print(rawSoil);
Serial.print(", Soil Moisture: ");
Serial.print(soil);
Serial.print("%, Relay (Watering): ");
Serial.println(digitalRead(relayPin) == HIGH ? "ON" : "OFF");
}
void onRelayChange() {
// Add your code here to act upon Relay change
if(relay){
digitalWrite(relayPin,HIGH); // relayPin ditarik ke HIGH untuk aktif
}else{
digitalWrite(relayPin,LOW); // relayPin ditarik ke LOW untuk non-aktif
}
}
void onLdrChange() {
// Baca nilai mentah dari sensor
int rawLdr = analogRead(LDR_PIN);
ldr = map(rawLdr, dark, bright, 0, 100);
// Hysteresis thresholds
const int thresholdOn = 70;
const int thresholdOff = 71;
if (ldr <= thresholdOn && !relay1) {
// Aktifkan relay untuk pencahayaan otomatis
relay1 = true;
digitalWrite(relay1Pin, HIGH);
}
else if (ldr >= thresholdOff && relay1) {
relay1 = false;
digitalWrite(relay1Pin, LOW);
}
// Display on serial monitor
Serial.print("Raw LDR Value: ");
Serial.print(rawLdr);
Serial.print(", LDR: ");
Serial.print(ldr);
Serial.print("%, Relay (Lighting): ");
Serial.println(digitalRead(relay1Pin) == HIGH ? "ON" : "OFF");
}
void onRelay1Change() {
// Add your code here to act upon Relay1 change
if(relay1){
digitalWrite(relay1Pin,HIGH); // relay1Pin ditarik ke LOW untuk aktif
}else{
digitalWrite(relay1Pin,LOW); // relay1Pin ditarik ke HIGH untuk non-aktif
}
}
void onPhChange() {
// Baca nilai analog dari sensor pH
int sensorValue = analogRead(analogInPin);
delay(500);
// Rumus didapat berdasarkan datasheet
float outputValue = (-0.0139 * sensorValue) + 7.7851;
// Periksa apakah outputValue adalah NaN
if (isnan(outputValue)) {
Serial.println(F("Error: outputValue is NaN!"));
return;
}
// Pastikan outputValue tidak negatif
if (outputValue < 0) outputValue = 0;
// Tampilkan nilai outputValue
Serial.print(F("pH Value: "));
Serial.println(outputValue);
// Tampilkan nilai RAW/ADC soil dan LDR secara bergantian pada LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Soil RAW: ");
lcd.print(analogRead(SOIL_PIN));
lcd.setCursor(0, 1);
lcd.print("Soil: ");
lcd.print(soil);
lcd.print("%");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("LDR RAW: ");
lcd.print(analogRead(LDR_PIN));
lcd.setCursor(0, 1);
lcd.print("LDR: ");
lcd.print(ldr);
lcd.print("%");
delay(2000);
// Tampilkan nilai pH
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("ADC: ");
lcd.print(sensorValue);
lcd.setCursor(0, 1);
lcd.print("pH: ");
lcd.print(outputValue);
delay(2000);
}