Arduino Mega+ Nextion Screen- Q about serial communication

Hey everyone! I have a Nextion screen 7' connected with an arduino Mega 2560 and I wanna communicate them by a serial portI; I've tried many libraries but the don't work fine or they are too simple for my application.
There are many examples in this forum where u change of pages or text and I was wonder how can I can do the same with pictures.

I show u the c

if(digitalRead(53)==HIGH){
  Serial1.print("p0.pic=16");  
  Serial1.write(0xff);
  Serial1.write(0xff);
  }

  if(digitalRead(53)==LOW){
  Serial1.print("p0.pic=15");  //o page0.p0.pic=15
  Serial1.write(0xff);  
  Serial1.write(0xff);
  Serial1.write(0xff);
  }

The images ID are 15 and 16; both of the same size and the picture too.
I tried this optiosn too but it doesn't work

  Serial1.print("page0.p0.pic=15"); 
  Serial1.write(0xff);  
  Serial1.write(0xff);
  Serial1.write(0xff);
Serial1.print("p0.pic="); 
Serial1.print(15);
  Serial1.write(0xff);  
  Serial1.write(0xff);
  Serial1.write(0xff);

Thank u, greetings

Also I've been reading the forum and I found the EasyNextionLibrary by Seithan: link http://https://forum.arduino.cc/index.php?topic=676240.0 In the TriggerCode, there is an application very similar what I've been looking for and I made this one but it didn't work eather.

#include "EasyNextionLibrary.h"  // Include EasyNextionLibrary
#include "trigger.h"

EasyNex myNex(Serial1); // Create an object of EasyNex class with the name < myNex >
                       // Set as parameter the Serial you are going to use

void setup(){
  myNex.begin(9600); // Begin the object with a baud rate of 9600
                     // If no parameter was given in the begin(), the default baud rate of 9600 will be used
}

void loop(){
  myNex.writeNum("p3.pic", 10);    // Set picture 1 as background picture for p3
  delay (5000);

  myNex.writeNum("p3.pic", 9);    // Set picture 1 as background picture for p3
  delay (5000);
  
}

Could u give me any suggestion? Commentary?
Thank u people! So nice to be part of this community

Hello again Poli :slight_smile:

I can't comment on EasyNextionLibrary by Seithan, he does things in a different way to me.

I'd have to do some investigation to give you a proper answer but have a look at Instruction Set - Nextion and look for 11. Vis, I think that's what you need. You need both images to be on the page then you select which one to show. I think. Can't exactly remember.

If you try it out and don't succeed then please create a simple HMI file with 2 images on one page and your best attempt at switching between them, put them in a zip file and upload to this discussion then I'll have a closer look.

As far as I can remember you can't associate an image with a page by codes from the serial port, they have to be already associated with the page, then you use vis to show and hide them.

To be clear, I don't mean as a background image, I mean have picture object on the page, even if it has to be the same size as the whole page. Also have a look at cropped images, see part 4 of my tutorial, there is an example in there of how to use them.

This topic was automatically closed after 75 days. New replies are no longer allowed.