I have an esp32 devkit v1 board and connected 3 sensors which are photoresistor (ky-018), dht-11 and capacitive soil moisture sensor.
dht-11 is connected to D14 (works)
photoresistor(ky-018) connected to D13 (doesn't work)
capacitive soil moisture sensor connected to D15 (doesn't work)
Code: Select all
#include "DHT.h"
#define DHTPIN 14 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11
#include <WiFi.h>
const char* ssid = "Cgates_E031F1"; // ESP32 and ESP8266 uses 2.4GHZ wifi only
const char* password = "60E541C32F";
DHT dht(DHTPIN, DHTTYPE);
const byte lightPin = 13;
int lightReading;
int lightReadingpercent=0;
const int RELAY_PIN = 15; // the Arduino pin, which connects to the IN pin of relay
int sensorPin27 = 15; // soil moisture
const int AirValue = 4095; //you need to replace this value with Value_1
const int WaterValue = 2200; //you need to replace this value with Value_2
const int SensorPin = 15;
int soilMoistureValue = 0;
int soilmoisturepercent=0;
const int Lightvalue = 0;
const int Darkvalue = 4095;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
// begin Wifi connect
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(2000);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
//end Wifi connect
pinMode(RELAY_PIN, OUTPUT);//relay
pinMode (sensorPin27, INPUT); // soil moisture
Serial.println(F("DHTxx test!")); //dht
;
dht.begin();
}
void loop() {
// put your main code here, to run repeatedly:
lightReading = analogRead(lightPin); //0-4095 12bit -- esp8266 10bit 0-1023 -- arduino 8bit 0-254
Serial.print("Light reading = ");
lightReadingpercent = map(lightReading, Darkvalue, Lightvalue, 0, 100 );
Serial.print(lightReadingpercent);
Serial.println(" %");
Serial.println();
delay(500);
soilMoistureValue = analogRead(SensorPin); //put Sensor insert into soil
soilmoisturepercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100);
if(soilmoisturepercent > 100)
{
Serial.println("Soil moisture ");
Serial.println("100 %");
delay(500);
}
else if(soilmoisturepercent <0)
{
Serial.println("Soil moisture ");
Serial.println("0 %");
delay(500);
}
else if(soilmoisturepercent >=0 && soilmoisturepercent <= 100)
{
Serial.println("Soil moisture "); //go to next line
Serial.print(soilmoisturepercent);
Serial.println("%");
delay(500); // soil end
}
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Serial.print(F(" Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("C "));
Serial.print(f);
Serial.print(F("F Heat index: "));
Serial.print(hic);
Serial.print(F("C "));
Serial.print(hif);
Serial.println(F("F"));
//Serial.println(sensorPin27);
int moisture_level = map(analogRead(sensorPin27), 0, 1023, 100, 0); //read sensor and scale the reading 100% to 0%
delay(500); //wait 0.5seconds