Arduino Fish Tank Controller

Views 153
Sweets 0
Images 0

9 months ago Topic
DaVaR
Administrator
Total Posts: 18
Joined: August 27, 2017
Profile
Posted: November 10, 2017 @ 08:45 AM
I created this fish tank controller a while back.

Signature
Send me a PM if your interested in purchasing any of the PCBs I've created.
Reply By: DaVaR
8 months ago #1
DaVaR
Administrator
Total Posts: 18
Joined: August 27, 2017
Profile
Posted: November 22, 2017 @ 04:03 AM
Latest Code For The Fish Tank Controller Note: This code requires communication with a web server, but I can not locate the web server code.
Code
/*
  DaVaR Fish Tank Controller
  v 2.3
  http://wordpress.yousmellmail.com/arduino-fish-tank-aquarium-controller-project/
*/

/*-----( Import needed libraries )-----*/
#include 
#include  // Used for Ethernet
#include  
#include 
#include 

/*-----( Declare Constants )-----*/
//#define DEBUG   // Shows information in the serial monitor
#define RELAY_ON 1
#define RELAY_OFF 0

/*-----( Declare objects )-----*/
/*-----( Declare Variables )-----*/
#define Relay_1  26  // Arduino Digital I/O pin number
#define Relay_2  28
#define Relay_3  30
#define Relay_4  32
#define Relay_5  27  
#define Relay_6  29
#define Relay_7  31
#define Relay_8  33

// Temp Sensor data port
#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

// Assign the addresses of your 1-Wire temp sensors.
DeviceAddress tank1_temp = { 0x28, 0x70, 0xF9, 0x7D, 0x06, 0x00, 0x00, 0x37 };
DeviceAddress tank2_temp = { 0x28, 0x48, 0x2E, 0x7E, 0x06, 0x00, 0x00, 0xFC };
DeviceAddress room_temp = { 0x28, 0x7D, 0xFE, 0x7D, 0x06, 0x00, 0x00, 0x2C };

// **** ETHERNET SETTING ****
// Arduino Uno pins: 10 = CS, 11 = MOSI, 12 = MISO, 13 = SCK
// Ethernet MAC address - must be unique on your network - MAC Reads T4A001 in hex (unique in your network)
byte mac[] = { 0x00, 0xAA, 0xBA, 0xCA, 0xAD, 0x31 };                                       
// For the rest we use DHCP (IP address and such)
EthernetClient client;
char server[] = "192.168.1.31"; // IP Adres (or name) of server to dump data to

// Get info for fish feeder settings and stuff
char inString[32]; // string for incoming serial data
int stringPos = 0; // string index counter
boolean startRead = false; // is reading?
String feed_fish_command = "feed_fish_sent";
String tank_light_settings = "LIGHTS_ON";
String tank_air_pump_settings = "AIRPUMP_ON";

// Setting up delay timer thingy
long previousMillis = 0;        // will store last time updated
long interval = 40000;           // interval at which to delay (milliseconds)

// **** LED Pins ****
//int ledPin22 =  22;  // LED to show when in night mode
//int ledPin23 =  23;  // LED to show when in feeding mode

// Speaker Beep Beep
int speakerPin = A8;  // Notification speaker
 
// Servo settings for fish feeder
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 

// Setup the values for script
boolean tank_lights_on = false;
boolean tank_lights_off = false;
boolean tank_air_pump_on = false;
boolean tank_air_pump_off = false;
boolean tank_cir_pump_on = false;
boolean tank_filter_on = false;

#ifdef DEBUG
  #define feed_delay 10000  // Delay for pumps on/off and feedings (10 Sec.) // For Testing
#else
  #define feed_delay 300000  // Delay for pumps on/off and feedings (5 Min)
#endif

