I recently published the firmware for a smart greenhouse controller I have been working on: an ESP32 that reads soil moisture, air temperature and light, waters the plants and runs a ventilation fan on its own, and reports everything over MQTT. The control logic is exercised in a Python simulator for now, with on-hardware sensor calibration as the next step.
The most interesting part of the project was not connecting the sensors. It was making sure the controller fails safe. Here are the three design decisions that mattered most, with code you can reuse.
1. Hysteresis, so the relays do not chatter
The naive way to control a pump is if (soil < threshold) pump on. The problem: soil moisture hovers right around the threshold, so the relay clicks on and off every few seconds. Relays die young that way, and the pump hates it too.
The fix is two thresholds instead of one. Water below 35%, stop above 55%:
if (soilPct < 35 && pumpCooldownOver()) { pumpOn(); } else if (soilPct > 55) { pumpOff(); } The fan gets the same treatment: on at 30 C, off at 27 C. Between the two values the controller simply keeps doing whatever it was already doing. That deadband is what makes a cheap relay board survive a real installation.
2. Every actuator gets a failsafe
Ask "what breaks this?" for each output, then code the answer:
- Pump run limit: 60 seconds maximum per cycle. If a hose pops off or a sensor lies, the pump cannot run dry for hours.
- Mandatory rest: 5 minutes between watering cycles, so soil has time to absorb water before the next reading. Without this, wet sensors read dry near the surface and you overwater.
- Sensor-loss rule: if the DHT22 stops returning valid readings, irrigation is disabled entirely. A controller that cannot sense should not act.
That last one is the rule I would put in any project: default to doing nothing when your inputs are untrustworthy.
3. Tune the logic in a simulator before touching hardware
Analog soil sensors are not comparable between boards or even between sensors, so thresholds like 35% mean nothing until calibrated. I read the raw ADC value with the probe in dry air (SOIL_DRY) and in water (SOIL_WET) and map between them.
But calibration only helps if the control rules around it are right. So the repo includes simulator/simulate.py, a small Python model of soil drying and temperature drift that runs the exact same rules as the firmware:
python simulator/simulate.py This let me catch real problems, like pump rest time being shorter than the soil absorption time, without a plant in sight. When the hardware build happens, the thresholds get calibrated per sensor, but the logic arrives already tested.
Telemetry you can actually use
Everything publishes to MQTT every 10 seconds as one JSON object:
{"temp_c":26.4,"humidity":61.0,"soil_pct":48,"light_pct":72,"pump":0,"fan":0,"manual":0} One topic, one payload, so Node-RED or Home Assistant can chart it with zero parsing work. There is also a manual override mode through greenhouse/1/cmd/* topics, because a system you cannot override is a system you will eventually have to unplug.
Takeaway
Most of the value in an automation project is in the boring parts: hysteresis, timeouts, rest periods, and refusing to act on bad data. The sensor wiring takes an afternoon. Getting the failure behavior right is what makes it something you would leave running.
Full firmware, wiring table and the simulator are on GitHub: smart-greenhouse-esp32. Feedback and war stories from your own builds are very welcome.
