Hi, I have began to undertake a project that allows for control over a pump and ventilation fans in a small greenhouse as well as being able to view data on temperature, humidity and soil moisture remotely. Using an ESP8266 I have set up a soft access point and a web server. The server should display the data wanted and allow for an on/off button for the pump and for the fan. Currently the site works in the way I want it to. It displays this data (Dummy numbers for now, planning to code in the sensors after) and when I click the on button for the pump or fan it updates the page and it becomes an off button. However, the output pins are not doing what I need them to. They are attached to relays so that when there is output the relay switches and allows the pump/fan to draw current. Currently, only pin 13 is giving an output and it is just constantly turning on and off regardless of what buttons I press on the website. Below is the code and I will link the device I am using. Please help me make this work in the way I would like it to.
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
/* Put your SSID & Password */
const char* ssid = "Greenhouse";
const char* password = "123456789";
ESP8266WebServer server(80);
bool PumpStatus = LOW;
bool FanStatus = LOW;
int Temp = 30;
int Hum = 50;
int Moist = 50;
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT); //Pump
pinMode(12, OUTPUT); //Fan
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("Access Point IP:");
Serial.println(myIP);
server.on("/", handle_OnConnect);
server.on("/bothOn", handle_bothOn);
server.on("/bothOff", handle_bothOff);
server.on("/fanOn", handle_fanOn);
server.on("/pumpOn", handle_pumpOn);
server.onNotFound(handle_NotFound);
server.begin();
//Serial.println("HTTP Server Started");
}
void loop() {
server.handleClient();
}
void handle_OnConnect() {
PumpStatus = LOW;
FanStatus = LOW;
digitalWrite(13, LOW);
digitalWrite(12, LOW);
Serial.println("Pump: OFF, Fan: OFF");
server.send(200, "text/html", updateWebpage(PumpStatus, FanStatus));
}
void handle_bothOn() {
PumpStatus = HIGH;
FanStatus = HIGH;
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
Serial.println("Pump: ON, Fan: ON");
server.send(200, "text/html", updateWebpage(PumpStatus, FanStatus));
}
void handle_bothOff() {
PumpStatus = LOW;
FanStatus = LOW;
digitalWrite(13, LOW);
digitalWrite(12, LOW);
Serial.println("Pump: OFF, Fan: OFF");
server.send(200, "text/html", updateWebpage(PumpStatus, FanStatus));
}
void handle_fanOn() {
PumpStatus = LOW;
FanStatus = HIGH;
digitalWrite(13, LOW);
digitalWrite(12, HIGH);
Serial.println("Pump: OFF, Fan: ON");
server.send(200, "text/html", updateWebpage(PumpStatus, FanStatus));
}
void handle_pumpOn() {
PumpStatus = HIGH;
FanStatus = LOW;
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
Serial.println("Pump: ON, Fan: OFF");
server.send(200, "text/html", updateWebpage(PumpStatus, FanStatus));
}
void handle_NotFound(){
server.send(404, "text/plain", "Not found");
}
String updateWebpage(uint8_t PumpStatus,uint8_t FanStatus){
String page = "<!DOCTYPE html> <html>\n";
page += "<style>\n";
page += "table, th, td {border:1px solid black;}\n";
page += ".block {display: block;width: 100%;border: none;background-color: #04AA6D;color: white;padding: 14px 28px;font-size: 16px;cursor: pointer;text-align: center;}\n";
page += ".block:hover {background-color: #ddd;color: black;}\n";
page += "</style>\n";
page += "<body>\n";
page += "<h2>Greenhouse Control Panel</h2>\n";
page += "<table style=\"width:100%\">\n";
page += "<tr>\n";
page += "<th>Temperature (° C)</th>\n";
page += "<th>Humidity (%)</th>\n";
page += "<th>Soil Moisture (%)</th>\n";
page += "</tr>\n";
page += "<tr>\n";
page += "<th>" + String(Temp) + "</th>\n";
page += "<th>" + String(Hum) + "</th>\n";
page += "<th>" + String(Moist) + "</th>\n";
page += "</tr>";
if(PumpStatus && FanStatus){
page += "<a href = \"/fanOn\"<button class=\"block\">Turn Pump Off</button></a>\n";
page += "<a href = \"/pumpOn\"<button class=\"block\">Turn Fan Off</button></a>\n";
}
else if(PumpStatus && !FanStatus){
page += "<a href = \"/bothOff\"<button class=\"block\">Turn Pump Off</button></a>\n";
page += "<a href = \"/bothOn\"<button class=\"block\">Turn Fan On</button></a>\n";
}
else if(!PumpStatus && FanStatus){
page += "<a href = \"/bothOn\"<button class=\"block\">Turn Pump On</button></a>\n";
page += "<a href = \"/bothOff\"<button class=\"block\">Turn Fan Off</button></a>\n";
}
else{
page += "<a href = \"/pumpOn\"<button class=\"block\">Turn Pump On</button></a>\n";
page += "<a href = \"/fanOn\"<button class=\"block\">Turn Fan On</button></a>\n";
}
page +="</body>\n";
page +="</html>\n";
return page;
}
https://www.jaycar.com.au/duinotech-uno-r3-main-board-with-wi-fi/p/XC4411