K
K
Kot Kotov2020-04-04 12:54:09
Arduino
Kot Kotov, 2020-04-04 12:54:09

How to display a variable on nextion?

Do not output a variable of type int. It just disappears on screen when it's displayed.

#include <SoftwareSerial.h>;
#include "NexText.h"

SoftwareSerial mySerial(A0, A1); // A0 - к TX сенсора, A1 - к RX

byte cmd[9] = {0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79}; 
unsigned char response[9];

unsigned int ppm;

NexText t0 = NexText(0, 1, "ppm");

void setup() {
  
  nexInit();
  Serial.begin(9600);
  mySerial.begin(9600);
}

void loop() 
{
  
  
  t0.setText(ppm);
  
  mySerial.write(cmd, 9);
  memset(response, 0, 9);
  mySerial.readBytes(response, 9);
  int i;
  byte crc = 0;
  for (i = 1; i < 8; i++) crc+=response[i];
  crc = 255 - crc;
  crc++;

  if ( !(response[0] == 0xFF && response[1] == 0x86 && response[8] == crc) ) {
    Serial.println("CRC error: " + String(crc) + " / "+ String(response[8]));
    t0.setText("temperatureCTemp");
  } else {
    unsigned int responseHigh = (unsigned int) response[2];
    unsigned int responseLow = (unsigned int) response[3];
    ppm = (256*responseHigh) + responseLow;
    Serial.println(ppm);
    
  }
  delay(1000);
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question