How to integrate nextion screen with esp8266?

Hello guys … How are you? , I need help with ESP8266 code and Nextion screen. I’m not able to integrate my code with the screen code !! Is there a specific library for ESP8266? I’ve checked several tutorials but I couldn’t solve it myself, can someone help me please? blynk code works perfectly! Here is my code:


#define BLYNK_PRINT Serial
#include <AccelStepper.h>

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
#include <EEPROM.h>
#include <SPI.h>
#include "Nextion.h"
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxxxxxxxxxxx";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "JADY";
char pass[] = "celio2366";


BlynkTimer timer;


#include "PushButton.h"
#define pinSTOP_CORTINA1  4
#define pinSTOP_CORTINA2  0
#define pino_enable_CORTINA 10
int velocidade_motor_CORTINA = 500;
int aceleracao_motor_CORTINA = 500;

//STEP, DIR
AccelStepper motor_CORTINA(1, 5, 16 );

PushButton STOP_CORTINA1(pinSTOP_CORTINA1);
PushButton STOP_CORTINA2(pinSTOP_CORTINA2);

NexProgressBar HUMIDADE        = NexProgressBar(0, 16, "HUMIDADE");
NexProgressBar TERMOMETRO      = NexProgressBar(0, 15, "TERMOMETRO");
NexSlider   SLIDERCORTINA      = NexSlider(0, 17, "CORTINA");
NexText txt_temp               = NexText(0,10, "TESTO_TEMP");
NexText txt_umid               = NexText(0,12, "TEXTO H");
NexButton BOTAO_SOBE           = NexButton(0, 4, "ABRIRCORTINA");
NexButton BOTAO_POSICAO        = NexButton(0, 5,"POSCORTINA");
NexButton BOTAO_DESCE          = NexButton(0, 6,"FECHARCORTINA");
NexSlider BOTAO_SLIDER_SUPORTE = NexSlider(0, 3, "SUPORTETV");
NexButton BOTAO_15GRAUS        = NexButton(0, 7, "GRAUS15");
NexButton BOTAO_45GRAUS        = NexButton(0, 8, "GRAUS45");
NexButton BOTAO_90GRAUS        = NexButton(0, 9, "GRAUS90");

NexTouch * nex_listen_list [] = {
    &BOTAO_SOBE ,
    &BOTAO_POSICAO ,
    &BOTAO_DESCE ,
    &BOTAO_15GRAUS ,
    &BOTAO_45GRAUS ,
    &BOTAO_90GRAUS ,
    NULL
};

int ESTADO_BOTAO_CORTINA_50;
int ESTADO_BOTAO_CORTINA_25;
int ESTADO_BOTAO_FECHA_CORTINA;
int ESTADO_BOTAO_ABRE_CORTINA;
int ESTADO_BOTAO_POSICIONA_CORTINA_ABRE;
int ESTADO_BOTAO_POSICIONA_CORTINA_FECHA;
int POSICAO_CORTINA;
int SOBE_CORTINA = 0;
int DESCE_CORTINA = 0;
int latchFlag3;//10 SEGUNDOS CORTINA
int latchFlag4;//20 SEGUNDOS CORTINA





void ABRE_CORTINA() {
  digitalWrite(pino_enable_CORTINA, LOW);
  SOBE_CORTINA = 1;

}
void FECHA_CORTINA() {
  digitalWrite(pino_enable_CORTINA, LOW);
  DESCE_CORTINA = 1;

}
void ENABLE_BLYNK_CORTINA() {
  digitalWrite(pino_enable_CORTINA, HIGH);
  DESCE_CORTINA = 0;
  SOBE_CORTINA = 0;
}

BLYNK_CONNECTED() {
   
   Blynk.virtualWrite(V0, "         INICIALIZANDO BLYNK");
  
}

