My daughter has a school project to make any useful invention. She chose to make a device named the Plant-O-Meter, and it will display the temperature,humidity,and soil moisture level of a plant on any device with an internet browser. She is using a DHT11 for temp and humidity and a capacitive soil sensor to read soil moisture, and while I know this is a Arduino forum, and we are using an esp32 due to size and built in wifi, we are using the Arduino IDE, this seems to be a programing problem not a hardware problem.
the sensor seems to be working without a problem if we use
const int soilSensor = 15;
int soilVal = 0;
void setup() {
Serial.begin(115200);
}
void loop()
{
soilVal = analogRead(soilSensor);
int soil = map(soilVal, 3350, 1500, 0, 100);
soil = constrain(soil, 0, 100);
Serial.print(soilVal);
Serial.print(">>>>>");
Serial.println(soil);
delay(2000);
}
But when we add it to the DHT11 code (works without a problem) it is not reading the sensor.
#include <WiFi.h>
#include <WebServer.h>
#include <WiFiClient.h>
#include "DHT.h"
#define DHTPIN 4
#define DHTTYPE DHT11
const char* ssid = "Plant-O-Meter";
const char* password = "SHARKTANK2019";
const int soilSensor = 15;
int soilVal = 0;
IPAddress local_ip(192,168,1,1);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);
WebServer server(80);
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
dht.begin();
Serial.begin(115200);
delay(100);
WiFi.mode(WIFI_AP);
server.begin();
WiFi.softAP(ssid,password);
WiFi.softAPConfig(local_ip, gateway, subnet);
server.on("/", handle_OnConnect);
delay(100);
}
void loop()
{
server.handleClient();
}
void handle_OnConnect()
{
float temp = dht.readTemperature(true);
float humid = dht.readHumidity();
soilVal = analogRead(soilSensor);
int soil = map(soilVal, 3350, 1500, 0, 100);
//soil = constrain(soil, 0, 100);
server.send(200, "text/html", SendHTML(temp,humid, soil));
}
void handle_NotFound(){server.send(404, "text/plain", "Not Found");}
String SendHTML(float temp, float humid, int soil)
{
String ptr = "<!DOCTYPE html> <html>\n";
ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
ptr +="<title>Plant-O-Meter </title>\n";
ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n";
ptr +="p {font-size: 24px;color: #444444;margin-bottom: 10px;}\n";
ptr +="</style>\n";
ptr +="<script>\n";
ptr +="setInterval(loadDoc,250);\n";
ptr +="function loadDoc() {\n";
ptr +="var xhttp = new XMLHttpRequest();\n";
ptr +="xhttp.onreadystatechange = function() {\n";
ptr +="if (this.readyState == 4 && this.status == 200) {\n";
ptr +="document.body.innerHTML =this.responseText}\n";
ptr +="};\n";
ptr +="xhttp.open(\"GET\", \"/\", true);\n";
ptr +="xhttp.send();\n";
ptr +="}\n";
ptr +="</script>\n";
ptr +="</head>\n";
ptr +="<body>\n";
ptr +="<div id=\"webpage\">\n";
ptr +="<h1>Plant-O-Meter</h1>\n";
ptr +="<p>Temperature: ";
ptr +=temp;
ptr +="°F</p>";
ptr +="<p>Humidity: ";
ptr +=humid;
ptr +="%</p>";
ptr +="<p>Soil Moisture: ";
ptr +=soil;
ptr +="%</p>";
ptr +="</div>\n";
ptr +="</body>\n";
ptr +="</html>\n";
return ptr;
}
The DHT11 updates and displays properly, but the soil sensor will not.
I am sorry if this is sloppy, I have no background in programing, and she is only 8 years old. This project is due in 3 days, any help at all we would be very grateful. I would like to make as few changes to the code as possible, she will need to present this in a few days, she did all the programing so far, and understands all of it so far, but don't want to over load her with a ton of new stuff in such a short time frame.