16xRelay with ESP12F Onboard and two Shift Register (74HC595)

Hello Lennaert,

Great, I still didn’t try the code yet but I checked it a bit, I saw that you are running a for loop over all the relays every time you receive a request from the browser.
I think you can just call
Set_Relay_Status((RELAY_ID) unit, (RELAY_STATE) stat);
in
checkcommand() function instead of calling switcher() and put the
Update_Relays_State(); call in the loop() it should work

I managed to control the relays individually using Amazon Alexa
I am planning to add a web server as well also Ota and Otahttp + public Ip sharing
Below is a demo for Alexa’s part

2 Likes

Atef, yep, noticed I could use the individual addressing at some point, but left it in. In the full version I have groups and some arrays defining clock times and such in conjunction with other elements. So the loop will undergo some expansion :slight_smile:

Very glad it works, this unit will be a central switching unit obviously, which will connect to a nodemcu v3 and a raspberry for various purposes . If you have question or needs for something web related or php/mysql, javascript, linux; just send me a message and perhaps I can help out. Thanks again.

.

Quick question Atef, any idea why a shift happens when I reset the device ?

all relays get moved 1 position when I reset it, before doing something (push button or send commands).

I can circumvent it, but I am guessing you have a little more insight as to why :slight_smile:

Hello Lennaert,

I guess that’s random and due maybe to the powering behavior could ba a missing pull down resistor on the latch pin but can you try to set all the relays to 0 during the setup function.
In my Alexa example I am reading the previous status from eeprom (stored before reset) and apply it to the relays and if there is no stored data I just set all to 0

Can you try it and let me know.
If it doesn’t work I would suggest if you have an oscilloscope try to observe the latch pin during boot also the shifter powering if latch changing while shift is powered then we will have this behavior

If software solution doesn’t work i think adding a pull down resistor may do the job.

Have a nice day

Update

Setting all the pins to 0 in the setup worked.

But there is a side effect lol

The relay, which position I left on, comes back on after the setup post reset. (as do other relays I enable/disable)

But one caveat, the units left in ON state, I can not switch to OFF before I press ON again first.

So: all off > enable relays 4 (ON) > reset > relay 4 Comes back ON > unable to switch off unless I press ON relay 4 first.

Bonus caveat, if, after reset, I press a relay to to ON, which was not ON before reset, it works directly, BUT, the ones I had in ON state before reset, which came on after reset, suddenly go out again lol

So …

All off > relay 4 ON > reset > (4 comes back on) enable relay other then 4 (i.e. 5), 4 goes OFF and the selected relay comes on (i.e. 5).

Yep, made total sense to hehe

The other way around, seems to work, but could be part of the problem ofc. (switching OFF to ON)

I think it has something to do with the JS function and data exchange with the browser.
It would be better to use bidirectional exchange between the browser and the ESP.
For example, if you want one day to add another way to control the relays (button) you need to make sure that if the browser when open it has to be updated with the status of each relay.
And here I think the browser still thinks that the relay 4 is ON even after restart.
So maybe you need to automatically trigger the reload of the page on reset.

4 goes OFF and the selected relay comes on:

Yes makes sense because every time the shifter is loaded with all the relays status (16 bits)
if the update for relay 4 somehow got missed it will be done the next time another relay is updated (relay 5 in this case)

Morning,

The current buttons work rather primitive, gives a command (switch) unit number and state.
Its a rather flat approach without feedback relating to their current state.
I will implement the push system for feedback on states and see if if makes a difference.

In my previous version I used uri encoder to transmit special characters (":" in my case), but in this newer version it gave weird results with the encoded string. I’ll look into this again to be sure it’s not causing interference.

I don’t think its due to the unit having missed a browser assignment, being processed after a reset, would be a bit obvious., as I am the one pressing the button, and if nothing happens and I reset, I should be able to reason, that it was my own action if a switch occurs :slight_smile: but alas.
Here as well, I’ll look into excluding this as a potential cause.

I appreciate the feedback a lot Atef, thanks.

Hi,

Better to use something like toggle buttons

Hi,
I have a page with toggle buttons as well, but I wanted the individual buttons working properly and reliable first, as my system is sort of a climate control system, it needs to work flawless when operational. The individual on off buttons were sort of a premise to the use of other functions switching stuff like heaters, fans, humidifier and extractors on or of based on a variety of sensor values.

Hoping to round up the reliability of switching today :slight_smile:

Olá, sou do Brasil, adquiri uma placa igual , não entendo de programação, alguém poderia me disponibilizar o código completo? Obrigado

@autoX Thanks for posting your ESPHome config code. Will your code work for this 24 Channel ESP-12F relay module?

EDIT:
After going through this thread one more time and ESPHome documentation I assume, for the 24 channel ESP-12F relay module I mentioned above the only significant change that need to be made to your config is the sr_count am I right?