BLYNK_WRITE(V1) { //ABRE CORTINA 
ESTADO_BOTAO_ABRE_CORTINA = param.asInt();

if (ESTADO_BOTAO_ABRE_CORTINA == 1){
if(POSICAO_CORTINA == 4){
Blynk.virtualWrite(V0, "          CORTINA JÁ ESTÁ ABERTA");  
}else{
ABRE_CORTINA();
timer.setTimeout(10000L, []() {  // Timed Lambda Function - Latching Button release after 5 seconds
// ----- Stop your timed thing here
ENABLE_BLYNK_CORTINA(); 
Blynk.virtualWrite(V0, "                CORTINA ABERTA");
  });

  }
 }
}
BLYNK_WRITE(V4) { //FECHA CORTINA 
ESTADO_BOTAO_FECHA_CORTINA = param.asInt();

if (ESTADO_BOTAO_FECHA_CORTINA == 1){
if(POSICAO_CORTINA == 1){
Blynk.virtualWrite(V0, "                CORTINA JÁ ESTÁ FECHADA");
}else{
 FECHA_CORTINA();
 
timer.setTimeout(10000L, []() {  
// ----- Stop your timed thing here
ENABLE_BLYNK_CORTINA(); 
Blynk.virtualWrite(V0, "               CORTINA FECHADA");
  });
  }
 }
}

BLYNK_WRITE(V6) { //POSIÇAO EM 50%
  ESTADO_BOTAO_CORTINA_50 = param.asInt();
  if (ESTADO_BOTAO_CORTINA_50 == 1) {
 
  if (POSICAO_CORTINA == 1 && latchFlag3 == 0) {
          latchFlag3 = 1;  
          Blynk.virtualWrite(V6, 1);  // Reset Latching Button to OFF
          ABRE_CORTINA();
          Blynk.virtualWrite(V0, "         POSICIONANDO CORTINA EM 50%");

          timer.setTimeout(10000L, []() {  
            // ----- Stop your timed thing here
          ENABLE_BLYNK_CORTINA(); 
          Blynk.virtualWrite(V0, "          PARANDO CORTINA EM 50%");
            

            // -----
            Blynk.virtualWrite(V6, 0);  // Reset Latching Button to OFF
            latchFlag3 = 0;  // resets to allow next interaction
            POSICAO_CORTINA = 3;
            EEPROM.write(0,POSICAO_CORTINA);
            EEPROM.commit();
            Blynk.virtualWrite(V13, "CORTINA EM 50%");
          });  // END Timer Function
         }else {
    if (ESTADO_BOTAO_CORTINA_50 == 0 && latchFlag3 == 1) {  // If you try to tun off the button before time is up
      Blynk.virtualWrite(V6, 1);  // Restore Latching Button ON until timer is finished
    }
   }// SE POSIÇÃO 01

   if (POSICAO_CORTINA == 2 && latchFlag3 == 0) {
          latchFlag3 = 1;  // Keeps from allowing button press more then once while relay activated
          // ----- Start your timed thing here
          Blynk.virtualWrite(V6, 1);
          ABRE_CORTINA();
          Blynk.virtualWrite(V0, "POSICIONANDO CORTINA EM 50%");

          timer.setTimeout(5000L, []() {  // Timed Lambda Function - Latching Button release after 5 seconds
            // ----- Stop your timed thing here
          ENABLE_BLYNK_CORTINA(); 
          Blynk.virtualWrite(V0, "          PARANDO CORTINA EM 50%");
            

            // -----
            Blynk.virtualWrite(V6, 0);  // Reset Latching Button to OFF
            latchFlag3 = 0;  // resets to allow next interaction
            POSICAO_CORTINA = 3;
            EEPROM.write(0,POSICAO_CORTINA);
            EEPROM.commit();
            Blynk.virtualWrite(V13, "               CORTINA EM 50%");
          });  // END Timer Function
         }else {
    if (ESTADO_BOTAO_CORTINA_50 == 0 && latchFlag3 == 1) {  // If you try to tun off the button before time is up
      Blynk.virtualWrite(V6, 1);  // Restore Latching Button ON until timer is finished
    }
   }// SE POSIÇÃO 02

   if (POSICAO_CORTINA == 3){

   Blynk.virtualWrite(V13, "          CORTINA JÁ ESTÁ EM 50%");
    
   }

   if (POSICAO_CORTINA == 4 && latchFlag3 == 0) {
          latchFlag3 = 1;  // Keeps from allowing button press more then once while relay activated
          // ----- Start your timed thing here
          Blynk.virtualWrite(V6, 1);
          FECHA_CORTINA();
          Blynk.virtualWrite(V0, "POSICIONANDO CORTINA EM 50%");

          timer.setTimeout(5000L, []() {  // Timed Lambda Function - Latching Button release after 5 seconds
            // ----- Stop your timed thing here
          ENABLE_BLYNK_CORTINA(); 
          Blynk.virtualWrite(V0, "         PARANDO CORTINA EM 50%");
            

            // -----
            Blynk.virtualWrite(V6, 0);  // Reset Latching Button to OFF
            latchFlag3 = 0;  // resets to allow next interaction
            POSICAO_CORTINA = 3;
            EEPROM.write(0,POSICAO_CORTINA);
            EEPROM.commit();
            Blynk.virtualWrite(V13, "               CORTINA EM 50%");
          });  // END Timer Function
         }else {
    if (ESTADO_BOTAO_CORTINA_50 == 0 && latchFlag3 == 1) {  // If you try to tun off the button before time is up
      Blynk.virtualWrite(V6, 1);  // Restore Latching Button ON until timer is finished
    }
   }// SE POSIÇÃO 04
  } 
 }
  BLYNK_WRITE(V7) { //STOP CORTINA
  int STOP_CORTINA = param.asInt();
  if (STOP_CORTINA == 1) {

    Blynk.virtualWrite(V0, "                 STOP CORTINA");
    digitalWrite(pino_enable_CORTINA, HIGH);
    DESCE_CORTINA = 0;
    SOBE_CORTINA = 0;
  }

}//V7

