Start a new topic

Arduino + Nextion some of serial messages is missing.


I have only one page and some buttons and checkboxes on it. I'm controlling two fan with the checkboxes on the HMI screen. It's working but sometimes it seems that some messages don't reach to the arduino. The image of the checkbox changes on the screen but when I follow the flow in serial monitor, it seems that arduino misses some messages.
On the second stage I want to print some data to the screen. I have a DHT sensor and there has to be temperature and humidity data on the screen. This stage is also working. I thought maybe the way I use to print the temp. and humid cause this mess but I tried without them result is still same. I'm not using delay function, I have used millis()
Here is my code Is there any safer method which I can use?


#include <SoftwareSerial.h>
#include <DHT.h>
#include <DHT_U.h>
#include "Nextion.h"

const int motor1Pin1    = 5;         //dijital çıkışımızı tanımladık
const int motor1Pin2    = 6;
const int motor2Pin1    = 9;
const int motor2Pin2    = 10;
const int ledPin        = 8;
unsigned long previousMillis=0;
const long interval = 5*1000; //5 saniye
boolean fan1State;
boolean fan2State;
boolean lightingState;
boolean measurementState;

#define DHTPIN 7
#define DHTTYPE DHT11
SoftwareSerial nextion(2, 3); //NextionTX--->pin2 NextionRX--->pin3
Nextion myNextion(nextion, 9600);

//Nextion Variables//

void setup () {
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(motor2Pin1, OUTPUT);
  pinMode(motor2Pin2, OUTPUT);
  pinMode(ledPin,     OUTPUT);


void loop () {
//  unsigned long currentMillis = millis();
//  if(currentMillis-previousMillis >=interval){
//    previousMillis=currentMillis;
//      ////////////////////SICAKLIK ÖLÇÜMLERİ////////////////////////////////
//  float h = dht.readHumidity();
//  float t = dht.readTemperature();
//  if (isnan(h) || isnan(t)) {
//    Serial.println("Failed to read from DHT sensor!");
//    return;
//  }
////  Serial.print("Humidity: ");
////  Serial.print(h);
////  Serial.print(" %\t");
////  Serial.print("Temperature: ");
////  Serial.print(t);
////  Serial.println(" *C ");
//  myNextion.setComponentText("t5", String(t));  // Sıcaklık
//  myNextion.setComponentText("t7", String(h));  //Nem
//////////////////////SICAKLIK ÖLÇÜMLERİ////////////////////////////////

  String message = myNextion.listen(); //check for message
  if (message != "") { // if a message is received...
    Serial.println(message); //...print it out

  if (message == "65 0 5 0 ffff ffff ffff") {   //IŞIK DÜĞMESİ

    if (lightingState == false) {
      lightingState = true;
      digitalWrite(ledPin, HIGH);
    else if (lightingState == true) {
      lightingState = false;
      digitalWrite(ledPin, LOW);

  else if(message == "65 0 7 0 ffff ffff ffff"){   //FAN 1 DÜĞMESİ

    if (fan1State == false) {
      fan1State = true;
      Serial.println("fan 1 çalıştı");

    else if (fan1State == true) {
      fan1State = false;
      Serial.println("fan 1 durdu");


  else if(message == "65 0 9 0 ffff ffff ffff"){   // FAN 2 DÜĞMESİ

    if (fan2State == false) {
      fan2State = true;
      Serial.println("fan 2 çalıştı");

    else if (fan2State == true) {
      fan2State = false;
      Serial.println("fan 2 çalıştı");      

void fan1_start()   {
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH);
void fan2_start()   {
  digitalWrite(motor2Pin1, LOW);
  digitalWrite(motor2Pin2, HIGH);
void fan1_stop()    {
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
void fan2_stop()    {
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin2, LOW);




In function... 

void fan2_stop()

Is this line doing as you intended? 

  digitalWrite(motor1Pin2, LOW);

Or did you actually mean? 

  digitalWrite(motor2Pin1, LOW);


@indev2 I realized that mistake 10 minutes after I posted in the forum. I corrected it but result is still same. I tried to increase baud rate from 9600 to 115200 but at the end there is no respond at all in my hmi (Changed via nextion editor + NexHardware.cpp line 225).


Both links are not working. I searched in the forum yesterday quite long time but I couldn't find proper answer. Could you suggest some tips?


New forum - must register here and login

Login or Signup to post a comment