sn74hc595:
  - id: 'sn74hc595_hub'
    data_pin: GPIO14
    clock_pin: GPIO13
    latch_pin: GPIO12
    oe_pin: GPIO05

    # Number of daisy-chained shift registers, up-to 256. Defaults to 1.
    sr_count: 3  # <-- this ?

I am yet to receive the module. I’ll update the thread after testing with your code with modification for 24 channel.

I assume there are 3 level Stifter in the board.
So I would try to increase to 3 :wink:

Yet, I have only tested the 16 relay boards.
So let us know if it works

@autoX Thanks for the prompt reply. Sure, I’ll update the thread with my result once I receive the board.

Hi Christian,
I’m having trouble using a pin for a temperature sensor. The sensor does not register on the pins I used. Do you know how I can solve this?

I came across this thread after several failed attempts to work this 16 relay thing out… thank you sir Christian! that did the job!

1 Like

Hello there, I might be a bit late, but have you managed to resolve this issue? I’m experiencing the exact same problem on my end. My esp8266 keeps freezing after leaving it unplugged for some time

Could you share your code, it would be of great help to me

Hi Matt,
The file that you’ve shared with the manual, code and utilities has been marked as a dangerous file for some reason where it’s being hosted by Mediafire. Is there any way you could make this available to us again through another link? I’m having a tricky time getting one of these to connect to ESPHome.

Boa Noite pessoal, Good evening everyone,

Encaminho aqui os codigos do programa que deu certo aqui : eu comprei a placa com 24 reles (esp12f _relay_x24_VI_2)

I forward here the codes of the program that worked here: I bought the board with 24 relays (esp12f _relay_x24_VI_2)

o codigo :

// Pinos conectados ao 74HC595
int latchPin = 12; // Latch pin do 74HC595
int clockPin = 13; // Clock pin do 74HC595
int dataPin = 14; // Data pin do 74HC595
int oePin = 5; // Não utilizado no código, mas configurado como OUTPUT

void setup() {
// Configura todos os pinos como OUTPUT
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(oePin, OUTPUT);

// Inicializa a comunicação serial
Serial.begin(115200);

// Inicia todos os relés desligados
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, 0x00); // Primeiro registrador (8 relés)
shiftOut(dataPin, clockPin, MSBFIRST, 0x00); // Segundo registrador (8 relés)
shiftOut(dataPin, clockPin, MSBFIRST, 0x00); // Terceiro registrador (8 relés)
digitalWrite(latchPin, HIGH);
}

void loop() {
for (int i = 0; i < 24; i++) {
// Liga o relé correspondente
Serial.print("Turning on relay: ");
Serial.println(i + 1);
activateRelay(i);
delay(500); // Mantém o relé ligado por 500 ms

// Desliga todos os relés
Serial.println("Turning off all relays");
deactivateAllRelays();
delay(500);  // Aguardando antes de passar para o próximo relé

}
}

// Função para ativar um relé específico
void activateRelay(int relay) {
byte relayState1 = 0x00;
byte relayState2 = 0x00;
byte relayState3 = 0x00;

if (relay < 8) {
relayState1 = (1 << relay); // Ativa o bit correspondente no primeiro registrador
} else if (relay < 16) {
relayState2 = (1 << (relay - 8)); // Ativa o bit correspondente no segundo registrador
} else {
relayState3 = (1 << (relay - 16)); // Ativa o bit correspondente no terceiro registrador
}

digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, relayState3);
shiftOut(dataPin, clockPin, MSBFIRST, relayState2);
shiftOut(dataPin, clockPin, MSBFIRST, relayState1);
digitalWrite(latchPin, HIGH);
}

// Função para desativar todos os relés
void deactivateAllRelays() {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, 0x00);
shiftOut(dataPin, clockPin, MSBFIRST, 0x00);
shiftOut(dataPin, clockPin, MSBFIRST, 0x00);
digitalWrite(latchPin, HIGH);
}

consegui tambem o codigo com 16 reles : I also got the code with 16 relays:

// LCTech 16ch Relay board with 2x74HC595 shift registers - replicates close to out of box relay testing function
int latchPin = 12; // Latch pin of 74HC595 is connected to Digital pin 5
int clockPin = 13; // Clock pin of 74HC595 is connected to Digital pin 6
int dataPin = 14; // Data pin of 74HC595 is connected to Digital pin 4
int oePin = 5; // oePin - not using this

void setup()
{
// Set all the pins of 74HC595 as OUTPUT
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(oePin, OUTPUT);

// Setup serial comms
Serial.begin(115200);
}

void loop()
{

  Serial.println("Relay #16 = 0b0000");       
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, LSBFIRST, 0b0000);
  digitalWrite(latchPin, HIGH);
  delay(3000);

  Serial.println("Relay #8 = 0b0001");       
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, LSBFIRST, 0b0001);
  digitalWrite(latchPin, HIGH);
  delay(3000);

}

Utilizando estes modelos junto com chatgbt vcs podem desenvolver qualquer programa…
Using these models together with chatgbt you can develop any program…