BLYNK_WRITE(V8) { //CORTINA EM 25%
  ESTADO_BOTAO_CORTINA_25 = param.asInt();
  if (ESTADO_BOTAO_CORTINA_25 == 1){
  
  if (POSICAO_CORTINA == 1 && latchFlag3 == 0) {
          latchFlag3 = 1;  // Keeps from allowing button press more then once while relay activated
          // ----- Start your timed thing here
          Blynk.virtualWrite(V8, 1);  // Reset Latching Button to OFF
          ABRE_CORTINA();
          Blynk.virtualWrite(V0, "      POSICIONANDO CORTINA EM 25%");

          timer.setTimeout(50000L, []() {  // Timed Lambda Function - Latching Button release after 5 seconds
            // ----- Stop your timed thing here
          ENABLE_BLYNK_CORTINA(); 
          Blynk.virtualWrite(V0, "          PARANDO CORTINA EM 25%");
            

            // -----
            Blynk.virtualWrite(V8, 0);  // Reset Latching Button to OFF
            latchFlag3 = 0;  // resets to allow next interaction
            POSICAO_CORTINA = 2;
            EEPROM.write(0,POSICAO_CORTINA);
            EEPROM.commit();
            Blynk.virtualWrite(V13, "                 CORTINA EM 25%");
          });  // END Timer Function
         }else {
    if (ESTADO_BOTAO_CORTINA_25 == 0 && latchFlag3 == 1) {  // If you try to tun off the button before time is up
      Blynk.virtualWrite(V8, 1);  // Restore Latching Button ON until timer is finished
    }
   }// SE POSIÇÃO 01

   if(POSICAO_CORTINA == 2){
    Blynk.virtualWrite(V13, "          CORTINA JÁ ESTÁ  EM 25%");
   }

   if (POSICAO_CORTINA == 3 && latchFlag3 == 0) {
          latchFlag3 = 1;  // Keeps from allowing button press more then once while relay activated
          // ----- Start your timed thing here
          Blynk.virtualWrite(V8, 1);  // Reset Latching Button to OFF
          FECHA_CORTINA();
          Blynk.virtualWrite(V0, "      POSICIONANDO CORTINA EM 25%");

          timer.setTimeout(5000L, []() {  // Timed Lambda Function - Latching Button release after 5 seconds
            // ----- Stop your timed thing here
          ENABLE_BLYNK_CORTINA(); 
          Blynk.virtualWrite(V0, "          PARANDO CORTINA EM 25%");
            

            // -----
            Blynk.virtualWrite(V8, 0);  // Reset Latching Button to OFF
            latchFlag3 = 0;  // resets to allow next interaction
            POSICAO_CORTINA = 2;
            EEPROM.write(0,POSICAO_CORTINA);
            EEPROM.commit();
            Blynk.virtualWrite(V13, "                 CORTINA EM 25%");
          });  // END Timer Function
         }else {
    if (ESTADO_BOTAO_CORTINA_25 == 0 && latchFlag3 == 1) {  // If you try to tun off the button before time is up
      Blynk.virtualWrite(V8, 1);  // Restore Latching Button ON until timer is finished
    }
   }// SE POSIÇÃO 03

   if (POSICAO_CORTINA == 1 && latchFlag3 == 0) {
          latchFlag3 = 1;  // Keeps from allowing button press more then once while relay activated
          // ----- Start your timed thing here
          Blynk.virtualWrite(V8, 1);  // Reset Latching Button to OFF
          FECHA_CORTINA();
          Blynk.virtualWrite(V0, "          POSICIONANDO CORTINA EM 25%");

          timer.setTimeout(5000L, []() {  // Timed Lambda Function - Latching Button release after 5 seconds
            // ----- Stop your timed thing here
          ENABLE_BLYNK_CORTINA(); 
          Blynk.virtualWrite(V0, "               PARANDO CORTINA EM 25%");
            

            // -----
            Blynk.virtualWrite(V8, 0);  // Reset Latching Button to OFF
            latchFlag3 = 0;  // resets to allow next interaction
            POSICAO_CORTINA = 2;
            EEPROM.write(0,POSICAO_CORTINA);
            EEPROM.commit();
            Blynk.virtualWrite(V13, "                 CORTINA EM 25%");
          });  // END Timer Function
         }else {
    if (ESTADO_BOTAO_CORTINA_25 == 0 && latchFlag3 == 1) {  // If you try to tun off the button before time is up
      Blynk.virtualWrite(V8, 1);  // Restore Latching Button ON until timer is finished
    }
   }// SE POSIÇÃO 04
  }
}//V8

