Nextion Dual Button Sync

Hello,

I want to use dual buttons on Nextion. I’m using physically dual push button and Blynk control . These buttons working synk together. I want to add new buttons on Nextion and want to work synk with bylnk and physical button. How can i make this? Thanks.

I am sharing my code.

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <Adafruit_Sensor.h>
#define BLYNK_PRINT Serial
#include <DHT.h>
#include <Adafruit_BMP085.h>

Adafruit_BMP085 bmp;
#define I2C_SCL 19
#define I2C_SDA 18

float dst,bt,bp,ba;
char dstmp[20],btmp[20],bprs[20],balt[20];
bool bmp085_present=true;

#define DHTPIN 33
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE); //Defining the pin and the dhttype

BlynkTimer timer;

int toggleState_1 = 1;
int pushButton1State = HIGH;

int toggleState_2 = 1;
int pushButton2State = HIGH;

int toggleState_3 = 1;
int pushButton3State = HIGH;

int toggleState_4 = 1;
int pushButton4State = HIGH;

int wifiFlag = 0;

#define AUTH “sDRszgH6y1vraxIjqzvMJ8ry” // You should get Auth Token in the Blynk App.
#define WIFI_SSID “--” //Enter Wifi Name
#define WIFI_PASS “--” //Enter wifi Password

#define WIFI_LED 2 //D25
#define RELAY_PIN_1 22 //Role1
#define RELAY_PIN_2 23 //Role2
#define RELAY_PIN_3 4 //Role3
#define RELAY_PIN_4 21 //Role4

#define PUSH_BUTTON_1 13 //Buton1
#define PUSH_BUTTON_2 12 //Buton2
#define PUSH_BUTTON_3 26 //Buton3
#define PUSH_BUTTON_4 25 //Buton4

#define VPIN_BUTTON_1 V3 //Blynk Virtual Pin
#define VPIN_BUTTON_2 V4
#define VPIN_BUTTON_3 V7
#define VPIN_BUTTON_4 V8

int analogInput1 = 34 ; //Servis Akü
int analogInput2 = 35 ; //Motor Akü

float correctionfactor = -0.59;
float vout1 = 0.0;
float vin1 = 0.0;
float vout2 = 0.0;
float vin2 = 0.0;

float R1 = 30000; // !! resistance of R1 !!
float R2 = 7350; // !! resistance of R2 !!
int value = 0;

void relayOnOff(int relay){

switch(relay){
case 1:
if(toggleState_1 == 0){
digitalWrite(RELAY_PIN_1, HIGH); // turn on relay 1
toggleState_1 = 1;
}
else{
digitalWrite(RELAY_PIN_1, LOW); // turn off relay 1
toggleState_1 = 0;
}
delay(500);
break;
case 2:
if(toggleState_2 == 0){
digitalWrite(RELAY_PIN_2, HIGH); // turn on relay 2
toggleState_2 = 1;
}
else{
digitalWrite(RELAY_PIN_2, LOW); // turn off relay 2
toggleState_2 = 0;
}
delay(500);
break;

break;
case 3:
if(toggleState_3 == 0){
digitalWrite(RELAY_PIN_3, HIGH); // turn on relay 3
toggleState_3 = 1;
}
else{
digitalWrite(RELAY_PIN_3, LOW); // turn off relay 3
toggleState_3 = 0;
}
delay(500);
break;
case 4:
if(toggleState_4 == 0){
digitalWrite(RELAY_PIN_4, HIGH); // turn on relay 4
toggleState_4 = 1;
}
else{
digitalWrite(RELAY_PIN_4, LOW); // turn off relay 4
toggleState_4 = 0;
}
delay(500);
break;
default : break;
}
}

BLYNK_CONNECTED() {
// Request the latest state from the server
Blynk.syncVirtual(VPIN_BUTTON_1);
Blynk.syncVirtual(VPIN_BUTTON_2);
Blynk.syncVirtual(VPIN_BUTTON_3);
Blynk.syncVirtual(VPIN_BUTTON_4);
}

// When App button is pushed – switch the state

BLYNK_WRITE(VPIN_BUTTON_1) {
toggleState_1 = param.asInt();
digitalWrite(RELAY_PIN_1, toggleState_1);
}

BLYNK_WRITE(VPIN_BUTTON_2) {
toggleState_2 = param.asInt();
digitalWrite(RELAY_PIN_2, toggleState_2);
}

BLYNK_WRITE(VPIN_BUTTON_3) {
toggleState_3 = param.asInt();
digitalWrite(RELAY_PIN_3, toggleState_3);
}

BLYNK_WRITE(VPIN_BUTTON_4) {
toggleState_4 = param.asInt();
digitalWrite(RELAY_PIN_4, toggleState_4);
}