void setup()   /****** SETUP: RUNS ONCE ******/
{
  // Get the serial monitor ready for echo infomation
  #ifdef DEBUG
    Serial.begin(9600);
    Serial.println("  --------------------------------------------------- ");
    Serial.println("  Starting the Aquarium Controller. ");
    Serial.println("  --------------------------------------------------- ");
  #endif
      pinMode(speakerPin, OUTPUT);
      beep(50);
      beep(50);
      beep(50);
  
  Ethernet.begin(mac);
  
  #ifdef DEBUG
    Serial.println("  DaVaR Fish Tank Controller v2.3");
    Serial.println("  --------------------------------------------------- ");
    Serial.print("  IP Address        : ");
    Serial.println(Ethernet.localIP());
    Serial.print("  Subnet Mask       : ");
    Serial.println(Ethernet.subnetMask());
    Serial.print("  Default Gateway IP: ");
    Serial.println(Ethernet.gatewayIP());
    Serial.print("  DNS Server IP     : ");
    Serial.println(Ethernet.dnsServerIP());
    Serial.println("  --------------------------------------------------- ");
  #endif
  
    // Start up the library
    sensors.begin();
    // set the resolution to 10 bit (good enough?)
    sensors.setResolution(tank1_temp, 10);
    sensors.setResolution(tank2_temp, 10);
    sensors.setResolution(room_temp, 10);
  
  //-------( Initialize Pins so relays are inactive at reset)----
    digitalWrite(Relay_1, RELAY_OFF);
    digitalWrite(Relay_2, RELAY_OFF);
    digitalWrite(Relay_3, RELAY_OFF);
    digitalWrite(Relay_4, RELAY_OFF); 
    digitalWrite(Relay_5, RELAY_OFF);
    digitalWrite(Relay_6, RELAY_OFF);
    digitalWrite(Relay_7, RELAY_OFF);
    digitalWrite(Relay_8, RELAY_OFF); 
    
  //---( THEN set pins as outputs )----  
    pinMode(Relay_1, OUTPUT);   
    pinMode(Relay_2, OUTPUT);  
    pinMode(Relay_3, OUTPUT);  
    pinMode(Relay_4, OUTPUT); 
    pinMode(Relay_5, OUTPUT);   
    pinMode(Relay_6, OUTPUT);  
    pinMode(Relay_7, OUTPUT);  
    pinMode(Relay_8, OUTPUT);     
    delay(4000); //Check that all relays are inactive at Reset
  
    myservo.attach(34);  // attaches the servo on pin 9 to the servo object 
    
    #ifdef DEBUG
      Serial.println("  Setting the fish feeder servo to upward position. ");
      Serial.println("  --------------------------------------------------- ");
    #endif
  
    myservo.write(0);
  
    // Send info to web server
    Serial.println("  --------------------------------------------------- ");
    update_to_server("PowerUP", 1);  //sending data to server for tank 1
    Serial.println("  --------------------------------------------------- ");
    update_to_server("PowerUP", 2);  //sending data to server for tank 2
    Serial.println("  --------------------------------------------------- ");
    Serial.println("  Turning on the Water Filters and Circulation Pump.");
      digitalWrite(Relay_4, RELAY_ON); // Circulation Pump ON
      digitalWrite(Relay_5, RELAY_ON); // Tank 1 Water Filter ON
      digitalWrite(Relay_6, RELAY_ON); // Tank 2 Water Filter ON
    Serial.println("  --------------------------------------------------- ");
    Serial.println("  Controller Setup Now Finished.  Moving on to loop.");
    Serial.println("  --------------------------------------------------- ");
    

}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
      delay(5000);
    #ifdef DEBUG
      Serial.print("  - ");
      Serial.print(" .... ");
      Serial.println(" -  ");
    #endif
    
    //Setup delay timer
    // if there's no net connection, but there was one last time
    // through the loop, then stop the client:
    if (client.connected()) {
      #ifdef DEBUG
        Serial.println("  ........................................ ");
        Serial.println("  ..Closing any previous net connections.. ");
        Serial.println("  ........................................ ");
      #endif
      client.stop();
    }

// Run the following every 60 seconds  
unsigned long currentMillis = millis();