BLYNK_WRITE(V9) { //POSICIONA ABRE CORTINA 
ESTADO_BOTAO_POSICIONA_CORTINA_ABRE = param.asInt();

if (ESTADO_BOTAO_POSICIONA_CORTINA_ABRE == 1){
if(POSICAO_CORTINA == 4){
Blynk.virtualWrite(V13, "          CORTINA JÁ ESTÁ ABERTA");  
}else{
ABRE_CORTINA();
 }
 if(ESTADO_BOTAO_POSICIONA_CORTINA_ABRE == 0){
  ENABLE_BLYNK_CORTINA(); 
  }
 }
}

BLYNK_WRITE(V11) { //POSICIONA FECHA CORTINA 
ESTADO_BOTAO_POSICIONA_CORTINA_FECHA = param.asInt();

if (ESTADO_BOTAO_POSICIONA_CORTINA_FECHA == 1){
if(POSICAO_CORTINA == 1){
Blynk.virtualWrite(V13, "          CORTINA JÁ ESTÁ FECHADA");
}else{
FECHA_CORTINA();
  }
if(ESTADO_BOTAO_POSICIONA_CORTINA_FECHA == 0){
 ENABLE_BLYNK_CORTINA();   
  }
 }
}

void BOTAO_15GRAUSPopCallback (void * ptr) {//CONTINA EM 25%
    //tState.setText ("State: on");
    Blynk.syncVirtual(V8);
    Blynk.virtualWrite(V0, "          TESTE TELA NEXTION V8");
}
void BOTAO_45GRAUSPopCallback (void * ptr) {//CORTINA EM 50%
    //tState.setText ("State: on");
    Blynk.syncVirtual(V6);
    Blynk.virtualWrite(V0, "          TESTE TELA NEXTION V6");
}
void BOTAO_90GRAUSPopCallback (void * ptr) {//STOP CORTINA
    //tState.setText ("State: on");
    Blynk.syncVirtual(V7);
    Blynk.virtualWrite(V0, "          TESTE TELA NEXTION V7");
}

