Arduino and Nextion dont communicate

#include "Nextion.h"
#include <DallasTemperature.h>
#include <OneWire.h>

#include <SoftwareSerial.h>

#define NEXTION_SERIAL_RX_PIN 0
#define NEXTION_SERIAL_TX_PIN 1

#define NEXTION_BAUDRATE 9600 // Change this if your Nextion display uses a different baud rate

#define ONEWIRE_PIN_1 9       // Sensor 1 on pin 9
#define ONEWIRE_PIN_2 12      // Sensor 2 on pin 12
#define ONEWIRE_PIN_3 13      // Sensor 3 on pin 13

SoftwareSerial HMISerial(NEXTION_SERIAL_RX_PIN,NEXTION_SERIAL_TX_PIN); 

//nexSerial Serial(10,11);

NexPage page1 = NexPage(1, 1, "second");
NexPage page2 = NexPage(2, 1, "third");

// NexText nexTFurnace = NexText(1, 7, "tfurnace");
// NexText nexTSolar = NexText(1, 5, "tsolar");
// NexText nexTBoiler = NexText(1, 6, "tboiler");

NexButton b0Second = NexButton(1, 8, "b0"); 
NexButton b0Third = NexButton(2, 2, "b0");

NexNumber n0TSolar = NexNumber(2, 3, "n0");
NexNumber n1TFurnace = NexNumber(2, 16, "n1");

NexNumber nexTFurnace = NexNumber(1, 18, "n0");
NexNumber nexTSolar = NexNumber(1, 19, "n1");
NexNumber nexTBoiler = NexNumber(1, 20, "n2");

NexTouch *nex_listen_list[] =
{
  &b0Second,
  &b0Third,
  &n0TSolar,
  &n1TFurnace,
  NULL
};

OneWire ds_1(ONEWIRE_PIN_1);     //sensor 1 na pin 9
OneWire ds_2(ONEWIRE_PIN_2);     //sensor 2 na pin 12
OneWire ds_3(ONEWIRE_PIN_3);     //sensor 3 na pin 13

DallasTemperature sensorSolar(&ds_1);
DallasTemperature sensorBoiler(&ds_2);
DallasTemperature sensorFurnace(&ds_3);

float temp_1; //Solar
float temp_2; //Boiler
float temp_3; //Furnace

float tsolar = 0;
float tkotol = 0;


void tempReading() 
{
  sensorSolar.requestTemperatures();       //Žiadosť
  sensorBoiler.requestTemperatures();
  sensorFurnace.requestTemperatures();

  temp_1 = sensorSolar.getTempCByIndex(0);     //Čítanie
  temp_2 = sensorBoiler.getTempCByIndex(0); 
  temp_3 = sensorFurnace.getTempCByIndex(0); 

  Serial.print(" Solar: ");
  Serial.print(temp_1);
  Serial.print(" Boiler: ");
  Serial.print(temp_2);
  Serial.print(" Furnace: ");
  Serial.print(temp_3);
  Serial.println("");

}

void fromNumberToString()
{

  // Send updated values to the Nextion display
  HMISerial.print("n0.val=");
  HMISerial.print(temp_1);
  nexSerial.write(0xFF);
  nexSerial.write(0xFF);
  nexSerial.write(0xFF);

  HMISerial.print("n1.val=");
  HMISerial.print(temp_2);
  nexSerial.write(0xFF);
  nexSerial.write(0xFF);
  nexSerial.write(0xFF);

  HMISerial.print("n2.val=");
  HMISerial.print(temp_3);
  nexSerial.write(0xFF);
  nexSerial.write(0xFF);
  nexSerial.write(0xFF);
  
  HMISerial.print("tsolar.val= " + String(temp_3)); //aj toto robi
  HMISerial.write(0xFF);
  HMISerial.write(0xFF);
  HMISerial.write(0xFF);

  //Serial.flush();

  // static char temperature3CTemp[4];
  // dtostrf(temp_3, 4, 1, temperature3CTemp);  //So jedným decimálnym číslom
  // nexTFurnace.setText(temperature3CTemp);
  // delay(10);

  // static char temperature1CTemp[4];
  // dtostrf(temp_1, 4, 1, temperature1CTemp);  //So jedným decimálnym číslom
  // nexTSolar.setText(temperature1CTemp);
  // delay(10);

  // static char temperature2CTemp[4];
  // dtostrf(temp_2, 4, 1, temperature2CTemp);  //So jedným decimálnym číslom
  // nexTBoiler.setText(temperature2CTemp);
  // delay(10);
}

