2

Currently I have a setup running an Arduino BME 280 sensor and a 4.3" Nextion screen. What my program does is sent all the calculated values from the BME to numbers and progress bars in my Nextion on a screen called "Run".

This page consists of 3 values: Delta T, Temperature and Humidity as well as 3 buttons for an alarm. Each of these displayed values is connected to their own progress bar. Each of the individual values has a lower and upper alarm setting which can be adjusted on a following Nextion screen called "Settings".

So basically what I am struggling with is obtaining the obtaining the values from my "settings" screen to write the "if" function in my Arduino i.e (if the button is enabled on the run page and the value is less than X on the "Settings" page or more than Y on the "Settings" page, sound the alarm).

I have used the .getValue function, but it does not return a value to my Arduino program. Can anyone please assist. It will be highly appreciated.

// Include required libraries
#include <NexTouch.h>
#include <NexDualStateButton.h>
#include <NexNumber.h>
#include <NexProgressBar.h>
#include <NexText.h>
#include <Nextion.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

//Define BME parameters
#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme; // I2C

//Define variables
int Delta_T;
int Temp_disp;
float Temp_wb;
float Temp;
int rh;

//Define NEXTION variables
//Transport Page
NexNumber Trans_deltaT = NexNumber(2, 10, "n0");
NexNumber Trans_T = NexNumber(2, 11, "n1");
NexNumber Trans_H = NexNumber(2, 12, "n2");
   
//Run Page
NexNumber Run_deltaT = NexNumber(3, 14, "n0");
NexNumber Run_T = NexNumber(3, 15, "n1");
NexNumber Run_H = NexNumber(3, 16, "n2");
NexDSButton Delta_Temp= NexDSButton(3, 12, "bt0");
NexDSButton Temperature= NexDSButton(3, 22, "bt1");
NexDSButton Hum= NexDSButton(3, 13, "bt2");

//Settings Page
NexNumber DT_min= NexNumber(4, 5, "n6");
NexNumber DT_max= NexNumber(4, 12, "n3");
NexNumber T_min= NexNumber(4, 6, "n7");
NexNumber T_max= NexNumber(4, 14, "n4");
NexNumber Hum_min= NexNumber(4, 7, "n8");
NexNumber Hum_max= NexNumber(4, 16, "n5");

NexTouch *nex_listen_list[] = {
  &Delta_Temp,
  &Temperature,
  &Hum,
  &Run_deltaT,
//  &DT_min,
//  &DT_max,
//  &T_min,
//  &T_max,
//  &Hum_min,
//  &Hum_max
    NULL
};

void Delta_TempPopCallback(void*ptr) {
  uint32_t DT = 0;          // Button State
  uint32_t DT_act = 0;
  uint32_t DelT_min = 0;    //Define DT_min setting
  uint32_t DelT_max = 0;    //Define DT_max setting
 
  DT_min.getValue(&DelT_min);
  DT_max.getValue(&DelT_max);
  Delta_Temp.getValue(&DT);
  Run_deltaT.getValue(&DT_act);
 
  Serial.println();
  Serial.println();
  Serial.println(DelT_min);
 
  // Read value of dual state button to know the state (0 or 1)

  if (DT == 0 && DT_act < DelT_min || DT_act > DelT_max) {  // If dual state button is equal to 1 (meaning is ON)...
    digitalWrite(2, HIGH);  // Turn ON internal LED
    Serial.println("ON");
  } else {                  // Since the dual state button is OFF...
    digitalWrite(2, LOW);   // Turn OFF internal LED
    Serial.println("OFF");
  }
}

void setup() {
  Serial.begin(9600);
  bme.begin(); 
  pinMode(2, OUTPUT);
  Delta_Temp.attachPop(Delta_TempPopCallback);
}


void loop() {
  Temp = bme.readTemperature();
  Temp_disp = Temp;
  // Serial.print("Temperature = ");
  // Serial.print(Temp);
  // Serial.println(" *C");
   
  rh=bme.readHumidity();
  // Serial.print("Humidity = ");
  // Serial.print(rh);
  // Serial.println(" %");
     
  Temp_wb = atan(0.151977 * pow(rh + 8.313659, 0.5)) * Temp + atan(Temp + rh) - atan(rh - 1.676331) + 0.00391838 * pow(rh, 1.5) * atan(0.023101 * rh) - 4.686035;
  // Serial.print("Wet Bulb Temp= ");
  // Serial.print(Temp_wb);
  // Serial.println(" *C");
     
  Delta_T=Temp-Temp_wb;
  // Serial.print("Delta T=");
  // Serial.print(Delta_T);
  // Serial.println(" *C");
  // Serial.println();   

  Trans_deltaT.setValue(Delta_T);
  Trans_T.setValue(Temp_disp);
  Trans_H.setValue(rh);

  Run_deltaT.setValue(Delta_T);
  Run_T.setValue(Temp_disp);
  Run_H.setValue(rh);

  nexLoop(nex_listen_list);

  delay(50);
}
1
  • What do you mean by "does not return a value"? Why don't you print out more of the values you're trying to read to get more debugging info - at least all of those that you're using in the if? Also I'd recommend you put parens in expressions like a && b || c . (a&&b)||c and a&&(b||c) are very different and C precedence rules are not always intuitive.
    – Mat
    Dec 13 '20 at 8:47
0

I had to repeat the getvalue code multiple times, there seems to be a bug which a youtuber pointed out in his video. https://github.com/rvxfahim/AHU-Firmware/blob/main/development_HMI.ino you can see from line 133, I called it so many times to be safe because it sometimes returned nothing or probably returned zero I don't remember it was a long time ago.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.