Nodemcu has about 8 IOs. For most of the task this might be enough,NodeMCU IO extender comes into picture if you want to more number of output pins. I will be using shift register for extending the output pins. One shift register will give 8 output pins, if you still want more we can chain shift registers to get additional output pins.
Another good reason to use shift register outputs than nodemcu pins is to reduce the stress on the nodemcu IOs to drive the loads, the shift register IOs will have higher current rating than the nodemcu IOs.
I have connected 8 LEDs to the outputs of the shift register.
It is very simple to control the shift registers ( 74HC595) . We put data bit on SER line, then give a pulse on Shift register clock pin (SRCLK) , the data will be read to the first bit of Shift register, if we give another clk pulse on SRCLK the first bit will move to the second bit, second to the third … The first bit will have the level of SER . When ever you want to move the ShiftRegister contents to the Storage Register , we give a pulse on RCLK. The OUTPUT level of the pins will match with the contents of Storage register,
Shift register requires minimum of 3 controller pins to operate.
The following function can push 16 bits of data to the shift registers (assuming two shifts registers cascaded)
Using these functions, I have developed a remote load control application , using which we can control status of the loads by sending messages on MQTT channel.
Nodemcu will be listening on a MQTT channel for control messages. We can control status of any led by publishing a message on the channel nodemcu is listening.
I am JSON format to exchange the data, nodemcu will except two fields in every control message , load number and status.
ln : load number
ts : target status
Following function should be called with the data received over MQTT , it decodes the message and control the loads appropriately