analog sensor value to web server school project need help please!
Posted: Sun Dec 01, 2019 5:15 pm
My daughter is making a temperature, humidity, and soil moisture sensor for a school project (now due in 2 days) She had to make an invention and she is calling this the Plant-O-Meter. We have been able to get it mostly working except the soil sensor sending to the web server.
If we do this the sensor reads without problems in the serial monitor
but when it comes time to add it to the web server it fails
sorry if things are sloppy, I have no background in programing, just like to tinker. And she is 8 years old lol.
Any help would be appreciated. I know there are probably 1000 different way to do this, but I would love it if we had to change as little code as possible, so far she understands how it all works, and she needs to be able to explain it in just a couple days.
If we do this the sensor reads without problems in the serial monitor
Code: Select all
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);
}
Code: Select all
#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;
}
Any help would be appreciated. I know there are probably 1000 different way to do this, but I would love it if we had to change as little code as possible, so far she understands how it all works, and she needs to be able to explain it in just a couple days.