if(currentMillis - previousMillis > interval) {
  // save the last time you connected to server for update
  previousMillis = currentMillis;  
      
  // *** Update Status to Database *** //      
  
          //Update Current Status of fish tanks to database
           #ifdef DEBUG
            Serial.println();
            Serial.println("  ......................................... ");
            Serial.println("  ..Updating Current Status of Fish Tanks.. ");
            Serial.println("  ......................................... ");
           #endif
           
              delay(3000);
              update_cur_status("Status", 1);  //sending data to server - tank 1
              delay(3000);
              update_cur_status("Status", 2);  //sending data to server - tank 2
          
           #ifdef DEBUG
            Serial.println("  .................END..................... ");
            Serial.println();
           #endif
           
  // *** Feed Fish Database Check *** //
  
          //Check to see if user wants to feed fish from website
          delay(3000); //wait 3 seconds before connecting again 
          
          // Website Feed Fish Command Thingy
          #ifdef DEBUG
            Serial.println();
            Serial.println("  ................................................. ");
            Serial.println("  ..Connecting to Web Site For Data.... ");
            Serial.println("  ................................................. ");
            Serial.println("  .....Getting Data For Fish Feed...... ");
          #endif
          
          String pageValue = connectAndRead("/fish/data_for_controller.php?read_data=TRUE"); //connect to the server and read the output
  
          delay(3000); //wait 3 seconds  
          
          #ifdef DEBUG
            Serial.print("  - Data From Server :: ");
            Serial.print(pageValue); //print out the findings.
            Serial.println(" :: ");
          #endif
        
          // If time to feed fish, feed fish
          if (pageValue == feed_fish_command){
            #ifdef DEBUG
               Serial.println("  ................................................. ");
               Serial.println("  ..Website Command Sent to Feed Fish.. ");
               Serial.println("  ................................................. "); 
            #endif
            
            // Feeding fish
            servo_feed_fish();
          } else {
            #ifdef DEBUG
              Serial.println("  -- Feed Fish is FALSE!! --  "); 
            #endif
          }
            #ifdef DEBUG
               Serial.println("  ................................................. ");
               Serial.println("  ..End Feed Fish Check.. ");
               Serial.println("  ................................................. "); 
            #endif
  
  // *** Lights ON/OFF Database Check *** //  
          
          #ifdef DEBUG
            Serial.println("  ................................................. ");
            Serial.println("  .....Getting Data For Lights......... ");
          #endif
          
          String pageValue_lights = connectAndRead("/fish/light_control.php?read_data=TRUE"); //connect to the server and read the output
  
          delay(3000); //wait 3 seconds  
          
          #ifdef DEBUG
            Serial.print("  - Data From Server :: ");
            Serial.print(pageValue_lights); //print out the findings.
            Serial.println(" :: ");
            Serial.println("  ................................................. ");
            Serial.println("  ..Website Command Sent for Lights.... ");
            Serial.println("  ................................................. "); 
            #endif
          
          // If time to feed fish, feed fish
          if (pageValue_lights == tank_light_settings){
            //Turn Lights ON
            tank_lights_on = true;
            #ifdef DEBUG
              Serial.println("  -- LIGHTS_ON --  "); 
            #endif
          } else {
            //Turn Lights OFF
            tank_lights_on = false;
            #ifdef DEBUG
              Serial.println("  -- LIGHTS_OFF --  "); 
            #endif
          }
          
          #ifdef DEBUG
            Serial.println("  ................................................. ");
            Serial.println("  ......End Light Settings Check......... ");
            Serial.println("  ................................................. ");
            Serial.println();
          #endif
          
          delay(3000); //wait 3 seconds
 
  
      // Turns lights on if they are not already and requested to be on    
      if(tank_lights_on == true){
          
           if(tank_lights_off == false){
             
            digitalWrite(Relay_1, RELAY_ON); // Tank 1 Light ON
            digitalWrite(Relay_2, RELAY_ON); // Tank 2 Light ON
             
            // Send info to web server
            update_to_server("StartOfDay", 1);  //sending data to server - tank 1
            update_to_server("StartOfDay", 2);  //sending data to server - tank 2
             
            #ifdef DEBUG
              Serial.println("  ................................................. ");
              Serial.println("  Start of day. ");
              Serial.println("  Tank Light ON. ");
              Serial.println("  Goodmorning Fish. ");
              Serial.println("  ................................................. ");
            #endif
            
            beep(100);
            beep(100);
            
           }
           tank_lights_off = true;
  
      // Turns lights off if they are requested to be off      
      }else{
        //Starting to get dark... lets feed the fish and set a delay before 
        //The lights turn off
        
         if(tank_lights_off == true){
           
          #ifdef DEBUG
            Serial.println("  ................................................. ");
            Serial.println("  End of day. ");
          #endif
          
          tank_lights_off = false;
  
          #ifdef DEBUG
            Serial.println("  Tank Light OFF. ");
            Serial.println("  Goodnight Fish. ");
            Serial.println("  ................................................. ");
          #endif
          beep(100);
          beep(100);
          
          digitalWrite(Relay_1, RELAY_OFF); // Tank 1 Light OFF
          digitalWrite(Relay_2, RELAY_OFF); // Tank 2 Light OFF
  
          
          // Send info to web server
          update_to_server("EndOfDay", 1);  //sending data to server - tank 1
          update_to_server("EndOfDay", 2);  //sending data to server - tank 2
         } 
  
      }   
    
  // *** Air Pump ON/OFF Database Check *** //  
    
          // Check to see if website wants the air pump on or off
          #ifdef DEBUG
            Serial.println("  ................................................. ");
            Serial.println("  .....Getting Data For Air Pump......... ");
          #endif
          
          String pageValue_airpump = connectAndRead("/fish/air_control.php?read_data=TRUE"); //connect to the server and read the output
  
          delay(3000); //wait 3 seconds  
          
          #ifdef DEBUG
            Serial.print("  - Data From Server :: ");
            Serial.print(pageValue_airpump); //print out the findings.
            Serial.println(" :: ");
            Serial.println("  ................................................. ");
            Serial.println("  ..Website Command Sent for Air Pump.... ");
            Serial.println("  ................................................. "); 
            #endif
          
          // Checking website to see if air pump is on/off
          if (pageValue_airpump == tank_air_pump_settings){
            //Turn Lights ON
            tank_air_pump_on = true;
            #ifdef DEBUG
              Serial.println("  -- AIRPUMP_ON --  "); 
            #endif
          } else {
            //Turn Lights OFF
            tank_air_pump_on = false;
            #ifdef DEBUG
              Serial.println("  -- AIRPUMP_OFF --  "); 
            #endif
          } // End pageValue_airpump
          
          #ifdef DEBUG
            Serial.println("  ................................................. ");
            Serial.println("  ......End Air Pump Settings Check......... ");
            Serial.println("  ................................................. ");
            Serial.println();
          #endif
          
          delay(3000); //wait 3 seconds
          
              if(tank_air_pump_on == true){
                if(tank_air_pump_off == false){
                  // Air Pump ON
                    #ifdef DEBUG
                      Serial.println("  ................................................. ");
                      Serial.println("  Web Site Command Sent to turn Air Pump ON. ");
                      Serial.println("  ................................................. ");
                    #endif
                  // Update website that air pump is on
                  update_to_server("AirPumpON", 1);  //sending data to server - tank 1
                  update_to_server("AirPumpON", 2);  //sending data to server - tank 2
                  digitalWrite(Relay_3, RELAY_ON); // Air Pump ON
                }
                tank_air_pump_off = true;
              } else {
                if(tank_air_pump_off == true){
                  // Air Pump OFF
                    #ifdef DEBUG
                      Serial.println("  ................................................. ");
                      Serial.println("  Web Site Command Sent to turn Air Pump OFF. ");
                      Serial.println("  ................................................. ");
                    #endif
                  // Update website that air pump is on
                  update_to_server("AirPumpOFF", 1);  //sending data to server - tank 1
                  update_to_server("AirPumpOFF", 2);  //sending data to server - tank 2
                  digitalWrite(Relay_3, RELAY_OFF); // Air Pump OFF
                  tank_air_pump_off = false;
                }
              } // End Air Pump Check
       }

   delay(1000);
   
}//--(end main loop )---


