Arduino

now browsing by category

Arduino related posts

 

Arduino controlling my reef aquarium

Reef aquarium

Reef aquarium

I have an Reef Aquarium, he has approximately 700 liters and too much equipment to take care, so why not put Arduino working for me?

Yes, that’s what i done and i will share with you because with the system i have, i can remove it from the aquarium and put it controlling anything i need. The hardware it’s already mounted, i just need to program him to do something different.

CAM00087

Arduino electrical cabinet

The system have two DIY boards, one have what is called RTC (Real Time Clock) to give to the Arduino the input of time, I need real time to control lights and make logs for example, because you can’t get current time and date from him, have two input connections and another one to read temperature sensor values.

The other one it’s to switch outputs by 24v relays.

rtc

DIY Arduino shield with RTC, inputs and temperature reading

Arduino outputs DIY shield

Arduino outputs DIY shield

If you noticed, ethernet shield it’s mounted on Arduino, with internet connection, i can control, monitorize and send email alerts.

mounted

Ethernet shield mounted

I like to discover and do new things, thats why i done my own shields. But if you take a look for example at ebay, you find shields at a good price, prices that makes better, buy than make at home. Sometimes the money you spend more when buying on the internet it’s the posting cost. For other project i bought a relays shield instead of making my own. Less time spend to do other things.

Ok, now i have Arduino with ethernet shield to connect to the internet, outlets connected to the output relays to give power to anything i want, two inputs to receive information like status of level sensors, Real time clock to control things by hour\date, and interface for temperature input.

Now the code that controls my aquarium. I will explain some parts, you can download the code and ask help if don’t understand something or need help developing your own code.

First we need to declare and initiate some variables, like Arduino IP to communicate with Arduino trough the internet, MAC address and port address for our Arduino ethernet shield, IP of the website where you will monitorize and control Arduino.


// ethernet shield configuration
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //because you must assign one MAC to the Arduino ethernet shield
byte ip[] = { 192, 168, 1, 10 }; // arduino ip
byte WebControlIP[] = {130, 185, 82, 44}; //your website ip
EthernetServer server(80); //server on port 80

Constants for inputs and outputs

// constants won't change. They're used here to set input pin numbers:
const int floatSensor1 = 22;   // float sensor N1 (maximum level)
const int floatSensor2 = 24;   // float sensor N2(minimum level)
const int tempSensor = 15;     //LM35 temperature sensor
// set output pin numbers:
const int relay1 = 30;    //output relay1 - Moonlight
const int relay2 = 32;    //output relay2 - Top sump pump
const int relay3 = 34;    //output relay3 - Down sump pump
const int relay4 = 36;    //output relay4 - Movement pump
const int relay5 = 38;    //output relay5 - Skimmer
const int relay6 = 40;    //output relay6 - Osmose water pump
const int relay7 = 42;    //output relay7 - Reserve
const int relay8 = 45;    //output relay8 - White light
const int relay9 = 47;    //output relay9 - Blue Light
const int relay10 = 49;   //output relay10 - Heater

Variables to help us 

// variables will change:
int float1State = 0;    // variable for reading the float1 status
int float2State = 0;    // variable for reading the float1 status
float tempSetpoint = 26;    //temperature setpoint
float temperature;    //water temperature
int systemMode = 1; //0-manual 1-automatic
int arduinoStart = 0; //to control arduino fails
int alarmTemperatureAlarm = 0; //status of temperature alarm, 1- active alarm
int resetActiveAlarm = 0 ; //status of reset alarm, 1- active alarm
 
int lightMoonON_H = 10;    //HOUR to turn ON the Moon light
int lightMoonON_M = 0;    //MINUTES to turn ON the Moon light
int lightMoonOFF_H = 22;    //HOUR to turn OFF the Moon light
int lightMoonOFF_M = 0;    //MINUTES to turn OFF the Moon light
int moonState = 0; //status off the moonlight
 
int lightBlueON_H = 11;    //HOUR to turn ON the Blue light
int lightBlueON_M = 0;    //MINUTES to turn ON the Blue light
int lightBlueOFF_H = 21;    //HOUR to turn OFF the Blue light
int lightBlueOFF_M = 0;    //MINUTES to turn OFF the Blue light
int blueState = 0;  //status off the blue light
 
