Hello all,
When clicking buttons, on the second page of the screen, nextion.available() is false all the time
Program code:
#include <DHT11.h>
#include <SoftwareSerial.h>
#include <Nextion.h>
DHT11 dht11(2);
intValue;
StringTosend;
String data; // variable for storing data
SoftwareSerial nextion(8, 9); // Nextion TX to pin 8 and RX to pin 9 of Arduino
Nextion myNextion(nextion, 9600); //create a Nextion object named myNextion using the nextion serial port @ 9600bps
void SendAdd(int id, int NumGraf, int val)
{
if (NumGraf == 0) Value = map(val,0,125,0,255);
if (NumGraf == 1) Value = map(val,0,100,0,255);
Tosend = "add";
Tosend += id;
Tosend += ",";
Tosend += NumGraf;
Tosend += ",";
Tosend += Value;
nextion.print(Tosend);
nextion.write(0xff);
nextion.write(0xff);
nextion.write(0xff);
}
void setup() {
Serial.begin(9600);
nextion.begin(9600);
myNextion.init();
pinMode(LED_BUILTIN, OUTPUT);
pinMode(3, OUTPUT);
digitalWrite(3, 1);
}
void loop()
{
int temperature = 0;
int humidity = 0;
int result = dht11.readTemperatureHumidity(temperature, humidity);
//update temperature and humidity text
myNextion.setComponentText("page0.t0", String(temperature));
myNextion.setComponentText("page0.t1", String(humidity));
//update temperature and humidity graphs
SendAdd(5,0, temperature);
SendAdd(5,1, humidity);
// wait for data from the display
if (nextion.available())
{
char inc;
inc = nextion.read();
data += inc;
Serial.println("nextion.available");
if (inc == 0x0A) //end of command
{
// if 'on' came
if (data.indexOf("on") >= 0)
{
digitalWrite(LED_BUILTIN, 1);
digitalWrite(3, 0);
data = "";
Serial.println("on");
}
}
if (inc == 0x0A) //end of command
{
if 'off' came
if (data.indexOf("off") >= 0)
{
digitalWrite(LED_BUILTIN, 0);
digitalWrite(3, 1);
data = "";
Serial.println("off");
}
}
}
}
If excluded, the code is:
int temperature = 0;
int humidity = 0;
int result = dht11.readTemperatureHumidity(temperature, humidity);
//update temperature and humidity text
myNextion.setComponentText("page0.t0", String(temperature));
myNextion.setComponentText("page0.t1", String(humidity));
//update temperature and humidity graphs
SendAdd(5,0, temperature);
SendAdd(5,1, humidity);
Then nextion.available() works and returns true.
Tell me how to solve this problem. So that the graphs would be drawn on the first page and there would be a reaction to pressing buttons on the second page.