void servo_feed_fish(){
  
        delay(1000);
  
        #ifdef DEBUG
          Serial.println("  ................................................. ");
          Serial.println("  Starting to feed fish first time. ");
          Serial.println("  Turning off Water Filters and Circulation Pump. ");
          Serial.println("  ................................................. ");
        #endif
        beep(500);
        beep(50);
        beep(50);
        
        // Turn off Water filters and Circulation pump while feeding fish.
        digitalWrite(Relay_4, RELAY_OFF); // Circulation Pump OFF
        digitalWrite(Relay_5, RELAY_OFF); // Tank 1 Water Filter OFF
        digitalWrite(Relay_6, RELAY_OFF); // Tank 2 Water Filter OFF
        
        // Turn servo to feed fish
        for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
        {                                  // in steps of 1 degree 
          myservo.write(pos);              // tell servo to go to position in variable 'pos' 
          delay(15);                       // waits 15ms for the servo to reach the position 
        } 
        for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
        {                                
          myservo.write(pos);              // tell servo to go to position in variable 'pos' 
          delay(15);                       // waits 15ms for the servo to reach the position 
        } 
        #ifdef DEBUG
          Serial.println("  Ending feed fish first time. ");
          Serial.println("  ---- ");
          Serial.println("  Starting to feed fish second time. ");
        #endif
        for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
        {                                  // in steps of 1 degree 
          myservo.write(pos);              // tell servo to go to position in variable 'pos' 
          delay(15);                       // waits 15ms for the servo to reach the position 
        } 
        for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
        {                                
          myservo.write(pos);              // tell servo to go to position in variable 'pos' 
          delay(15);                       // waits 15ms for the servo to reach the position 
        } 
        
        // Update website that fish have been fed
        update_to_server("FeedFish", 1);  //sending data to server - tank 1
        update_to_server("FeedFish", 2);  //sending data to server - tank 2
        
        #ifdef DEBUG
          Serial.println("  ................................................. ");
          Serial.println("  Ending feed fish second time. ");
          Serial.println("  Waiting for set delay time to turn pumps back on ");
          Serial.println("  ................................................. ");
        #endif
        
        beep(50);
        
        delay(feed_delay);
        // Trigger relays to turn pumps back on
        digitalWrite(Relay_4, RELAY_ON); // Circulation Pump ON
        digitalWrite(Relay_5, RELAY_ON); // Tank 1 Water Filter ON
        digitalWrite(Relay_6, RELAY_ON); // Tank 2 Water Filter ON
               
        #ifdef DEBUG
          Serial.println("  ................................................. ");
          Serial.println("  Water Filter and Circulation Pump back on ");
          Serial.println("  Returning back to the normal loop");
          Serial.println("  ................................................. ");
        #endif
        beep(50);
        beep(50);
        beep(500);
}

