Home IoT Hub Series · 15

[Part 15] Do not leave windows open in the rain - rain-sensor node

Detect rainwater with a rain sensor module and send a wet/dry value to the hub.

This part detects rainwater with a simple rain sensor plate. It is useful for windows, laundry, balcony plants, and outdoor alerts.

The sensor plate can face the rain, but the controller board should stay inside a protected box.

[Part 15] Do not leave windows open in the rain - rain-sensor node hero image
System architecture and data pipeline note
This article is part of the Home IoT Hub system connected to the central NodeMCU server built in Part 2: Building the Brain. The rain sensor reports rain contact for window, laundry, and outdoor-node protection decisions. The collected data is sent over the local IoT_Hub_Net network and updates slot 18 for rain detection on the central server (/set?no18=value), becoming part of the decision pipeline for the 20-channel smart-home controller.

Parts and cost

PartQtyUnit price
Wemos D1 Mini1 pc$1.50
rain sensor module1 pc$0.70
Jumper wires 3 pc
Total$2.20

Pin wiring

[Part 15] Do not leave windows open in the rain - rain-sensor node wiring diagram
Sensor / moduleBoard pin
VCC3.3V
GNDGND
AOUTA0

Full code

[Part 15] Do not leave windows open in the rain - rain-sensor node - Code 2
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

const char* ssid     = "IoT_Hub_Net";
const char* password = "iothub1234";
const char* hubURL   = "http://192.168.1.1";

#define RAIN_PIN   A0
#define SLOT_NO    18
#define THRESHOLD  500 // 500 or lower = raining
#define INTERVAL   10000

void setup() {
  Serial.begin(115200);
  pinMode(RAIN_PIN, INPUT);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.print("Connecting to IoT_Hub_Net");
  while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }
  Serial.println("\nConnected!");
}

void loop() {
  int raw = analogRead(RAIN_PIN);
  int rain = (raw < THRESHOLD) ? 1 : 0;

  Serial.print("Rain: "); Serial.print(raw);
  Serial.print(" -> "); Serial.println(rain ? "RAINING" : "DRY");

  WiFiClient client; HTTPClient http;
  String url = String(hubURL) + "/set?no"
             + String(SLOT_NO) + "=" + String(rain);
  http.begin(client, url); http.GET(); http.end();
  Serial.println("HTTP -> " + url);

  delay(INTERVAL);
}

Upload and check

Connect the Wemos D1 Mini to the PC with a USB data cable, select the ESP8266 board and port in the Arduino IDE, upload the sketch, then open Tools → Serial Monitor and set the baud rate to 115200.

Serial Monitor

After uploading, open Tools → Serial Monitor in the Arduino IDE and set the baud rate to 115200. The following is an example Serial Monitor flow for checking normal operation. This is not a real screenshot. It shows intermittent sensor-board power used to reduce electrode corrosion.

[BOOT] Home IoT Node #15 - Rain sensor
[POWER] rain board VCC=ON for 800 ms
[SENSOR] rain_raw=736 threshold=500 rain=NO
[POWER] rain board VCC=OFF to prevent corrosion
[HTTP] GET /set?no18=0
[HUB] 200 OK - slot no18=DRY
[LOOP] next rain check in 300000 ms
  • If Connected or connected appears, the Wi-Fi step passed.
  • If HTTP 200 or 200 OK appears, the value reached the brain server.
  • If the value does not update, check both the slot number in the URL and the sensor wiring.

Build notes

  • Touch the plate with a wet finger or a few water drops and confirm the hub changes the Rain slot.
  • Power on the hub brain first so the node can join IoT_Hub_Net.
  • Use the same slot number written in the source code.
  • Open the serial monitor at 115200 baud and confirm HTTP 200.
  • If the value does not appear on the hub, recheck wiring, GPIO number, and Wi-Fi connection.

Putting the Rain Sensor to Work

A rain sensor reports Clear/Raining the moment drops hit its sensing plate. Simple as it is, it makes a great trigger for everyday automation. Wired into your Home IoT Hub, it shines in these situations.

Ways to Use It

  • Laundry, window and balcony alerts: Get a phone notification when rain starts while you are out, so you remember the laundry on the line or the open window.
  • Automatic awnings and smart plugs: With a hub rule, a Raining signal can retract a motorized awning or cut power to an outdoor smart plug, reducing shock and short-circuit risk.
  • Smart plant watering: Skip the watering pump on rainy days to avoid wasting water and over-soaking. Perfect for balcony gardens and small plots.
  • Camping and car camping: Placed on a tarp or tent, it warns you of pre-dawn rain so you can prepare in time.

Recommended Placement

  • Put it where the sky is open and rain lands directly. Avoid spots shielded by eaves or awnings.
  • Always mount the plate at a 15-30 degree tilt so water runs off instead of pooling. Laid flat, it keeps reporting 'Raining' long after the rain stops.
  • Keep it away from air-conditioner units and drain hoses: condensation and drip water get mistaken for rain. Stay out of lawn sprinkler range too.

Troubleshooting checkpoints

The node code can look short, but real home installation is affected by placement, power, Wi-Fi quality, and sensor noise. Check these points before letting the node become a decision input for the brain server.

SymptomLikely causeField fix
The electrodes corrode within daysContinuous 5V across wet electrodes causes electrolysisPower VCC from a digital pin only during measurement, then set it LOW to reduce corrosion.
It still says rain after the rain stopsWater film or dirt keeps surface resistance lowMount the board at a slight angle and clear the rain state only after continuous dry readings.
Dew or fog is detected as rainThe board senses wetness, not actual rainfall amountLower sensitivity and interpret it together with outdoor temperature/humidity and time conditions.
Readings are unstable in winterIce or frost remains across electrodesUse it as a reference in winter or replace it with a waterproof optical rain sensor if control accuracy matters.

Practical Tips and Cautions

  • Cleaning schedule: The plate's copper traces collect dust, fine dust and oxidation over time, dulling sensitivity. Wipe it every month or two with a dry brush or an alcohol swab.
  • Threshold tuning: Read the analog value, not just the digital output, to tell drizzle from a downpour. Use the module's trimmer to raise or lower the threshold for your environment.
  • Dew and frost false alarms: Morning condensation and frost are water too, so they read as rain. Add a 'only if it persists for several minutes' condition to your alert rule to cut false positives sharply.
  • Waterproofing and wiring: The plate may get wet, but never let the board or connectors flood. House the controller in a waterproof box, and add a downward U-shaped drip loop in the cable so water cannot track back into the enclosure.

FAQ

Q. It still shows 'Raining' after the rain stopped.
Water is lingering on the plate. Tilt it more steeply and nudge the analog threshold up so it returns to Clear as soon as the film of water thins out.

Q. Does it detect snow?
Dry snow is hard to catch; it only reacts once snow melts into water on the plate. Treat winter precipitation detection as a rough backup only.

Q. How long does the plate last?
Outdoors, sensitivity drops from corrosion after about 6 months to a year. The plate is cheap to replace on its own, so swap it when response gets sluggish.