Read Nextion Display Data

Hello.
I am trying to read data that has been selected on my Nextion Screen but not sure on how to do this :thinking:
I have got my program on the screen working and displaying how i want it, now i just need my Arduino to read this data so it can adjust my LED strips.

One of my pages for the colour values has 3 slider bars:

i have included libraries:

#include <doxygen.h>
#include <NexConfig.h>
#include <NexRadio.h>
#include <NexSlider.h>
#include <Nextion.h>
#include <NexTouch.h>
#include <NexUpload.h>
#include <NexVariable.h>

and have written the setup code for these:

//*** Objects To Recieve data from ***
//Page 1
NexSlider RedBar = NexSlider(1,2,"RedBar");
NexSlider GreenBar = NexSlider(1,5,"GreenBar");
NexSlider BlueBar = NexSlider(1,8,"BlueBar");

//Page 2
NexSlider SpeedBar = NexSlider(2,1,"SpeedBar");

//Page 3
NexRadio Pulse = NexRadio(3,9,"Pulse");
NexRadio COn = NexRadio(3,10,"COn");
NexRadio Cycle = NexRadio(3,1,"Cycle");
NexRadio DOn = NexRadio(3,2,"DOn");

//*** Touch Event Items ***
NexTouch *nex_listen_list[] = {
  //Page 1
  &RedBar,   //Slider added
  &GreenBar, //Slider added
  &BlueBar,  //Slider added
  
  //Page 2
  &SpeedBar, //Slider added
  
  //Page 3
  &Pulse,    //Radio added
  &COn,      //Radio added
  &Cycle,    //Radio added
  &DOn,     //Radio added
};

the part I am unsure on is how to translate the information for these into integer values for the sliders and string values for the radio buttons.

Thanks in advance

I have created a void to read the data from Serial1 of my Arduino Nano, and setup a Serial print so can see on the PC the values it returns, but i am just getting empty values:

void HMI_read_Speed() {

  if(Serial1.available()) {
    char character = "";
    String data_from_display = "";
    delay(30);

    while(Serial1.available()) {
      character = char(Serial1.read());
      data_from_display += character;
    }
    
    Serial.println(data_from_display);
    
  }
}

It isn't on the Nextion side as when debugged the output file was giving a value

Update: I have changed how going about collecting the data, i have setup the code on the nextion editor to send data in a certain format to the arduino:

The code i have for reading this data is this:

