Remote Greenhouse Control issues

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 (&deg 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

Welcome

I see nothing wrong in your code so it is probably a hardware problem. Are you sure you have connected your relays to the correct pins ?

Edit: on the board that you are using, the IO pins are those of the Arduino Uno, not the ESP8266. Your code is for the ESP8266, so it can't control the IO pins directly. You have to make another sketch for the Uno, where it will receive Serial commands from the ESP8266 to command those pins 12 and 13.

For example, ESP8266 send Serial command "13 on", Uno receives this message, parses it, and sets the pin state.

See the documentation of the board for how to setup the dip switches to enable Serial communication between the two MCUs

1 Like

I suggest to debug the code on your workbench with LEDs + series resistors on the outputs instead of relays, which introduce other complications.

This is very helpful, thank you very much. So I would upload the current sketch to the ESP, just changing the parts where it sets digital output to high/low to instead write something to the serial monitor and then upload a sketch to the arduino that reads serial monitor inputs and sets the digital pins accordingly? If so are there functions that read and store the contents of the serial monitor?

Yes

But I noticed on this board in your link, there are 12 "esp pins" available, maybe you can use them to connect your relays :slight_smile:

1 Like

You say your output pins are connected to relays, that is a no-no. Post an annotated schematic showing how you have wired this, include all connections, power, ground, and wire over 10"/25cm in length. Links to the technical information on the hardware would be a big help.

Hi, I don't have an annotated schematic at the moment and probably won't create one to be honest as this is just a small scale project. However, if you could, I would like some further explanation as to why connecting output pins to relays is not a good idea and what I can do instead. Thanks :slight_smile:

That is easy to answer, the processor pins are not designed to drive an inductive load and the relay probably draws more power then the pin was designed to deliver. Here are some simple guidelines that cost your Arduino distributor money as you do not fry them and purchase more.
Gil's Crispy Critter Rules, they apply to processor hardware:
Rule #1. A Power Supply the Arduino is NOT!
Rule #2. Never Connect Anything Inductive to an Arduino!
Rule #3 Don't connecting or disconnecting wires with power on.
Rule #4 Do not apply power to any pin unless you know what you are doing.
LaryD's Corollarys
Coro #1 when first starting out, add a 220R resistor in series with both Input and Output pins.
Coro #2 buy a DMM (Digital Multi-meter) to measure voltages, currents and resistance.
Violating these rules tends to make crispy critters out of Arduinos.
You can buy relay modules that have a driver on the board with the relay(s) and terminals. These work OK.

https://www.jaycar.com.au/arduino-compatible-4-channel-12v-relay-module/p/XC4440

This is what I am using, think I meant a relay module and not a relay. Sorry for the mix up, this should be okay to use right?

That should work fine. Solid state relays with zero crossing are best for mains power. Also be sure to keep the wires connected to the Arduino under 10"/25cm for good results.

1 Like

What feedback are you going to get when the fan or pump actually begins operation? It's one thing to remotely turn a relay on or off, but unless you don't care if something actually happens, you need to plan for more feedback.

Yes but you need to power it off the 5v input that also powers the ardu, not power it from the ardu itself.

You also need to *put flyback diodes on any heavy loads like pumps or valve relays which are on the same power rail as the ardu input by running a diode from ground to positive (at the load terminals) or you will get ghosts in the machine.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.