// Function to update the server with status of tanks
// Only Runs when there is an event taking place, feedfish, endofday..
void update_to_server(char* local_tank_status, int local_tank_id){
  
    int relay_status_1 = digitalRead(Relay_1);   //get status of relay for tank 1
    int relay_status_2 = digitalRead(Relay_2);   //get status of relay for tank 2
  
    if (!client.connect(server, 80)) {
      #ifdef DEBUG
        Serial.println("  !! Connection failure detected: Resetting ENC !! ");  // only use serial when debugging
      #endif
    } else {
      client.stop();
    } 
    
      #ifdef DEBUG
        Serial.println("");
        Serial.println("      ................................................. ");
        Serial.println("      - Getting Temp Sensors Data -  ");
        Serial.print("      Getting temperatures...\n\r");
      #endif
        sensors.requestTemperatures();
      #ifdef DEBUG
        Serial.print("      Tank 1 temperature is: ");
        printTemperature(tank1_temp);
        Serial.print("\n\r");
        Serial.print("      Tank 2 temperature is: ");
        printTemperature(tank2_temp);
        Serial.print("\n\r");
        Serial.print("      Room temperature is: ");
        printTemperature(room_temp);
        Serial.print("\n\r");
        Serial.println("      ................................................. ");
        
        // if there's incoming data from the net connection.
        // send it out the serial port.  This is for debugging
        // purposes only:
        if (client.available()) {
          char c = client.read();
          Serial.print(c);
        }
      #endif
      
    
    // if you get a connection, report back via serial:
    if (client.connect(server, 80)) 
    {
       #ifdef DEBUG
        Serial.print("  -- Tank ID: ");  // only use serial when debugging
        Serial.print(local_tank_id);
        Serial.println("  -- ");
      #endif
      
      #ifdef DEBUG
        Serial.println("  !! Connected to Server !! ");  // only use serial when debugging
      #endif
      
      // Make a HTTP request:
      #ifdef DEBUG
        Serial.println("  !! Sending Data !! ");  // only use serial when debugging
      #endif
      
      // Tank 1 Data
      client.print( "GET /fish/add_data.php?");
      client.print("ti");
      client.print("=");
      client.print(local_tank_id);          // Tank ID 1/2/3/4 etc.
      client.print("&tl");
      client.print("=");
      if (local_tank_id == 1){
        client.print(relay_status_1);         // Tank light status ON/OFF - 1/0
      }else if (local_tank_id == 2){
        client.print(relay_status_2);         // Tank light status ON/OFF - 1/0
      }
      client.print("&tt");
      client.print("=");
        // Get the tank temp
        if (local_tank_id == 1){
           getTemperature(tank1_temp);
        }
        // Get the tank temp
        if (local_tank_id == 2){
           getTemperature(tank2_temp);
        }
      client.print("&rt");
      client.print("=");
        // Get the room temp
        getTemperature(room_temp);
      client.print("&ts");
      client.print("=");
      client.print(local_tank_status);  // Tank Status
      client.print("&tkn");
      client.print("=");
      client.print("82648568851323");  // Tank Status
    
      client.println( " HTTP/1.1");
      client.println( "Host: 192.168.1.31" );
      client.print(" Host: ");
      client.println(server);
      client.println( "Connection: close" );
      client.println();
      client.println();
      
      // if the server's disconnected, stop the client:
      if (client.connected()) {
        #ifdef DEBUG
          Serial.println("  Disconnecting from server...  ");
        #endif
        client.stop();
      }
      
    }
    else 
    {
      // you didn't get a connection to the server:
      client.stop();
      #ifdef DEBUG
        Serial.println("  !! Connection failed !! ");  // only use serial when debugging
      #endif
    }

}

