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.
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 .