So these days i have been make somethings that i want to share to you all its an diy reef controller wifi 2 part controller, right now its just a work in progress and not tested BUT the code has been writen, component list here and more!! later on i will try to add specific parameter testing but thos is what you have right now
Component List for node mcu part
- NodeMCU ESP8266
- DHT11 Sensor (or DHT22 for higher accuracy)
- 3-Channel Relay Module
- Heater (connected to relay)
- Aquarium Light (connected to relay)
- Return Pump (connected to relay)
- Jumper Wires (male-to-male and male-to-female as needed)
- Power Supply (5V or 12V depending on relay module and components)
- Breadboard (optional, for prototyping)
- Resistors (if required for pull-up connections, typically 10kΩ)
Pin-to-Pin Connections
1. NodeMCU (ESP8266):
- Power and Ground:
- VIN: Connected to the positive terminal of your 5V power supply (for relays and sensors).
- GND: Common ground for all components.
- DHT11 Sensor:
- VCC: Connected to 3.3V pin on NodeMCU.
- GND: Connected to GND on NodeMCU.
- Data Pin: Connected to D4 on NodeMCU.
- Relay Module:
- Relay Module VCC: Connected to 5V (from power supply).
- Relay Module GND: Connected to GND (common ground with NodeMCU).
- IN1 (Relay 1 - Heater): Connected to D1.
- IN2 (Relay 2 - Light): Connected to D2.
- IN3 (Relay 3 - Return Pump): Connected to D3.
Wiring for Relays
2. Relay Module Wiring:
- Each relay has three terminals:
- COM (Common): Connect to one wire of the component (heater, light, or pump).
- NO (Normally Open): Connect to the live wire of the power supply feeding the component.
- NC (Normally Closed): Leave unconnected for this application (used only if you want the relay to be ON by default).
Example for Heater:
- Cut the live wire of the heater's power cable.
- Connect one cut end to the COM terminal.
- Connect the other cut end to the NO terminal of the relay.
- Leave the neutral wire connected directly to the heater.
Final Circuit Overview
Power Supply:
- Power the NodeMCU via its VIN pin with a regulated 5V supply.
- Relay module powered by the same 5V supply.
Components Powered by Relays:
- The heater, aquarium light, and return pump are powered independently through the relay module using their respective power supplies (e.g., AC mains or DC sources).
Connections:
- Ensure common ground between NodeMCU, relays, and the power supply.
Diagram Summary
- NodeMCU to Components:
- DHT11 → D4 (Data), 3.3V, GND
- Relay 1 (Heater) → D1
- Relay 2 (Light) → D2
- Relay 3 (Return Pump) → D3
- Relays to Components:
- Heater, light, and pump are each connected through their respective relays (COM and NO terminals).
Final Notes
- Double-check all connections for continuity before powering on.
- Use a 5V/2A power adapter to ensure enough current for the NodeMCU and relays.
- Ensure the components connected to the relays (heater, light, pump) match the relay module's rated voltage and current.
HERE IS NODEMCU CODE:- -
Code:
#include <ESP8266WiFi.h> #include <ESP8266WebServer.h> #include <DHT.h> // WiFi Credentials const char *ssid = "Your_SSID"; const char *password = "Your_PASSWORD"; // DHT Sensor #define DHTPIN D4 // GPIO pin where the DHT sensor is connected #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); // Relay Pins #define HEATER_PIN D1 #define LIGHT_PIN D2 #define RETURN_PUMP_PIN D3 // Web server on port 80 ESP8266WebServer server(80); void setup() { Serial.begin(115200); dht.begin(); // Initialize relays pinMode(HEATER_PIN, OUTPUT); pinMode(LIGHT_PIN, OUTPUT); pinMode(RETURN_PUMP_PIN, OUTPUT); digitalWrite(HEATER_PIN, LOW); digitalWrite(LIGHT_PIN, LOW); digitalWrite(RETURN_PUMP_PIN, LOW); // Connect to WiFi Serial.println("Connecting to WiFi..."); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("."); } Serial.println("WiFi connected."); Serial.print("IP address: "); Serial.println(WiFi.localIP()); // Define server routes server.on("/", handleRoot); server.on("/api/data", handleApiData); server.on("/api/control", handleControl); server.begin(); Serial.println("HTTP server started."); } void loop() { server.handleClient(); } // Serve HTML page void handleRoot() { String html = R"rawliteral( <!DOCTYPE html> <html> <head> <title>Reef Controller</title> <style> body { font-family: Arial, sans-serif; text-align: center; } .control { margin: 20px; } button { padding: 10px 20px; font-size: 16px; cursor: pointer; } </style> </head> <body> <h1>Reef Controller</h1> <div id="temperature">Loading temperature...</div> <div id="humidity">Loading humidity...</div> <div class="control"> <h3>Control Panel</h3> <button onclick="sendCommand('heater', 1)">Turn Heater ON</button> <button onclick="sendCommand('heater', 0)">Turn Heater OFF</button><br><br> <button onclick="sendCommand('light', 1)">Turn Light ON</button> <button onclick="sendCommand('light', 0)">Turn Light OFF</button><br><br> <button onclick="sendCommand('pump', 1)">Turn Pump ON</button> <button onclick="sendCommand('pump', 0)">Turn Pump OFF</button> </div> <script> async function fetchData() { const response = await fetch('/api/data'); const data = await response.json(); document.getElementById('temperature').innerText = `Temperature: ${data.temperature} °C`; document.getElementById('humidity').innerText = `Humidity: ${data.humidity} %`; } async function sendCommand(device, state) { const response = await fetch(`/api/control?device=${device}&state=${state}`); const result = await response.text(); alert(result); } setInterval(fetchData, 5000); // Refresh data every 5 seconds fetchData(); // Initial fetch </script> </body> </html> )rawliteral"; server.send(200, "text/html", html); } // API to send sensor data void handleApiData() { float temperature = dht.readTemperature(); float humidity = dht.readHumidity(); String json = "{\"temperature\": " + String(temperature) + ", \"humidity\": " + String(humidity) + "}"; server.send(200, "application/json", json); } // API to control relays void handleControl() { String device = server.arg("device"); int state = server.arg("state").toInt(); if (device == "heater") { digitalWrite(HEATER_PIN, state); server.send(200, "text/plain", "Heater control successful."); } else if (device == "light") { digitalWrite(LIGHT_PIN, state); server.send(200, "text/plain", "Light control successful."); } else if (device == "pump") { digitalWrite(RETURN_PUMP_PIN, state); server.send(200, "text/plain", "Pump control successful."); } else { server.send(400, "text/plain", "Invalid device."); } }
___________________________________________________________________________________________________________________________________________________________________________________
i dont know why this is happening but here is arduino componentlist
___________________________________________________________________________________________________________________________________________________________________________________
Pin-to-Pin Connections ( Arduino mega )
1. Arduino Mega 2560:
- Power and Ground:
- 5V Pin: Connected to the positive terminal of your 5V power supply (for relays and sensors).
- GND Pin: Common ground for all components.
- virtuabotixRTC (DS1302):
- CLK (Clock): Connected to D6 on Mega.
- DAT (Data): Connected to D7 on Mega.
- RST (Reset): Connected to D8 on Mega.
- VCC: Connected to 5V.
- GND: Connected to GND.
- DS18B20 Temperature Sensor:
- VCC: Connected to 5V.
- GND: Connected to GND.
- Data Pin: Connected to D3 on Mega.
- Add a 4.7kΩ pull-up resistor between the Data pin and VCC.
- Adafruit TCS34725 Color Sensor:
- VIN: Connected to 5V.
- GND: Connected to GND.
- SCL (Clock): Connected to SCL (Pin 21) on Mega.
- SDA (Data): Connected to SDA (Pin 20) on Mega.
- 3-Channel Relay Module:
- Relay Module VCC: Connected to 5V (from power supply).
- Relay Module GND: Connected to GND (common ground with Mega).
- IN1 (Relay 1 - Heater): Connected to D22.
- IN2 (Relay 2 - Light): Connected to D23.
- IN3 (Relay 3 - Return Pump): Connected to D24.
- Servo Motor (Feeder):
- Signal Pin: Connected to D32 on Mega.
- VCC: Connected to 5V (ensure adequate current supply for servo).
- GND: Connected to GND.
- Leak Sensors:
- Leak Sensor 1 Signal: Connected to D4.
- Leak Sensor 2 Signal: Connected to D5.
- Power and Ground: Connected to 5V and GND, respectively.
- Water Level Sensors:
- Water Level Sensor 1 Signal: Connected to D11.
- Water Level Sensor 2 Signal: Connected to D12.
- Power and Ground: Connected to 5V and GND, respectively.
- Power and Ground:
Relay Wiring
Each relay has three terminals:
- COM (Common): Connect to one wire of the component (heater, light, or pump).
- NO (Normally Open): Connect to the live wire of the power supply feeding the component.
- NC (Normally Closed): Leave unconnected for this application.
Example for Heater:
- Cut the live wire of the heater's power cable.
- Connect one cut end to the COM terminal.
- Connect the other cut end to the NO terminal of the relay.
- Leave the neutral wire connected directly to the heater.
- Repeat for the light and return pump, using their respective relays.
Final Circuit Overview
Power Supply:
- Arduino Mega: Powered via USB or a regulated 5V/2A adapter connected to the 5V pin.
- Relay Module: Powered via the same 5V supply.
Connections:
- Ensure common ground between the Mega, sensors, and relays.
- Components connected to the relays (heater, light, pump) are powered independently via their respective power supplies.
Diagram Summary
- Arduino Mega to Sensors:
- DS1302 (RTC) → D6, D7, D8
- DS18B20 → D3 (Data with pull-up), 5V, GND
- TCS34725 → SDA (Pin 20), SCL (Pin 21)
- Leak Sensors → D4, D5
- Water Level Sensors → D11, D12
- Relays:
- Relay 1 (Heater) → D22
- Relay 2 (Light) → D23
- Relay 3 (Return Pump) → D24
- Servo Motor:
- Signal → D32
- Arduino Mega to Sensors:
Component Notes
- Relay Current Rating: Ensure relays can handle the current requirements of the heater, light, and pump.
- Servo Motor Power: If the servo draws significant current, use an external power supply for the servo.
- Pull-up Resistors: Place pull-up resistors (4.7kΩ or 10kΩ) on the DS18B20 data line.
ok arduino code now
-
Code:
// Arduino Mega 2560 Code #include <Wire.h> #include <RTClib.h> #include <Servo.h> #include <OneWire.h> #include <DallasTemperature.h> #include <Adafruit_TCS34725.h> // RTC Configuration RTC_DS1307 rtc; // DS18B20 Temperature Sensor Configuration #define ONE_WIRE_BUS 3 OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); // Color Sensor (TCS34725) Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_600MS, TCS34725_GAIN_1X); // PWM Pins for Lighting #define DAYLIGHT_PIN 9 #define MOONLIGHT_PIN 10 // Leak Sensors #define LEAK_SENSOR_1 4 #define LEAK_SENSOR_2 5 // Water Level Sensors #define WATER_LEVEL_1 11 #define WATER_LEVEL_2 12 // Servo (Feeder) #define SERVO_PIN 32 Servo feederServo; // Relay Pins for Pumps #define RETURN_PUMP_PIN 22 #define HEATER_PIN 23 // Timing Variables unsigned long lastUpdate = 0; const unsigned long updateInterval = 1000; // 1 second // Helper Functions for Lighting Control float sineWaveBrightness(float t, float startTime, float endTime) { if (t < startTime || t > endTime) return 0; float phase = (t - startTime) / (endTime - startTime) * 3.14159; return sin(phase) * 255; } void setup() { Serial.begin(9600); // For UART Communication with NodeMCU // Initialize RTC if (!rtc.begin()) { Serial.println("Couldn't find RTC"); while (1); } if (!rtc.isrunning()) { Serial.println("RTC is NOT running, setting the time..."); rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // Set RTC to compile time } // Initialize Sensors sensors.begin(); tcs.begin(); // Initialize Pins pinMode(DAYLIGHT_PIN, OUTPUT); pinMode(MOONLIGHT_PIN, OUTPUT); pinMode(LEAK_SENSOR_1, INPUT); pinMode(LEAK_SENSOR_2, INPUT); pinMode(WATER_LEVEL_1, INPUT); pinMode(WATER_LEVEL_2, INPUT); pinMode(RETURN_PUMP_PIN, OUTPUT); pinMode(HEATER_PIN, OUTPUT); digitalWrite(RETURN_PUMP_PIN, LOW); digitalWrite(HEATER_PIN, LOW); // Initialize Servo feederServo.attach(SERVO_PIN); feederServo.write(0); // Default position } void loop() { unsigned long currentMillis = millis(); // Get current time from RTC DateTime now = rtc.now(); int hr = now.hour(); int min = now.minute(); float hour = hr + min / 60.0; // Convert to a float for sine wave calculation // Lighting Control float daylightBrightness = sineWaveBrightness(hour, 7, 9) + sineWaveBrightness(hour, 17, 18); analogWrite(DAYLIGHT_PIN, daylightBrightness); if (hour >= 18 || hour < 6) { analogWrite(MOONLIGHT_PIN, 50); // Low moonlight brightness } else { analogWrite(MOONLIGHT_PIN, 0); } // Sensor Monitoring and Actuator Control if (currentMillis - lastUpdate >= updateInterval) { lastUpdate = currentMillis; // Temperature Sensor sensors.requestTemperatures(); float temperature = sensors.getTempCByIndex(0); Serial.print("Temperature: "); Serial.println(temperature); // Leak Sensors bool leak1 = digitalRead(LEAK_SENSOR_1); bool leak2 = digitalRead(LEAK_SENSOR_2); Serial.print("Leak Sensor 1: "); Serial.println(leak1 ? "No Leak" : "Leak Detected"); Serial.print("Leak Sensor 2: "); Serial.println(leak2 ? "No Leak" : "Leak Detected"); // Water Level Sensors bool level1 = digitalRead(WATER_LEVEL_1); bool level2 = digitalRead(WATER_LEVEL_2); Serial.print("Water Level 1: "); Serial.println(level1 ? "Normal" : "Low"); Serial.print("Water Level 2: "); Serial.println(level2 ? "Normal" : "Low"); // Return Pump Control digitalWrite(RETURN_PUMP_PIN, level1 ? HIGH : LOW); // Heater Control if (temperature < 25.0) { // Example threshold digitalWrite(HEATER_PIN, HIGH); } else { digitalWrite(HEATER_PIN, LOW); } // Communicate data to NodeMCU Serial.print("DATA:"); Serial.print(temperature); Serial.print(","); Serial.print(leak1); Serial.print(","); Serial.print(leak2); Serial.print(","); Serial.print(level1); Serial.print(","); Serial.println(level2); } }