# Comunication data ESP32 with Nextion Display

**URL:** <https://forum.arduino.cc/t/comunication-data-esp32-with-nextion-display/1133085>\
**Category:** General Guidance\
**Created:** [May 31, 2023, 2:33pm UTC](https://forum.arduino.cc/t/comunication-data-esp32-with-nextion-display/1133085 "2023-05-31T14:33:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![satrioadyt](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/satrioadyt/32/969334_2.png) [@satrioadyt](https://forum.arduino.cc/u/satrioadyt)\
**Post date:** [May 31, 2023, 2:33pm UTC](https://forum.arduino.cc/t/comunication-data-esp32-with-nextion-display/1133085/1 "2023-05-31T14:33:00Z")

</div>

```arduino
#include "Nextion.h"
// #include <Robojax_AllegroACS_Current_Sensor.h>
#include <SPI.h>
// #include <loraid.h>
#define MODE_LONG_RANGE_MODE 0x80

long interval = 5000; // 10 s interval to send message
long previousMillis = 0; // will store last time message sent
unsigned int counter = 0; // message counter

uint32_t next, myInt = 0;
int currentpage;

///////12 VOLT 1
const int voltageSensorPin = A2; // sensor pin
float vIn; // measured voltage (3.3V = max. 16.5V, 5V = max 25V)
float vOut;
float voltageSensorVal; // value on pin A3 (0 - 1023)
const float factor = 3.35; // reduction factor of the Voltage Sensor shield
const float vCC = 5.0; // Arduino input voltage (measurable by voltmeter)
int bat_percentage;
int prevbat_percent = 0;
int millisBegin = 0;
int millisNow;
int millisBorder = 1000;

///////60 VOLT
float correctionfactor = .5;
int analogInput = A0;
float vout60 = 0.0;
float vin60 = 0.0;
float R1 = 30000 + 68000;
float R2 = 7500;
int value = 0;
int bat_percent3;

///////12 VOLT 2
// const int voltageSensorPin2 = A1; // sensor pin
float vIn2; // measured voltage (3.3V = max. 16.5V, 5V = max 25V)
float vOut2;
float voltageSensorVal2; // value on pin A3 (0 - 1023)
const float factor2 = 4.92; // reduction factor of the Voltage Sensor shield
const float vCC2 = 5.00; // Arduino input voltage (measurable by voltmeter)
int bat_percentage2;

////ACS758
const int VIN = A3; // define the Arduino pin A0 as voltage input (V in)
const float VCC = 5.04;// supply voltage
const int MODEL = 5; // enter the model
// Robojax_AllegroACS_Current_Sensor robojax(MODEL,VIN);
float curr1;

NexPage p0 = NexPage(0,0,"page0");
NexPage p1 = NexPage(1,0,"page1");
NexPage p2 = NexPage(2,0,"page2");
NexPage p3 = NexPage(3,0,"page3");
NexPage p4 = NexPage(4,0,"page4");

NexPicture p0_p1 = NexPicture(0,1,"p0");
NexPicture p0_p2 = NexPicture(0,2,"p0");
NexPicture p0_p3 = NexPicture(0,3,"p0");
NexButton p0_b0 = NexButton(0,6,"b0");

NexButton p1_b0 = NexButton(1,6,"b0");

NexButton p2_b0 = NexButton(2,6,"b0");

NexButton p3_b1 = NexButton(3,2,"b1");
NexButton p3_b2 = NexButton(3,3,"b2");

NexButton p4_b1 = NexButton(4,2,"b1");
NexButton p4_b3 = NexButton(4,3,"b3");

NexTouch *nex_listen_list[] = {
  
 
  &p0,
  &p1,
  &p2,
  &p3,
  &p4,
  &p0_p1,
  &p0_p2,
  &p0_p3,
  &p0_b0,
  &p1_b0,
  &p2_b0,
  &p3_b1,
  &p3_b2,
  &p4_b1,
  &p4_b3,
  
  NULL
};

void p0PopCallback(void *ptr)
{
    p0.show();
}

void p1PopCallback(void *ptr)
{
    p1.show();
}

void p2PopCallback(void *ptr)
{
    p2.show();
}

void p3PopCallback(void *ptr)
{
    p3.show();
}

void p4PopCallback(void *ptr)
{
    p4.show();
}

void p0_p1PopCallback(void *ptr)
{
    uint32_t number = 0;

    p0_p1.getPic(&number);

    if (number == 1)
    {
        number = 2;    
    }
    else
    {
        number = 1;
    }
    
    p0_p1.setPic(number);
}

void p0_p2PopCallback(void *ptr)
{
    uint32_t number = 0;

    p0_p2.getPic(&number);

    if (number == 1)
    {
        number = 2;    
    }
    else
    {
        number = 1;
    }
    
    p0_p2.setPic(number);
}

void p0_p3PopCallback(void *ptr)
{
    uint32_t number = 0;

    p0_p1.getPic(&number);

    if (number == 1)
    {
        number = 2;    
    }
    else
    {
        number = 1;
    }
    
    p0_p3.setPic(number);
}
    
void p0_b0_Release(void *ptr) {
  p3.show();
  currentpage=3;
}

void p1_b0_Release(void *ptr) {
  p3.show();
  currentpage=3;
}

void p2_b0_Release(void *ptr) {
  p3.show();
  currentpage=3;
  
}

void p3_b1_Release(void *ptr) {
  p0.show();
  currentpage=0;
}
void p3_b2_Release(void *ptr) {
  p4.show();
  currentpage=4;
}
void p4_b1_Release(void *ptr) {
  p3.show();
  currentpage=3;
}

void p4_b3_Release(void *ptr) {
  p3.show();
  currentpage=3;
}

//void do_every_so_often() {
// myInt = (myInt + 1) % 101;
// p0_n0.setValue(myInt);
//}

void setup() {

  pinMode(voltageSensorPin, INPUT);
  pinMode(voltageSensorPin2, INPUT);
  pinMode(analogInput, INPUT);

  nexInit();
  p0_p1.attachPop(p0_p1PopCallback);
  p0_p2.attachPop(p0_p2PopCallback);
  p0_p3.attachPop(p0_p3PopCallback);
  p0_b0.attachPop(p0_b0_Release, &p0_b0);
  
  p1_b0.attachPop(p1_b0_Release, &p1_b0);
  
  p2_b0.attachPop(p2_b0_Release, &p2_b0);
  
  p3_b1.attachPop(p3_b1_Release, &p3_b1);
  p3_b2.attachPop(p3_b2_Release, &p3_b2);
  p4_b1.attachPop(p4_b1_Release, &p4_b1);
  p4_b3.attachPop(p4_b3_Release, &p4_b3);
  

  Serial.begin(9600);
  Serial2.begin(9600);
  //dbSerial.begin(9600);
  
  uint8_t active_page = 0;
  int currentpage = 0;

  lora.init();
  lora.setDeviceClass(CLASS_A);
  lora.setDataRate(2);
  lora.setAccessKey("aa5531c60d6c9e1d:11aee152dcec42ee");
  lora.setDeviceId("7a2b62ef");
  
}

/////MAPFLOAT
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

float mapfloat2(float x2, float in_min2, float in_max2, float out_min2, float out_max2)
{
  return (x2 - in_min2) * (out_max2 - out_min2) / (in_max2 - in_min2) + out_min2;
}

float mapfloat3(float x3, float in_min3, float in_max3, float out_min3, float out_max3)
{
  return (x3 - in_min3) * (out_max3 - out_min3) / (in_max3 - in_min3) + out_min3;
}

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

void HMICommandEnd() {
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);
};

void loop() {

  char myStr2[175];
  char x[6], y[6], v[6], z[6], u[6], r[6];
  char outStr[200]; 
  int recvStatus = 0; 
  unsigned long currentMillis = millis();

  
  nexLoop(nex_listen_list);
  if(millis() >= next) {
    next = millis()+500;
    //do_every_so_often();
  }

    //////////ACS712 20A1
    int adc = analogRead(A4);
    float voltage2 = adc * 5 / 1023.0;
    float current2 = (voltage2 - 2.5) / 0.100;

    //////////ACS712 5A
    int adc2 = analogRead(A5);
    float voltage3 = adc * 5 / 1023.0;
    float current3 = (voltage3 - 2.5) / 0.185;

    ///////////POWER 1
    float power1 = vin60 * robojax.getCurrent();

    ///////////POWER 2
    float power2 = vIn * current2;

    ///////////POWER 3
    float power3 = vIn2 * current3;

  if (currentMillis - previousMillis > interval) {
    
 /////12 VOLT1
    voltageSensorVal = analogRead(voltageSensorPin); // read the current sensor value (0 - 1023) 
    vOut = (voltageSensorVal / 1024) * vCC; // convert the value to the real voltage on the analog pin
    vIn = vOut * factor; // convert the voltage on the source by multiplying with the factor
    bat_percentage = mapfloat(vIn, 11.5, 12.65, 0, 100);
   
    
    if (bat_percentage >=100)
    {
    bat_percentage = 100;
    }
    if (bat_percentage <= 0)
    {
    bat_percentage = 1;
    }

    //////12 VOLT1

    ///////60 VOLt
    value= analogRead(analogInput);
    vout60 = (value*4.9735)/1023.0;
    vin60 = vout60/(R2/(R1+R2));

    vin60 = vin60 - correctionfactor;
    bat_percent3 = mapfloat3(vin60, 52.50, 65.00, 0, 100);
   
    
    if (bat_percent3 >=100)
    {
    bat_percent3 = 100;
    }
    if (bat_percent3 <= 0)
    {
    bat_percent3 = 1;
    }

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

    
    //////////12 VOLT2
    voltageSensorVal2 = analogRead(voltageSensorPin2); // read the current sensor value (0 - 1023) 
    vOut2 = (voltageSensorVal2 / 1024) * vCC2; // convert the value to the real voltage on the analog pin
    vIn2 = vOut2 * factor2; // convert the voltage on the source by multiplying with the factor
    bat_percentage2 = mapfloat2(vIn2, 11.5, 12.65, 0, 100);
   
    
    if (bat_percentage2 >=100)
    {
    bat_percentage2 = 100;
    }
    if (bat_percentage2 <= 0)
    {
    bat_percentage2 = 1;
    }

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

    /////////CURRENT1
    curr1 = float(robojax.getCurrent(),2);
    Serial.print("Current: ");
    Serial.print(robojax.getCurrent(),2); // print the current with 3 decimal places
    Serial.println("A");
    Serial.print("\n");
   //robojax.debug();

    //////////CURRENT2
    Serial.print("Current2: ");
    Serial.print(current2,2);
    Serial.println("A");
    Serial.print("\n");

    //////////CURRENT3
    Serial.print("Current3: ");
    Serial.print(current3,2);
    Serial.println("A");
    Serial.print("\n");

    ///////////POWER 1

    Serial.print("Power1: ");
    Serial.print(power1);
    Serial.println("W");
    Serial.print("\n");

    ///////////POWER 2

    Serial.print("Power2: ");
    Serial.print(power2);
    Serial.println("W");
    Serial.print("\n");

    ///////////POWER 3

    Serial.print("Power3: ");
    Serial.print(power3);
    Serial.println("W");
    Serial.print("\n");

    
    //////////////////////////
    Serial.print("60Batt V= ");
    Serial.println(vin60,2);
    
    Serial.print("Percentage= ");
    Serial.print(bat_percent3);
    Serial.println(" %");
    Serial.print("\n ");

    
    Serial.print("12Batt1 V: ");
    Serial.print(vIn);
    Serial.println(" V");   
    
    
    Serial.print("Percentage= ");
    Serial.print(bat_percentage);
    Serial.println(" %");
    Serial.print("\n ");

    Serial.print("12Batt2 V: ");
    Serial.print(vIn2);
    Serial.println(" V");   
    
    Serial.print("Percentage= ");
    Serial.print(bat_percentage2);
    Serial.println(" %");
    Serial.print("\n");

    dtostrf(vIn, 2, 1, x);
    dtostrf(bat_percentage, 2, 0, y);
    dtostrf(vIn2, 2, 1, v);
    //dtostrf(bat_percentage2, 2, 0, w);
    dtostrf(vin60, 2, 1, z);
    //dtostrf(bat_percent3, 2, 0, t);
    dtostrf(curr1, 2, 1, u);
    dtostrf(current2, 2, 1, r);
    
    
    previousMillis = currentMillis;

    sprintf(myStr2, "{\"1\":%s, \"2\":%s, \"3\":%s, \"4\":%s, \"5\":%s, \"6\":%s}",
            x, y, v, z, u, r, counter);
    Serial.println(myStr2);
    lora.sendToAntares((unsigned char *)myStr2, strlen(myStr2), 0);

  }

  recvStatus = lora.readData(outStr);
  if (recvStatus) {
    Serial.println(outStr);
  }
  lora.update();

/////NEXTION

     if(currentpage==0){
      
    int newbat = bat_percentage;
      if (newbat > prevbat_percent + 2){
          Serial.println("Charging");
          prevbat_percent = newbat;
          millisNow = millis();

        p0_p2.setPic(7);
        currentpage=0;
    }
    
    else {
       p0_p1.setPic(6);
    }
      
       if(millis() - newbat > 3000){
       prevbat_percent = newbat;
       newbat = millis();
     }

     if (bat_percentage<=20){
          Serial.println("Low");
     // p2.show();
      //currentpage=2;
        p0_p3.setPic(8);
        
    }
    
    else {
        p0_p1.setPic(6);
    }
    Serial.print("\n");
    
     //String voltin = "n1.val=\""+String(vIn,4)+"\"";
     //Serial2.print(voltin);
     Serial2.print("t1.txt=\"");
     Serial2.print(vIn);
     Serial2.print("\"");
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     
     Serial2.print("t2.txt=\"");
     Serial2.print(vIn2);
     Serial2.print("\"");
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     nexSerial.write(0xff);

     Serial2.print("t3.txt=\"");
     Serial2.print(vin60);
     Serial2.print("\"");
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     
     Serial2.print("t0.txt=\"");
     Serial2.print(bat_percentage);
     Serial2.print("\"");
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     nexSerial.write(0xff);

     Serial2.print("t4.txt=\"");
     Serial2.print(bat_percentage2);
     Serial2.print("\"");
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     nexSerial.write(0xff);

     Serial2.print("t5.txt=\"");
     Serial2.print(bat_percent3);
     Serial2.print("\"");
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     nexSerial.write(0xff);

     Serial2.print("t6.txt=\"");
     Serial2.print(curr1);
     Serial2.print("\"");
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     
     Serial2.print("t7.txt=\"");
     Serial2.print(power1);
     Serial2.print("\"");
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     nexSerial.write(0xff);

     Serial2.print("t8.txt=\"");
     Serial2.print(current2);
     Serial2.print("\"");
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     nexSerial.write(0xff);

     Serial2.print("t9.txt=\"");
     Serial2.print(power2);
     Serial2.print("\"");
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     nexSerial.write(0xff);

     Serial2.print("t10.txt=\"");
     Serial2.print(current3);
     Serial2.print("\"");
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     nexSerial.write(0xff);

     Serial2.print("t11.txt=\"");
     Serial2.print(power3);
     Serial2.print("\"");
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     
     HMICommandEnd();
     }

     if(currentpage==1){
     Serial2.print("t1.txt=\"");
     Serial2.print(vIn);
     Serial2.print("\"");
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     
     Serial2.print("t2.txt=\"");
     Serial2.print(vIn2);
     Serial2.print("\"");
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     nexSerial.write(0xff);

     Serial2.print("t3.txt=\"");
     Serial2.print(vin60);
     Serial2.print("\"");
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     
     Serial2.print("t0.txt=\"");
     Serial2.print(bat_percentage);
     Serial2.print("\"");
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     HMICommandEnd();
     }
     
     if(currentpage==2){
     Serial2.print("t1.txt=\"");
     Serial2.print(vIn);
     Serial2.print("\"");
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     
     Serial2.print("t2.txt=\"");
     Serial2.print(vIn2);
     Serial2.print("\"");
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     nexSerial.write(0xff);

     Serial2.print("t3.txt=\"");
     Serial2.print(vin60);
     Serial2.print("\"");
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     
     Serial2.print("t0.txt=\"");
     Serial2.print(bat_percentage);
     Serial2.print("\"");
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     HMICommandEnd();
     }

     if(currentpage==3){
     Serial2.print("t1.txt=\"");
     Serial2.print(vIn);
     Serial2.print("\"");
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     
     Serial2.print("t0.txt=\"");
     Serial2.print(bat_percentage);
     Serial2.print("\"");
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     nexSerial.write(0xff);

     Serial2.print("t2.txt=\"");
     Serial2.print(vIn2);
     Serial2.print("\"");
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     nexSerial.write(0xff);

     Serial2.print("t4.txt=\"");
     Serial2.print(bat_percentage2);
     Serial2.print("\"");
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     
     Serial2.print("t3.txt=\"");
     Serial2.print(vin60);
     Serial2.print("\"");
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     nexSerial.write(0xff);

     Serial2.print("t5.txt=\"");
     Serial2.print(bat_percent3);
     Serial2.print("\"");
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     HMICommandEnd();

     }

     if(currentpage==4){
     Serial2.print("t6.txt=\"");
     Serial2.print(curr1);
     Serial2.print("\"");
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     nexSerial.write(0xff);

     Serial2.print("t7.txt=\"");
     Serial2.print(current2);
     Serial2.print("\"");
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     nexSerial.write(0xff);

     Serial2.print("t8.txt=\"");
     Serial2.print(current3);
     Serial2.print("\"");
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     nexSerial.write(0xff);
 
     Serial2.print("t9.txt=\"");
     Serial2.print(power1);
     Serial2.print("\"");
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     nexSerial.write(0xff);

     Serial2.print("t10.txt=\"");
     Serial2.print(power2);
     Serial2.print("\"");
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     nexSerial.write(0xff);

     Serial2.print("t11.txt=\"");
     Serial2.print(power3);
     Serial2.print("\"");
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     nexSerial.write(0xff);
     HMICommandEnd();
     }
     
    delay(5000);
    
    nexLoop(nex_listen_list);
    }

```

why the nexPage, Nexbutton etc. always error ? althought the nextion libraries has installed. I hope in this forum guys for my final project.

---

<div class="post-metadata">

**Author:** ![Railroader](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/railroader/32/274550_2.png) [@Railroader](https://forum.arduino.cc/u/Railroader)\
**Post date:** [May 31, 2023, 2:48pm UTC](https://forum.arduino.cc/t/comunication-data-esp32-with-nextion-display/1133085/2 "2023-05-31T14:48:13Z")

</div>

Please post the error message.

---

<div class="post-metadata">

**Author:** ![Whandall](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/whandall/32/222457_2.png) [@Whandall](https://forum.arduino.cc/u/Whandall)\
**Post date:** [May 31, 2023, 3:22pm UTC](https://forum.arduino.cc/t/comunication-data-esp32-with-nextion-display/1133085/3 "2023-05-31T15:22:40Z")

</div>

Remember to use a voltage divider on the Nextion TX pin,  
the Nextion is a 5V device, the ESP32 is 3.3V.

---

<div class="post-metadata">

**Author:** ![hammy](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/hammy/32/238813_2.png) [@hammy](https://forum.arduino.cc/u/hammy)\
**Post date:** [May 31, 2023, 9:38pm UTC](https://forum.arduino.cc/t/comunication-data-esp32-with-nextion-display/1133085/4 "2023-05-31T21:38:45Z")

</div>

I thought there was code you loaded to the Nextion via an SD card, then code you ran on the Arduino to communicate to the display .  
Do we have both here ?

Have you run example codes ? Accounted for the different serial port numbering?  
( google “esp32 nextion”)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [November 27, 2023, 9:39pm UTC](https://forum.arduino.cc/t/comunication-data-esp32-with-nextion-display/1133085/5 "2023-11-27T21:39:16Z")

</div>

Closed on November 27, 2023, 9:39pm UTC

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