Power Outage code Problems

Share you PDE file with our community

Posts: 471
Joined: Sat Jan 14, 2012 2:08 pm
Location: Montgomery Village, MD
PostPosted: Mon Jul 15, 2013 7:27 pm
first, here is my complete code if needed for review:

Code: Select all
    #include <ReefAngel_Features.h>
    #include <Globals.h>
    #include <RA_Wifi.h>
    #include <Wire.h>
    #include <OneWire.h>
    #include <Time.h>
    #include <DS1307RTC.h>
    #include <InternalEEPROM.h>
    #include <RA_NokiaLCD.h>
    #include <RA_ATO.h>
    #include <RA_Joystick.h>
    #include <LED.h>
    #include <RA_TempSensor.h>
    #include <RA_Colors.h>
    #include <RA_CustomColors.h>
    #include <Relay.h>
    #include <RA_PWM.h>
    #include <Timer.h>
    #include <Memory.h>
    #include <IO.h>
    #include <Salinity.h>
    #include <PH.h>
    #include <WiFiAlert.h>
    #include <ReefAngel.h>
   
     // Globals for Params on Custom Main
    byte x,y;
    char text[10];
   

    // Define Custom Variables for Portal
    #define Var_Power        1


    // Custom Menu
    #include <avr/pgmspace.h>
    prog_char menu0_label[] PROGMEM = "Turn Lights On";
    prog_char menu1_label[] PROGMEM = "Turn Lights Off";
    prog_char menu2_label[] PROGMEM = "Start Feeding";
    prog_char menu3_label[] PROGMEM = "Start Water Change";
    prog_char menu4_label[] PROGMEM = "Clear Overheat";
    prog_char menu5_label[] PROGMEM = "Calibrate pH";
    prog_char menu6_label[] PROGMEM = "Calibrate Salinity";
    PROGMEM const char *menu_items[] = {
    menu0_label, menu1_label, menu2_label, menu3_label, menu4_label, menu5_label, menu6_label  };

    void MenuEntry1()
    {
    ReefAngel.DisplayMenuEntry("Item 1");ReefAngel.Relay.RelayMaskOn = ReefAngel.LightsOnPorts;
    #ifdef RelayExp
    for ( byte i = 0; i < MAX_RELAY_EXPANSION_MODULES; i++ )
    {
        ReefAngel.Relay.RelayMaskOnE[i] = ReefAngel.LightsOnPortsE[i];
    }
    #endif  // RelayExp
    ReefAngel.Relay.Write();

    }
    void MenuEntry2()
    {
    ReefAngel.DisplayMenuEntry("Item 2");ReefAngel.Relay.RelayMaskOn = 0;
    #ifdef RelayExp
    for ( byte i = 0; i < MAX_RELAY_EXPANSION_MODULES; i++ )
    {
        ReefAngel.Relay.RelayMaskOnE[i] = 0;
    }
    #endif  // RelayExp
    ReefAngel.Relay.Write();

    }
    void MenuEntry3()
    {
    ReefAngel.DisplayMenuEntry("Item 3");ReefAngel.FeedingModeStart();

    }
    void MenuEntry4()
    {
    ReefAngel.DisplayMenuEntry("Item 4");ReefAngel.WaterChangeModeStart();

    }
    void MenuEntry5()
    {
    ReefAngel.DisplayMenuEntry("Item 5");ReefAngel.OverheatClear();

    }
    void MenuEntry6()
    {
    ReefAngel.DisplayMenuEntry("Item 6");ReefAngel.SetupCalibratePH();
    ReefAngel.DisplayedMenu = ALT_SCREEN_MODE;

    }
    void MenuEntry7()
    {
    ReefAngel.DisplayMenuEntry("Item 7");ReefAngel.SetupCalibrateSalinity();
    }
   
    // End Custom Menu Globals

    // Define Relay Ports by Name
    #define DT_LightsOne       1
    #define DT_LightsTwo       2
    #define Unused3            3
    #define Unused4            4
    #define DT_LWM             5
    #define GT_LWM             6
    #define Unused7            7
    #define Unused8            8

    #define Unused11           Box1_Port1
    #define Unused12           Box1_Port2
    #define GT_LightsOne       Box1_Port3
    #define GT_LightsTwo       Box1_Port4
    #define DT_RWM             Box1_Port5
    #define GT_RWM             Box1_Port6
    #define DT_Heat            Box1_Port7
    #define DT_Pump            Box1_Port8
   
    #define RV_Heat            Box2_Port1
    #define RV_Pump            Box2_Port2
    #define GT_Heat            Box2_Port3
    #define Unused24           Box2_Port4
    #define Unused25           Box2_Port5
    #define Unused26           Box2_Port6
    #define Skimmer            Box2_Port7
    #define GT_Pump            Box2_Port8

   
   
    void setup() {
        ReefAngel.Init();  //Initialize controller
        ReefAngel.InitMenu(pgm_read_word(&(menu_items[0])),SIZE(menu_items)); // Initialize the menu
        ReefAngel.FeedingModePorts = DT_LWM | GT_LWM | GT_Pump | DT_Pump | DT_RWM | GT_RWM | Skimmer; // Turn off when feeding mode is activated is activated
        ReefAngel.WaterChangePorts = DT_Heat| DT_LWM | DT_RWM | GT_LWM | GT_RWM | GT_Pump | DT_Pump; // Turn off when water change mode is activated
        ReefAngel.TempProbe = T2_PROBE;  // Set the 2nd plug to be DT Main temp
        ReefAngel.OverheatProbe = T2_PROBE;  // to monitor the overheat and temperatures
        ReefAngel.OverheatShutoffPorts = DT_LightsOne | DT_LightsTwo | DT_Heat | GT_LightsOne | GT_LightsTwo | GT_Heat | Skimmer;  // Items turned off when Overheat temperature exceeded
        InternalMemory.OverheatTemp_write( 829 ); // Set the DT Overheat temperature setting
        ReefAngel.LightsOnPorts = DT_LightsOne | DT_LightsTwo | GT_LightsOne | GT_LightsTwo; // Toggle Lights
 
   // Ports that default on
        ReefAngel.Relay.On(DT_Pump); // Turn on DT Pump
        ReefAngel.Relay.On(GT_Pump); // Turn on GT Pump   
        ReefAngel.Relay.On(Skimmer); // Turn on Skimmer
    }
    void loop()
    {

        CheckPower();           // Monitor for power outages
 
        ReefAngel.StandardLights( DT_LightsOne,14,00,22,00 ); // DT Lights on at 2:00pm and off at 10:00pm
        ReefAngel.StandardLights( DT_LightsTwo,22,00,01,00 ); // DT-Moon Lights on at 10:00am and off at 1:00am
        ReefAngel.StandardLights( GT_LightsOne,14,00,21,45 );  //GT Lights Main 2:00pm - 9:45pm
        ReefAngel.StandardLights( GT_LightsTwo,15,00,22,30 ); //GT Lights Actinic 3:00pm - 10:30pm
        ReefAngel.WavemakerRandom( DT_LWM,60,100 );
        ReefAngel.Relay.Set( DT_RWM, !ReefAngel.Relay.Status( DT_LWM ) ); //Randomize DT powerheads
        ReefAngel.WavemakerRandom1( GT_LWM,60,100 );
        ReefAngel.Relay.Set( GT_RWM, !ReefAngel.Relay.Status( GT_LWM ) ); //Randomize GT powerheads
        StandardHeater1( DT_Heat,779,791 ); //DT-Heat
        StandardHeater2( GT_Heat,779,791 ); //GT-Heat
        ReefAngel.Relay.On(Skimmer); // Turn on Skimmer
           
       // if the hour is 2p or 8p, minute is 55 and seconds is 0, start the feeding mode
          if ( ((hour() == 14) || (hour() == 20)) &&
          (minute() == 55) &&
          (second() == 0) )
          {
          ReefAngel.FeedingModeStart();
          }   
       
          if (hour()>=1 && hour()<6)  // Turn on Reservoir Pump
          ReefAngel.Relay.On(RV_Pump);
          else
          ReefAngel.Relay.Off(RV_Pump);
 
        // turn on RV_Heat when temp falls below 76.0 and turn off when temp gets above 80.0
        if (ReefAngel.Params.Temp[T3_PROBE] <= 780 && ReefAngel.Params.Temp[T3_PROBE] > 0) ReefAngel.Relay.On(RV_Heat);
        if (ReefAngel.Params.Temp[T3_PROBE] >= 791) ReefAngel.Relay.Off(RV_Heat);     
       
        //turn off DT pump when switch is tripped and sound buzzer
        //ReefAngel.Relay.Set(DT_Pump,ReefAngel.IO.GetChannel(0));
         //ReefAngel.PWM.SetDaylight(100-(ReefAngel.IO.GetChannel(0)*100));
       
        //turn off GT pump when switch is tripped and sound buzzer
        // ReefAngel.Relay.Set(GT_Pump,ReefAngel.IO.GetChannel(1));
        // ReefAngel.PWM.SetDaylight(100-(ReefAngel.IO.GetChannel(1)*100));
             
        //tell the portal we have a overflow problem
        // ReefAngel.CustomVar[0]=ReefAngel.IO.GetChannel(0);
        // ReefAngel.CustomVar[1]=ReefAngel.IO.GetChannel(1);
       //  ReefAngel.CustomVar[7]=1;
         
        //set up dosing pump one to run twice a week for 4 minutes
         //ReefAngel.Relay.Set(Box1_Port2,((weekday()==1||weekday()==4) && hour()==14 && minute()<6));
         //ReefAngel.Relay.Set (Box1_Port3,(hour()%2==0)); //stir chemicals in water top off unit

         ReefAngel.Portal("rossbryant1956");
         ReefAngel.ShowInterface();
    }

