Problemas al ejecutar comunicación mediante expansor pcf8574 y esp32 blynk

Buen dia.
tengo problemas al intentar ejecutar la aplicacion de blynk ejemlo de las 4 lamparas mediante enlace wifi y los pines del esp32 #define button1_pin… estoy usando un modulo de expacion pcf8574 con la siguente librerias. y una pantalla lcd 16x2…

[Unformatted code removed by moderator]

@j1jorge Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

Disculpe no logro hacer que se copie correctamente.

[Unformatted code removed by moderator]

I’ve given you triple backticks to copy and paste, but you don’t seem to want to use them. Until you use the correct characters your code will continue to be deleted.

Pete.

1 Like

[quote=“PeteKnight, post:2, topic:66951”]

#define BLYNK_TEMPLATE_ID "TMPL2pLn-D8U_"
#define BLYNK_TEMPLATE_NAME "10 led"
#define BLYNK_AUTH_TOKEN "Mff8DsT0MJqsZuzlm5etG9Qi9CKpvWs-"
#define BLYNK_PRINT Serial
#include <LiquidCrystal_I2C.h>
#include "Arduino.h"
#include "PCF8574.h"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
void keyPressedOFFPCF8574();
#define ARDUINO_UNO_INTERRUPTED_PIN 2
LiquidCrystal_I2C lcd(0x27,16,2);
PCF8574 pcf8574A(0x20);
PCF8574 pcf8574B(0x21);
char auth[] = BLYNK_AUTH_TOKEN;
unsigned long timeElapsed;
bool keyChanged = 1;
char ssid[] = "S.T";
char pass[] = "asdfghjkl";

BlynkTimer timer;

int val0 = pcf8574A.digitalRead(P0);
int val1 = pcf8574A.digitalRead(P1);
int val2 = pcf8574A.digitalRead(P2);
int val3 = pcf8574A.digitalRead(P3);

#define button1_pin val0
#define button2_pin val1
#define button3_pin val2
#define button4_pin val3

#define relay1_pin 19
#define relay2_pin 18
#define relay3_pin 5
#define relay4_pin 17

int relay1_state = 0;
int relay2_state = 0;
int relay3_state = 0;
int relay4_state = 0;

#define button1_vpin    V0
#define button2_vpin    V1
#define button3_vpin    V2 
#define button4_vpin    V3

BLYNK_CONNECTED() {
  Blynk.syncVirtual(button1_vpin);
  Blynk.syncVirtual(button2_vpin);
  Blynk.syncVirtual(button3_vpin);
  Blynk.syncVirtual(button4_vpin);
}

BLYNK_WRITE(button1_vpin) {
  relay1_state = param.asInt();
  digitalWrite(relay1_pin, relay1_state);
}

BLYNK_WRITE(button2_vpin) {
  relay2_state = param.asInt();
  digitalWrite(relay2_pin, relay2_state);
}

BLYNK_WRITE(button3_vpin) {
  relay3_state = param.asInt();
  digitalWrite(relay3_pin, relay3_state);
}

BLYNK_WRITE(button4_vpin) {
  relay4_state = param.asInt();
  digitalWrite(relay4_pin, relay4_state);
}

void setup()
{
  
  Serial.begin(115200);
  pcf8574A.begin();
  
 	pcf8574A.pinMode(P0, INPUT);
	pcf8574A.pinMode(P1, INPUT);
	pcf8574A.pinMode(P2, INPUT);
	pcf8574A.pinMode(P3, INPUT);
 
  pinMode(relay1_pin, OUTPUT);
  pinMode(relay2_pin, OUTPUT);
  pinMode(relay3_pin, OUTPUT);
  pinMode(relay4_pin, OUTPUT);
  
  digitalWrite(relay1_pin, HIGH);
  digitalWrite(relay2_pin, HIGH);
  digitalWrite(relay3_pin, HIGH);
  digitalWrite(relay4_pin, HIGH);
  
  Blynk.begin(auth, ssid, pass);
 
}
bool keyPressed = 0;
void loop()
{
  Blynk.run();
  timer.run();
  if (keyPressed){
		uint8_t val0 = pcf8574A.digitalRead(P0);
		uint8_t val1 = pcf8574A.digitalRead(P1);
		uint8_t val2 = pcf8574A.digitalRead(P2);
		uint8_t val3 = pcf8574A.digitalRead(P3);
		Serial.print(" P0 ");
		Serial.print(val0);
		Serial.print(" P1 ");
		Serial.print(val1);
		Serial.print(" P2 ");
		Serial.print(val2);
		Serial.print(" P3 ");
		Serial.println(val3);
		keyPressed= false;
	}
  
  listen_push_buttons();
}

