Help Nextion display - temperature / humidity with photon

For france you’d need to set (DST) or +1.0 for Central European Standard Time.

1 Like

Ah yes, thank you, I saw this line, but I didn’t know the number inside corresponded to hour hour, I wanted to add…

So, everything seems to be ok now, but there is something strange anyway. The temperature sensors worked fine at the beginning, and since a moment (but don’t know when) it begins to show some weird values (increase to 40 ºC to decrease to 25 ºC, which is the good temperature).
I thought it was a voltage problem due to the amount of thing plugged on the Photon, but I tested the voltage and it’s the same than without anything plugged.

I am also using some if loop to trigger delays when the temperature and the hour reach the values I set. And to do so, I am using comparison but sometimes, this comparison are inversed : less than equal more than, more than equal less than and about the delay, HIGH equal LOW and LOW equal HIGH.
I know it’s really strange, It’s the first time I have something like that, I checked the connections and everything is good.
I don’t understand why.

This is all the code I flashed in my Photon, I am pretty sure there is many thing that can be changed, but I am a novice :slight_smile:

I had to add another buffer and to use the first way I did to display the humidity on the screen because the code you gave me didn’t works. I had the temperature from one of my sensors instead of the humidity, I on’t know why because I checked every line, because I thought there was a conflict, but nothing :confused:

About the calcul of the temperature sensors to convert voltage into degrees, I also had to modify the values, otherwise, with 5.0 (there are plugged on the VIN) the values was like -5.8 ºC.

////// LIBRARIES ///////
#include <ITEADLIB_Nextion.h>
#include <Adafruit_DHT.h>

////// TEMPERATURE ///////
int chaudHaut = A0;
int chaudBas = A1;
int froid = A2;

////// HUMIDITY ///////
#define DHTPIN A3
#define DHTTYPE DHT11

////// RELAYS ///////
int relayLed = D0;
int relayLampe = D1;
int relayTapis = D2;
int relay4 = D3;
int relay5 = D4;

////// LED ///////
int led = A4;
int brightness = 0;
int fadeAmount = 2;

////// DISPLAY ///////
USARTSerial& nexSerial = Serial1;

NexText t0 = NexText(0, 4, "t0"); // chaud haut
NexText t1 = NexText(0, 5, "t1"); // chaud bas
NexText t2 = NexText(0, 6, "t2"); // froid
NexText t3 = NexText(0, 7, "t3"); // humidity
NexText t8 = NexText(0, 12, "t8"); // hour
NexText t10 = NexText(0, 13, "t10"); // day
NexText t11 = NexText(0, 14, "t11"); // month
NexText t12 = NexText(0, 15, "t12"); // year

int hour = Time.hour();
int format = Time.hourFormat12();

