I have tried to expand to 32 Outputs but some bits are always copied on the outputs (4x74595)
esphome:
name: test_esp_shift
platform: ESP8266
board: d1_mini
includes:
- binSwitch.h
wifi:
ssid: "xxxxxx"
password: "xxxxxxxxxx"
manual_ip:
static_ip: 192.168.2.244
gateway: 192.168.2.1
subnet: 255.255.252.0
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Test Esp Fallback Hotspot"
password: "xxxxxxxxxx"
captive_portal:
# Enable logging
logger:
# Enable Home Assistant API
api:
password: "44324432"
ota:
password: "xxxxxxx"
web_server:
port: 80
switch:
- platform: custom
lambda: |-
auto switch1 = new binSwitch(0);
auto switch2 = new binSwitch(1);
auto switch3 = new binSwitch(2);
auto switch4 = new binSwitch(3);
auto switch5 = new binSwitch(4);
auto switch6 = new binSwitch(5);
auto switch7 = new binSwitch(6);
auto switch8 = new binSwitch(7);
auto switch9 = new binSwitch(8);
auto switch10 = new binSwitch(9);
auto switch11 = new binSwitch(10);
auto switch12 = new binSwitch(11);
auto switch13 = new binSwitch(12);
auto switch14 = new binSwitch(13);
auto switch15 = new binSwitch(14);
auto switch16 = new binSwitch(15);
auto switch17 = new binSwitch(16);
auto switch18 = new binSwitch(17);
auto switch19 = new binSwitch(18);
auto switch20 = new binSwitch(19);
auto switch21 = new binSwitch(20);
auto switch22 = new binSwitch(21);
auto switch23 = new binSwitch(22);
auto switch24 = new binSwitch(23);
auto switch25 = new binSwitch(24);
auto switch26 = new binSwitch(25);
auto switch27 = new binSwitch(26);
auto switch28 = new binSwitch(27);
auto switch29 = new binSwitch(28);
auto switch30 = new binSwitch(29);
auto switch31 = new binSwitch(30);
auto switch32 = new binSwitch(31);
App.register_component(switch1);
App.register_component(switch2);
App.register_component(switch3);
App.register_component(switch4);
App.register_component(switch5);
App.register_component(switch6);
App.register_component(switch7);
App.register_component(switch8);
App.register_component(switch9);
App.register_component(switch10);
App.register_component(switch11);
App.register_component(switch12);
App.register_component(switch13);
App.register_component(switch14);
App.register_component(switch15);
App.register_component(switch16);
App.register_component(switch17);
App.register_component(switch18);
App.register_component(switch19);
App.register_component(switch20);
App.register_component(switch21);
App.register_component(switch22);
App.register_component(switch23);
App.register_component(switch24);
App.register_component(switch25);
App.register_component(switch26);
App.register_component(switch27);
App.register_component(switch28);
App.register_component(switch29);
App.register_component(switch30);
App.register_component(switch31);
App.register_component(switch32);
return {switch1,switch2,switch3,switch4,switch5,switch6,switch7,switch8,switch9,switch10,switch11,switch12,switch13,switch14,switch15,switch16,switch17,switch18,switch19,switch20,switch21,switch22,switch23,switch24,switch25,switch26,switch27,switch28,switch29,switch30,switch31,switch32};
switches:
- name: "Switch 1"
id: switch_1
- name: "Switch 2"
id: switch_2
- name: "Switch 3"
id: switch_3
- name: "Switch 4"
id: switch_4
- name: "Switch 5"
id: switch_5
- name: "Switch 6"
id: switch_6
- name: "Switch 7"
id: switch_7
- name: "Switch 8"
id: switch_8
- name: "Switch 9"
id: switch_9
- name: "Switch 10"
id: switch_10
- name: "Switch 11"
id: switch_11
- name: "Switch 12"
id: switch_12
- name: "Switch 13"
id: switch_13
- name: "Switch 14"
id: switch_14
- name: "Switch 15"
id: switch_15
- name: "Switch 16"
id: switch_16
- name: "Switch 17"
id: switch_17
- name: "Switch 18"
id: switch_18
- name: "Switch 19"
id: switch_19
- name: "Switch 20"
id: switch_20
- name: "Switch 21"
id: switch_21
- name: "Switch 22"
id: switch_22
- name: "Switch 23"
id: switch_23
- name: "Switch 24"
id: switch_24
- name: "Switch 25"
id: switch_25
- name: "Switch 26"
id: switch_26
- name: "Switch 27"
id: switch_27
- name: "Switch 28"
id: switch_28
- name: "Switch 29"
id: switch_29
- name: "Switch 30"
id: switch_30
- name: "Switch 31"
id: switch_31
- name: "Switch 32"
id: switch_32
#include "esphome.h"
// global variable to store state of bits
long rele_state = 0;
// pins of the shift register (74HC595)
const byte outputLatch_pin = 15;
const byte clock_pin = 14;
const byte mosi_pin = 13;
class binSwitch : public Component, public Switch
{
long pinNum;
bool SetBit(bool pinValue, long number)
{
if (pinValue)
bitSet(rele_state, number);
else
bitClear(rele_state, number);
SetRele();
return pinValue;
}
void SetRele()
{
digitalWrite(clock_pin, LOW);
digitalWrite(outputLatch_pin, LOW);
shiftOut(mosi_pin, clock_pin, MSBFIRST, rele_state);
digitalWrite(outputLatch_pin, HIGH);
}
public:
// pin - output pin of the shift register (0..7)
binSwitch(long pin) { pinNum = pin; }
void setup() override
{
pinMode(outputLatch_pin, OUTPUT);
pinMode(clock_pin, OUTPUT);
pinMode(mosi_pin, OUTPUT);
digitalWrite(outputLatch_pin, HIGH);
SetRele();
}
void write_state(bool state) override
{
publish_state(SetBit(state, pinNum));
}
};
i also put in some “longs” instead of byte because of 32Bit, but it will not run.
Is there an error?