// *** Read Data From Screen ***
void Read_LED_Data() {

  if(Serial1.available()) {
    char character = "";
    String data_from_display = "";
    delay(30);

    while(Serial1.available()) {
      character = char(Serial1.read());
      data_from_display += character;
    }

    //Format of Incoming String "A000000" - Letter to determine which Object is sending data & Numbers for value.
    
    //Speed
    if(data_from_display.substring(0,1) == "S") {                     //Check Start of string for Delay Slider Data ("S")
      cycleSpeed = data_from_display.substring(2,7).toInt();          //Convert Rest of string to Numerical Value
    }

    //Colour
    if(data_from_display.substring(0,1) == "R") {                     //Check Start of string for Red Slider Data ("R")
      RedValue = data_from_display.substring(2,7).toInt();            //Convert Rest of string to Numerical Value
    }

    if(data_from_display.substring(0,1) == "G") {                     //Check Start of string for Green Slider Data ("G")
      GreenValue = data_from_display.substring(2,7).toInt();          //Convert Rest of string to Numerical Value
    }

    if(data_from_display.substring(0,1) == "B") {                     //Check Start of string for Blue Slider Data ("B")
      BlueValue = data_from_display.substring(2,7).toInt();           //Convert Rest of string to Numerical Value
    }
  
    //Program
    if(data_from_display.substring(0,1) == "P") {                     //Check Start of string for Pulse Radio Data ("P")
      cabinetProg = "Pulse";                                          //Set Value of cabinetProg to "Pulse"
    }

    if(data_from_display.substring(0,1) == "D") {                     //Check Start of string for COn Radio Data ("D")
      cabinetProg = "On";                                             //Set Value of cabinetProg to "On"
    }

    if(data_from_display.substring(0,1) == "U") {                     //Check Start of string for DOn Radio Data ("U")
      unitProg = "On";                                                //Set Value of unitProg to "DOn"
    }

    if(data_from_display.substring(0,1) == "C") {                     //Check Start of string for Cycle Radio Data ("C")
      unitProg = "Cycle";                                             //Set Value of unitProg to "Cycle"
    }

    //Test Settings updating in serial monitor
    Serial.println("New Settings:-");
    Serial.print(" Loop Delay:");
    Serial.print(cycleSpeed);
    Serial.print(" | RGB Value: ");
    Serial.print(" R: ");
    Serial.print(RedValue); 
    Serial.print(" G: ");
    Serial.print(GreenValue);
    Serial.print(" B: ");
    Serial.print(BlueValue);
    Serial.print(" | Cabinet Program: ");
    Serial.print(cabinetProg);
    Serial.print(" | Unit/Display Program: ");
    Serial.println(unitProg);

}

when first tested this code it was reading fine but when i added to my loop to apply to my LEDs it stopped reading.

my loop code is:

void loop() {
  
  //*** Read LED Program ***
  Read_LED_Data();                                         // Call Read_LED_Data to check for a change between Loops

  // *** Cabinet Program ***
  if(cabinetProg == "Pulse"){  //Pulse Loop
    
    // *** Set Cabinet lights to Pulse and white ***
    for (int i = 255; i > 0; i--) {                        // This loop will Gradually Increase the colour value until Highest value
      for(int i = 0; i < NUM_LEDS_PER_STRIP; i++) {        // This loop will go over each led one at a time
        leds[1][i] = CRGB::White;
      } 
      FastLED.show();
      delay(10);
    }
  
    for (int i = 0; i < 256; i++) {                       // This loop will Gradually lower the colour value until Lowest value
        for(int i = 0; i < NUM_LEDS_PER_STRIP; i++) {     // This loop will go over each led one at a time
          leds[1][i] = CRGB::Black;
        } 
        FastLED.show();
        delay(10);
    }
  
  } else { //On
    
    // *** Set Cabinet lights to on and white ***
    for(int i = 0; i < NUM_LEDS_PER_STRIP; i++) {         // This loop will go over each led one at a time
      leds[1][i] = CRGB::White;
      FastLED.show();
    }
     
  }

  
  // *** Unit/Display Program ***
  if(unitProg == "Cycle"){ //Cycle Loop
    
    for(int i = 0; i < NUM_LEDS_PER_STRIP; i++) {         // This loop will go over each led one at a time
      
      //*** Read Display Data ***
      Read_LED_Data();                                    // Call Read_LED_Data to check for a change between LEDs
      
      // *** Display LED ***
        leds[0][i] = CRGB(RedValue,BlueValue,GreenValue); //Set Selected Colour
        FastLED.show();
        leds[0][i] = CRGB::Black;
        delay(cycleSpeed); // Selected Delay
  
    }
    
  } else { //On

    // *** Set Unit lights to on***
    for(int i = 0; i < NUM_LEDS_PER_STRIP; i++) {         // This loop will go over each led one at a time
      leds[0][i] = CRGB(RedValue,BlueValue,GreenValue);   //Set Selected Colour
      FastLED.show();
    }
    
  }
  
}

i have minimised the library to:

// *** Libraries ***
#include <FastLED.h>
#include <Wire.h>

and my setup to:

// *** Setup LEDs ***
#define NUM_STRIPS 2
#define NUM_LEDS_PER_STRIP 32
CRGB leds[NUM_STRIPS][NUM_LEDS_PER_STRIP];

// *** Startup Speed / Colour / Program ***
int RedValue =0;
int GreenValue =255;
int BlueValue =0;
int cycleSpeed =100;
String cabinetProg = "On";
String unitProg = "Cycle";


void setup() {
  
  //*** Setup Monitor ***
  Serial.begin(9600);
  Serial1.begin(9600);


  // *** Setup LEDs ***
  FastLED.addLeds<NEOPIXEL, 2>(leds[0], 32); // No. LEDs on pin 2 (TV Display = 32, Sofa Shelf = 24) 
  FastLED.addLeds<NEOPIXEL, 4>(leds[1], 18); // No. LEDs on pin 4 (TV Cabinet & Sofa Cabinet = 18) 

  // *** Set Cabinet lights to on and white ***
  for(int i = 0; i < NUM_LEDS_PER_STRIP; i++) {
    leds[1][i] = CRGB::White;
    FastLED.show();
  }  

}

Problem solved, when i did some rewiring i had put my data wire to an empty row :upside_down_face:

Data is being read now

Hello,
Welcome.
Glad you got it sorted.

The thing you called a 'void' is actually called a function. The word at the left side before the function name is the type of variable the function returns, with void meaning it does not return anything.

Ahh so its a bit like the functions used in Visual basic? just formatted differently?

Sorry I didn't know VB so can't say.

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