void keyPressedOFFPCF8574(){

	 keyPressed = true;
   }

void listen_push_buttons(){
    
    if(digitalRead(button1_pin) == LOW){
      delay(200);
      control_relay(1);
      Blynk.virtualWrite(button1_vpin, relay1_state); 
    }
    
    else if (digitalRead(button2_pin) == LOW){
      delay(200);
      control_relay(2);
      Blynk.virtualWrite(button2_vpin, relay2_state); 
    }
    
    else if (digitalRead(button3_pin) == LOW){
      delay(200);
      control_relay(3);
      Blynk.virtualWrite(button3_vpin, relay3_state); 
    }
   
    else if (digitalRead(button4_pin) == LOW){
      delay(200);
      control_relay(4);
      Blynk.virtualWrite(button4_vpin, relay4_state); 
    }
    
}

void control_relay(int relay){

  if(relay == 1){
    relay1_state = !relay1_state;
    digitalWrite(relay1_pin, relay1_state);
    Serial.print("Relay1 State = ");
    Serial.println(relay1_state);
    delay(50);
  }
 
  else if(relay == 2){
    relay2_state = !relay2_state;
    digitalWrite(relay2_pin, relay2_state);
    delay(50);
  }
  
  else if(relay == 3){
    relay3_state = !relay3_state;
    digitalWrite(relay3_pin, relay3_state);
    delay(50);
  }
  
  else if(relay == 4){
    relay4_state = !relay4_state;
    digitalWrite(relay4_pin, relay4_state);
    delay(50);
  }
 
}```

This makes no sense in C++
Take a look at the PCF8574.h library examples and see how it should be done.

Pete.

#define button1_pin pcf8574A.digitalRead(P0)
#define button2_pin pcf8574A.digitalRead(P1)
#define button3_pin pcf8574A.digitalRead(P2)
#define button4_pin pcf8574A.digitalRead(P3)

lo intente de esta manera e igual no reconoce la entradas del modulo de expiación

It won’t work that way either, because you’re making-up code that makes no sense.

Go and look at the PCF8574.h library examples.

Pete.

estoy probando con la lectura de todas las entradas
PCF8574::DigitalInput diA = pcf8574A.digitalReadAll();
imprimo en monitor serial y si se ve las entradas llamada diA.p0 hasta la diB.p7 pero no lo reconoce el codigo…

#define BLYNK_TEMPLATE_NAME "10 led"
#define BLYNK_AUTH_TOKEN "Mff8DsT0MJqsZuzlm5etG9Qi9CKpvWs-"
#define BLYNK_PRINT Serial
#include <LiquidCrystal_I2C.h>
#include "PCF8574.h"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char auth[] = BLYNK_AUTH_TOKEN;
#define ARDUINO_UNO_INTERRUPTED_PIN 2
LiquidCrystal_I2C lcd(0x27,16,2);
PCF8574 pcf8574A(0x20, ARDUINO_UNO_INTERRUPTED_PIN, 0);
PCF8574 pcf8574B(0x21, ARDUINO_UNO_INTERRUPTED_PIN, 0);



char ssid[] = "S.T";
char pass[] = "asdfghjkl";
unsigned long timeElapsed;
bool keyChanged = 1;
BlynkTimer timer;


//#define button1_pin pcf8574A.digitalRead(P0)
//#define button2_pin pcf8574A.digitalRead(P1)
//#define button3_pin pcf8574A.digitalRead(P2)
//#define button4_pin pcf8574A.digitalRead(P3)

#define relay1_pin 19
#define relay2_pin 18
#define relay3_pin 5
#define relay4_pin 17

int relay1_state = 0;
int relay2_state = 0;
int relay3_state = 0;
int relay4_state = 0;

#define button1_vpin    V0
#define button2_vpin    V1
#define button3_vpin    V2 
#define button4_vpin    V3

BLYNK_CONNECTED() {
  Blynk.syncVirtual(button1_vpin);
  Blynk.syncVirtual(button2_vpin);
  Blynk.syncVirtual(button3_vpin);
  Blynk.syncVirtual(button4_vpin);
}


BLYNK_WRITE(button1_vpin) {
  relay1_state = param.asInt();
  digitalWrite(relay1_pin, relay1_state);
}

BLYNK_WRITE(button2_vpin) {
  relay2_state = param.asInt();
  digitalWrite(relay2_pin, relay2_state);
}

BLYNK_WRITE(button3_vpin) {
  relay3_state = param.asInt();
  digitalWrite(relay3_pin, relay3_state);
}

BLYNK_WRITE(button4_vpin) {
  relay4_state = param.asInt();
  digitalWrite(relay4_pin, relay4_state);
}

void setup()
{
	Serial.begin(115200);
  lcd.begin(16,2);
  lcd.init();
  lcd.backlight();
  lcd.clear();
	
  // Set pinMode to INPUT
  pcf8574A.pinMode(P0, INPUT);
  pcf8574A.pinMode(P1, INPUT);
  pcf8574A.pinMode(P2, INPUT);
  pcf8574A.pinMode(P3, INPUT);
  pcf8574A.pinMode(P4, INPUT);
  pcf8574A.pinMode(P5, INPUT);
  pcf8574A.pinMode(P6, INPUT);
  pcf8574A.pinMode(P7, INPUT);
	pcf8574B.pinMode(P0, INPUT);
  pcf8574B.pinMode(P1, INPUT);
  pcf8574B.pinMode(P2, INPUT);
  pcf8574B.pinMode(P3, INPUT);
  pcf8574B.pinMode(P4, INPUT);
  pcf8574B.pinMode(P5, INPUT);
  pcf8574B.pinMode(P6, INPUT);
  pcf8574B.pinMode(P7, INPUT);
    
  pinMode(19,OUTPUT);
  pinMode(18,OUTPUT);
  pinMode( 5,OUTPUT);
  pinMode(17,OUTPUT);
  pinMode(16,OUTPUT);
  pinMode( 4,OUTPUT);
  pinMode( 2,OUTPUT);
  pinMode(15,OUTPUT);
  
  pinMode(27,OUTPUT);
  pinMode(14,OUTPUT);
  pinMode(12,OUTPUT);
  pinMode(13,OUTPUT);
  pinMode(32,OUTPUT);
  pinMode(33,OUTPUT);
  pinMode(25,OUTPUT);
  pinMode(26,OUTPUT);


  pinMode(relay1_pin, OUTPUT);
  pinMode(relay2_pin, OUTPUT);
  pinMode(relay3_pin, OUTPUT);
  pinMode(relay4_pin, OUTPUT);

  digitalWrite(relay1_pin, HIGH);
  digitalWrite(relay2_pin, HIGH);
  digitalWrite(relay3_pin, HIGH);
  digitalWrite(relay4_pin, HIGH);

  Blynk.begin(auth, ssid, pass);
}


void loop(){
     PCF8574::DigitalInput diA = pcf8574A.digitalReadAll();
     PCF8574::DigitalInput diB = pcf8574B.digitalReadAll();     
    
  lcd.setCursor(0,0);
  lcd.print(!diA.p0);lcd.print(!diA.p1);lcd.print(!diA.p2);lcd.print(!diA.p3);lcd.print(!diA.p4);lcd.print(!diA.p5);lcd.print(!diA.p6);lcd.print(!diA.p7);
  lcd.print(!diB.p0);lcd.print(!diB.p1);lcd.print(!diB.p2);lcd.print(!diB.p3);lcd.print(!diB.p4);lcd.print(!diB.p5);lcd.print(!diB.p6);lcd.print(!diB.p7);
  lcd.setCursor(0,1);
  lcd.print(digitalRead(19)); lcd.print(digitalRead(18)); lcd.print(digitalRead(5)); lcd.print(digitalRead(17)); lcd.print(digitalRead(16)); lcd.print(digitalRead(4)); lcd.print(digitalRead(2)); lcd.print(digitalRead(15)); 
  lcd.print(digitalRead(32)); lcd.print(digitalRead(33)); lcd.print(digitalRead(25)); lcd.print(digitalRead(26)); lcd.print(digitalRead(27)); lcd.print(digitalRead(14)); lcd.print(digitalRead(12)); lcd.print(digitalRead(13));  
  
  Serial.print("LECTURA ENTRADAS 0 A 16 .......");
	Serial.print(!diA.p0);Serial.print(" - ");Serial.print(!diA.p1);Serial.print(" - ");Serial.print(!diA.p2);Serial.print(" - ");Serial.print(!diA.p3);Serial.print(" - ");Serial.print(!diA.p4);Serial.print(" - ");Serial.print(!diA.p5);Serial.print(" - ");Serial.print(!diA.p6);Serial.print(" - ");Serial.print(!diA.p7);Serial.print(" - ");
  Serial.print(!diB.p0);Serial.print(" - ");Serial.print(!diB.p1);Serial.print(" - ");Serial.print(!diB.p2);Serial.print(" - ");Serial.print(!diB.p3);Serial.print(" - ");Serial.print(!diB.p4);Serial.print(" - ");Serial.print(!diB.p5);Serial.print(" - ");Serial.print(!diB.p6);Serial.print(" - ");Serial.println(!diB.p7);
  delay(50);
      
  Blynk.run();
  timer.run();  
  listen_push_buttons();
  
}

void listen_push_buttons(){
     PCF8574::DigitalInput diA = pcf8574A.digitalReadAll();
     PCF8574::DigitalInput diB = pcf8574B.digitalReadAll();
   
    if(digitalRead(diA.p0)  == LOW){
      delay(200);
      control_relay(1);
      Blynk.virtualWrite(button1_vpin, relay1_state); 
    }
   
    else if (digitalRead(diA.p1) == LOW){
      delay(200);
      control_relay(2);
      Blynk.virtualWrite(button2_vpin, relay2_state); 
    }
   
    else if (digitalRead(diA.p2) == LOW){
      delay(200);
      control_relay(3);
      Blynk.virtualWrite(button3_vpin, relay3_state); 
    }
   
    else if (digitalRead(diA.p3) == LOW){
      delay(200);
      control_relay(4);
      Blynk.virtualWrite(button4_vpin, relay4_state); 
    }
  
   
}

void control_relay(int relay){
  
  if(relay == 1){
    relay1_state = !relay1_state;
    digitalWrite(relay1_pin, relay1_state);
    Serial.print("Relay1 State = ");
    Serial.println(relay1_state);
    delay(50);
  }
 
  else if(relay == 2){
    relay2_state = !relay2_state;
    digitalWrite(relay2_pin, relay2_state);
    delay(50);
  }
  
  else if(relay == 3){
    relay3_state = !relay3_state;
    digitalWrite(relay3_pin, relay3_state);
    delay(50);
  }
 
  else if(relay == 4){
    relay4_state = !relay4_state;
    digitalWrite(relay4_pin, relay4_state);
    delay(50);
  }
  
}```
![esp32 y modulo de espancion|690x387](upload://iDm6hkaODTuGEJAJUVoUW28iGux.png)

Please edit your post and delete the screenshot of your serial monitor.
Copy the serial monitor text, and paste it between triple backticks, the same way that you do with code.

Pete.

14:31:48.158 -> [5290] Connected to WiFi
14:31:48.158 -> [5290] IP: 192.168.0.100
14:31:48.158 -> [5290] 
14:31:48.158 ->     ___  __          __
14:31:48.158 ->    / _ )/ /_ _____  / /__
14:31:48.158 ->   / _  / / // / _ \/  '_/
14:31:48.158 ->  /____/_/\_, /_//_/_/\_\
14:31:48.158 ->         /___/ v1.2.0 on ESP32
14:31:48.158 -> 
14:31:48.158 ->  #StandWithUkraine    https://bit.ly/swua
14:31:48.158 -> 
14:31:48.158 -> 
14:31:48.158 -> [5300] Connecting to blynk.cloud:80
14:31:49.291 -> [6413] Ready (ping: 103ms).
14:31:49.678 -> LECTURA ENTRADAS 0 A 16 .......0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0
14:31:49.772 -> LECTURA ENTRADAS 0 A 16 .......0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0
14:31:49.908 -> LECTURA ENTRADAS 0 A 16 .......0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0
14:31:50.034 -> LECTURA ENTRADAS 0 A 16 .......0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0
14:31:50.113 -> LECTURA ENTRADAS 0 A 16 .......0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0
14:31:50.232 -> LECTURA ENTRADAS 0 A 16 .......0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0
14:31:50.367 -> LECTURA ENTRADAS 0 A 16 .......0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0
14:31:50.473 -> LECTURA ENTRADAS 0 A 16 .......0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0
14:31:50.583 -> LECTURA ENTRADAS 0 A 16 .......0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0
14:31:50.696 -> LECTURA ENTRADAS 0 A 16 .......0 - 0 - 1 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0
14:31:50.797 -> LECTURA ENTRADAS 0 A 16 .......0 - 0 - 1 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0
14:31:50.908 -> LECTURA ENTRADAS 0 A 16 .......0 - 0 - 1 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0
14:31:51.034 -> LECTURA ENTRADAS 0 A 16 .......0 - 0 - 1 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0
14:31:51.140 -> LECTURA ENTRADAS 0 A 16 .......0 - 0 - 1 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 0

las lecturas de los pulsadores la realiza correctamente. la aplicacion mediante blynk trabaja perfectamente, no reconoce son los pulsadores el codigo (void listen_push_buttons()

I’d suggest that you start by removing all of this rubbish from your void loop…

Then, I don’t see the point in having this code in your void listen_push_buttons()

Then, you need to remove all of the delays from void listen_push_buttons() and stop calling it from your void loop, but call it with a BlynkTimer instead.

Pete.

1 Like