void setup() 
{
  Serial.begin(NEXTION_BAUDRATE); //Hardware serial
  nexSerial.begin(NEXTION_BAUDRATE);
  nexInit(); // Library
  HMISerial.begin(NEXTION_BAUDRATE);

  HMISerial.write(0xFF);
  HMISerial.write(0xFF);
  HMISerial.write(0xFF);

  //HMISerial.begin(NEXTION_BAUDRATE); //Software serial // toto apsolutne nist nerobi
  
 
  // nexSerial.write(0xFF);
  // nexSerial.write(0xFF);
  // nexSerial.write(0xFF);
  

  sensorSolar.begin(); //inicializacia senzora
  sensorBoiler.begin();
  sensorFurnace.begin();

  HMISerial.write(0xFF);
  HMISerial.write(0xFF);
  HMISerial.write(0xFF);

  Serial.println("Sensors on!");
}

void loop() 
{
  nexLoop(nex_listen_list);
  tempReading();
  fromNumberToString();

  Serial.println();  

  delay(1000);  

}

This is my code, and I have tried many thing but it just doesnt work... To be honest I was searching through internet for help, but I didnt get to the solution... And after many videos of code, I came to point where I dont understand why I have so many Serials to communicate. Can you guys explain me what do I need to use? Im using ITEADLIB_Arduino_Nextion-master library, I changed the #define nexSerial Serial1 to #define nexSerial Serial, because I use Arduino Uno WiFi. I use Nextion NX8048K070_011C, it is Enhanced version.
Output I get with this code:

15:40:03.354 -> ���bkcmd=1���recvRetCommandFinished err
15:40:03.453 -> page 0���recvRetCommandFinished err
15:40:03.648 -> Sensors on!
15:40:05.838 ->  Solar: 22.00 Boiler: 22.56 Furnace: 22.19
15:40:05.838 -> ���������

Thank you very much, any help is welcome!

does your code even compile?

you commented out

//nexSerial Serial(10,11);

but you use it

also you have connected your screen on the hardware serial port using software serial

and you begin both the hardware and software ones... (and nexSerial)

  Serial.begin(NEXTION_BAUDRATE); //Hardware serial
  nexSerial.begin(NEXTION_BAUDRATE);
  nexInit(); // Library
  HMISerial.begin(NEXTION_BAUDRATE);

which arduino do you have and how did you connect the screen?

Yes it compile, I was just trying to do something with but it is in Nextion.h library in NexConfig.h as #define nexSerial Serial.

Thats what I dont understand, how should I initialization the communication? Which Serial is needed for sending and receiving the data? The first serial is for serial monitor, I know that, but then I get lost with nexSerial and SoftwareSerial.

I have Arduino Uno WiFi ESP8266, I have power supply 6V, 2A, for both, so the GND is same, Arduino TX1 + Nextion RX, Arduino RX1 + Nextion TX.

Btw. the code on ESP8266 works fine, Im sending temperatures to database, now Im working with this display.

what's that ? I've heard of Arduino UNO R4 WiFi but it has an ESP32-S3

ah it's not an Arduino (the company) product

this board is not very much used around here, it's a cumbersome combo with the 2 MCUs

you'll need to keep pins 0 and 1 for Serial
use 2 other pins for SoftwareSerial and configure the nextion to use those

1 Like
#include "Nextion.h"
#include <DallasTemperature.h>
#include <OneWire.h>
#include <SoftwareSerial.h>

#define NEXTION_SERIAL_RX_PIN 2
#define NEXTION_SERIAL_TX_PIN 3
#define NEXTION_BAUDRATE 9600