// Function that updates a single database entry at a given
// Timer Interval to allow updates on tanks throughout the day
// Gives user an idea of tank status throughout the day
void update_cur_status(char* local_tank_status_u, int local_tank_id_u){
  
    int relay_status_1 = digitalRead(Relay_1);   //get status of relay for tank 1
    int relay_status_2 = digitalRead(Relay_2);   //get status of relay for tank 2
  
    if (!client.connect(server, 80)) {
      #ifdef DEBUG
        Serial.println("  !! Connection failure detected: Resetting ENC !! ");  // only use serial when debugging
      #endif
    } else {
      client.stop();
    } 
    
      #ifdef DEBUG
        Serial.println("");
        Serial.println("      ................................................. ");
        Serial.println("      - Getting Temp Sensors Data -  ");
        Serial.print("      Getting temperatures...\n\r");
      #endif
        sensors.requestTemperatures();
      #ifdef DEBUG
        Serial.print("      Tank 1 temperature is: ");
        printTemperature(tank1_temp);
        Serial.print("\n\r");
        Serial.print("      Tank 2 temperature is: ");
        printTemperature(tank2_temp);
        Serial.print("\n\r");
        Serial.print("      Room temperature is: ");
        printTemperature(room_temp);
        Serial.print("\n\r");
        Serial.println("      ................................................. "); 
        
        // if there's incoming data from the net connection.
        // send it out the serial port.  This is for debugging
        // purposes only:
        if (client.available()) {
          char c = client.read();
          Serial.print(c);
        }
      #endif
      
    // if you get a connection, report back via serial:
    if (client.connect(server, 80)) 
    {
       #ifdef DEBUG
        Serial.println("  ................................................. ");
        Serial.print("  -- Tank ID: ");  // only use serial when debugging
        Serial.print(local_tank_id_u);
        Serial.println("  -- ");
        Serial.print("  -- Status: ");  // only use serial when debugging
        Serial.print(local_tank_status_u);
        Serial.println("  -- ");
      #endif
      
      #ifdef DEBUG
        Serial.println("  !! Connected to Server !! ");  // only use serial when debugging
      #endif
      
      // Make a HTTP request:
      #ifdef DEBUG
        Serial.println("  !! Updating Data !! ");  // only use serial when debugging
      #endif
      
      // Tank 1 Data
      client.print( "GET /fish/add_data_update.php?");
      client.print("ti");
      client.print("=");
      client.print(local_tank_id_u);          // Tank ID 1/2/3/4 etc.
      client.print("&tl");
      client.print("=");
      if (local_tank_id_u == 1){
        client.print(relay_status_1);         // Tank light status ON/OFF - 1/0
      }else if (local_tank_id_u == 2){
        client.print(relay_status_2);         // Tank light status ON/OFF - 1/0
      }
      client.print("&tt");
      client.print("=");
        // Get the tank temp
        if (local_tank_id_u == 1){
           getTemperature(tank1_temp);
        }
        // Get the tank temp
        if (local_tank_id_u == 2){
           getTemperature(tank2_temp);
        }
      client.print("&rt");
      client.print("=");
        // Get the room temp
        getTemperature(room_temp);
      client.print("&ts");
      client.print("=");
      client.print(local_tank_status_u);  // Tank Status
      client.print("&tkn");
      client.print("=");
      client.print("82648568851323");  // Tank Status
    
      client.println( " HTTP/1.1");
      client.println( "Host: 192.168.1.31" );
      client.print(" Host: ");
      client.println(server);
      client.println( "Connection: close" );
      client.println();
      client.println();
      
      // if the server's disconnected, stop the client:
      if (client.connected()) {
        #ifdef DEBUG
          Serial.println("  Disconnecting from server...  ");
        #endif
        client.stop();
      }
      
     
    }
    else 
    {
      // you didn't get a connection to the server:
      client.stop();
      #ifdef DEBUG
        Serial.println("  !! Connection failed !! ");  // only use serial when debugging
      #endif
    }

}