void setup(void)
{
  // Debug console
   
  Serial.begin(115200);
  Serial.println(POSICAO_CORTINA);
  EEPROM.begin(1);
  
  POSICAO_CORTINA = EEPROM.read(0);
  
  pinMode(pino_enable_CORTINA, OUTPUT);
  
  motor_CORTINA.setMaxSpeed(velocidade_motor_CORTINA);
  motor_CORTINA.setSpeed(velocidade_motor_CORTINA);
  motor_CORTINA.setAcceleration(aceleracao_motor_CORTINA);
  
  nexInit();
  
  BOTAO_15GRAUS.attachPop(BOTAO_15GRAUSPopCallback, &BOTAO_15GRAUS);
  BOTAO_45GRAUS.attachPop(BOTAO_45GRAUSPopCallback, &BOTAO_45GRAUS);
  BOTAO_90GRAUS.attachPop(BOTAO_90GRAUSPopCallback, &BOTAO_90GRAUS);
  
  //Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8080);
  //Blynk.begin(auth, ssid, pass, IPAddress(10,0,0,102), 9443);
}

void loop(void)
{
  nexLoop(nex_listen_list);
  
  Serial.println("EEPROM: ");
  Serial.println(EEPROM.read(0));
  Serial.println();
  
  STOP_CORTINA1.button_loop();
  STOP_CORTINA2.button_loop();

  if (STOP_CORTINA1.pressed()) {

    ENABLE_BLYNK_CORTINA(); 
    POSICAO_CORTINA = 1;
    EEPROM.write(0,POSICAO_CORTINA);
    EEPROM.commit();
    Blynk.virtualWrite(V0, "          CORTINA EM POSIÇÃO FECHADO");
  }
  if (STOP_CORTINA2.pressed()) {

    ENABLE_BLYNK_CORTINA(); 
    POSICAO_CORTINA = 4;
    EEPROM.write(0,POSICAO_CORTINA);
    EEPROM.commit();
    Blynk.virtualWrite(V0, "          CORTINA EM POSIÇÃO ABERTA");
  }
    
  if (SOBE_CORTINA ==  1) { //ABRE CORTINA

    motor_CORTINA.moveTo(10000);
  }
  if (DESCE_CORTINA == 1) { //FECHA CORTINA

    motor_CORTINA.moveTo(-10000);
  }
  motor_CORTINA.run();
  Blynk.run();
  timer.run();
}```

I have the ITEADLIB library, I made a change in the NexConfig folder, changing #define nexSerial Serial2 to #define nexSerial Serial, but it also didn't work !!

Yes, you’re using it…

Your code won’t work properly with Blynk with a void loop like that.

In addition, your EEPROM won’t last long if you keep reading it during every void loop cycle, and writing to it the way you do.
EEPROM is good for 100,000 read/write operations only, and using EEPROM is so old that it’s virtually obsolete. It was replaced by SPIFFS, which is no deprecated in favour of LittleFS.

The biggest issue when using a Nextion with a NodeMCU is lack of serial ports. The NodeMCU has 1.5 hardware serial ports, the bald port is Tx only. As the Nextion needs Tx and Rx you can either connect it to UART0 (Serial) and use the Tx of UART1 (Serial1) via an FTDI for debugging, or use Serial for debugging and a SoftwareSerial port for the Nextion. The problem with this is that SoftwareSerial is only good for speeds up to 9600 baud, so you need to configure your Nextion to 9600 also. In addition, you have to ensure that the correct SoftwareSerial library is being used, as the Arduino AVR library doesn’t work properly with ESP8266 based boards.

I go for the first option, and connect my Nextions to Serial and debug using Serial1.

Pete.

Pete.

Nextion code is mostly a case of serial type reads and sends… not much else to it, just don’t hog the void loop()

I have a topic here using Blynk and Nextion… it is on an ESP32, but aside form it’s extra serial ports the type of MCU isn’t really relevant, so you should be able to glean some coding ideas from how I wrote mine.

Since my use was only display, my communication needs where simple and I just used a single timed function to do the transmitting to the Nextion.

Also search for other examples, in case they use the touch features as well…

https://community.blynk.cc/search?q=nextion