Esp 32 Relay triggers

I am using a esp32 with a ds3231 and relays. I want to trigger these relays by time. Run one relay for 6 hours and run another for 6 hours. Like 1 -6 om one relay and 7-16 on the other. I want to see the time they run per day and week month. So right now I got to get the code working and then upload to the community. Thanks for the help

 `#include "Wire.h"

#define SDA_PIN 21
#define SCL_PIN 22
#define DS3231_I2C_ADDRESS 0x68


const int relay1 = 2; //Digital pin that the Relay is connected
const int relay2 = 5; //Digital pin that the Relay is connected
const int OnHour1 = 12;
const int OnMin1 = 25;
const int OffHour1 = 12;
const int OffMin1 = 25;


// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (val / 10 * 16) + (val % 10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return ( (val / 16 * 10) + (val % 16) );
}
void setup()
{
  Wire.begin();
  Serial.begin(9600);
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);

  digitalWrite(relay1, LOW); //OFF
  digitalWrite(relay2, LOW); //OFF

  // set the initial time here:
  //DS3231 seconds, minutes, hours, day, date, month, year
  //setDS3231time(30,30,18,4,22,11,17);
}
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
                   dayOfMonth, byte month, byte year)
{
  // sets time and date data to DS3231
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set next input to start at the seconds register
  Wire.write(decToBcd(second)); // set seconds
  Wire.write(decToBcd(minute)); // set minutes
  Wire.write(decToBcd(hour)); // set hours
  Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
  Wire.write(decToBcd(month)); // set month
  Wire.write(decToBcd(year)); // set year (0 to 99)
  Wire.endTransmission();
}
void readDS3231time(byte *second,
                    byte *minute,
                    byte *hour,
                    byte *dayOfWeek,
                    byte *dayOfMonth,
                    byte *month,
                    byte *year)
{
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}
void displayTime()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
                 &year);
  // send it to the serial monitor
  Serial.print(hour, DEC);
  // convert the byte variable to a decimal number when displayed
  Serial.print(":");
  if (minute < 10)
  {
    Serial.print("0");
  }
  Serial.print(minute, DEC);
  Serial.print(":");
  if (second < 10)
  {
    Serial.print("0");
  }
  Serial.print(second, DEC);
  Serial.print(" ");
  Serial.print(dayOfMonth, DEC);
  Serial.print("/");
  Serial.print(month, DEC);
  Serial.print("/");
  Serial.print(year, DEC);
  Serial.print(" Day of week: ");
  switch (dayOfWeek) {
    case 1:
      Serial.println("Sunday");
      break;
    case 2:
      Serial.println("Monday");
      break;
    case 3:
      Serial.println("Tuesday");
      break;
    case 4:
      Serial.println("Wednesday");
      break;
    case 5:
      Serial.println("Thursday");
      break;
    case 6:
      Serial.println("Friday");
      break;
    case 7:
      Serial.println("Saturday");
      break;
  }
}
void loop()
{
  displayTime(); // display the real-time clock data on the Serial Monitor,
  delay(1000); // every second


  if (readDS3231time (byte hour) == Onhour1 && (readDS3231time (byte min) == OnMin1)
  {
    digitalWrite (Relay1, HIGH);
  }
  else if (readDS3231time byte hour) == OffHour1 && (readDS3231time byte min == OffMin)
  {
    digitalWrite (Relay1, HIGH);
  }

}
)
)
)`

@self.raymond can you provide more detail of what you want to do with cayenne.

Yes once I get these relays to work I need for Cayenne to watch these relays and record when they come on and off. I would love to be able to control these relays on line but have to stop the code so I can turn a relay on and off. There will be another set of relays to control a 12 volt valve. Cayenne watch and record. Also a set of temp probe to be used to control the 12volt valve. Monitoring will be very important to this project. Thanks for the help

Sprinkfitter

when the if statement from your code is satisfied and relay is turned on, send this time value to cayenne. eg send the “hour” to channel 1 value widget and “minutes” to channel 2 value widget. thus this will give the time the relay is switched on.

if you want to do this online then you can use scheduling feature to turn on and off at a particular time.

this also can be done using cayenne and triggers.

1 Like