NodeMCU 8 Relay-PCF8574-I2C

Updated: Dec 4, 2021


In this project, we will control 8 relays via our smarthexiot software.You can continue this project after reviewing our previous relay control and pcf8574 examples.




#include <ESP8266WiFi.h> //The library we will use for the wifi connection
#include <PubSubClient.h> // The library we will use for the mqtt connection

//mqtt connection library settings
WiFiClient espClient;
PubSubClient client(espClient);

//
#include <Wire.h>
#include <PCF8574.h>
PCF8574 pcf20(0x20);
//

//settings for internet and mqtt connection
const char* ssid = ""; // wifi SSID
const char* password =  ""; // wifi password 
const char* mqttServer = ""; // mqtt server ip  to be sent by smartespiot by mail 
const int mqttPort = ; // mqtt port   to be sent by smartespiot by mail 
const char* mqttUser = ""; // mqtt username  to be sent by smartespiot by mail 
const char* mqttPassword = ""; // mqtt pass to be sent by smartespiot by mail 

//
//****

String Topik; //Our variable that we will use for comparison by keeping the tags/topic information from mqtt
int relaystatus1 = 0; // relay status will be send smartespiot
int relaystatus2 = 0; // relay status will be send smartespiot
int relaystatus3 = 0; // relay status will be send smartespiot
int relaystatus4 = 0; // relay status will be send smartespiot
int relaystatus5 = 0; // relay status will be send smartespiot
int relaystatus6 = 0; // relay status will be send smartespiot
int relaystatus7 = 0; // relay status will be send smartespiot
int relaystatus8 = 0; // relay status will be send smartespiot

String relay1, relay2, relay3, relay4, relay5, relay6, relay7, relay8; // our variable to hold the message information
char message_store[100]; // variable to save the incoming message as a character

//***//
int wificounter = 0; // for counting the number of trying connections
//**//

void setup() {
  Serial.begin(115200); // start serial communication at 115200 speed
  Wire.begin(14, 12); // d6 scl d5 sda
  pcf20.begin();



  pcf20.write(0, 1);
  pcf20.write(1, 1);
  pcf20.write(2, 1);
  pcf20.write(3, 1);
  pcf20.write(4, 1);
  pcf20.write(5, 1);
  pcf20.write(6, 1);
  pcf20.write(7, 1);
  WiFi.begin(ssid, password); // start the wifi connection process

  while (WiFi.status() != WL_CONNECTED) { //
    delay(5000); //wait 5000ms
    Serial.println("Connecting to wifi..."); // wait until the wifi process connects
    //// trying 10 times connection after reset to esp
    wificounter = wificounter + 1;
    if (wificounter == 10) {
      ESP.reset();
    }
    /////
  }


  Serial.println("Wifi connection ready :)"); // print the connection made information to the serial port

  client.setServer(mqttServer, mqttPort);//start connecting to the mqtt server from the specified port
  /////*****
  client.setCallback(callback);
  ////******
  while (!client.connected()) { //wait until we connect to mqtt
    Serial.println("Connecting to MQTT server...");

    if (client.connect("nodemcu1", mqttUser, mqttPassword )) {//login to mqtt server with username and password with smartespIOT name

      Serial.println("MQTT connection established :) ");

    } else {

      Serial.print("Unable to connect... ");// If the connection cannot be established, let's give a warning
      Serial.print(client.state());             // print the problem description
      // We can understand the problem according to the information returned from the clients state.
      //-4 : MQTT_CONNECTION_TIMEOUT - the server did not respond in the ongoing time
      //-3 : MQTT_CONNECTION_LOST - network connection lost
      //-2 : MQTT_CONNECT_FAILED - network connection failed
      //-1 : MQTT_DISCONNECTED - disconnected
      //0 : MQTT_CONNECTED - connection established
      //1 : MQTT_CONNECT_BAD_PROTOCOL - The server does not support the requested MQTT version
      //2 : MQTT_CONNECT_BAD_CLIENT_ID - The server did not accept your information
      //3 : MQTT_CONNECT_UNAVAILABLE - The server did not accept the connectionetmed
      //4 : MQTT_CONNECT_BAD_CREDENTIALS - username password not accepted
      //5 : MQTT_CONNECT_UNAUTHORIZED - you are not authorized to connect


      delay(2000);   // wait two seconds and try connecting again
      ////
      ESP.reset(); // unable to connect esp reset
      ///
    }
  }
  ////*****
  client.subscribe("nodemcu1/relay1");
  client.subscribe("nodemcu1/relay2");
  client.subscribe("nodemcu1/relay3");
  client.subscribe("nodemcu1/relay4");
  client.subscribe("nodemcu1/relay5");
  client.subscribe("nodemcu1/relay6");
  client.subscribe("nodemcu1/relay7");
  client.subscribe("nodemcu1/relay8");
  ////*****
}