#define ONEWIRE_PIN_1 9       // Sensor 1 on pin 9
#define ONEWIRE_PIN_2 12      // Sensor 2 on pin 12
#define ONEWIRE_PIN_3 13      // Sensor 3 on pin 13

SoftwareSerial HMISerial(NEXTION_SERIAL_RX_PIN, NEXTION_SERIAL_TX_PIN);

NexText nexTFurnace = NexText(1, 7, "tfurnace");
NexText nexTSolar = NexText(1, 5, "tsolar");
NexText nexTBoiler = NexText(1, 6, "tboiler");

NexPage page1 = NexPage(1, 1, "second");
NexPage page2 = NexPage(2, 1, "third");

NexButton b0Second = NexButton(1, 8, "b0");
NexButton b0Third = NexButton(2, 2, "b0");

NexNumber n0TSolar = NexNumber(2, 3, "n0");
NexNumber n1TFurnace = NexNumber(2, 16, "n1");

// NexNumber nexTFurnace = NexNumber(1, 18, "n0");
// NexNumber nexTSolar = NexNumber(1, 19, "n1");
// NexNumber nexTBoiler = NexNumber(1, 20, "n2");

OneWire ds_1(ONEWIRE_PIN_1);     // Sensor 1 on pin 9
OneWire ds_2(ONEWIRE_PIN_2);     // Sensor 2 on pin 12
OneWire ds_3(ONEWIRE_PIN_3);     // Sensor 3 on pin 13

DallasTemperature sensorSolar(&ds_1);
DallasTemperature sensorBoiler(&ds_2);
DallasTemperature sensorFurnace(&ds_3);

float temp_1; // Solar
float temp_2; // Boiler
float temp_3; // Furnace

void tempReading() 
{
  sensorSolar.requestTemperatures();       
  sensorBoiler.requestTemperatures();
  sensorFurnace.requestTemperatures();

  temp_1 = sensorSolar.getTempCByIndex(0);     
  temp_2 = sensorBoiler.getTempCByIndex(0); 
  temp_3 = sensorFurnace.getTempCByIndex(0); 

  Serial.print(" Solar: ");
  Serial.print(temp_1);
  Serial.print(" Boiler: ");
  Serial.print(temp_2);
  Serial.print(" Furnace: ");
  Serial.println(temp_3);
}
void sendToNextion() 
{
  //Serial.flush();

  static char temperature3CTemp[4];
  dtostrf(temp_3, 4, 1, temperature3CTemp);  //So jedným decimálnym číslom
  nexTFurnace.setText(temperature3CTemp);
  delay(10);

  static char temperature1CTemp[4];
  dtostrf(temp_1, 4, 1, temperature1CTemp);  //So jedným decimálnym číslom
  nexTSolar.setText(temperature1CTemp);
  delay(10);

  static char temperature2CTemp[4];
  dtostrf(temp_2, 4, 1, temperature2CTemp);  //So jedným decimálnym číslom
  nexTBoiler.setText(temperature2CTemp);
  delay(10);
}

void setup() 
{
  Serial.begin(9600); // Start hardware serial for debugging
  HMISerial.begin(NEXTION_BAUDRATE); // Start software serial for Nextion display
  nexInit(); // Initialize Nextion library

  sensorSolar.begin(); // Initialize sensors
  sensorBoiler.begin();
  sensorFurnace.begin();

  Serial.println("Sensors on!");
}

void loop() 
{
  tempReading();
  sendToNextion();
  delay(1000);  
}

This is updated version of code and I get this output:

16:38:49.585 -> ���bkcmd=1���recvRetCommandFinished err
16:38:49.714 -> page 0���recvRetCommandFinished err
16:38:49.876 -> Sensors on!
16:38:52.081 ->  Solar: 23.25 Boiler: 23.69 Furnace: 23.19
16:38:52.113 -> tfurnace.txt="23.2"���recvRetCommandFinished err
16:38:52.178 -> tsolar.txt="23.3"���recvRetCommandFinished err
16:38:52.276 -> tboiler.txt="23.7"���recvRetCommandFinished err

