#define BLYNK_TEMPLATE_ID "TMPL3y_0KXnKn"
#define BLYNK_TEMPLATE_NAME "soil monitoring system"
#define BLYNK_AUTH_TOKEN "rhnZrYB3VGakgpJmpsM7phc73JjUTULR"
#define VIRTUAL_PIN V0
#include <BlynkSimpleEsp32.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define RE 8
#define DE 7
char auth[] = "rhnZrYB3VGakgpJmpsM7phc73JjUTULR";
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
const byte nitro[] = {0x01, 0x03, 0x00, 0x1e, 0x00, 0x01, 0xe4, 0x0c};
const byte phos[] = {0x01, 0x03, 0x00, 0x1f, 0x00, 0x01, 0xb5, 0xcc};
const byte pota[] = {0x01, 0x03, 0x00, 0x20, 0x00, 0x01, 0x85, 0xc0};
byte values[11];
#include <DHT.h>
#include <LiquidCrystal.h>
// Define pin numbers
#define DHT_PIN 16 // Pin to which DHT22 sensor is connected
#define BUZZER_PIN 17 // Pin to which the buzzer is connected
#define MOISTURE_SENSOR_PIN 34 // Pin to which the moisture sensor is connected
// Define the type of DHT sensor
#define DHT_TYPE DHT22 // Change to DHT11 if using DHT11 sensor
DHT dht(DHT_PIN, DHT_TYPE);
// Initialize the LCD (pins connected to the LCD: RS, E, D4, D5, D6, D7)
LiquidCrystal lcd(2, 4, 5, 18, 19, 21);
int red_led = 25;
int green_led = 26;
int blue_led = 27;
void setup() {
pinMode(RE, OUTPUT);
pinMode(DE, OUTPUT);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialize with the I2C addr 0x3C (128x64)
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(500);
display.clearDisplay();
display.setCursor(25, 15);
display.setTextSize(1);
display.setTextColor(WHITE);
display.println(" NPK Sensor");
display.setCursor(25, 35);
display.setTextSize(1);
display.print("Menghitung ...");
display.display();
delay(3000);
// Initialize the LCD
lcd.begin(16, 2);
lcd.print("Temp:");
lcd.setCursor(0, 1);
lcd.print("Humidity:");
// Initialize the DHT sensor
dht.begin();
// Set up the buzzer pin
pinMode(BUZZER_PIN, OUTPUT);
// Initialize serial communication
Serial.begin(115200);
// Set up the pins for the moisture sensor LEDs
pinMode(red_led, OUTPUT); // Red LED
pinMode(green_led, OUTPUT); // Green LED
pinMode(blue_led, OUTPUT); // Blue LED
Blynk.begin(auth, ssid, pass);
while (Blynk.connect() == false) {
Serial.println("Couldn't connect to Blynk.");
}
// Initialize LEDC for RGB LED control
ledcSetup(0, 5000, 8); // LEDC channel 0, 5 kHz frequency, 8-bit resolution for red LED
ledcSetup(1, 5000, 8); // LEDC channel 1, 5 kHz frequency, 8-bit resolution for green LED
ledcSetup(2, 5000, 8); // LEDC channel 2, 5 kHz frequency, 8-bit resolution for blue LED
// Attach LEDC channels to the GPIO pins
ledcAttachPin(red_led, 0); // Red LED
ledcAttachPin(green_led, 1); // Green LED
ledcAttachPin(blue_led, 2); // Blue LED
}
void loop() {
byte val1, val2, val3;
val1 = random(0, 350); // Generate random value for nitrogen (0-255)
delay(250);
val2 = random(0, 350); // Generate random value for phosphorous (0-255)
delay(250);
val3 = random(0, 330); // Generate random value for potassium (0-255)
delay(250);
Serial.println();
Serial.print("Nitrogen: ");
Serial.print(val1);
Serial.println(" mg/kg");
Serial.print("Phosphorous: ");
Serial.print(val2);
Serial.println(" mg/kg");
Serial.print("Potassium: ");
Serial.print(val3);
Serial.println(" mg/kg");
Serial.println();
delay(2000);
display.clearDisplay();
display.setTextSize(2);
display.setCursor(0, 5);
display.print("N: ");
display.print(val1);
display.setTextSize(1);
display.print(" mg/kg");
display.setTextSize(2);
display.setCursor(0, 25);
display.print("P: ");
display.print(val2);
display.setTextSize(1);
display.print(" mg/kg");
display.setTextSize(2);
display.setCursor(0, 45);
display.print("K: ");
display.print(val3);
display.setTextSize(1);
display.print(" mg/kg");
display.display();
// Read the moisture sensor value
int moistureValue = analogRead(MOISTURE_SENSOR_PIN);
// Determine moisture level and set LEDs accordingly
if (moistureValue <= 100) {
led_color(0, 0, 0); // Turn off RGB LED
} else if (moistureValue > 100 && moistureValue <= 499) {
led_color(0, 255, 0); // Green
} else if (moistureValue > 499 && moistureValue <= 1023) {
led_color(255, 0, 0); // Red
}
// Read temperature and humidity from the DHT sensor
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Display temperature on the LCD
lcd.setCursor(6, 0);
lcd.print(temperature, 1);
lcd.print("C");
// Display humidity on the LCD
lcd.setCursor(10, 1);
lcd.print(humidity, 1);
lcd.print("%");
// Check if temperature is above a threshold value and activate the buzzer
if (temperature > 30.0 || temperature < 0.0) {
digitalWrite(BUZZER_PIN, HIGH);
tone(BUZZER_PIN, 10000); // Generate a tone of 1000 Hz
//Blynk.virtualWrite(VIRTUAL_PIN, value)
} else {
digitalWrite(BUZZER_PIN, LOW);
noTone(BUZZER_PIN); // Stop the tone
}
}
void led_color(int red_value, int green_value, int blue_value) {
ledcWrite(0, red_value); // Set red LED brightness
ledcWrite(1, green_value); // Set green LED brightness
ledcWrite(2, blue_value); // Set blue LED brightness
}