Postlad/Guides/ESP32 greenhouse monitor

ESP32 greenhouse monitor: soil moisture, air and light online

One ESP32 can watch a greenhouse bench: how wet the soil is, how warm and humid the air is, and how much light reaches the plants. Every five minutes it sends the four numbers in one plain HTTP request, and they appear on a live chart you can open from your phone or share with whoever waters when you are away.

ESP32 greenhouse monitor wiringA capacitive soil probe connects its analog output to ESP32 GPIO34, a DHT22 connects its data pin to GPIO4, and a BH1750 light meter connects SDA to GPIO21 and SCL to GPIO22. All three sensors take power from 3V3 and ground.ESP32 board3V3GNDGPIO34 (ADC1)GPIO4GPIO21 SDAGPIO22 SCLSoil probecapacitive, AO outDHT22air temp, humidityBH1750light in lux, I2CVCCGND
Signal wires only. Every sensor's VCC goes to 3V3 and every GND to GND. The soil probe must sit on an ADC1 pin such as GPIO34.

Reviewed, not yet bench-tested on this exact board and sensor combination.

The program comes from the Postlad setup wizard, the same code the wizard gives you when you pick an ESP32, a soil moisture probe, a DHT22 and a BH1750 with a five-minute interval. The wizard can also lay out other combinations if your greenhouse uses different parts.

Parts

  • ESP32 development board (ESP32-WROOM-32 type, such as an ESP32 DevKit)
  • Capacitive soil moisture probe with an analog output (often sold as "v1.2" or "v2.0")
  • DHT22/AM2302 temperature and humidity sensor
  • BH1750 light meter breakout (often labelled GY-302)
  • 10 kΩ resistor if your DHT22 is a bare four-pin sensor
  • Jumper wires, a USB cable and a stable 5 V USB supply
  • Arduino IDE 2.x with the ESP32 board package
  • Libraries: DHT sensor library, Adafruit Unified Sensor and BH1750
  • A free Postlad account and a stream write key

Wiring

Sensor pinESP32 pinNotes
Soil probe VCC3V33.3 V keeps the probe's output inside the ESP32's input range.
Soil probe AOGPIO34An ADC1 pin; see the next section.
Soil probe GNDGND
DHT22 VCC (+)3V3
DHT22 DATAGPIO4A bare four-pin sensor also needs 10 kΩ between DATA and VCC. Breakout boards already have it.
DHT22 GND (−)GND
BH1750 VCC3V3
BH1750 SDAGPIO21The ESP32's default I2C data pin.
BH1750 SCLGPIO22The ESP32's default I2C clock pin.
BH1750 GNDGND
BH1750 ADDRGNDLeave it unconnected if your board has no ADDR pad.

Why the soil probe is on GPIO34: ADC1 pins only

The ESP32 has two analog-to-digital converters. ADC2 is shared with the WiFi radio, and while WiFi is on, analog reads on ADC2 pins fail. The symptom looks exactly like a broken probe: a reading that never changes or an error value, even though the wiring is fine.

So an analog sensor on a WiFi logger belongs on an ADC1 pin: GPIO32 to GPIO39. Most development boards expose GPIO32, 33, 34, 35, 36 and 39. GPIO34 to 39 are input-only, which is fine for an analog sensor. The setup wizard only offers ADC1 pins for analog sensors on the ESP32, and this program reads GPIO34.

GPIO4, used for the DHT22, is an ADC2 pin, but the DHT22 is a digital sensor, so that does not matter.

Complete program

The wizard produces two files. Make a new sketch, paste the first file into the main tab, add a new tab called secrets.h for the second, then put your WiFi name, WiFi password and write key in secrets.h. Choose ESP32 Dev Module, the right port, and upload.

cpp
// ESP32 -> Postlad
//
// Made by app.postlad.com/setup. Two things to change, both in secrets.h:
// your Wi-Fi name and your Wi-Fi password. Nothing in THIS file needs editing.
//
// Readings appear at https://app.postlad.com/streams
//   Soil moisture probe -> f1 soil moisture
//   DHT22 temperature and humidity -> f2 air temperature, f3 humidity
//   BH1750 light meter -> f4 light
//
// Plain http:// is deliberate and supported. A board with no room for a
// certificate store still works, which is the whole point of this service.

