Skip to content

Instantly share code, notes, and snippets.

@jenschr jenschr/HiGrow_server.ino
Last active Jul 22, 2019

Embed
What would you like to do?
Arduino example for using the HiGrow board based on ESP32 as a server, so you can check in on your plant to see how it's doing
/*
* Arduino example for using the HiGrow board based on ESP32 as a server, so you can check in on your plant to see how it's doing
*
* Details on the hardware and how to use it can be found on:
* http://flashgamer.com/blog/comments/higrow-esp32-moisture-and-temperature-sensor#capacitive
*/
#include "DHT.h"
#include <WiFi.h>
#define DHTTYPE DHT11
const int LEDPIN = 16;
const int DHTPIN = 22;
const int SOILPIN = 32;
const char* ssid = "yourssid";
const char* password = "yourpasswd";
WiFiServer server(80);
DHT dht(DHTPIN, DHTTYPE);
int waterlevel;
float humidity;
float temperature;
float hic;
unsigned long nextMeasurement;
void setup() {
Serial.begin(115200);
pinMode(LEDPIN,OUTPUT);
// setup wifi
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());
// start the server
server.begin();
// Turn LED off to show that we're connected
digitalWrite(LEDPIN, HIGH);
}
void loop() {
// always maintain the wifi
maintainServer();
// Update measurements only each second
unsigned long currentTime = millis();
if ( currentTime > nextMeasurement )
{
waterlevel = analogRead(SOILPIN);
humidity = dht.readHumidity();
temperature = dht.readTemperature();
hic = dht.computeHeatIndex(temperature, humidity, false);
nextMeasurement = currentTime + 1000;
}
}
void maintainServer() {
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
Serial.println("New Client."); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// the content of the HTTP response follows the header:
client.print("<p>Moisture: ");
client.print(waterlevel);
client.print("<br>");
client.print("Humidity: ");
client.print(humidity);
client.print("<br>");
client.print("Measured temp: ");
client.print(temperature);
client.print("<br>");
client.print("Perceived temp: ");
client.print(hic);
client.print("</p>");
client.print("<p>Click <a href=\"/H\">here</a> to turn the LED off.<br>");
client.print("Click <a href=\"/L\">here</a> to turn the LED on.</p>");
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
} else { // if you got a newline, then clear currentLine:
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
// Check to see if the client request was "GET /H" or "GET /L":
if (currentLine.endsWith("GET /H")) {
digitalWrite(LEDPIN, HIGH); // GET /H turns the LED on
}
if (currentLine.endsWith("GET /L")) {
digitalWrite(LEDPIN, LOW); // GET /L turns the LED off
}
}
}
// close the connection:
client.stop();
Serial.println("Client Disconnected.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.