void setup() {;
    pinMode(chaudHaut, INPUT);
    pinMode(chaudBas, INPUT);
    pinMode(froid, INPUT);
    ////// HUMIDITY ///////
    ////// RELAYS ///////
    pinMode(relayLed, OUTPUT);
    pinMode(relayLampe, OUTPUT);
    pinMode(relayTapis, OUTPUT);
    pinMode(led, OUTPUT);
    pinMode(relay4, OUTPUT);
    pinMode(relay5, OUTPUT);
    digitalWrite(relay4, HIGH);
    digitalWrite(relay5, HIGH);
    ////// DISPLAY ///////

void loop() {
    /////////////////////////////////////////////////////////////// TEMPERATURE ///////////////////////////////////////////////////////////////
    ////////// Sonde chaud haut //////////
    int readingCH = analogRead(chaudHaut);  
    float voltageCH = readingCH * 0.8; //20
    voltageCH /= 1024.0;
    float tempCH = (voltageCH - 0.5) * 100;
    ////////// Sonde chaud bas //////////
    int readingCB = analogRead(chaudBas);  
    float voltageCB = readingCB * 0.8; //20.5
    voltageCB /= 1024.0;
    float tempCB = (voltageCB - 0.5) * 100;
    ////////// Sonde froid  //////////
    int readingF = analogRead(froid);  
    float voltageF = readingF * 5; //20.5
    voltageF /= 1024.0;
    float tempF = (voltageF - 2) * 100;
    /////////////////////////////////////////////////////////////// DISPLAY ///////////////////////////////////////////////////////////////
    char buffer [64];
    char buffer1 [64];
    int humi = dht.getHumidity();
    snprintf(buffer, sizeof(buffer), "%.1f", tempCH);
    snprintf(buffer, sizeof(buffer), "%.1f", tempCB);
    snprintf(buffer, sizeof(buffer), "%.1f", tempF);
    memset(buffer1, 0, sizeof(buffer1));
    itoa(humi, buffer1, 10);

    snprintf(buffer, sizeof(buffer), "%s", (const char*)Time.format("%T"));
    snprintf(buffer, sizeof(buffer), "%s", (const char*)Time.format("%D"));

    /////////////////////////////////////////////////////////////// RELAYS ///////////////////////////////////////////////////////////////
    if(hour >= 10){
        digitalWrite(relayLed, LOW);
        analogWrite(led, brightness);
        if (brightness < 100) {
            brightness = brightness + fadeAmount;
    if(tempCB < 31){ //this part works fine now, but something it's inversed
        digitalWrite(relayTapis, LOW); //here LOW works like HIGH
        digitalWrite(relayTapis, HIGH);  //here HIGH works like LOW
    if(tempCH < 31){
        digitalWrite(relayLampe, LOW);
        digitalWrite(relayLampe, HIGH);   
    if(hour >= 22){
        digitalWrite(relayLed, HIGH);
        /*if(tempCH > 28){
            digitalWrite(relayLampe, LOW);
            digitalWrite(relayLampe, HIGH);   
        if(tempCB > 28){
            digitalWrite(relayTapis, LOW);
            digitalWrite(relayTapis, HIGH);   

Your voltageXX /= 1024.0; should actually be voltageXX /= 4095.0; since the ADCs on the Particle devices have 12bit resolution unlike Arduino which use 10bit.
Also the factor 0.8 (and 5) would probably need adjusting for the 3.3V logic - be carefule not to feed any voltage higher than 3.3V into analog INPUT pins.

I don’t really like repeating myself but you still got that wrong - hence the code didn’t work, since this is not what I gave you but what you had wrong right from the beginning

    memset(buffer1, 0, sizeof(buffer1));
    itoa(humi, buffer1, 10);

you can’t set the text before you put the value into the buffer!

If you get the order right, you will also get the correct values in the correct fields.


It works better indeed, thank you, I thought it was the same ADCs.
But, I still have huge different values when the sensors refresh one time I have 28 and then 31 etc. there is nothing fix however the temperature is fixed I don’t know where is the problem. I’m using TMP36 sensors plugged on the 3V3 and this is the code modified with your advice:

    int readingCH = analogRead(chaudHaut);  
    float voltageCH = readingCH * 3.3; //20
    voltageCH /= 4095.0;
    float tempCH = (voltageCH - 0.5) * 100;

I’m sorry I wouldn’t offend you, It works fine for the temperature and I thank you a lot for your help, but not for the humidity, that’s why I reuse the first thing I found with another buffer.

Actually I thought I had understand but apparently not,

If I put the value on the buffer and then I set the text, like this (that’s what I understood)

memset(buffer1, 0, sizeof(buffer1));
itoa(humi, buffer1, 10);

It doesn’t work. But I really would use your method, but I tried everything again and again and nothing works, I still have the temperature of one of the sensors.

memset(buffer1, 0, sizeof(buffer1));
itoa(humi, buffer1, 10);

Still not

memset(buffer1, 0, sizeof(buffer1)); // this only CLEARS the buffer
itoa(humi, buffer1, 10); // this converts the numeric data into string and stores in buffer1
t3.setText(buffer1); // only after both of the above you can send it to the display

And this will still work

    snprintf(buffer, sizeof(buffer), "%d", humi);

The reason why my code provided above didn’t work for you must have been due to the fact that you are using an int (which needs a %d in snprintf()) while I used a double (which requires the provided %f).

You can’t just take the one line without considering the other - consistency is key here.

To get better readings of your TMP36 you may want to search the forum for TMP36 and TMP35 to find how to improve reading quality.


Hi @ScruffR
sorry for my late reply. I could continue my project and I understand better the way of using Nextion display. I used your code for the humifity and its works fine now.
Thank you again for your help and your time.

I still have some difficulties to have stable values from the temperature sensors (even with a 0.1 cap) but I’ll continue to look for a solution. And after some complication I finally could automatically switch on and off the light plugged on a relay. At the beginning I was using an if but that doesn’t worked and then I found the TimeAlarm library that is pretty easy to use and it’s way better.

So thank you again for everything !