void CheckPower() {
  static boolean powerOutage=false;
  static WiFiAlert powerAlert;

  // Power Outage - turn off everything
  if (!ReefAngel.Relay.IsRelayPresent(EXP1_RELAY))
  // && (!ReefAngel.Relay.IsRelayPresent(EXP2_RELAY))) // Expansion Relay NOT present
  {
    powerOutage=true;
    ReefAngel.Relay.Off(DT_LightsOne);
    ReefAngel.Relay.Off(DT_LightsTwo);
    ReefAngel.Relay.Off(Port3);
    ReefAngel.Relay.Off(Port4);
    ReefAngel.Relay.Off(Port7);
    ReefAngel.Relay.Off(Port8);
    powerAlert.Send("Power+Outage!");
    //ReefAngel.CustomVar[Var_Power]=1;
  }

  // Power Restored - Turn things back on
   if (powerOutage && ReefAngel.Relay.IsRelayPresent(EXP1_RELAY)) // Expansion Relay NOT present
  {
   
    powerOutage=false;
    LastStart=now();
    powerAlert.Send("Power+restored.",true);
    // ReefAngel.CustomVar[Var_Power]=1;   
  }
}
     void StandardHeater1(byte HeaterRelay, int LowTemp, int HighTemp)
        {
        if (ReefAngel.Params.Temp[T2_PROBE] == 0) return; // Don't turn the heater on if the temp is reading 0
        if (ReefAngel.Params.Temp[T2_PROBE] <= LowTemp && ReefAngel.Params.Temp[T2_PROBE] > 0) ReefAngel.Relay.On(HeaterRelay); // If sensor 2 temperature <= LowTemp - turn on heater
        if (ReefAngel.Params.Temp[T2_PROBE] >= HighTemp) ReefAngel.Relay.Off(HeaterRelay); // If sensor 2 temperature >= HighTemp - turn off heater
        }


   void StandardHeater2(byte HeaterRelay, int LowTemp, int HighTemp) //Heater for Grow Tank
    {
        if (ReefAngel.Params.Temp[T1_PROBE] == 0) return;  // Don't turn the heater on if the temp is reading 0
        if (ReefAngel.Params.Temp[T1_PROBE] <= LowTemp && ReefAngel.Params.Temp[T1_PROBE] > 0) ReefAngel.Relay.On(HeaterRelay);  // If sensor temperature <= LowTemp - turn on heater
        if (ReefAngel.Params.Temp[T1_PROBE] >= HighTemp) ReefAngel.Relay.Off(HeaterRelay);  // If sensor temperature >= HighTemp - turn off heater
    }
       
    void DrawCustomMain()
    {
      int x,y;
      char text[7];
     
      // Date and Time
      ReefAngel.LCD.DrawDate( 6, 122 );  //6 pixels left, 122 lines down
      pingSerial();
     
      // I/O Expansion
      byte bkcolor;
      x = 14;
      y = 11;
      for ( int a=0;a<6;a++ )
      {
      ReefAngel.LCD.DrawCircleOutline( x+(a*20),y,4,COLOR_MEDIUMORCHID );
      if ( ReefAngel.IO.GetChannel(a) ) bkcolor=COLOR_WHITE; else bkcolor=COLOR_GRAY;
      ReefAngel.LCD.FillCircle( x+(a*20),y,2,bkcolor );
      }
      pingSerial();

      // Parameters
 
      ReefAngel.LCD.DrawText(0,255,18,26,"GT");  //18 left, 10 down
      ConvertNumToString(text, ReefAngel.Params.Temp[T1_PROBE], 10);
      ReefAngel.LCD.DrawLargeText(COLOR_INDIANRED, 255, 10, 37, text, Num8x8);
      pingSerial();

      ReefAngel.LCD.DrawText(0,255,57,26,"DT"); //57 left, 10 down
      ConvertNumToString(text, ReefAngel.Params.Temp[T2_PROBE], 10);
      ReefAngel.LCD.DrawLargeText(COLOR_INDIANRED, 255, 50, 37, text, Num8x8);
      pingSerial();
     
      ReefAngel.LCD.DrawText(0,255,94,26,"Res"); //94 left, 10 down
      ConvertNumToString(text, ReefAngel.Params.Temp[T3_PROBE], 10);
      ReefAngel.LCD.DrawLargeText(COLOR_INDIANRED, 255, 90, 37, text, Num8x8);
      pingSerial();

      ReefAngel.LCD.DrawText(0,255,16,52,"DT pH"); //18 left, 39 down
      ConvertNumToString(text, ReefAngel.Params.PH, 100);
      ReefAngel.LCD.DrawLargeText(COLOR_INDIANRED, 255, 15, 65, text, Num8x8);
      pingSerial();

      ReefAngel.LCD.DrawText( 0,255,57,52, "GT pH"); //57 left, 39 down
      ConvertNumToString(text, ReefAngel.Params.PHExp, 100);
      ReefAngel.LCD.DrawLargeText(COLOR_INDIANRED, 255, 55, 65, text, Num8x8);
      pingSerial();

      ReefAngel.LCD.DrawText(0,255,100,52,"Sal"); //100 left, 39 down
      ConvertNumToString(text, ReefAngel.Params.Salinity, 10);
      ReefAngel.LCD.DrawLargeText(COLOR_INDIANRED, 255, 98, 65, text, Num8x8);
      pingSerial();

      // Main Relay Box
      byte TempRelay = ReefAngel.Relay.RelayData;
      TempRelay &= ReefAngel.Relay.RelayMaskOff;
      TempRelay |= ReefAngel.Relay.RelayMaskOn;
      ReefAngel.LCD.DrawOutletBox( 12, 80, TempRelay );
      pingSerial();

       // Relay Expansion
      TempRelay = ReefAngel.Relay.RelayDataE[0];
      TempRelay &= ReefAngel.Relay.RelayMaskOffE[0];
      TempRelay |= ReefAngel.Relay.RelayMaskOnE[0];
      ReefAngel.LCD.DrawOutletBox( 12, 93, TempRelay );
      pingSerial(); 
     
      // Relay Expansion
      TempRelay = ReefAngel.Relay.RelayDataE[1];
      TempRelay &= ReefAngel.Relay.RelayMaskOffE[1];
      TempRelay |= ReefAngel.Relay.RelayMaskOnE[1];
      ReefAngel.LCD.DrawOutletBox( 12, 107, TempRelay );
      pingSerial(); 
    }

    void DrawCustomGraph()
    {
    // ReefAngel.LCD.DrawGraph( 5, 5 );
    }


