ESP8266 with 8ch relay board

I’m new to this forum, excuse me if I posted in the wrong place

Let me introduce myself. I'm completely new to Arduino and have no coding experience (except on Basic 25 years ago). I can call myself a jack of all trades, I do understand how electricity works and have some knowledge on basic electronic components: resistor, capacitor, transistor, diode. Nothing about chips, processors.
About a month ago I watched a video about Node-Red and got all excited, bought RPi3 with bunch of accessories and decided to build control system for outdoor lights.
And actually got prototype working with 8channel relay board connected to gpio pins on rpi. Controlled through a web page driven by nodered. But to actually deploy this setup in my situation is not simple. Mechanical timer for outdoor lights located in detached garage, I was planning to gut it and replace with relays and rpi, but they don't fit. Another issue is that I have one more timer in the basement for outdoor lights around the house. With my approach it would require 2 RPis and additional boxes to hide them. I start looking for other solutions and found ESP8266 with plenty of gpio pins and small form factor.
Yesterday I received 2 units with shields and additional relay boards and spent all night figuring out how to make it work. Totally different animal comparing to rpi, I had to learn how to flash firmware on it, than load a scketch using Arduino Ide, than configure MQTT to talk to it. Thanks to all these tutorials I managed to get it working and I can control one relay on pin D2. Sketch I uploaded meant to read Dht Sensor and one relay, couldn't find one to just control relays. After reading a lot I realized that using gpio pins on ESP8266 is not as simple as on Rpi
Here is the question: Is it possible to utilize 8 gpio pins for relays? Looks like I can only use 5 of them without some special care(beyond my knowledge)
Should I just get 2 more ESP8266 and have each control 4ch relays?
Where do I look for sample sketches?

Thank you
Sorry for long post

What type of ESP8266 do you have?

NodeMCU 1.0 Esp-12e with CH340 chip

Alright, that should be able to work just fine. Can you post your current sketch here, just so I can see what you already have? From there you should be able to modify it to be able to control 8 GPIOs.

MisterMel:
Alright, that should be able to work just fine. Can you post your current sketch here, just so I can see what you already have? From there you should be able to modify it to be able to control 8 GPIOs.

Im not planning to have DHT sensors connected, not yet at least. goal is to have 8 working relays.
my main concern is that according to this article A Beginner's Guide to the ESP8266 certain gpio must be in specific state during boot. in my case all 8 have to be HIGH. if i understood correctly it might cause issues or board will become inaccessible

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include “DHT.h”
#include <Wire.h>
// Uncomment one of the lines bellow for whatever DHT sensor type you’re using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321

// Change the credentials below, so your ESP8266 connects to your router
const char* ssid = “test”;
const char* password = “12345678”;

// Change the variable to your Raspberry Pi IP address, so it connects to your MQTT broker
const char* mqtt_server = “MQTT”;

// Initializes the espClient. You should change the espClient name if you have multiple ESPs running in your home automation system
WiFiClient espClient;
PubSubClient client(espClient);

// DHT Sensor - GPIO 5 = D1 on ESP-12E NodeMCU board
const int DHTPin = 5;

// Lamp - LED - GPIO 4 = D2 on ESP-12E NodeMCU board
const int lamp = 4;

// Initialize DHT sensor.
DHT dht(DHTPin, DHTTYPE);

// Timers auxiliar variables
long now = millis();
long lastMeasure = 0;

// Don’t change the function below. This functions connects your ESP8266 to your router
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print(“Connecting to “);
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(”.”);
}
Serial.println("");
Serial.print("WiFi connected - ESP IP address: ");
Serial.println(WiFi.localIP());
}

