Start a new topic

Arduino Variable created from Nextion Text box

Hi, 


Apologies if this has already been covered, but a few hours of searching and testing and I'm still not getting the desired effect.


The Setup - 

I have keyboard in a hmi file, that populates a text box called data

The Enter button on that keyboard (called b_crlf) 


I want to take that data text box (as a string of text) and populate a variable inside my Arduino Mega, so that I can pass that variable into an LCD display when Enter button is pushed.


The actual event of sending text to the LCD on a button push is easy - however I am having no luck getting the text from the DATA box into an Arduino variable.


Any Ideas?


My current Arduino code is below

  

#include <Nextion.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C  lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // 0x27 is the I2C bus address for an unmodified backpack

void b_crlfPopCallback(void *ptr);


//Nextion Assign Buttons
NexButton b_crlf = NexButton(1, 23, "b_crlf"); //Enter Button

//Nextion Assign Text
NexText data = NexText(1, 2, "data"); //Data Text Field

NexTouch *nex_listen_list[] = 
{
    &b_crlf,
    NULL
};

void b_crlfPopCallback(void *ptr)
{

  lcd.home (); // set cursor to 0,0
  lcd.clear();
  lcd.print("???"); 
  };

void setup() {
  //Setup the Touchscreen - MUST BE BEFORE SERIAL.BEGIN!
  nexInit();
 
  //Setup Serial Connection
 Serial.begin(9600);

 //Attach button actions
  b_crlf.attachPop(b_crlfPopCallback,&b_crlf);
 

//Setup LCD Screens
 lcd.begin (16,2); // for 16 x 2 LCD module
 lcd.setBacklightPin(3,POSITIVE);
 lcd.setBacklight(HIGH);

  
}
  void loop(){
    nexLoop(nex_listen_list);
  };

  




Nevermind everyone - I've fixed this one myself. You have to make sure that the Nextion is being powered by the Arduino (not an external source!) or your Serial will say recvRetString[0,] 



for anyone who is interested this is my final code


 

#include <Nextion.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C  lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // 0x27 is the I2C bus address for an unmodified backpack

void b_crlfPopCallback(void *ptr);


//Nextion Assign Buttons
NexButton b_crlf = NexButton(1, 23, "b_crlf"); //Enter Button

//Nextion Assign Text
NexText data1 = NexText(1, 2, "data"); //Data Text Field

char text_char[17];


NexTouch *nex_listen_list[] = 
{
    &b_crlf,
    NULL
};

void b_crlfPopCallback(void *ptr)
{
  data1.getText(text_char,17);
  Serial.print (text_char);

  lcd.home (); // set cursor to 0,0
  lcd.clear();
  lcd.print(text_char); 
  };

void setup() {
  //Setup the Touchscreen - MUST BE BEFORE SERIAL.BEGIN!
  nexInit();
 
  //Setup Serial Connection
 Serial.begin(9600);

 //Attach button actions
  b_crlf.attachPop(b_crlfPopCallback,&b_crlf);
 

//Setup LCD Screens
 lcd.begin (16,2); // for 16 x 2 LCD module
 lcd.setBacklightPin(3,POSITIVE);
 lcd.setBacklight(HIGH);

  
}
  void loop(){
    nexLoop(nex_listen_list);
  };

 

PS: When you check the library code (and everyone should check the code)
NexHardware.cpp:  Serial is already initialized with dbSerialBegin in nexInit()


Thanks, that's good to know. I'm slowly working my way through - although I tend to be a bit more 'dump a lot of code in and make it work' when I get an idea in my head. Your articles have been a definite help thus far.


Cheers.

It's not forcibly needed to power the Arduino and the Nextion from the same supply. But in case of separate supplies, you'll need to connect the GND of both together, so that they have the same reference potential. Thus, 3 wires are needed: TX -> RX, RX -> TX, GND -> GND


I'm not working with Arduino but with processors powered @3.3V while I power the Nextion @5V and the 3 wire connection as above works without problems.

Thats also good to know thanks, I had a feeling it had something to do with the Ground pin. Thanks for your input. Cheers

my Nextion is powered by arduino i still got  recvRetString[0,]

Good.  You have told one detail, left many out.

 - does setup() send ok or err to Serial Monitor?

If ok, timing issue.

image


it is working after adding a small delay

bushra - stop double posting everywhere


I am not going to type the same answer in every place.

- timing issue in code.

Login or Signup to post a comment