aurdino

mail@pastecode.io avatar
unknown
c_cpp
a month ago
3.4 kB
1
Indexable
Never
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
#include <WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
//#include <Adafruit_BMP085.h>

#define I2C_ADDR 0x27
#define BACKLIGHT_PIN 2 
#define DHTPIN 4
#define DHTTYPE DHT11
#define LCD_COLUMNS 16
#define LCD_ROWS 2

#define MOISTURE_PIN 15


//Adafruit_BMP085 bmp;
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_ROWS);
/************************* Adafruit.io Setup *********************************/
#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883
#define AIO_USERNAME    "Venky123111"
#define AIO_KEY         "aio_gBKq48QgC2gGRWEfdWo0PtjFCbx2"

WiFiClient client;

Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_USERNAME, AIO_KEY);
Adafruit_MQTT_Publish temperature = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/temperature");
Adafruit_MQTT_Publish humidity = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/humidity");
Adafruit_MQTT_Publish Rain = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/rain");
Adafruit_MQTT_Publish Moisture = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/moisture");

//Adafruit_MQTT_Publish pressure = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/pressure");

void setup() {
  pinMode(2,INPUT); //rain
  pinMode(5,OUTPUT);
  pinMode(MOISTURE_PIN, INPUT);


  Serial.begin(9600);
  dht.begin();
    // Initialize the LCD
  lcd.init();

  // Turn on the backlight
  lcd.backlight();

  // Connect to Wi-Fi
  WiFi.begin("OnePlus 9R", "okay...?");
  Serial.print("Connecting to Wi-Fi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Connected to Wi-Fi!");

  // Connect to Adafruit IO
  mqtt.connect();
}

void loop() {
  delay(7000);

  int h = dht.readHumidity();
  float t = dht.readTemperature();
  float rain = digitalRead(2);
  int moistureValue = digitalRead(MOISTURE_PIN);

  rain = map(rain,0,1,1,0);
  moistureValue = map(moistureValue, 0, 1, 1, 0);

if(moistureValue == 1){
  Serial.println("Soil Moisture Level LOW");
  /*lcd.setCursor(9, 0);
  lcd.print("Raining");
  digitalWrite(5,HIGH);*/
}
if(moistureValue != 1){
  Serial.println("Soil Moisture Level HIGH");

}
  
   if (rain == 1){
  Serial.println("It is raining ");
  lcd.setCursor(8, 1);
  lcd.print("Raining");
  digitalWrite(5,HIGH);
 }
 if (rain != 1){
  Serial.println("It is not Raining ");
  lcd.setCursor(8, 1);
  lcd.print("       ");
  digitalWrite(5,LOW);
 }

  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t Temperature: ");
  Serial.print(t);
  Serial.println(" *C");

  lcd.setCursor(0, 0);
  lcd.print("Temp:");
  lcd.print(t);
  lcd.print("C");
  
  lcd.setCursor(0, 1);
  lcd.print("Hum:");
  lcd.print(h);
  lcd.print("%");


  // Publish data to Adafruit IO
  if (!temperature.publish(t)) {
    Serial.println(F("Failed to publish temperature to Adafruit IO!"));
  }
  if (!humidity.publish(h)) {
    Serial.println(F("Failed to publish humidity to Adafruit IO!"));
  }
  if (! Rain.publish(rain)) {
    Serial.println(F("Failed to publish rain data to Adafruit IO!"));
  }

  if (! Moisture.publish(moistureValue)){
  Serial.println(F("Failed"));
  }
}
Leave a Comment