void beep(unsigned char delayms){
  digitalWrite(A8, 20);      // Almost any value can be used except 0 and 255
                           // experiment to get the best tone
  delay(delayms);          // wait for a delayms ms
  digitalWrite(A8, 0);       // 0 turns it off
  delay(delayms);          // wait for a delayms ms   
}  

// Function that gets current temp
void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) {
    #ifdef DEBUG
      Serial.print("      Error getting temperature");
    #endif
  } else {
    #ifdef DEBUG
      Serial.print("C: ");
      Serial.print(tempC);
      Serial.print(" F: ");
      Serial.print(DallasTemperature::toFahrenheit(tempC));
    #endif

  }
}

// Function that gets current temp
void getTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) {
    client.print("00");
  } else {
    client.print(DallasTemperature::toFahrenheit(tempC));         // Tank Temp
  }
}


//Functions for fish feeder from website command
String connectAndRead(char* read_data_page_url){
  //connect to the server
  
  #ifdef DEBUG
    Serial.println("  - Connecting... - ");
  #endif
  
  //port 80 is typical of a www page
  if (client.connect(server, 80)) {
    
    #ifdef DEBUG
      Serial.println("  - Connected - ");
    #endif
    
    client.print("GET ");
    client.println(read_data_page_url);
    client.println("HTTP/1.0");
    client.println();

    //Connected - Read the page
    return readPage(); //go and read the output

    // if the server's disconnected, stop the client:
    if (!client.connected()) {
      Serial.println();
      Serial.println("  Disconnecting from server...  ");
      Serial.println();
      client.stop();
    }

  }else{
    return "connection failed";
  }

}

String readPage(){
  //read the page, and capture & return everything between ''

  stringPos = 0;
  memset( &inString, 0, 32 ); //clear inString memory

  while(true){

    if (client.available()) {
      char c = client.read();

      if (c == '' is our ending character
          inString[stringPos] = c;
          stringPos ++;
        }else{
          //got what we need here! We can disconnect now
          startRead = false;
          client.stop();
          client.flush();
          
          #ifdef DEBUG
            Serial.println("  - Disconnecting - ");
          #endif
          
          return inString;

        }

      }
    }

  }

}

//*********( THE END )***********

Signature
Send me a PM if your interested in purchasing any of the PCBs I've created.
Forum Permissions
You cannot post in this forum.
You cannot moderate this forum.
You cannot administrate this forum.
Search Forums
Forum Recent Posts