A DIY Sprinkler Controller Using An ESP8266
A DIY Sprinkler Controller Using an ESP8266
A DIY Sprinkler Controller Using an ESP8266
If you have an in-ground sprinkler system, you may be looking for a way to make it smarter and more efficient. A smart sprinkler controller can help you save water, money, and time by adjusting the watering schedule based on the weather and your local restrictions. However, smart sprinkler controllers can be expensive and may not be compatible with your existing system. In this article, we will show you how to build your own smart sprinkler controller using an ESP8266, a low-cost Wi-Fi microchip that can be programmed with Arduino or MicroPython. This project will cost you less than $50 and will give you full control over your sprinkler system from anywhere using your smartphone.
DOWNLOAD: https://belagasi.blogspot.com/?file=2w3YVf
What You Will Need
To build this project, you will need the following components:
An ESP8266 development board, such as the NodeMCU or the Wemos D1 Mini. These boards have built-in USB-to-serial converters and voltage regulators, making them easy to use. You can find them online for around $5.
A 4-channel relay module. This module will allow you to switch on and off the solenoid valves that control the water flow to each zone of your sprinkler system. You can find them online for around $10.
A rain sensor. This sensor will detect if it is raining and prevent the sprinkler system from running unnecessarily. You can find them online for around $5.
A waterproof enclosure. This enclosure will house the ESP8266 board and the relay module and protect them from moisture and dust. You can find them online for around $10.
Some wires, connectors, and tools. You will need some jumper wires to connect the ESP8266 board to the relay module and the rain sensor, some wire nuts or soldering tools to connect the relay module to the solenoid valves, and some screws or zip ties to mount the enclosure. You will also need a USB cable to program the ESP8266 board and a 5V power supply to power it.
How to Wire It Up
The wiring diagram for this project is shown below:
As you can see, the ESP8266 board is connected to the relay module using four GPIO pins: D1, D2, D3, and D4. Each pin controls one relay channel, which in turn controls one solenoid valve. The rain sensor is connected to the A0 analog pin of the ESP8266 board, which will read its output voltage. The relay module is powered by the 5V pin of the ESP8266 board, while the solenoid valves are powered by a separate 24V AC power supply (not shown in the diagram). The ESP8266 board is also connected to a USB cable for programming and a 5V power supply for operation.
How to Program It
There are two ways to program the ESP8266 board: using Arduino or using MicroPython. Arduino is a popular platform for programming microcontrollers using C/C++ language, while MicroPython is a version of Python language that runs on microcontrollers. Both options have their advantages and disadvantages, so you can choose the one that suits you better.
Using Arduino
To program the ESP8266 board using Arduino, you will need to install the Arduino IDE on your computer and add support for the ESP8266 boards. You can follow this guide to do that.
Once you have set up the Arduino IDE, you can copy and paste the following code into a new sketch:
```c // Include libraries #include // For Wi-Fi connection #include // For HTTP requests #include // For JSON parsing // Define constants #define WIFI_SSID "Your Wi-Fi SSID" // Replace with your Wi-Fi network name #define WIFI_PASSWORD "Your Wi-Fi password" // Replace with your Wi-Fi network password #define API_KEY "Your OpenWeatherMap API key" // Replace with your OpenWeatherMap API key #define CITY_ID "Your city ID" // Replace with your city ID from OpenWeatherMap #define RAIN_THRESHOLD 500 // The analog value of the rain sensor that indicates rain #define ZONE_1 D1 // The GPIO pin that controls zone 1 #define ZONE_2 D2 // The GPIO pin that controls zone 2 #define ZONE_3 D3 // The GPIO pin that controls zone 3 #define ZONE_4 D4 // The GPIO pin that controls zone 4 #define RAIN_SENSOR A0 // The analog pin that reads the rain sensor // Define variables bool isRaining = false; // A flag to indicate if it is raining int rainValue = 0; // The analog value of the rain sensor float rainProbability = 0; // The probability of rain from the weather API int wateringTime = 0; // The watering time in minutes for each zone // Setup function runs once at startup void setup() // Initialize serial communication Serial.begin(9600); // Initialize GPIO pins as outputs pinMode(ZONE_1, OUTPUT); pinMode(ZONE_2, OUTPUT); pinMode(ZONE_3, OUTPUT); pinMode(ZONE_4, OUTPUT); // Turn off all relays digitalWrite(ZONE_1, LOW); digitalWrite(ZONE_2, LOW); digitalWrite(ZONE_3, LOW); digitalWrite(ZONE_4, LOW); // Connect to Wi-Fi network Serial.println("Connecting to Wi-Fi..."); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); while (WiFi.status() != WL_CONNECTED) delay(500); Serial.print("."); Serial.println("Connected!"); // Loop function runs repeatedly void loop() // Read the rain sensor value rainValue = analogRead(RAIN_SENSOR); Serial.print("Rain sensor value: "); Serial.println(rainValue); // Check if it is raining if (rainValue > RAIN_THRESHOLD) isRaining = true; Serial.println("It is raining!"); // Turn off all relays if they are on digitalWrite(ZONE_1, LOW); digitalWrite(ZONE_2, LOW); digitalWrite(ZONE_3, LOW); digitalWrite(ZONE_4, LOW); Serial.println("All zones turned off!"); delay(60000); // Wait for a minute and check again return; else isRaining = false; Serial.println("It is not raining!"); // Get the weather data from OpenWeatherMap API String url = " + String(CITY_ID) + "&appid=" + String(API_KEY); // Build the API request URL HTTPClient http; // Create an HTTPClient object http.begin(url); // Begin the HTTP request int httpCode = http.GET(); // Send the HTTP GET request and get the response code if (httpCode > 0) // Check if the request was successful String payload = http.getString(); // Get the response payload as a string Serial.println(payload); // Print the payload for debugging // Parse the JSON payload using ArduinoJson library DynamicJsonDocument doc(1024); // Create a JSON document object with a capacity of 1024 bytes DeserializationError error = deserializeJson(doc, payload); // Deserialize the payload into the JSON document object and check for errors if (error) // Check if there was an error in parsing the JSON payload Serial.print("JSON parsing failed: "); Serial.println(error.c_str()); http.end(); // End the HTTP request delay(60000); // Wait for a minute and try again return; else // No error in parsing the JSON payload JsonObject weather = doc["weather"][0]; // Get the weather object from the JSON document object String main = weather["main"]; // Get the main weather condition from the weather object as a string String description = weather["description"]; // Get the weather description from the weather object as a string JsonObject mainData = doc["main"]; // Get the main data object from the JSON document object float temp = mainData["temp"]; // Get the temperature from the main data object as a float in Kelvin JsonObject rainData = doc["rain"]; // Get the rain data object from the JSON document object float rainVolume = rainData["1h"]; // Get the rain volume in the last hour from the rain data object as a float in mm JsonObject sysData = doc["sys"]; // Get the sys data object from the JSON document object long sunrise = sysData["sunrise"]; // Get the sunrise time from the sys data object as a long in UNIX time format JsonObject cloudsData = doc["clouds"]; // Get the clouds data object from the JSON document object int cloudiness = cloudsData["all"]; // Get the cloudiness percentage from the clouds data object as an int JsonObject windData = doc["wind"]; // Get the wind data object from the JSON document object float windSpeed = windData["speed"]; // Get the wind speed from the wind data object as a float in m/s float windDeg = windData["deg"]; // Get the wind direction from the wind data object as a float in degrees rainProbability = doc["pop"]; // Get the probability of precipitation from the JSON document object as a float // Print the weather data for debugging Serial.print("Main: "); Serial.println(main); Serial.print("Description: "); Serial.println(description); Serial.print("Temperature: "); Serial.print(temp - 273.15); // Convert Kelvin to Celsius Serial.println(" C"); Serial.print("Rain volume: "); Serial.print(rainVolume); Serial.println(" mm"); Serial.print("Sunrise: "); Serial.println(sunrise); Serial.print("Cloudiness: "); Serial.print(cloudiness); Serial.println(" %"); Serial.print("Wind speed: "); Serial.print(windSpeed); Serial.println(" m/s"); Serial.print("Wind direction: "); Serial.print(windDeg); Serial.println(" "); Serial.print("Rain probability: "); Serial.print(rainProbability * 100); // Convert to percentage Serial.println(" %"); else // The request was not successful Serial.print("HTTP request failed: "); Serial.println(httpCode); http.end(); // End the HTTP request