////reconnect funtion //
void reconnect() {
  WiFi.disconnect();
  delay(100);

  WiFi.begin(ssid, password); // start the wifi connection process

  while (WiFi.status() != WL_CONNECTED) { //
    delay(5000); //wait 5000ms
    Serial.println("Connecting to wifi..."); // wait until the wifi process connects
    //// trying 10 times connection after reset to esp
    wificounter = wificounter + 1;
    if (wificounter == 10) {
      ESP.reset();
    }
    /////
  }


  Serial.println("Wifi connection ready :)"); // print the connection made information to the serial port

  client.setServer(mqttServer, mqttPort);//start connecting to the mqtt server from the specified port
  /////*****
  client.setCallback(callback);
  ////******
  while (!client.connected()) { //wait until we connect to mqtt
    Serial.println("Connecting to MQTT server...");

    if (client.connect("SmartEspIOT", mqttUser, mqttPassword )) {//login to mqtt server with username and password with smartespIOT name

      Serial.println("MQTT connection established :) ");

    } else {

      Serial.print("Unable to connect... ");// If the connection cannot be established, let's give a warning
      Serial.print(client.state());             // print the problem description
      // We can understand the problem according to the information returned from the clients state.
      //-4 : MQTT_CONNECTION_TIMEOUT - the server did not respond in the ongoing time
      //-3 : MQTT_CONNECTION_LOST - network connection lost
      //-2 : MQTT_CONNECT_FAILED - network connection failed
      //-1 : MQTT_DISCONNECTED - disconnected
      //0 : MQTT_CONNECTED - connection established
      //1 : MQTT_CONNECT_BAD_PROTOCOL - The server does not support the requested MQTT version
      //2 : MQTT_CONNECT_BAD_CLIENT_ID - The server did not accept your information
      //3 : MQTT_CONNECT_UNAVAILABLE - The server did not accept the connectionetmed
      //4 : MQTT_CONNECT_BAD_CREDENTIALS - username password not accepted
      //5 : MQTT_CONNECT_UNAUTHORIZED - you are not authorized to connect


      delay(2000);   // wait two seconds and try connecting again
      ////
      ESP.reset(); // unable to connect esp reset
      ///
    }
  }
  ////*****
  client.subscribe("nodemcu1/relay1");
  client.subscribe("nodemcu1/relay2");
  client.subscribe("nodemcu1/relay3");
  client.subscribe("nodemcu1/relay4");
  client.subscribe("nodemcu1/relay5");
  client.subscribe("nodemcu1/relay6");
  client.subscribe("nodemcu1/relay7");
  client.subscribe("nodemcu1/relay8");
  ////*****
}
////////////////////////////////////////
///****
void callback(char* topic, byte* payload, unsigned int length) { // Let's get the incoming topic-tag information and message
  int i = 0;
  Serial.println("message received");
  Serial.println(topic); // Let's print the incoming topic-tag information to the serial port
  Topik = String (topic); // write it to our variable to use the incoming topic information
  Serial.print("incomming message:");
  for (i = 0; i < length; i++) {
    Serial.print((char)payload[i]); // write the incoming message to the serial port
    message_store[i] = ((char)payload[i]); // store the incoming characters as characters in the message store
  }
  message_store[i] = '\0'; // find the end of the message

  // Now let's find our data from the incoming message

  if (Topik == "nodemcu1/relay1") {
    relay1 = (message_store);
  }

  if (Topik == "nodemcu1/relay2") {
    relay2 = (message_store);
  }

  if (Topik == "nodemcu1/relay3") {
    relay3 = (message_store);
  }

  if (Topik == "nodemcu1/relay4") {
    relay3 = (message_store);
  }

  if (Topik == "nodemcu1/relay5") {
    relay5 = (message_store);
  }

  if (Topik == "nodemcu1/relay6") {
    relay6 = (message_store);
  }

  if (Topik == "nodemcu1/relay7") {
    relay7 = (message_store);
  }

  if (Topik == "nodemcu1/relay8") {
    relay8 = (message_store);
  }

}
/////*******