#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
#include <Wire.h>
#include <BH1750.h>
#include "secrets.h"

const char* POSTLAD_URL = "http://api.postlad.com/u";
const char* STREAM_PAGE = "https://app.postlad.com/streams";
const unsigned long SEND_INTERVAL_MS = 300000UL;

WiFiClient client;

// Calibrate these two. Hold the probe in the air, note the raw number the
// serial monitor prints, then stand it in a glass of water and note that one.
const int SOIL_DRY = 3276;
const int SOIL_WET = 1433;
DHT dhtA(4, DHT22);
BH1750 lightMeter;

void connectWiFi() {
  if (WiFi.status() == WL_CONNECTED) return;
  Serial.print("Joining ");
  Serial.println(WIFI_SSID);
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.print("\nJoined. This board is ");
  Serial.println(WiFi.localIP());
}

// One attempt. Returns true only when Postlad stored the reading.
bool sendOnce(const String& url) {
  HTTPClient http;
  if (!http.begin(client, url)) {
    Serial.println("Could not start the request.");
    return false;
  }
  int status = http.GET();
  String reply = http.getString();
  http.end();
  reply.trim();
  if (status == 200) {
    Serial.print("stored -> ");
    Serial.println(reply);
    return true;
  }
  Serial.print("not stored (HTTP ");
  Serial.print(status);
  Serial.print("): ");
  Serial.println(reply);
  return false;
}

void setup() {
  Serial.begin(115200);
  delay(300);
  Serial.println();
  Serial.print("Your readings will appear at ");
  Serial.println(STREAM_PAGE);
  dhtA.begin();
  Wire.begin();
  lightMeter.begin();
  connectWiFi();
}

void loop() {
  connectWiFi();

  int soilRaw = analogRead(34);
  float soilPct = (SOIL_DRY - soilRaw) * 100.0 / (SOIL_DRY - SOIL_WET);
  if (soilPct < 0) soilPct = 0;
  if (soilPct > 100) soilPct = 100;
  Serial.print("soil raw reading: "); Serial.println(soilRaw);
  float dhtATemp = dhtA.readTemperature();
  float dhtAHum = dhtA.readHumidity();
  if (isnan(dhtATemp) || isnan(dhtAHum)) {
    Serial.println("The DHT22 did not answer. Check the three wires, then try again.");
    delay(2000);
    return;
  }
  float lux = lightMeter.readLightLevel();

  String url = String(POSTLAD_URL) + "?k=" + POSTLAD_KEY
             + "&f1=" + String(soilPct, 0)
             + "&f2=" + String(dhtATemp, 1)
             + "&f3=" + String(dhtAHum, 0)
             + "&f4=" + String(lux, 0)
             ;

  // Three tries, waiting longer each time. A router that is rebooting takes
  // a few seconds; giving up after one attempt loses a reading for nothing.
  unsigned long backoff = 2000;
  for (int attempt = 1; attempt <= 3; attempt++) {
    if (sendOnce(url)) {
      break;
    }
    if (attempt < 3) {
      delay(backoff);
      backoff = backoff * 2;
    }
  }

  delay(SEND_INTERVAL_MS);
}
cpp
#pragma once
// secrets.h — the only file with anything private in it.
//
// Keep this file off forums, out of screenshots and out of shared folders.
// The program file beside it can be pasted anywhere safely.

// Type your own Wi-Fi name and password between the quotes. 2.4 GHz only.
const char* WIFI_SSID     = "YOUR_WIFI_NAME";
const char* WIFI_PASSWORD = "YOUR_WIFI_PASSWORD";

// Your Postlad write key. It can only add readings to one stream — it cannot
// read anything, change anything, or reach your account.
const char* POSTLAD_KEY   = "YOUR_KEY";

What the program does, exactly:

  • Reads the soil probe on GPIO34 as a raw number from 0 to 4095, prints it as soil raw reading:, then converts it to a percentage between your SOIL_DRY and SOIL_WET numbers, clamped to 0 to 100. Only the percentage is sent, rounded to a whole number, as f1.
  • Reads air temperature and humidity from the DHT22 and sends them as f2 and f3. If the DHT22 does not answer, the program skips the whole cycle and tries again two seconds later, so nothing is sent for that cycle, not even the soil reading.
  • Reads the BH1750 in lux and sends it as f4.
  • Sends one GET request, prints the server's reply, and tries up to three times with a growing pause if the request fails. Then it waits five minutes.