The part I having the most trouble with is the power outage section:

Code: Select all
void CheckPower() {
  static boolean powerOutage=false;
  static WiFiAlert powerAlert;

  // Power Outage - turn off everything
  if (!ReefAngel.Relay.IsRelayPresent(EXP1_RELAY))
  // && (!ReefAngel.Relay.IsRelayPresent(EXP2_RELAY))) // Expansion Relay NOT present
  {
    powerOutage=true;
    ReefAngel.Relay.Off(DT_LightsOne);
    ReefAngel.Relay.Off(DT_LightsTwo);
    ReefAngel.Relay.Off(Port3);
    ReefAngel.Relay.Off(Port4);
    ReefAngel.Relay.Off(Port7);
    ReefAngel.Relay.Off(Port8);
    powerAlert.Send("Power+Outage!");
    //ReefAngel.CustomVar[Var_Power]=1;
  }

  // Power Restored - Turn things back on
   if (powerOutage && ReefAngel.Relay.IsRelayPresent(EXP1_RELAY)) // Expansion Relay NOT present
  {
   
    powerOutage=false;
    LastStart=now();
    powerAlert.Send("Power+restored.",true);
    // ReefAngel.CustomVar[Var_Power]=1;   
  }


I have two expansion units and relay boxes, you'll notice one of those relays commented out in this section because I was unable to follow the instructions that Lee gave me. I was trying to get the code to fire if both units dropped off the power grid. I have the main relay and RA on a UPS and the wifi alert is working great because when I was messing with it my phone was going berserk! Thanks Lee

So, Thx in advance, hope you see something I don't.
Roscoe's Reefs - Starting Over Again:

Building new 29g Nano after landlord went berserk over my 4 75 gallon tanks, Multiple RA's, Water mixing stations, etc. Your help welcomed in remembering all I've forgotten.
User avatar
Posts: 5348
Joined: Fri Jul 20, 2012 9:42 am
PostPosted: Tue Jul 16, 2013 2:38 am
Were you getting a compiler error or was it just not working?

Return to My PDE/INO file

Who is online

Users browsing this forum: No registered users and 2 guests

cron