This is a tutorial on your board.
https://forum.arduino.cc/t/atmega328p-esp8266-wifi-tutorial/944380

You need to do the trouble shooting to determine if you have a hardware issue with the Arduino to Nextion connection or if there is an issue with your syntax for the Nextion.

I have Arduino Uno WiFi ESP8266, I have power supply 6V, 2A, for both, so the GND is same, Arduino TX1 + Nextion RX, Arduino RX1 + Nextion TX.

How is 6v being applied to the Arduino? How is the Nextion being powered? Are you certain about the cross connection between the rx/tx wires.

Do you have another Arduino you can use to test the software serial function on your board?

Regarding the syntax of communication to the Nextion there have been many reported issues with using "Nextion.h".

It would be better to use either the methods of Perry Bebbington documented in

https://forum.arduino.cc/t/using-nextion-displays-with-arduino/580289

or alternatively the EasyNextionLibrary by Seithan which is available through the library manager.

https://github.com/Seithan/EasyNextionLibrary/tree/master

You will not find much support on this Arduino forum for using the ITEAD Nextion.h library.

1 Like

Thank you for reply, I will check that out.

I bought in a store a classic power supply, you can change the output of voltage, so I set it to 6V and 2A is needed, Nextion recommend it (Power supply recommend:5V, 2.0A, DC). Im powering them from the same power source so the GND is the same, thats also one of the requirements for communication Arduino-Nextion.
With 6V, 2A, everything works fine, Nextion doesnt blink, and Arduino have its own voltage stabilizer.

Im sure about RX/TX wires, because I tried many times to change them and nothing happened.

Just like J-M-K said in his last reply, Im not using RX0 and TX1 for communication Arduino-Nextion because I need that for serial communication, so I have RX on pin 2 and TX on pin 3.

Yes I have another Arduino, these days I will test it with Arduino UNO (original one) and maybe Arduino Mega.

The Arduino should be powered with either a regulated 5v through the usb port or 5v pin, or a voltage higher than 7v though the barrel jack which will use the 5v onboard regulator.

Software Serial on pins 2 and 3 should be fine.

I will test it with Arduino UNO (original one) and maybe Arduino Mega.

If you test with the Mega, use one of the additional hardware Serial Ports available, and not software serial.

EDIT: I'm a little unclear if in the last code you posted you are trying to send text to a number field, or if you have changed the HMI program.

When I couldnt update text, I tried with numbers, in hmi file both are there so it should be a problem. And Im always using up to date tft file.

Right now Im using Arduino UNO, still doesnt work.

#include <DallasTemperature.h>
#include <OneWire.h>
#include "Nextion.h"
#include <SoftwareSerial.h>

SoftwareSerial NextionSerial(2, 3); // RX, TX

NexPage page0 = NexPage(0, 0, "first");
NexPage page1 = NexPage(1, 0, "second");
NexPage page2 = NexPage(2, 0, "third");

NexText nexTSolar = NexText(1, 5, "tsolar");
NexText nexTBoiler = NexText(1, 6, "tboiler");
NexText nexTFurnace = NexText(1, 7, "tfurnace");    //VYPIS AKO STRING

NexButton b0Second = NexButton(1, 8, "b0");
NexButton b0Third = NexButton(2, 2, "b0");

NexNumber n0TSolar = NexNumber(2, 3, "n0");
NexNumber n1TFurnace = NexNumber(2, 16, "n1");

// NexNumber nexTSolar = NexNumber(1, 17, "n1");
// NexNumber nexTBoiler = NexNumber(1, 18, "n2");
// NexNumber nexTFurnace = NexNumber(1, 19, "n0");  //VYPIS AKO CISLO


//////////////////////////////////////////////////////////////

OneWire ds_1(11);     //sensor 1 na pin 11
OneWire ds_2(12);     //sensor 2 na pin 12
OneWire ds_3(13);     //sensor 3 na pin 13

//Then search every pin for its address and request temps
DallasTemperature sensor_1(&ds_1);
DallasTemperature sensor_2(&ds_2);
DallasTemperature sensor_3(&ds_3);

