1

Q: Is it possible to send data in variables from Arduino to Nextion through SerialPrint? If so, what is the correct syntax?

I have an Arduino Uno connected to a Nextion NX4024T032 and a Sunfounder RC522, with a USB run from my PC into the Uno. I want the Nextion to show the name associated with an RFID card from simple if/then statements in the .ino file.

I created what I thought was a simple set of .ino and .tft files to test the Nextion updating from data sent from the Arduino by serial print. I have confirmed in Serial Monitor on the PC that the RFID reader is functioning and that the Uno is sending out the information I am asking it to print. But the Nextion is not changing text to match what I am trying to send it. It just displays the value entered for .txt in the HMI editor.

I am trying to do this without nextion libraries.

Pinout for the RC522 is as in the .ino below (9-13); plus GND and 3.3V on the Uno.

Pinout for the Nextion is Nextion TX to Uno 1, Nextion RX to Uno 0, plus GND and 5V on the Uno.

Here's the .ino:

/*
 *-----------------------------------------------------
 * Aggiunti pin per arduino Mega
 * add pin configuration for arduino mega
 * http://mac86project.altervista.org/
 ---------------------------------------------------Nicola Coppola
 * Pin layout should be as follows:
 * Signal     Pin              Pin               Pin
 *            Arduino Uno      Arduino Mega      MFRC522 board
 * ------------------------------------------------------------
 * Reset      9                5                 RST
 * SPI SS     10               53                SDA
 * SPI MOSI   11               51                MOSI
 * SPI MISO   12               50                MISO
 * SPI SCK    13               52                SCK
 */

#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.

void setup() {
    Serial.begin(9600); // Initialize serial communications with the PC
    SPI.begin();            // Init SPI bus
    mfrc522.PCD_Init(); // Init MFRC522 card
    Serial.println("Scan PICC to see UID and type...");
}

void loop() {

//condensed to remove RFID code

  if (UID_string == "4294935680")
  {
    Serial.println("va0.txt=\"Maia\"");
  }

  if (UID_string == "31615")
  {
    Serial.println("va0.txt=\"Claire\"");
  }

  if (UID_string == "24498")
  {
    Serial.println("va0.txt=\"Jack\"");
  }
}

The .tft I loaded to the Nextion using an SD card is just a text box (t0) on page0, a timer event and a value. The timer should update t0.txt on the display to equal most recent va0.txt in the serial print every 400 ms.

tm0

va0

to

UPDATE:

While trying to understand syntax for commands to send from Arduino Uno to Nextion, I came across directions for sending data a character at a time. So, a very new version below, with all RFID parts removed.

 * Pin layout should be as follows:
 * Signal     Pin              Pin               Pin
 *            Arduino Uno      Arduino Mega      MFRC522 board
 * ------------------------------------------------------------
 * Reset      9                5                 RST
 * SPI SS     10               53                SDA
 * SPI MOSI   11               51                MOSI
 * SPI MISO   12               50                MISO
 * SPI SCK    13               52                SCK
 */

#include <SPI.h>
#include <SoftwareSerial.h>
#define SS_PIN 10
#define RST_PIN 9
SoftwareSerial mySerial(0,1);
   
void setup() {
    Serial.begin(9600); // Initialize serial communications with the PC
    SPI.begin();            // Init SPI bus    

  while (!Serial) { // wait for serial port to connect. Needed for native USB port only
    ;
  }

  Serial.println("Serial On"); //Print this messages when the serial port is connected
  mySerial.begin(9600); // set the data rate for the SoftwareSerial port
  
}

//NOTE:Credit to RamjetX : http://forum.arduino.cc/index.php?topic=89143.0 

void writeString(String stringData) { // Used to serially push out a String with Serial.write()

  for (int i = 0; i < stringData.length(); i++)
  {
    mySerial.write(stringData[i]);   // Push each char 1 by 1 on each loop pass  
  }

  mySerial.write(0xff); //We need to write the 3 ending bits to the Nextion as well
  mySerial.write(0xff); //it will tell the Nextion that this is the end of what we want to send.
  mySerial.write(0xff);

} // end writeString function

void loop() {
  String sendThis = "";
  sendThis = "t0.txt=\"working\""; //Declare and initialise the string we will send

  delay(300); //

  writeString(sendThis); // Use a function to write the message character by character to the Nextion because
  mySerial.write(sendThis) gives you an error due to a datatype mismatch*/
}

Conclusion: The SerialPrintln() statements still send text to the serial monitor, but mySerial.write does not show in the serial monitor and the Nextion screen still does not update to show "working" as passed in t0.txt .

7
  • I have condensed the RFID code and added explicit questions at the top of the post. Please clarify what 'fixed text' would look like? Commented Dec 7, 2020 at 20:09
  • Thank you for the direction and replies. I am pretty sure I want to use variables if possible, because later I want to use the Nextion to show multiple buttons showing text strings coming from a MySQL cloud-based db. My understanding is that Nextion is useful for storing much of what will be displayed on screen (.e.g, graphics), and so my plan is to send variables to Nextion and change only the .txt values of the buttons. At the moment, I am simply trying to get started with Arduino directing Nextion at all, while keeping in mind the pins that will be used by the RFID module. Commented Dec 7, 2020 at 20:23
  • I see your point - well said. I will take another run at it and post an update. Thanks again. Commented Dec 7, 2020 at 20:28
  • use software serial for the Nextion ... see software serial example sketch that passes input from serial monitor to the device ... type the Nextion comnands into serial monitor Commented Dec 7, 2020 at 20:29
  • read the instruction set info at the Nextion website ... you missed something Commented Dec 7, 2020 at 21:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.