Nextion displey - problém s načtením dat z displeje

Odpovědět
chossecko
Příspěvky: 1
Registrován: 29 čer 2020, 21:00
Reputation: 0

Nextion displey - problém s načtením dat z displeje

Příspěvek od chossecko » 29 čer 2020, 21:39

Dobrý den, potřeboval bych pomoc s načtením hodnot z displeje, načtení se provadí pomocí funkce getText - té jako parametr předávám jmeno objektu(nazev objektu definový v nextion edtioru jsem kontroloval..). Při každém zavolaní fuknce getText se mi do console vypíše příkáz, který má být odeslan do displeje k získání hodnot, příkaz - get inputFFill.txt���. Možná bude i problém v samotném přenosu po seriové lince, kterou vyoužívám pro debbuging a zárověn pro odeslání příkazů. Handlery udalostí onclick, onrelease fungují. Děkuji za rady, omlouvám se za spageti kod


Kód: Vybrat vše

#include <SPI.h>
//WIFI
#include <ESP8266WiFi.h>
//JSON
#include <AsyncJson.h>
#include <ArduinoJson.h>
//Server
#include <ESPAsyncWebServer.h>
#include <WiFiClient.h>
#include <SoftwareSerial.h>
//Display
#define nextion Serial
// #define Serial Serial1
//TIME LIB
#include <millisDelay.h>
millisDelay firstFillDelayObject;
// input pin for Analog reading Temperature
#include <OneWire.h>
#include <DallasTemperature.h>

const int inputTemperature = 12;
OneWire oneWireDS(inputTemperature);
DallasTemperature senzoryDS(&oneWireDS);

//Wifi ap-ssid, pw
const char *ssid = "ESP-AP";
const char *password = "123456789";

//from display
// NexText inputFFill = NexText(5, 2, "inputFFill");
// NexText fieldTimeout = NexText(5, 5, "fieldTimeout");
// NexButton buttonSleepMode = NexButton(4, 2, "btnSleepMode");

char buffer[10] = {0};

//Register object textNumber, buttonSubmit, buttonStop, to the touch event list.
// NexTouch *nex_listen_list[] = {
//     &fieldText,
//     &buttonSubmit,
//     &buttonStop,
//     &buttonSleepMode,
//     NULL};
// String dayInWeekFromUser;

// ANALOG VALUES
//potentiometer
String readingInt;
String currentHours;
String currentMinutes;
String currentDayInWeek;

//temperature
// String temperature;
float temperature;

int count = 0;

//eletricValve
boolean stateEletricValve = LOW;
#define outputPinEletricValve 14
boolean statePumpFilter = 0;
boolean stateHeating = 0;

AsyncWebServer server(80);

String nextionReceive(boolean read_data);



void printFreeHeap()
{
  delay(200);
  Serial.println("DRAM Heap memory");
  Serial.println(ESP.getFreeHeap());
}
// DISPLAY functions
void nextionInit(int speedInit)
{
  nextion.begin(speedInit);
}

//------------------------------------------------------------------
void send_Command(const char *cmd)
{ // odeslani dat do displeje
  nextion.print(cmd);
  nextion.write(0xFF);
  nextion.write(0xFF);
  nextion.write(0xFF);
}

void sendText(String componentID, String text)
{ // odesli 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) { // odesli text
//   String txt = componentID + ".txt=\"" + text + "\"";
//   send_Command(txt.c_str());
// }

void pageReturn(String page_ID)
{
  if (page_ID == "15")
  {
    
  }
}


String getText(String componentID)
{ // nacti text
  // 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; // znacka
  char bite;              // promenna pro ulozeni znaku
  String cmd;             // promenna pro ulozeni textu
  byte countEnd = 0;      // pocitadlo
  unsigned long previous; // cas spusteni
  int timeout = 1000;     // doba po kterou se ceka na prichozi data
  previous = millis();

  do
  { // cekani na spravnou odpoved
    if (nextion.available())
    { // kdyz jsou k dispozici data, precti data
      bite = nextion.read();
      cmd += bite;
      if ((byte)bite == 0xff)
        countEnd++;
      if (countEnd == 3)
        answer = true;
    }
  } while (!answer && !((unsigned long)(millis() - previous) >= timeout)); // ceka na spravnou hodnotu, nebo uplynuti casu

  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
    if (cmd[0] == 0x70)
    { // String variable data returns
      // X70+Variable Content in ASCII code+End
      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;
    }
  }
}