Wire.begin() starts the I2C bus. The BH1750 library expects the sketch to do that itself, and without it the light reading comes back as -2.

Open Serial Monitor at 115200 baud. A good start looks like this:

text
Your readings will appear at https://app.postlad.com/streams
Joining YOUR_WIFI_NAME
.....
Joined. This board is 192.168.1.47
soil raw reading: 2381
stored -> ok 12

ok 12 means the reading was stored and 12 readings have been stored this month.

Calibrate the soil probe

Every probe reads differently, and the numbers in the program (SOIL_DRY = 3276, SOIL_WET = 1433) are starting points, not measurements of your probe. Two readings fix that.

  1. Upload the program with the probe connected and Serial Monitor open.
  2. Hold the clean, dry probe in the air. Note the soil raw reading: number. Press the board's EN or RST button to get a fresh reading without waiting five minutes. Take two or three and use a typical value.
  3. Stand the probe in a glass of water, up to the line printed on it and no further. The electronics at the top must stay dry. Note that number the same way.
  4. Put the air number in SOIL_DRY and the water number in SOIL_WET, then upload again.

On most capacitive probes the raw number goes down as the soil gets wetter. The program's formula works in either direction, so a probe that reads the other way round still maps dry to 0 % and wet to 100 %, as long as the two numbers came from that probe.

Calibrate each probe on its own. Two probes from the same bag can read quite differently.

What the percentage means

The number on the chart is relative, not volumetric water content. 0 % means "as dry as air" and 100 % means "as wet as a glass of water". Saturated soil usually reads well below 100 %, and the same moisture reads differently in peat, compost and clay, at different temperatures, and with the probe pushed in to a different depth.

That is still useful. Water a pot thoroughly and note the reading an hour later. Note it again on the day the plant starts asking for water. Those two numbers, from your probe in your soil, are your scale. Keep the probe in the same place and at the same depth so the numbers stay comparable.

Capacitive versus resistive probes

A resistive probe, the bare two-pronged fork, passes current through the soil between two exposed metal prongs. That current drives electrolysis: one prong slowly dissolves, the readings drift, and within weeks to months of continuous use the probe is visibly corroded and putting metal into the pot. If you use one anyway, power it only for the moment of each reading, which needs a code change not covered here.

A capacitive probe measures the soil through a coated board, with no exposed metal in the soil, so it does not corrode in the same way. The cut edges and the electronics at the top are still unprotected on many cheap boards. Seal the edges with nail varnish or similar, and cover the top with heat-shrink, before it goes into wet soil.

Optional: send the raw reading too

If you want to watch a probe drift over a season, send the raw number as a fifth field. In the program, change the end of the URL lines to:

cpp
             + "&f4=" + String(lux, 0)
             + "&f5=" + String(soilRaw)
             ;

Then name f5 "soil raw" on the stream page, with no unit. This is a small change to the generated code; the rest stays as it is.

Configure the chart

The wizard names the fields for you. If you created the stream yourself, set:

FieldNameUnitDecimals
f1soil moisture%0
f2air temperature°C1
f3humidity%0
f4lightlx0

One reading every 300 seconds is 8,928 readings in a 31-day month, well inside the Free plan's 100,000 a month per account. Every reading is kept at full detail for 30 days, then an hourly minimum, maximum and average for 12 months.

Placing the sensors

  • DHT22: keep it out of direct sun. A sensor in sunshine measures its own warm case, not the air. A small shaded, ventilated cover is enough.
  • BH1750: face it up, at plant height, where condensation will not sit on it. It measures lux, which is weighted to the human eye, not the light plants use for growth, so treat it as "how bright, and for how long", not as a grow-light measurement. Its range tops out around 65,000 lux, and full summer sun can exceed that, so a flat line at the top on a bright day means the sensor has run out of range.
  • Soil probe: keep the wire to the board short. A long analog wire picks up noise. Push the probe in to the same depth each time you move it.
  • The board: in a box, away from spray and condensation. Greenhouses are humid, and bare boards corrode.

