Start a new topic

Help with ESP8266 and Nextion

Hey guys.. Pulling my hair out here..  I've been up and down the forums and Google to try to find out how to hook my NodeMCU up to my Nextion and cant get it to work.  From what I could find, I think I need to use Software Serial and was going down the that avenue, but it still does not work. 


Has anyone got a sample on how to get the ESP working with the Nextion?  A basic "hello world" sample... Maybe something that changes the value of a text box?


To add to the above....  I've modified nexconfig.h with the below and set this (SoftwareSerial HMISerial(9,10); // RX,TX) in my sketch.  Im using the NodeMCU V1.0 board. RX=9 TX=10. I modified one of the samples with this and cant get it to work.


#include <SoftwareSerial.h>

extern SoftwareSerial HMISerial;


#define nexSerial HMISerial

You will need to send across the serial your command followed by three 0xFF bytes

set t0.txt-maxl to atleast 11, and send something like


Serial.print("t0.txt="hello world");

Serial.write(0xFF);

Serial.write(0xFF);

Serial.write(0xFF);

Thx, but that won't compile. I am using the CompText sample provide by Nextion (below), and Serial.write is not working.  I modifed it to use Software Serial.  I understand the need to send the 0xFF commands but dont see that anywhere in their sample code? I thought it would be built into the library?  


 

#include "Nextion.h"
#include <ESP8266WiFi.h>

SoftwareSerial HMISerial(4,5);  // RX,TX  see NexConfig.h - using software serial for ESP8266



void t0PopCallback(void *ptr);
void b0PopCallback(void *ptr);
void b1PopCallback(void *ptr);

/*
 * Declare a text object [page id:0,component id:1, component name: "t0"]. 
 */
NexText t0 = NexText(0, 1, "t0");

/*
 * Declare a button object [page id:0,component id:2, component name: "b0"]. 
 */
NexButton b0 = NexButton(0, 2, "b0");

/*
 * Declare a button object [page id:0,component id:3, component name: "b1"]. 
 */
NexButton b1 = NexButton(0, 3, "b1");

char buffer[100] = {0};

/*
 * Register object t0, b0, b1, to the touch event list.  
 */
NexTouch *nex_listen_list[] = 
{
    &t0,
    &b0,
    &b1,
    NULL
};

/*
 * Text component pop callback function. 
 */
void t0PopCallback(void *ptr)
{
    dbSerialPrintln("t0PopCallback");
    t0.setText("50");
}

/*
 * Button0 component pop callback function.
 * In this example,the value of the text component will plus one every time when button0 is released.
 */
void b0PopCallback(void *ptr)
{
    uint16_t len;
    uint16_t number;
    
    dbSerialPrintln("b0PopCallback");

    memset(buffer, 0, sizeof(buffer));
    t0.getText(buffer, sizeof(buffer));
    
    number = atoi(buffer);
    number += 1;

    memset(buffer, 0, sizeof(buffer));
    itoa(number, buffer, 10);
    
    t0.setText(buffer);
}

/*
 * Button1 component pop callback function.
 * In this example,the value of the text component will minus one every time when button1 is released.
 */
void b1PopCallback(void *ptr)
{
    uint16_t len;
    uint16_t number;
    
    dbSerialPrintln("b1PopCallback");

    memset(buffer, 0, sizeof(buffer));
    t0.getText(buffer, sizeof(buffer));
    
    number = atoi(buffer);
    number -= 1;

    memset(buffer, 0, sizeof(buffer));
    itoa(number, buffer, 10);
    
    t0.setText(buffer);
}

void setup(void)
{
    nexSerial.begin(9600);
    
    /* Set the baudrate which is for debug and communicate with Nextion screen. */
    nexInit();

    /* Register the pop event callback function of the current text component. */
    t0.attachPop(t0PopCallback);

    /* Register the pop event callback function of the current button0 component. */
    b0.attachPop(b0PopCallback);

    /* Register the pop event callback function of the current button1 component. */
    b1.attachPop(b1PopCallback);

    dbSerialPrintln("setup done");


   //t0.setText("72");
   //Serial.write(0xFF);
   //Serial.write(0xFF);
   //Serial.write(0xFF); 
}

void loop(void)
{
    /*
     * When a pop or push event occured every time, 
     * the corresponding component[right page id and component id] in touch event list will be asked.
     */
    nexLoop(nex_listen_list);
}

 

So if you are using the IteadLib Arduino Nextion Library

-- You will need to visit your NexConfig.h file to configure your serial

-- Review NexHardware.cpp for sendCommand, etc

-- Then Review NexText.cpp, you'll see NexText.setText calls sendCommand


So then it might be something like

// declare text1 as page 0 component 1 .objname t0 and use

NexText  text1  NexText(0,1,"t0");

text1.setText("hello world");


If there are other libraries, I'm open to recommendations. Is software serial necessary for esp's?
If there are other libraries, I'm open to recommendations. Is software serial necessary for esp's?

You'll need to review your documentation for your MCU and if using Arduino, review your options.

There certainly are other compilers for the ESP, but the same steps to research will exist

@Dave A


Hi,


I'm guessing you're up to speed with the requirements for the ESP core library installation in the Arduino IDE. You need that minimum before anything will compile (for ESP) under that environment.

That done, in your case, I would stay away from the Nextion library for your first few trial projects.

I've got several months experience working with the 2 devices, and as the library files were immature around the time I first tried them, I gave up and proceeded with my own solutions.


If you'd like to reply to this, giving me a rough idea of your setup, I'll get a little sketch written up for you to play with. Which display do you have ?


Regards


Steve. 

@Steve, thanKS for the offer. My ultimate goal is to pull weather data using Weather Underground. I just tried this library ( https://github.com/bborncr/nextion) and still no go. Ive got my Arduino IDE fully setup for the ESP and have a number of projects under my belt. That said, heres what I have: -NodeMcu ESP8266 as my main controller (link below) - Nextion 5v to vcc, gnd to gnd both on the NodeMCU. Note the Node vcc puts out 4.7v -powered by usb - nex RX to node tx, (pin5) nex tx to node rx (pin4) - I tried the dim screen example with the above library and got nothing. Also tried to change a text box value - ditto - I have the 5" Nextion display. non-enhanced version. http://www.banggood.com/Geekcreit-Doit-NodeMcu-Lua-ESP8266-ESP-12E-WIFI-Development-Board-p-985891.html?rmmds=search
FYI. I can compile and upload, there is just no activity between the esp and the nextion display.

Dave,


Thanks for the info, if you can give me a few hours, I'll be back with a sketch (tested) and simple HMI.


As it happens I'm bogged down with a bug on one of my ESP's, would love to get it nailed in the short term :)


Some rather deep stuff, where an ESP updates it's firmware from a new copy in it's own SPIFFS.


After a trigger call over websockets from another ESP which has my Nextion attached.


Sounds complicated, and it is, stick around I can help with a lot of your ESP-Nextion stuff.


Regards


Steve. 

Thanks Steve.  Looking forward to it.

Dave,


Try this,


files attached. Works OK in sim.


Z_j13VwnqLFyB1LDhMENzIWOcbM1p2kQHg.PNG


 

const char *t0h = "t0.txt=\"";
byte ter[4] = {0x22, 0xff, 0xff, 0xff};//0x22 is the double quote to end the text content.

String text [10] = {
  "Lorem ipsum dolor sit amet", 
  "consectetur adipiscing elit.",
  "Aliquam vestibulum odio id",
  "augue mollis, in gravida",
  "sapien efficitur. Sed at",
  "commodo justo. Aliquam erat",
  "volutpat. Vivamus in",
  "sollicitudin urna. Aliquam",
  "erat volutpat. Curabitur",
  "dignissim lobortis semper.",
};

void textLoop() {  
  for (int i = 0; i < 10; i ++ ) {   
    
      Serial1.print(t0h);
      Serial1.print(text[i]);
      Serial1.write(ter, 4);//write the 4 bytes of 'ter'
      Serial.print(t0h);//A copy to Serial monitor so you see whats going on :)
      Serial.print(text[i]);
      Serial.write(ter, 4);
      Serial.println();
      delay(1000);    
  }
  textLoop();
}


void setup() {
  
  Serial.begin(115200);
  Serial1.begin(115200);//NodeMcu D4, this is ESP TX1 to Nextion Yellow RX.
  Serial.flush();
  Serial1 .flush();
  //Connect Nextion TX Blue to ESP RX0 (The main RX pin.)
  //We can see the debug in Arduino's SM
  Serial.print("Simple ESP to Nextion Text Communication Test\n\n");
  Serial.print("In 2 seconds you should see 'Hello World' on the display\n\n");
  delay(2000);
  Serial1.print(t0h);
  Serial1.print("");
  Serial1.write(ter, 4);
  Serial1.print(t0h);
  Serial1.print("Hello World");
  Serial1.write(ter, 4);
  Serial.print(t0h);//Same again to Serial monitor
  Serial.print("Hello World");
  Serial.write(ter, 4);
  Serial.println();
  Serial.println();
  delay(5000);
  Serial.print("Now we'll loop through some text strings,\n\n");
  textLoop();
  
}

void loop() {  

}

 


HMI
(27.3 KB)
ino

Thx @indev2 I'll give this a go when I get home. ,.,and Steve's when he has a chance to post it. :-)

Login or Signup to post a comment