Introduction: Writing From Arduino Uno to Nextion
Hi all,
In previous instructables (1, 2, 3) we looked at how to send data from the Nextion to your Arduino Uno. We did this without using the libraries. Now I'd like to show you how you can send commands from the Arduino to a Nextion screen without the libraries. You can write almost any command that we find in the instruction set to the Nextion from the Arduino.
Step 1: Make Your Interface
For this instructable you can just download the interface. There is no code anywhere in the interface. It is much simpler than the previous instructables.
Attachments
Step 2: Connect Your Components
Refer to the picture and connect your Nextion to the Arduino. You should also connect the circuit.
The idea of this circuit is just to have 2 resistors with different values connected in series. In series resistors are voltage dividers. We can measure the different voltages over the resistors and output that reading to the screen. You can also measure ground and +5V to see if they are indeed read as 0 and 1023 by the Arduino.
EDIT: In the past I connected the Nextion to a digital pin for power, but this is not recommended. If possible rather power your Nextion from the 5V pin on the Arduino or an external 5V power supply.
Step 3: Upload the Arduino Sketch
Download the sketch and upload it to your board. I tried to add enough comments to the sketch to make it understandable.
Attachments
Step 4: Test
Play around with your A0 pin's jumper and read different voltages across the circuit. The value on the screen should change accordingly.
1 Person Made This Project!
- sdtag made it!
27 Comments
Question 1 year ago
I know this instructable is a bit older but I am having alot of difficulty understanding the code process for sending data to the Nextion panel. I am new to this but let me explain. I have a very simple code I compiled using various projects to obtain information from a BME280 and convert it and display it. It functions fine with a small OLED screen, however, I cannot for the life of me determine the correct commands to send it to the Nextion. I was able to do a sample project and get a diode to display data. Once i removed the diode information, added the BME280 information nothing worked.
3 years ago
Hi, excellent instructable. You can change the properties of the progress bar thus:
sendThis = "j0.pco="; //Build the part of the string that we know
sendThis.concat(65520); //Add the variable we want to send
writeString(sendThis); //*Use a function to write the message character by character to the Nextion
where pco is the colour of the bar - the above number gives yellow
sendThis = "j0.bco="; //Build the part of the string that we know
if (num>75)
{
sendThis.concat(63488); //Add the variable we want to send
}
else
{
sendThis.concat(1024); //Add the variable we want to send
}
writeString(sendThis); //*Use a function to write the message character by character to the Nextion
and this change the background colour of the bar from green to red when the variable num exceeds 75.
You can alter any of the 'green' properties in the Nextion editor - afaik :-)
Regards
Richard
Reply 3 years ago
Thank you for your comment.
Can you please give me some ideas how to send float to num field.
I tried dtostrf, but it doesn't work neither with num field neither text field.
5 years ago
Love the simplicity of your code. Well done.
I assume 'n0' is on the first page. Is it possible to send value on another Nextion page and if so what is the code?
Reply 5 years ago
Hi,
Can you maybe describe the case in what you want to use it? This will help me to give you the best answer as I have a couple of scenarios running through my head, but I am not sure which one will suit your needs.
Regards,
Gideon
Reply 5 years ago
Hi Gideon,
I think I found out how. - Make simple project with two pages.
On each page put a number box. I have Page0 with n0 and Page1 with n1. Make both number boxes GLOBAL.
Run debug. - whilst on page0 type instruction 'page1.n1,val=100' - I get error.
BUT....... make both pages GLOBAL and type the instruction again and it works. Brilliant.
You can even change other parameters like colours, pictures etc.
Bob
Reply 3 years ago
Just a little note about syntax:
page1.n1.val=100 // separator must be dot, not comma.
Reply 5 years ago
Hi Bob,
That's great! Sorry I couldn't be of more help. Running through some scenarios and ran into the problem that Nextion doesn't have global variables. Luckily I was wrong and as you've pointed out one can change an object from local to global by changing the 'vscope' attribute. Thanks for teaching me something.
Regards,
Gideon
Reply 5 years ago
Hi Gideon,
I have up to 6 WEMOS D1 mini clients sending temperature readings at 5 minute intervals to esp8266 server. I want to display: Sensor no., Date, Time, and Temperature on 3.5 inch Nextion (which I am awaiting delivery).
On second Nextion page I want to add these readings to Waveform plots. I can do this with Nextion editor debug instruction 'add 1,0,25' - where 1 is the waveform id, 0 is the channel and 25 is the temperature. (This will then show temperature history for many hours). But it only works if the plot is on first page.
It would be good to know how to push value/text of text, numbers and waveforms from Arduino/esp8266 using your simplified method to other pages.
Any help will be appreciated.
Thanks,
Bob
Reply 4 years ago
Hi again,
I sent a request from a different account (nukte) asking about your wemos clients sending to an ESP8266 server.
At my work, I would like to send temperatures from several wemos minis to a wemos host, and, while you see all sorts of ideas, most use the internet to upload to a service and then back to the host.
Because my application requires privacy I may not upload to cloud (etc.) but have to use the network inside of the building. So information may flow from wemos to router to host, but not out of the building. Any help is surely appreciated.
Reply 4 years ago
Hi;
I'm working on a project like yours, and wondered if you would allow me to read your code?
Thank you
Reply 4 years ago
Hi Nukte
My temperature project is quite big. It has RTC, SD card, oled as well as connection to Nextion and WiFi to remote sensors. You're welcome to what I have but its not for real beginner. Suggest you send me your PM with what you are seeking.
Will help in any way I can
Bob
4 years ago
Hi Mr.Gideon
Thanks for yours sharing.
I made your project but,
ı created two diffretns page in nextion editor.
First pages is is potentiometer project.
2625525525526255255255262552552552625525525526255255255
2625525525526255255255262552552552625525525526255255255"
why did this ?
Thank you very much for your help.
Reply 4 years ago
MY ARDUINO CODES
#include <SoftwareSerial.h> //
SoftwareSerial mySerial(10, 11); // RX, TX
int KanalBir,KanalBir2,KanalIki,KanalIki2,KanalUc,KanalUc2;
int KanalBirBolme,KanalBirMod,KanalIkiBolme,KanalIkiMod,KanalUcBolme,KanalUcMod,ProgressBar;
int HavaHizAlarmi;
String message;
void setup() {
pinMode(0, INPUT);
pinMode(1,INPUT);
pinMode(2,INPUT);
pinMode(5,OUTPUT);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
Serial.begin(57600);
while (!Serial) { //
;
}
Serial.println("Serial On");
mySerial.begin(9600);
}
void loop() {
Serial.println(message);
while (!mySerial.available()){
KanalBir=analogRead(0);
KanalBir2=map(KanalBir,0,1023,0,50);
KanalBirBolme=KanalBir2/10;
KanalBirMod=KanalBir2%10;
ProgressBar=map(analogRead(0),0,1023,0,100);
//--------------------------------------------------------------
KanalIki=analogRead(1);
KanalIki2=map(KanalIki,0,1023,0,50);
//--------------------------------------------------------------
ProgressBar=map(analogRead(0),0,1023,0,100); //
//--------------------------------------------------------------
if ((analogRead(0)) < HavaHizAlarmi){
digitalWrite (5,HIGH);
delay (250);
digitalWrite (5,LOW);
delay (250);
}
if (message.length() > 14 ){
//-------------------------FAN AÇIK-----------------------------
if (message[3] == '0' && message[4] == '4' ){
delay (10);
digitalWrite(13, HIGH);
message = "";
}
// --------------------FAN KAPALI---------------------------------
if (message[3] == '1' && message[4] == '4' ){
delay (10);
digitalWrite(13, LOW);
message = "";
}
//-------------------------ISIK AÇIK------------------------------------
if ( message[3] == '0' && message[4] == '5' ){
delay(10);
digitalWrite(12, HIGH);
message = "";
}
//-------------------------ISIK KAPALI-------------------------------
if (message[3] == '1' && message[4] == '5' ){
delay (50);
digitalWrite(12, LOW);
message = "";
}
//-----------------------YUKARI HAREKET BAŞLADI----------------------
if (message[4] == '2' && message[5] == '1' ){
digitalWrite(2, HIGH);
delay(5);
message = "";
}
//----------------------YUKARI HAREKET DURDU---------------------------------
if (message[4] == '2' && message[5] == '0' ){
digitalWrite(2, LOW);
message = "";
}
//------------------------AŞAĞI HAREKET BAŞLADI-----------------------------------
if (message[4] == '3' && message[5] == '1' ){
digitalWrite(3, HIGH);
delay(5);
message = "";
}
//---------------------------AŞAĞI HAREKET DURDU-----------------------------
if (message[4] == '3' && message[5] == '0' ){
digitalWrite(3, LOW);
message = "";
}
//---------------------------HAVA HIZ SINIR ALARMI--------------------------------
if (message[4] == '7' && message[5] == '0' ){
HavaHizAlarmi = 150;
}
//------------------GELEN KODLARI FİLTRELEME----------------------------------------
message = "";
}
// --------------------sensör bilgilerini tanımlayıp gönderme---------------------------
//----------------------------------------------------------
if (message[4] != '9' && message[5] != '9' ){
String sendThis = "";
delay(50);
sendThis = "n0.val=";
sendThis.concat(KanalBirBolme);
writeString(sendThis);
message = "";
delay (10);
//----------------------------------------------------------
String sendThis2 = "";
delay(50);
sendThis2 = "n1.val=";
sendThis2.concat(KanalBirMod);
writeString(sendThis2);
message = "";
delay (10);
//----------------------------------------------------------
String sendThis3 = "";
delay(50);
sendThis3 = "n2.val=";
sendThis3.concat(KanalIki2);
writeString(sendThis3);
message = "";
delay (10);
//-----------------------------------------------------------
String sendThis4 = "";
delay(50);
sendThis4 = "j0.val=";
sendThis4.concat(ProgressBar);
writeString(sendThis4);
message = "";
delay (10);
String sendThis5 = "";
//----------------------------------------------------
delay(50);
sendThis5 = "n6.val=";
sendThis5.concat(analogRead(0)); //GEÇİCİ
writeString(sendThis5);
message = "";
delay (10);
//---------------------------------------------------
}
if ( message[0] == '2' ){ // çürük gelen kodlar için filtreleme
message = "";
}
}
while (mySerial.available()){
message.concat(mySerial.read());
}
}
//---------------------SENSÖR DATALATININ SONUNA ONDALIK EKLEME-------------------
void writeString(String stringData) {
for (int i = 0; i < stringData.length(); i++)
{
mySerial.write(stringData[i]);
}
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.write(0xff);
}// end writeString function
Reply 3 years ago
Hi.
I'm surely late to give you my answer. I'm quite sure that you've already find-out in the Nextion Instruction Set that the string 26255255255.... (and so on), starting with 26 that means 1A hex
N° 90x1A2,34Invalid Variable name or attribute0x1A 0xFF 0xFF 0xFFReturned when invalid Variable name or invalid attribute was used
3 years ago
This was amazing for me have spent hours on this as a beginer now i have it working many thanks
3 years ago on Step 4
This is the first time I got to send data to the nextion to work. Thank you great work.
3 years ago
Hi, I like your work. I want to connect the display with atmega128 my transmitter code is working with UART communication but how can I connect this UART code with the TFT display to send hello from atmega128? Please help me.
5 years ago
I like your work very much. Thank you so much.
You sent a variable here, but how is TXT sent?
for example;
When you have a 5v input, you see the "active" TXT on the screen
i try this;
String sendThis = "";
Delay (50);
SendThis = "t1.txt =";
SendThis.concat ("active");
WriteString (sendThis);
Reply 3 years ago
To change text use this:
sendThis = "text1.txt="; //Build the part of the string that we know
sendThis.concat('"'); // ( ' " ')
sendThis.concat("test");
sendThis.concat('"'); // ( ' " ')
writeString(sendThis);