float temp_1; //Kotao
float temp_2; //Solar
float temp_3; //Bojler

void setup() {

  Serial.begin(9600);
  NextionSerial.begin(9600);
  
  sensor_1.begin(); //iicijalizacija sensora
  sensor_2.begin();
  sensor_3.begin();
  
}

void loop() {
  // Read temperatures from sensors
  float temp_1 = readTemperature(sensor_1);
  float temp_2 = readTemperature(sensor_2);
  float temp_3 = readTemperature(sensor_3);

  // Display temperatures on Nextion display
  displayTemperature(nexTSolar, temp_1);
  displayTemperature(nexTBoiler, temp_2);
  displayTemperature(nexTFurnace, temp_3);

  
  // NextionSerial.write(0xff);
  // NextionSerial.write(0xff);
  // NextionSerial.write(0xff);

  delay(1000);
}

// Function to read temperature from sensor
float readTemperature(DallasTemperature sensor) {
  sensor.requestTemperatures();
  return sensor.getTempCByIndex(0);
}

// Function to display temperature on Nextion display
void displayTemperature(NexText obj, float temp) {
  char buffer[10];
  dtostrf(temp, 4, 1, buffer); // Convert float to char array with 1 decimal place
  Serial.write(buffer); // Set text on Nextion object

  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
}

Output:

18:58:56.219 -> 22.6���22.6���22.4���22.6���22.6���22.4���22.6��� and so on

SoftwareSerial NextionSerial(2, 3); // RX, TX

 // Display temperatures on Nextion display
  displayTemperature(nexTSolar, temp_1);
  displayTemperature(nexTBoiler, temp_2);
  displayTemperature(nexTFurnace, temp_3);
// Function to display temperature on Nextion display
void displayTemperature(NexText obj, float temp) {
  char buffer[10];
  dtostrf(temp, 4, 1, buffer); // Convert float to char array with 1 decimal place
  Serial.write(buffer); // Set text on Nextion object

  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
}

You need to use the software serial instance to communicate with the Nextion.

I changed that after I replied, but it didnt work anyway.
Today was a total failure, tomorrow I will try to do some project that are written step by step, and hopefully it will work. Based on that I will make program for my own project.
I will come back tomorrow, thanks a lot for trying to help me!

I found the solution finally, it was that stupid Nextion library ITEADLIB, just dont use it ever.

void sendToNextionWithoutLibrary() {
    char tempStr[8]; // Buffer for converted temperature strings
    char cmdBuffer[50]; // Buffer for the full command strings

    // Read temperatures
    float temp_1 = sensorSolar.getTempCByIndex(0);
    float temp_2 = sensorBoiler.getTempCByIndex(0);
    float temp_3 = sensorFurnace.getTempCByIndex(0);

    // Convert Solar temperature to string and send
    dtostrf(temp_1, 4, 1, tempStr); // Convert to string with 4 characters long and 1 decimal place
    sprintf(cmdBuffer, "t0.txt=\"%s\"", tempStr); // Format the command string
    HMISerial.print(cmdBuffer);
    HMISerial.write(0xFF); HMISerial.write(0xFF); HMISerial.write(0xFF); // Command terminator
    delay(100); // Short delay to ensure command processing

    // Convert Boiler temperature to string and send
    dtostrf(temp_2, 4, 1, tempStr); // Convert to string
    sprintf(cmdBuffer, "t1.txt=\"%s\"", tempStr); // Format the command string
    HMISerial.print(cmdBuffer);
    HMISerial.write(0xFF); HMISerial.write(0xFF); HMISerial.write(0xFF); // Command terminator
    delay(100); // Short delay

    // Convert Furnace temperature to string and send
    dtostrf(temp_3, 4, 1, tempStr); // Convert to string
    sprintf(cmdBuffer, "t2.txt=\"%s\"", tempStr); // Format the command string
    HMISerial.print(cmdBuffer);
    HMISerial.write(0xFF); HMISerial.write(0xFF); HMISerial.write(0xFF); // Command terminator
    delay(100); // Short delay
}

Hope this help somebody, my temperatures are finally on the display after a month...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.