//------------------------------------------------------------------

//------------------------------------------------------------------

//registr events from display
void buttonSubmitCallback(void *ptr)
{

  // memset(buffer, 0, sizeof(buffer));
  // itoa(number, buffer, 10);

  // fieldTimeout.setText(buffer);
}

void activateSleepMode(void *ptr)
{
  Serial.println("esp off");
  ESP.deepSleep(0);
}

////////////////////////////////////////////////////////////////////////////////////

//------------------------------------------------------------------

void checkDisplay()
{                              // kontrola prijatych dat
  if (nextion.available() > 0) // kontroluje obsah pameti, pokud nen nic odeslano, dalsi cast programu se neprovede
  {
    nextionReceive(true); // precist hodnoty z serial portu
  }
}

//------------------------------------------------------------------
void visible(String componentID, boolean visible)
{ // skryti/zobrazeni komponenty
  String vis = "vis " + componentID + "," + String(visible);
  send_Command(vis.c_str());
}
//------------------------------------------------------------------

////////////////////////////////////////////////////////////////////////////////////

void setTimeoutButton(uint32_t delay)
{
  char endMessage[2] = "0";
  unsigned int number = trunc((delay / 1000));

  char buffer[10] = {0};
  byte defaultState;

  Serial.println("number before division");
  Serial.println(number / 60);
  if (number / 60 >= 1)
  {
    defaultState = trunc(number / 60);
  }
  else
  {
    defaultState = number;
  }

  if (firstFillDelayObject.justFinished())
    defaultState = 0;

  memset(buffer, 0, sizeof(buffer));
  itoa(defaultState, buffer, 10);
  sendText("5", buffer);

  String delayString = String(delay);
  Serial.println("delay number truncated");
  Serial.println(number);
  Serial.println("delay string");
  Serial.println(delayString);
}

void setup()
{

  //Display
  nextionInit(9600);

  //Serial
  Serial.begin(9600);
  senzoryDS.begin();

  delay(1000);

  //Sets outputs pin
  pinMode(outputPinEletricValve, OUTPUT); //eletricValve

  //SPIFFS
  if (!SPIFFS.begin())
  {
    Serial.println("An error has occured while entering SPIFFS");
    while (1)
      ;
  }

  // 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();
}

float getTemperature()
{
  senzoryDS.requestTemperatures();
  // výpis teploty na sériovou linku, při připojení více čidel
  // na jeden pin můžeme postupně načíst všechny teploty
  // pomocí změny čísla v závorce (0) - pořadí dle unikátní adresy čidel
  return senzoryDS.getTempCByIndex(0);
}

void loop()
{
  //Display
  checkDisplay();

  //FIRST FILL POOL
  //just finished timer
  if (firstFillDelayObject.justFinished())
  {
    digitalWrite(outputPinEletricValve, LOW);
  }
  delay(20);
  //set text to timeoutObject
  if (firstFillDelayObject.remaining() >= 1)
  {
    setTimeoutButton(firstFillDelayObject.remaining());
  }
  //OPERATING HOURS






  //REGULATION temperature
  //Measurment
  temperature = getTemperature();
  delay(100);
  Serial.print("Teplota cidla DS18B20: ");
  Serial.print(String(temperature));
  Serial.println(" stupnu Celsia");
  delay(1000);

  delay(500);
  // Serial.println("potentiometer:");
  // Serial.println(readingInt);
  delay(300);
  // Serial.println("time");
  // Serial.println(currentDayInWeek);
  // Serial.println(currentHours);
  // Serial.println(currentMinutes);
}

mumin
Příspěvky: 1
Registrován: 31 črc 2020, 16:45
Reputation: 0

Re: Nextion displey - problém s načtením dat z displeje

Příspěvek od mumin » 31 črc 2020, 17:14

Ahoj, ano nemúžeš mít port obsazen zároveň terminálem a displejem. Pokud máš více jak 1 seriový port přesuň NEXTION na další. Pak ti to poběží.
např.

#define nextion Serial1

Odpovědět

Kdo je online

Uživatelé prohlížející si toto fórum: Žádní registrovaní uživatelé a 11 hostů