void with_internet(){
if (digitalRead(PUSH_BUTTON_1) == LOW) {
relayOnOff(1);
// Update Button Widget
Blynk.virtualWrite(VPIN_BUTTON_1, toggleState_1);
}
if (digitalRead(PUSH_BUTTON_2) == LOW) {
relayOnOff(2);
// Update Button Widget
Blynk.virtualWrite(VPIN_BUTTON_2, toggleState_2);
}

if (digitalRead(PUSH_BUTTON_3) == LOW) {
relayOnOff(3);
// Update Button Widget
Blynk.virtualWrite(VPIN_BUTTON_3, toggleState_3);
}

if (digitalRead(PUSH_BUTTON_4) == LOW) {
relayOnOff(4);
// Update Button Widget
Blynk.virtualWrite(VPIN_BUTTON_4, toggleState_4);
}
}
void without_internet(){
if (digitalRead(PUSH_BUTTON_1) == LOW) {
relayOnOff(1);
}

if (digitalRead(PUSH_BUTTON_2) == LOW) {
relayOnOff(2);
}

if (digitalRead(PUSH_BUTTON_3) == LOW) {
relayOnOff(3);
}
if (digitalRead(PUSH_BUTTON_4) == LOW) {
relayOnOff(4);
}
}

void checkBlynkStatus() { // called every 3 seconds by SimpleTimer

bool isconnected = Blynk.connected();
if (isconnected == false) {
wifiFlag = 1;
digitalWrite(WIFI_LED, LOW);
}
if (isconnected == true) {
wifiFlag = 0;
digitalWrite(WIFI_LED, HIGH);
}
}

void sendSensor()
{

if (!bmp.begin())
{
Serial.println(“Could not find a valid BMP085 sensor, check wiring!”);
while (1) {}
}

float h = dht.readHumidity();
float t = dht.readTemperature();

if (isnan(h) || isnan(t))
{
Serial.println(“Failed to read from DHT sensor!”);
return;
}

double gamma = log(h/100) + ((17.62*t) / (243.5+t));
double dp = 243.5*gamma / (17.62-gamma);

float bp = bmp.readPressure()/100;
float ba = bmp.readAltitude();
float bt = bmp.readTemperature();
float dst = bmp.readSealevelPressure()/100;

Blynk.virtualWrite(V5 , h); //DH
Blynk.virtualWrite(V6 , t);
Blynk.virtualWrite(V10, bp);
Blynk.virtualWrite(V11, ba);
Blynk.virtualWrite(V12, bt);
Blynk.virtualWrite(V13, dst);
Blynk.virtualWrite(V14, dp);

}

void setup()
{
Serial.begin(9600);

pinMode(RELAY_PIN_1, OUTPUT);
pinMode(PUSH_BUTTON_1, INPUT_PULLUP);
digitalWrite(RELAY_PIN_1, toggleState_1);

pinMode(RELAY_PIN_2, OUTPUT);
pinMode(PUSH_BUTTON_2, INPUT_PULLUP);
digitalWrite(RELAY_PIN_2, toggleState_2);

pinMode(RELAY_PIN_3, OUTPUT);
pinMode(PUSH_BUTTON_3, INPUT_PULLUP);
digitalWrite(RELAY_PIN_3, toggleState_3);

pinMode(RELAY_PIN_4, OUTPUT);
pinMode(PUSH_BUTTON_4, INPUT_PULLUP);
digitalWrite(RELAY_PIN_4, toggleState_4);

pinMode(WIFI_LED, OUTPUT);
dht.begin();
delay(10);
timer.setInterval(1000L, sendSensor);
Wire.begin(I2C_SDA, I2C_SCL);

WiFi.begin(WIFI_SSID, WIFI_PASS);
timer.setInterval(3000L, checkBlynkStatus); // check if Blynk server is connected every 3 seconds
Blynk.config(AUTH);
}

void loop()
{
if (WiFi.status() != WL_CONNECTED)
{
Serial.println(“Not Connected”);
}
else
{
Serial.println(” Connected”);
Serial.println(vin1); // sleep…
Serial.println(vin2); // sleep…
Blynk.run();
}

timer.run(); // Initiates SimpleTimer
if (wifiFlag == 0)
with_internet();
else
without_internet();

value = analogRead(analogInput1);
vout1 = (value * 3.3) / 4095.0;
vin1 = vout1 / (R2/(R1+R2));

value = analogRead(analogInput2);
vout2 = (value * 3.3) / 4095.0;
vin2 = vout2 / (R2/(R1+R2));

vin1 = vin1 – correctionfactor;
vin2 = vin2 – correctionfactor;

Blynk.virtualWrite(V0, vin1);
Blynk.virtualWrite(V16, vin2);

Serial.println(vin1); // sleep…
Serial.println(vin2); // sleep…
delay(500);
}

@jojoned 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:
```

Pete.

1 Like

I assume you are using the Nextion editor?
Have you ever tried using Nextion without Blynk?
You need to read the serial port and extract Nextion cmd
Tell us what kind of Nextion command you are using to send to the serial port

Hello,
Yes, i am using nextion editor and using NX4832t035_011 with ITEADsLIB_Arduino_Nextion-maste library.
I didn’t try without blynk. I just start to deal with coding and don’t have experience alot :frowning:

I use Nextion with Blynk , but it is not easy to explain how to

2022-06-09_204329

You have to start here to understand basic commands :

First of all, you appear to be using Blynk Legacy, which will stop working at the end of this year.
I suggest that you build your project around Blynk IoT.

Secondly, you should read this…

https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

Thirdly, working with Nextion displays is not easy. As @Blynk_Coeur suggests, you should start with a Nextion only sketch - preferably with just one or two push buttons.

Then, add your physical switches and get them changing the Nextion button statuses. I’d strongly recommend using interrupts on these physical buttons, among with a denounce routine.
Once you have this working you can add-in the Blynk functionality.

Pete.

1 Like