// This functions is executed when some device publishes a message to a topic that your ESP8266 is subscribed to
// Change the function below to add logic to your program, so when a device publishes a message to a topic that
// your ESP8266 is subscribed you can actually do something
void callback(String topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: “);
Serial.print(topic);
Serial.print(”. Message: ");
String messageTemp;

for (int i = 0; i < length; i++) {
Serial.print((char)message*);*
_ messageTemp += (char)message*;_
_
}_
_
Serial.println();_
_
// Feel free to add more if statements to control more GPIOs with MQTT*_
* // If a message is received on the topic room/lamp, you check if the message is either on or off. Turns the lamp GPIO according to the message*
* if(topic==“room/lamp”){*
* Serial.print("Changing Room lamp to ");*
* if(messageTemp == “on”){*
* digitalWrite(lamp, LOW);*
* Serial.print(“On”);*
* }*
* else if(messageTemp == “off”){*
* digitalWrite(lamp, HIGH);*
* Serial.print(“Off”);*
* }*
* }*
* Serial.println();*
}
// This functions reconnects your ESP8266 to your MQTT broker
// Change the function below if you want to subscribe to more topics with your ESP8266
void reconnect() {
* // Loop until we’re reconnected*
* while (!client.connected()) {*
* Serial.print(“Attempting MQTT connection…”);*
* // Attempt to connect*
_ /_
_
YOU MIGHT NEED TO CHANGE THIS LINE, IF YOU’RE HAVING PROBLEMS WITH MQTT MULTIPLE CONNECTIONS*_
* To change the ESP device ID, you will have to give a new name to the ESP8266.*
_ /_
_
if (client.connect(“ESP8266Client”)) {_
_
Serial.println(“connected”); _
_
// Subscribe or resubscribe to a topic*_
* // You can subscribe to more topics (to control more LEDs in this example)*
* client.subscribe(“room/lamp”);*
* } else {*
* Serial.print(“failed, rc=”);*
* Serial.print(client.state());*
* Serial.println(" try again in 5 seconds");*
* // Wait 5 seconds before retrying*
* delay(5000);*
* }*
* }*
}
// The setup function sets your ESP GPIOs to Outputs, starts the serial communication at a baud rate of 115200
// Sets your mqtt broker and sets the callback function
// The callback function is what receives messages and actually controls the LEDs
void setup() {
* pinMode(lamp, OUTPUT);*
* digitalWrite(lamp, HIGH);*

* dht.begin();*

* Serial.begin(115200);*
* setup_wifi();
client.setServer(mqtt_server, 1883);
_
client.setCallback(callback);_
_
}_
_
// For this project, you don’t need to change anything in the loop function. Basically it ensures that you ESP is connected to your broker*_
void loop() {
* if (!client.connected()) {*
* reconnect();*
* }*
* if(!client.loop())*
* client.connect(“ESP8266Client”);*
* now = millis();*
* // Publishes new temperature and humidity every 30 seconds*
* if (now - lastMeasure > 30000) {*
* lastMeasure = now;*
* // Sensor readings may also be up to 2 seconds ‘old’ (its a very slow sensor)*
* float h = dht.readHumidity();*
* // Read temperature as Celsius (the default)*
* float t = dht.readTemperature();*
* // Read temperature as Fahrenheit (isFahrenheit = true)*
* float f = dht.readTemperature(true);*
* // Check if any reads failed and exit early (to try again).*
* if (isnan(h) || isnan(t) || isnan(f)) {*
* Serial.println(“Failed to read from DHT sensor!”);*
* return;*
* }*
* // Computes temperature values in Celsius*
* float hic = dht.computeHeatIndex(t, h, false);*
* static char temperatureTemp[7];*
* dtostrf(hic, 6, 2, temperatureTemp);*

* // Uncomment to compute temperature values in Fahrenheit*
* // float hif = dht.computeHeatIndex(f, h);*
* // static char temperatureTemp[7];*
* // dtostrf(hic, 6, 2, temperatureTemp);*

* static char humidityTemp[7];*
* dtostrf(h, 6, 2, humidityTemp);*
* // Publishes Temperature and Humidity values*
* client.publish(“room/temperature”, temperatureTemp);*
* client.publish(“room/humidity”, humidityTemp);*

* Serial.print("Humidity: ");*
* Serial.print(h);*
* Serial.print(" %\t Temperature: ");*
* Serial.print(t);*
_ Serial.print(" C ");_
_
Serial.print(f);_
_ Serial.print(" F\t Heat index: ");_
_
Serial.print(hic);
_
_ Serial.println(" C ");_
_
// Serial.print(hif);_
_ // Serial.println(" F");_
_
}
_
}

After hours of research I managed to get 7 relays working, however due to limitations Ill be using only 6 of them and to finish my project will buy one more ESP-12E and 4ch relay.

Working sketch is attached, too long to be posted here.

sketch_shared.ino (8.64 KB)

A shift register (74HC595) uses three pins of the ESP and has eight outputs.
Many shift register chips can be daisy-chained, using the same three ESP pins.
I2C port expanders (two pins used) is another option.
Leo..