How can I do change button nextion with blynk

Hello,
I have a arduino mega with ethernet shield, nextion touchscreen and blynk app.

On the nextion I have 1 button with 2 possibility.
This button works perfect on nextion and change the possibility (open-close).
On blynk I cloned this button and blynk change the status of this button when I push it on nextion.
But when I push the button on blynk, the nextion don’t change and no actions are coming.

Who can help me please?
Thanks
Mike

Hi Mike,
It sounds like you’re almost there with getting this to work, but its difficult for us to help unless we know more details.

Is your Blynk button attached to a virtual pin, and is the button widget set to “Switch” rather than “Press”?

What do yo have in your corresponding BLYNK_WRITE function in your Mega code?

Which Nextion library are you using on the Mega?

How is the button set-up on the Nextion?

Pete.

Hello Pete,

  • blynk button is on virtual pin, and on switch modus.

  • nextion lib is the itealib nextion

  • for the button setup on nextion, I can only answer this evening sorry

  • relais_ph is the high/low output on Arduino

  • ModeFiltration is the action from the Nextion button

  • V9 is the virtual pin for the button

This action is with the Hex: 65 00 14 00 FF FF FF

What do you think?

#define BLYNK_PRINT Serial


#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";

#define W5100_CS  10
#define SDCARD_CS 4

// Set your LED and physical button pins here
const int relais_phPin = 36;
const int ModeFiltrationPin = "b9_p4";

BlynkTimer timer;
void checkPhysicalButton();

int relais_phState = LOW;
int ModeFiltrationState = HIGH;

// Every time we connect to the cloud...
BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncVirtual(V9);

  // Alternatively, you could override server state using:
  //Blynk.virtualWrite(V2, ledState);
}

// When App button is pushed - switch the state
BLYNK_WRITE(V9) {
  relais_phState = param.asInt();
  digitalWrite(relais_phPin, relais_phState);
}

void checkPhysicalButton()
{
  if (digitalRead(ModeFiltrationPin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (ModeFiltrationState != LOW) {

      // Toggle LED state
      relais_phState = !relais_phState;
      digitalWrite(relais_phPin, relais_phState);

      // Update Button Widget
      Blynk.virtualWrite(V9, relais_phState);
    }
    ModeFiltrationState = LOW;
  } else {
    ModeFiltrationState = HIGH;
  }
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card

  Blynk.begin(auth);
  // You can also specify server:
  //Blynk.begin(auth, "blynk-cloud.com", 80);
  //Blynk.begin(auth, IPAddress(192,168,1,100), 8080);

  pinMode(relais_phPin, OUTPUT);
  pinMode(ModeFiltrationPin, INPUT_PULLUP);
  digitalWrite(relais_phPin, relais_phState);

  // Setup a function to be called every 100 ms
  timer.setInterval(100L, checkPhysicalButton);
}

void loop()
{
  Blynk.run();
  timer.run();
}

maybe better with this example:

how can i V11 (Blynk button) do the same action as P4_b9 (Nextion button)???

void P4_b9PopCallback(void *ptr)
{
  ModeFiltration = !ModeFiltration;
  if (ModeFiltration == 0){
    strcpy(buffer, "MANU");
    duree_filtration = EEPROM.readDouble(addr_duree_filtration);
  }
  else {
    strcpy(buffer, "AUTO");
    duree_filtration = TEau / 2;
  }
  P4_b9.setText(buffer);
  P4_n1.setValue(duree_filtration);
  EEPROM.write(addr_ModeFiltration, ModeFiltration);
}

Hi Mike, I use a different Nextion library to you, so some of this is speculation…

What you want to achieve is that either a change of the Nextion button or the Blynk button will do the same thing.
The Nextion button calls the void P4_b9PopCallback(void *ptr) callback function and the Blynk button calls the BLYNK_WRITE(V11) callback.

Th simplest way to achieve this is to move the code that’s in your void P4_b9PopCallback(void *ptr) function into a function of its own, then call that new function from either the Nextion or the Blynk callback.
A bit like this (but this is pseudo code and I don’t fully understand some of the Nextion commands, so you’ll need to run with it a little…

void P4_b9PopCallback(void *ptr)
{
  toggle_filtration();
}

BLYNK_WRITE(V11)
{
  toggle_filtration();
}

void toggle_filtration() // new function
{
  ModeFiltration = !ModeFiltration;
  if (ModeFiltration == 0){
    strcpy(buffer, "MANU");
    duree_filtration = EEPROM.readDouble(addr_duree_filtration);
  }
  else {
    strcpy(buffer, "AUTO");
    duree_filtration = TEau / 2;
  }
  P4_b9.setText(buffer);
  P4_n1.setValue(duree_filtration);
  EEPROM.write(addr_ModeFiltration, ModeFiltration);
}

Your new toggle_filtration() function will need amending so that it updates the state of the Blynk button to reflect the change, as well as updating the Nextion button.
You’ve done some of this in your other example with the

code, but you didn’t really follow-through with updating the widget on V9 in the else part of the statement.

What I’m not clear about from your last piece of code is that you don’t seem to be taking account of what the Nextion button state is, it’s simply a toggle as opposed to an auto/manual. I guess this isn’t a problem though.

Does this help at all?

Pete.

@PeteKnight Please format your code correc… :rofl::joy::rofl::joy::rofl:

1 Like

sorry, the code is what I received from another person

Just ribbing @PeteKnight

@PeteKnight You’re Awesome! it works!
me happy!
thanks for all :wink:

1 Like

@daveblynk Sorted sir, very sorry sir, won’t do it again sir!

Pete.

1 Like