PerryBebbington, thank you for the quick reply, as I mentioned i have a problem with getting,setting data from,to the display specifically with the functions getText, setText, every time I call them - command that should be sent to the display is sent to the console - command: get inputFFill.txt��� . touchReturn function works. I’m trying to build my own library, because the library from the manufacturer Nextion is unusable and also thank you for the tip on Easy Nextion Library, i’ll try it
#include <SPI.h>
//WIFI
#include <ESP8266WiFi.h>
//Server
#include <ESPAsyncWebServer.h>
#include <WiFiClient.h>
//Display
#define nextion Serial
// #define Serial Serial1
//TIME LIB
#include <millisDelay.h>
millisDelay firstFillDelayObject;
//Wifi ap-ssid, pw
const char *ssid = "ESP-AP";
const char *password = "123456789";
//eletricValve
boolean stateEletricValve = LOW;
#define outputPinEletricValve 14
boolean statePumpFilter = 0;
boolean stateHeating = 0;
AsyncWebServer server(80);
String nextionReceive(boolean read_data);
// DISPLAY functions
void nextionInit(int speedInit)
{
nextion.begin(speedInit);
}
//------------------------------------------------------------------
void send_Command(const char *cmd)
{ // send data to display
nextion.print(cmd);
nextion.write(0xFF);
nextion.write(0xFF);
nextion.write(0xFF);
}
void sendText(String componentID, String text)
{
String txt = componentID + ".txt=\"" + text + "\"";
send_Command(txt.c_str());
}
void initFirstFillDelay(uint32_t startDelay)
{
//firstFill values
uint32_t firstFillDelayMs = startDelay * 60 * 1000;
delay(100);
firstFillDelayObject.start(firstFillDelayMs);
if (!firstFillDelayObject.isRunning())
{
Serial.println("timer has not been set");
}
else
{
Serial.println("timer has been set succesfully");
Serial.println(firstFillDelayMs);
digitalWrite(outputPinEletricValve, HIGH);
}
}
void stopFirstFill()
{
char buffer[10] = {0};
byte defaultState = 0;
if (firstFillDelayObject.isRunning())
{
firstFillDelayObject.finish();
digitalWrite(outputPinEletricValve, LOW);
Serial.println("button stop was pressed");
memset(buffer, 0, sizeof(buffer));
itoa(defaultState, buffer, 10);
sendText("fieldTimeout", "0");
}
else
{
Serial.println("timer didnt started yet");
}
}
long convertNextTextToInt(String text)
{
uint32_t number = text.toInt();
return number;
}
// void sendText(String componentID, String text) {
// String txt = componentID + ".txt=\"" + text + "\"";
// send_Command(txt.c_str());
// }
void pageReturn(String page_ID)
{
if (page_ID == "15")
{
}
}
String getText(String componentID)
{
// String txt = "get " + componentID + ".txt";
String txt = "get inputFFill.txt";
send_Command(txt.c_str());
txt = nextionReceive(false);
txt = txt.substring(1, txt.length() - 3);
Serial.println("function getText");
Serial.println(txt);
return txt;
}
void touchReturn(String page_ID, String component_ID, String touch_event)
{
if (page_ID == "5") //firstFill
{
if (component_ID == "3")
{
// digitalWrite(outputPinEletricValve, HIGH);
// String test = getText("inputFFill");
// Serial.println(test);
unsigned long delay = convertNextTextToInt(getText("inputFFill"));
Serial.println(delay);
if (delay > 0)
initFirstFillDelay(delay);
}
else if (component_ID == "4")
{
// digitalWrite(outputPinEletricValve, LOW);
stopFirstFill();
}
}
else if (page_ID == "15")
{ //Temperature
if (component_ID == "3")
{
// sendText("inputActualTmp", String(temperature));
// sendText("3", String(temperature));
}
}
}
String nextionReceive(boolean read_data)
{ //returns generic
boolean answer = false; // sign
char bite; //var for save sign
String cmd; //var for save text
byte countEnd = 0; // counter
unsigned long previous; // time start
int timeout = 1000; // time to wait for incoming data
previous = millis();
do
{ // waiting for correct answer
if (nextion.available())
{
bite = nextion.read();
cmd += bite;
if ((byte)bite == 0xff)
countEnd++;
if (countEnd == 3)
answer = true;
}
} while (!answer && !((unsigned long)(millis() - previous) >= timeout)); //waiting for corrent answer or elapsed time
if (read_data)
{ // read general data
if (cmd[0] == 0x65)
{ // Touch event return data
// 0X65 + Page ID + Component ID + TouchEvent + End
touchReturn(String(cmd[1], DEC), String(cmd[2], DEC), String(cmd[3], DEC));
}
else if (cmd[0] == 0x66)
{ // Current page ID number returns
// 0X66 + Page ID + End
pageReturn(String(cmd[1], DEC));
}
else if (cmd[0] == 0x67)
{ // Touch coordinate data returns
// 0X67++ Coordinate X High-order+Coordinate X Low-order+Coordinate Y High-order+Coordinate Y Low-order+TouchEvent State+End
}
else if (cmd[0] == 0x68)
{ // Touch Event in sleep mode
// 0X68++Coordinate X High-order+Coordinate X Low-order+Coordinate Y High-order+Coordinate Y Low-order+TouchEvent State+End
}
}
else
{ //read get data
Serial.println("reading valuesss");
if (cmd[0] == 0x70)
{ // String variable data returns
// X70+Variable Content in ASCII code+End
// touchReturn(String(cmd[1], DEC), String(cmd[2], DEC), String(cmd[3], DEC));
// getText(String(cmd[1], DEC));
Serial.println("reading valuesss 0x70");
return cmd;
}
else if (cmd[0] == 0x71)
{ // Numeric variable data returns
// 0X71+variable binary data(4 bytes little endian mode, low in front)+End
return cmd;
}
}
}
void checkDisplay()
{
if (nextion.available() > 0) // checking the memmory
{
nextionReceive(true); //read values from serial port
}
}
//------------------------------------------------------------------
void visible(String componentID, boolean visible)
{ // skryti/zobrazeni komponenty
String vis = "vis " + componentID + "," + String(visible);
send_Command(vis.c_str());
}
void setup()
{
//Display
nextionInit(9600);
//Serial
Serial.begin(9600);
// Server
WiFi.mode(WIFI_AP);
//AP - mode
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print(F("Soft-AP IP address = "));
Serial.println(myIP);
Serial.println("WiFi connected");
Serial.println("IP adress client: ");
Serial.println(WiFi.localIP());
Serial.println("Status: ");
Serial.print(WiFi.status());
server.begin();
}
void loop()
{
//Display
checkDisplay();
}