analog sensor value to web server school project need help please!

zasilom
Posts: 3
Joined: Sun Dec 01, 2019 5:02 pm

analog sensor value to web server school project need help please!

Postby zasilom » 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

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);
  

}
but when it comes time to add it to the web server it fails

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 +="&deg;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;
}
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.

costaud
Posts: 53
Joined: Wed Dec 30, 2015 5:09 pm

Re: analog sensor value to web server school project need help please!

Postby costaud » Mon Dec 02, 2019 2:57 am

I think GPIO15 maps to ADC2 on esp32 and ADC2 should not work with WIFI module for now.

Please use one channel from GPIO32~GPIO39(connect the sensor to that GPIO), which maps to ADC1 on esp32, and have a try.

Hope this would help your daughter :)

chegewara
Posts: 872
Joined: Wed Jun 14, 2017 9:00 pm

Re: analog sensor value to web server school project need help please!

Postby chegewara » Tue Dec 03, 2019 5:06 am

Where is the school that 8 yo kids are building such complicated projects?

Try to move this code from handle_OnConnect to loop:

Code: Select all

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);
  

Who is online

Users browsing this forum: No registered users and 9 guests