There is a delicate balance required to maintain optimal conditions in an aquarium. Whether it's monitoring the temperature, adjusting the lighting, or keeping an eye on the water level, managing these parameters is crucial for the well-being of the aquarium inhabitants. That's why I decided to build a Wi-Fi controlled aquarium using the ESP8266 microcontroller. In this blog post, I'll guide you through the process of setting up an aquarium controller that allows you to remotely monitor and control various parameters, and receive alerts or notifications when adjustments are needed. Let's dive in!
Prerequisites
To follow along with this tutorial, you'll need the following components:
1. ESP8266 development board (e.g., NodeMCU or Wemos D1 Mini)
2. Temperature sensor (e.g., DS18B20)
3. Light intensity sensor (e.g., BH1750)
4. Water level sensor (e.g., float switch or ultrasonic sensor)
5. Relay module
6. Breadboard and jumper wires
7. Smartphone or computer with Wi-Fi connectivity
8. USB cable for programming the ESP8266
9. A suitable aquarium and the necessary equipment (heater, lighting, etc.)
Setting up the Hardware
Before we dive into the code, let's set up the hardware components for our Wi-Fi controlled aquarium.
1. Connect the ESP8266 development board to your computer using a USB cable.
2. Install the necessary drivers for the ESP8266 board (if not already installed).
3. Place the temperature sensor, light intensity sensor, and water level sensor inside the aquarium according to their specifications.
4. Connect the sensors to the ESP8266 board using jumper wires and a breadboard.
5. Connect the relay module to control the aquarium heater or lighting.
Now that we have the hardware in place, let's move on to the software part in the next section.
Software Setup and ESP8266 Configuration
To start building our Wi-Fi controlled aquarium, we need to set up the software environment and configure the ESP8266 module. Follow the steps below:
1. Install the Arduino IDE (Integrated Development Environment) from the official Arduino website (https://www.arduino.cc/en/software).
2. Open the Arduino IDE and navigate to "File" -> "Preferences." In the "Additional Boards Manager URLs" field, paste the following URL: http://arduino.esp8266.com/stable/package_esp8266com_index.json.
3. Click "OK" to save the preferences and close the window.
4. Navigate to "Tools" -> "Board" -> "Boards Manager." In the search bar, type "esp8266" and install the "esp8266" board package by ESP8266 Community.
5. Once the installation is complete, select the appropriate ESP8266 board from the "Tools" -> "Board" menu (e.g., NodeMCU 1.0 or Wemos D1 Mini).
6. Connect your ESP8266 development board to your computer if you haven't already done so.
Now, we're ready to write the code for our aquarium controller. In the next section, I'll provide the necessary code snippets for monitoring temperature, controlling lighting, and detecting water level.
Monitoring Temperature
To monitor the temperature of the aquarium, we'll be using the DS18B20 temperature sensor. Follow the steps below to read the temperature values using the ESP8266:
1. Connect the DS18B20 sensor to the ESP8266 as follows:
- VCC pin to 3.3V
- GND pin to GND
- Data pin to a GPIO pin (e.g., D4)
2. Open the Arduino IDE and create a new sketch.
3. Add the following code to the sketch:
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is connected to GPIO pin D4
#define ONE_WIRE_BUS D4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(115200);
sensors.begin();
}
void loop() {
sensors.requestTemperatures();
float temperature = sensors.getTempCByIndex(0);
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
delay(1000);
}
4. Upload the sketch to your ESP8266 board by clicking on the "Upload" button.
5. Open the Serial Monitor by navigating to "Tools" -> "Serial Monitor" or by pressing Ctrl+Shift+M. Make sure the baud rate is set to 115200.
6. You should now see the temperature readings in the Serial Monitor. Test the sensor by placing it in different temperature conditions within the aquarium.
With this code, we can now monitor the temperature of our aquarium. In the next section, we'll move on to controlling the lighting.
Controlling Lighting
To control the lighting of the aquarium, we'll use a light intensity sensor (BH1750) and a relay module. The light intensity sensor will measure the ambient light level, and based on a predefined threshold, the relay module will turn the aquarium lighting on or off. Let's proceed with the steps:
1. Connect the BH1750 light intensity sensor to the ESP8266 as follows:
- VCC pin to 3.3V
- GND pin to GND
- SDA pin to a GPIO pin (e.g., D2)
- SCL pin to a GPIO pin (e.g., D1)
2. Connect the relay module to the ESP8266 as follows:
- VCC pin to 5V
- GND pin to GND
- IN pin to a GPIO pin (e.g., D3)
3. Open the Arduino IDE and create a new sketch.
4. Install the BH1750 library by following these steps:
- Navigate to "Sketch" -> "Include Library" -> "Manage Libraries."
- In the Library Manager, search for "BH1750" and install the library by Christopher Laws.
5. Add the following code to the sketch:
#include <BH1750.h>
BH1750 lightSensor;
// Pin D3 is connected to the relay module
const int relayPin = D3;
// Set the threshold value for turning the lights on or off
const int lightThreshold = 100;
void setup() {
pinMode(relayPin, OUTPUT);
lightSensor.begin();
}
void loop() {
// Read the light intensity value
float lightIntensity = lightSensor.readLightLevel();
// Check if the light intensity is below the threshold
if (lightIntensity < lightThreshold) {
// Turn on the lights by activating the relay
digitalWrite(relayPin, HIGH);
} else {
// Turn off the lights by deactivating the relay
digitalWrite(relayPin, LOW);
}
delay(1000);
}
6. Upload the sketch to your ESP8266 board.
7. Test the lighting control by covering the light intensity sensor or exposing it to different light conditions. You should observe the relay module turning on or off based on the light threshold value.
With this code, we can now control the lighting of our aquarium based on the ambient light level. In the next section, we'll focus on detecting the water level.
Detecting Water Level
To detect the water level in the aquarium, we'll use a water level sensor, such as a float switch or an ultrasonic sensor. In this section, I'll demonstrate how to implement water level detection using a float switch. Follow the steps below:
1. Connect the float switch to the ESP8266 as follows:
- One terminal of the float switch to a GPIO pin (e.g., D5)
- The other terminal of the float switch to GND
2. Open the Arduino IDE and create a new sketch.
3. Add the following code to the sketch:
// Pin D5 is connected to the float switch
const int floatSwitchPin = D5;
void setup() {
pinMode(floatSwitchPin, INPUT_PULLUP);
Serial.begin(115200);
}
void loop() {
int waterLevel = digitalRead(floatSwitchPin);
if (waterLevel == LOW) {
Serial.println("Water level is low! Please refill the aquarium.");
// Add code here to send a notification or alert
}
delay(1000);
}
4. Upload the sketch to your ESP8266 board.
5. Open the Serial Monitor and ensure the baud rate is set to 115200.
6. Test the water level detection by simulating a low water level condition. When the float switch is triggered (water level is low), you should see a message in the Serial Monitor indicating the need to refill the aquarium.
Monitoring and Controlling Parameters
So far, we've covered the individual components of our Wi-Fi controlled aquarium: temperature monitoring, lighting control, and water level detection. Now, let's combine these functionalities into a single program that can be accessed and controlled remotely over Wi-Fi.
1. Create a new sketch in the Arduino IDE.
2. Copy and paste the code snippets from the previous sections into this sketch.
3. Modify the code to include Wi-Fi connectivity and create a web server for remote access.
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <BH1750.h>
// Wi-Fi credentials
const char* ssid = "YourWiFiSSID";
const char* password = "YourWiFiPassword";
// Data wire is connected to GPIO pin D4
#define ONE_WIRE_BUS D4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// Pin D3 is connected to the relay module
const int relayPin = D3;
// Set the threshold value for turning the lights on or off
const int lightThreshold = 100;
// Pin D5 is connected to the float switch
const int floatSwitchPin = D5;
ESP8266WebServer server(80);
void setup() {
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Start the web server
server.on("/", handleRoot);
server.on("/temperature", handleTemperature);
server.on("/lighting", handleLighting);
server.on("/waterlevel", handleWaterLevel);
server.begin();
// Initialize sensors
sensors.begin();
// Set pin modes
pinMode(relayPin, OUTPUT);
pinMode(floatSwitchPin, INPUT_PULLUP);
// Start the Serial communication
Serial.begin(115200);
}
void loop() {
server.handleClient();
sensors.requestTemperatures();
int waterLevel = digitalRead(floatSwitchPin);
// Check if
the water level is low
if (waterLevel == LOW) {
Serial.println("Water level is low! Please refill the aquarium.");
// Add code here to send a notification or alert
}
// Read the light intensity value
BH1750 lightSensor;
lightSensor.begin();
float lightIntensity = lightSensor.readLightLevel();
// Check if the light intensity is below the threshold
if (lightIntensity < lightThreshold) {
digitalWrite(relayPin, HIGH);
} else {
digitalWrite(relayPin, LOW);
}
delay(1000);
}
void handleRoot() {
String html = "<html><body>";
html += "<h1>Aquarium Controller</h1>";
html += "<ul>";
html += "<li><a href='/temperature'>Temperature</a></li>";
html += "<li><a href='/lighting'>Lighting</a></li>";
html += "<li><a href='/waterlevel'>Water Level</a></li>";
html += "</ul>";
html += "</body></html>";
server.send(200, "text/html", html);
}
void handleTemperature() {
float temperature = sensors.getTempCByIndex(0);
String message = "Temperature: " + String(temperature) + " °C";
server.send(200, "text/plain", message);
}
void handleLighting() {
int lightState = digitalRead(relayPin);
String message = "Lighting: ";
if (lightState == HIGH) {
message += "ON";
} else {
message += "OFF";
}
server.send(200, "text/plain", message);
}
void handleWaterLevel() {
int waterLevel = digitalRead(floatSwitchPin);
String message = "Water Level: ";
if (waterLevel == LOW) {
message += "Low";
} else {
message += "Normal";
}
server.send(200, "text/plain", message);
}
7. Replace "YourWiFiSSID" and "YourWiFiPassword" with your actual Wi-Fi credentials.
8. Upload the sketch to your ESP8266 board.
9. Once the code is uploaded, open the Serial Monitor to obtain the IP address of the ESP8266.
10. In a web browser, enter the IP address of the ESP8266. You should see a web page displaying the available parameters: temperature, lighting, and water level.
11. Click on the links to view the current values of each parameter.
Congratulations! You've successfully built a Wi-Fi controlled aquarium using the ESP8266. You can now remotely monitor and control parameters such as temperature, lighting, and water level.
Future Enhancements
While we have created a basic Wi-Fi controlled aquarium controller, there are several ways to enhance and expand upon this project. Here are a few ideas for future improvements:
1. pH Monitoring and Control: Incorporate a pH sensor to monitor the acidity or alkalinity of the aquarium water. Implement a control mechanism to adjust the pH levels automatically if they deviate from the desired range.
2. Automated Feeding: Integrate an automated feeding system that dispenses food at scheduled intervals or on-demand. This feature ensures your aquatic pets are fed properly even when you're away.
3. Data Logging and Analytics: Implement a data logging mechanism to record the parameters over time. Use the logged data to analyze trends, identify patterns, and gain insights into the aquarium's overall health and conditions.
4. Mobile App Integration: Develop a mobile application that allows you to monitor and control the aquarium parameters from your smartphone. This provides a convenient and user-friendly interface for managing your aquarium remotely.
5. Alerting and Notifications: Enhance the system to send alerts and notifications to your mobile device or email when critical parameter levels are detected, such as temperature fluctuations, lighting failures, or low water levels.
Remember to always prioritize the safety and well-being of your aquatic pets while making any modifications or enhancements to your aquarium controller.
Happy aquarium keeping and exploring the world of IoT!