Watering decisions

The chart tells you when a pot is drying out and how fast. It is not an irrigation controller. Postlad stores and shows readings; it does not switch a pump or a valve, and nothing in this guide does either.

That is a deliberate boundary. A watering system that depends on WiFi and a web service can fail with a pump running. If you automate watering, make the controller work on its own, with a hard limit on how long it can run, and use the chart to check that it is doing what you expect.

For most people the chart is enough: look at the soil line, learn your own "needs water" number, and water by hand when the line gets there. On paid plans from Hobby you can also get an email when a value crosses a threshold you set.

Several pots or beds

Two ways, and they combine:

  • One board, several probes. The ESP32 has six usable ADC1 pins (GPIO32, 33, 34, 35, 36 and 39), so one board can read up to six soil probes. Ask the setup wizard for several soil probes and it puts them on GPIO34, GPIO35, GPIO32 and onward, each with its own SOIL_DRY and SOIL_WET numbers, sent as separate fields. The raw numbers are printed in pin order. A stream takes up to 16 fields, so a few probes plus air and light fit in one request.
  • Several boards, one page. Beds that are far apart are easier with a board each. Each board sends to its own stream, and a Postlad board shows several streams together on one page. The Free plan includes 2 streams.

Get an email if it stops

A greenhouse logger fails quietly: a tripped socket, a flat power bank, a router that moved. Turn on the email alert for when a stream stops sending. It is available on every plan, including Free, and it is off until you switch it on. With a five-minute interval, set the alert long enough that one missed reading does not wake you up.

Share the chart

Send whoever is watering a public or secret link to the stream. They do not need an account to view it. You can also embed the live chart on another page, and export CSV on every plan. The write key stays on the device and is never part of a viewer link.

Troubleshooting

Soil moisture is always 0 % or always 100 %

The probe has not been calibrated, or the raw reading is outside the two numbers. Read the soil raw reading: line in Serial Monitor and check it moves when the probe goes from air to water.

The raw soil reading never changes

Check that AO goes to GPIO34 and that the probe has 3V3 and GND. If you moved it to another pin, make sure that pin is ADC1 (GPIO32 to 39). A reading stuck at 4095 means the input is at or above the top of its range; a reading stuck at 0 usually means a loose wire.

Light shows -2 or -1

The BH1750 did not answer. Check SDA on GPIO21, SCL on GPIO22, power and ground, and that Wire.begin(); is in setup().

The Serial Monitor says the DHT22 did not answer

Check the three wires and the 10 kΩ pull-up on a bare sensor. While the DHT22 fails, the program sends nothing, which shows up as a gap on the chart rather than a false reading.

The reply starts with error

Postlad says why in the reply. error unknown_param: means the URL has a name other than k, f1 to f16, t, status or fmt, for example &soil=. error bad_value: means a value was not a plain decimal. In both cases nothing is stored.

The server returns 429

The stream received readings faster than one every 5 seconds, usually because the interval was shortened for testing. Put SEND_INTERVAL_MS back to a longer value.

The board never joins WiFi

The ESP32 uses 2.4 GHz only. Greenhouses are often at the edge of WiFi range; check the signal where the board will live, not at your desk.

FAQ

Which soil moisture sensor works best with an ESP32?

A capacitive probe with an analog output, powered from 3.3 V and read on an ADC1 pin such as GPIO34. Resistive forks corrode in continuous use.

Is the soil moisture percentage accurate?

It is a relative scale between your probe's dry-air and in-water readings, not volumetric water content. It is consistent enough to show when a pot is drying out, once you learn the numbers for your own soil.

Why does my soil sensor read nothing when WiFi is on?

It is probably on an ADC2 pin. ADC2 cannot be read while the ESP32's WiFi is running. Move the probe to GPIO32 to 39.

Can Postlad water my plants?

No. Postlad stores, charts and shares readings, and can email you. Watering control belongs in a controller that keeps working when the network does not.

How many readings does a five-minute interval use?

8,928 in a 31-day month. The Free plan allows 100,000 readings a month per account, so one greenhouse logger uses under a tenth of it.

Create the stream

Create a free account, then use the setup wizard to make the stream, name the fields and get the program with your choices filled in.

Create your free account →

Sources and verification