int lightWhiteON_H = 12;    //HOUR to turn ON the white light
int lightWhiteON_M = 0;    //MINUTES to turn ON the white light
int lightWhiteOFF_H = 20;    //HOUR to turn OFF the white light
int lightWhiteOFF_M = 0;    //MINUTES to turn OFF the white light
int whiteState = 0; //status off the white light

Every Arduino code have the “Setup” function, where we can initialize some stuff before the main code start running at the “Loop” function. Here i call functions to initialize RTC, ethernet server, serial communication, assign output and input variables to pins and set them on or off (LOW or HIGH)

void setup() {
  Wire.begin();
  RTC.begin();
  SPI.begin();
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.begin(9600);    //opens serial port, sets data rate to 9600 bps
  if (! RTC.isrunning()) {
 Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }
  delay(1000);
 
  pinMode(floatSensor1, INPUT);    // initialize the floatSensor1 pin as an input
  pinMode(floatSensor2, INPUT);    // initialize the floatSensor2 pin as an input
  pinMode(tempSensor, INPUT);    // initialize the tempSensor pin as an input:
  pinMode(relay1, OUTPUT);    //initialize the relays pins as an output
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);
  pinMode(relay5, OUTPUT);
  pinMode(relay6, OUTPUT);
  pinMode(relay7, OUTPUT);
  pinMode(relay8, OUTPUT);
  pinMode(relay9, OUTPUT);
  pinMode(relay10, OUTPUT);
 
  digitalWrite(relay1, LOW);  //set all relay Outputs OFF
  digitalWrite(relay2, LOW);
  digitalWrite(relay3, LOW);
  digitalWrite(relay4, LOW);
  digitalWrite(relay5, LOW);
  digitalWrite(relay6, LOW);
  digitalWrite(relay7, LOW);
  digitalWrite(relay8, LOW);
  digitalWrite(relay9, LOW);
  digitalWrite(relay10, LOW);
}

Now the “Loop” function. Like the name says, this function will work always till Arduino is on, runs the code till the end of the function and starts again from the beginning.

void loop(){
 Serial.println("[loop]begin");
 now = RTC.now();
 tempFunction();  //gets actual temperature
    TemperatureAlarmMail();  //send Temperature alarm to mail if outbounds setpoint
    resetAlarmMail();  //send alarm to mail on arduino reset or power fail
 sendIPandTemperature(now);  //send arduino IP and water temperature to the database
 listenForEthernetClients();  //check for web client
 delay(1000);
 if( systemMode == 1){ //if the system is in automatic mode will do the aquarium control
 automaticControl(now);
 delay(1000);
 }
 Serial.println("[loop]end");
 Serial.println();
}

Inside i have tempFunction that gets actual temperature, TemperatureAlarmMail that sends temperature alarm for configured email if bad temperature, resetAlarmMail that sends email in case of arduino fail or power fail, sendIPandTemperature that sends temperature and IP value to database to make logs, graphics and write new IP address in case my router has changed IP.

listenForEthernetClients that checks if exists connections to the server, if anyone are at the website communicating with arduino.

The last function it’s automaticControl that will control Aquarium in automatic mode. For security i make the heater turn on only by automatic control, imagine someone enter the website (with login), put the system in manual mode and turn on the heater, will burn all aquarium life…

void loop(){
 Serial.println("[loop]begin");
 now = RTC.now();
 tempFunction();  //gets actual temperature
    TemperatureAlarmMail();  //send Temperature alarm to mail if outbounds setpoint
    resetAlarmMail();  //send alarm to mail on arduino reset or power fail
 sendIPandTemperature(now);  //send arduino IP and water temperature to the database
 listenForEthernetClients();  //check for web client
 delay(1000);
 if( systemMode == 1){ //if the system is in automatic mode will do the aquarium control
 automaticControl(now);
 delay(1000);
 }
 Serial.println("[loop]end");
 Serial.println();
}

You will need:

Source code – download

Arduino ethernet library – import from Arduino software

RTC library -  import from Arduino software

SPI library - import from Arduino software

Wire library -  import from Arduino software