top of page

NodeMCU PCF8574-8INPUT-Read

Updated: Dec 4, 2021








#include <Wire.h>
#include <PCF8574.h>

#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);


//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 

//


PCF8574 pcf20(0x20);

int input0, input1, input2, input3, input4, input5, input6, input7;
void setup() {

  Serial.begin(9600);
  delay(100);

  Wire.begin(14, 12); // d6 scl d5 sda
  delay(250);
  pcf20.begin();
  delay(100);

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

  while (WiFi.status() != WL_CONNECTED) { //
    delay(500); //wait 500ms
    Serial.println("Connecting to wifi..."); // wait until the wifi process connects
  }
  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

  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

    }
  }

}

void loop() {

  client.loop(); // run the mqtt data processing function

  input0 = pcf20.read(0);
  input1 = pcf20.read(1);
  input2 = pcf20.read(2);
  input3 = pcf20.read(3);
  input4 = pcf20.read(4);
  input5 = pcf20.read(5);
  input6 = pcf20.read(6);
  input7 = pcf20.read(7);

  Serial.println("input0");
  Serial.println(input0);
  Serial.println("input1");
  Serial.println(input1);
  Serial.println("input2");
  Serial.println(input2);
  Serial.println("input3");
  Serial.println(input3);
  Serial.println("input4");
  Serial.println(input4);
  Serial.println("input5");
  Serial.println(input5);
  Serial.println("input6");
  Serial.println(input6);
  Serial.println("input7");
  Serial.println(input7);

  client.publish("devicename/input0", String(input0).c_str());// send input0
  client.publish("devicename/input1", String(input1).c_str());// send input0
  client.publish("devicename/input2", String(input2).c_str());// send input0
  client.publish("devicename/input3", String(input3).c_str());// send input0
  client.publish("devicename/input4", String(input4).c_str());// send input0
  client.publish("devicename/input5", String(input5).c_str());// send input0
  client.publish("devicename/input6", String(input6).c_str());// send input0
  client.publish("devicename/input7", String(input7).c_str());// send input0
delay(2000); // wait two seconds and send the message again
}

pcf8574_smartespiot_web
.zip
Download ZIP • 2KB


8 views0 comments

Recent Posts

See All