void loop() {

  client.loop(); // mrun the mqtt data processing function
  ///// test client connected and start reconnect ///
  if (!client.connected()) {
    reconnect();
  }


  ////relay 1
  if ((relay1 == "1") && (relaystatus1 == 0)) { //
    relaystatus1 = 1;
    pcf20.write(0, LOW);
    client.publish("nodemcu1/relaystatus1", String(relaystatus1).c_str());// send relay status
  }

  if ((relay1 == "0") && (relaystatus1 == 1)) {
    relaystatus1 = 0;
    pcf20.write(0, HIGH);
    client.publish("nodemcu1/relaystatus1", String(relaystatus1).c_str());// send relay status
  }
  /////
  ////relay 2
  if ((relay2 == "1") && (relaystatus2 == 0)) { //
    relaystatus2 = 1;
    pcf20.write(1, LOW);
    client.publish("nodemcu1/relaystatus2", String(relaystatus2).c_str());// send relay status
  }

  if ((relay2 == "0") && (relaystatus2 == 1)) {
    relaystatus2 = 0;
    pcf20.write(1, HIGH);
    client.publish("nodemcu1/relaystatus2", String(relaystatus2).c_str());// send relay status
  }
  /////
   ////relay 3
  if ((relay3 == "1") && (relaystatus3 == 0)) { //
    relaystatus3 = 1;
    pcf20.write(2, LOW);
    client.publish("nodemcu1/relaystatus3", String(relaystatus3).c_str());// send relay status
  }

  if ((relay3 == "0") && (relaystatus3 == 1)) {
    relaystatus3 = 0;
    pcf20.write(2, HIGH);
    client.publish("nodemcu1/relaystatus3", String(relaystatus3).c_str());// send relay status
  }
  /////

  ////relay 4
  if ((relay4 == "1") && (relaystatus4 == 0)) { //
    relaystatus4 = 1;
    pcf20.write(3, LOW);
    client.publish("nodemcu1/relaystatus4", String(relaystatus4).c_str());// send relay status
  }

  if ((relay4 == "0") && (relaystatus4 == 1)) {
    relaystatus4 = 0;
    pcf20.write(3, HIGH);
    client.publish("nodemcu1/relaystatus4", String(relaystatus4).c_str());// send relay status
  }
  /////
   ////relay 5
  if ((relay5 == "1") && (relaystatus5 == 0)) { //
    relaystatus5 = 1;
    pcf20.write(4, LOW);
    client.publish("nodemcu1/relaystatus5", String(relaystatus5).c_str());// send relay status
  }

  if ((relay5 == "0") && (relaystatus5 == 1)) {
    relaystatus5 = 0;
    pcf20.write(4, HIGH);
    client.publish("nodemcu1/relaystatus5", String(relaystatus5).c_str());// send relay status
  }
  /////
  ////relay 6
  if ((relay6 == "1") && (relaystatus6 == 0)) { //
    relaystatus6 = 1;
    pcf20.write(5, LOW);
    client.publish("nodemcu1/relaystatus6", String(relaystatus6).c_str());// send relay status
  }

  if ((relay6 == "0") && (relaystatus6 == 1)) {
    relaystatus6 = 0;
    pcf20.write(5, HIGH);
    client.publish("nodemcu1/relaystatus6", String(relaystatus6).c_str());// send relay status
  }
  /////
  ////relay 7
  if ((relay7 == "1") && (relaystatus7 == 0)) { //
    relaystatus7 = 1;
    pcf20.write(6, LOW);
    client.publish("nodemcu1/relaystatus7", String(relaystatus7).c_str());// send relay status
  }

  if ((relay7 == "0") && (relaystatus7 == 1)) {
    relaystatus7 = 0;
    pcf20.write(6, HIGH);
    client.publish("nodemcu1/relaystatus7", String(relaystatus7).c_str());// send relay status
  }
  /////
 ////relay 8
  if ((relay8 == "1") && (relaystatus8 == 0)) { //
    relaystatus8 = 1;
    pcf20.write(7, LOW);
    client.publish("nodemcu1/relaystatus8", String(relaystatus8).c_str());// send relay status
  }

  if ((relay8 == "0") && (relaystatus7 == 1)) {
    relaystatus8 = 0;
    pcf20.write(7, HIGH);
    client.publish("nodemcu1/relaystatus8", String(relaystatus8).c_str());// send relay status
  }
  /////

}

8relay_out_pcf8574_i2c_smartespiot_web
.zip
Download ZIP • 3KB

42 views0 comments

Recent Posts

See All