Photon,Relay Shield and Nextion Touchscreen Based Aquaponics/Indoor Greenhouse

Actually, I'd do that in the Nextion code on the display.
In fact I would do as much as possible on the display and only what's absolutely required in the MCU.

What would you want to get notified about this way?
I'd rather push that info from the display and only "subscribe" to that event via a dedicated callback - as you would do for any touch interaction with the display.

See i have a function setup like this right now.

void pageNumber()
{
        sendCurrentPageId(&number);
        if(number!=0)
        {
                splash.show();
        }
}

This is called by a timer. But that is not the best way to do it. In the editor there are commands for making the screen go to sleep and wake up on touch. But on wake up it goes to the page it went to sleep on. What i am doing right now is forcing the page to change to the first page based on the timer and then the function for sleep kicks in right after on the now current page. “thsp” sets a timer from last touch but the timer i have set up is active from the moment the page is loaded. What i need is the page to time out from last touch and on wake up go to another page.

I agree with putting most of the program on the touch screen itself and believe me that is what i am trying to do. The only stuff that i want to set up via MCU are the interactive texts and pictures.

In Nextion Editor you can place a Timer component and a Variable component, increment the variable via a timer script, reset the variables via any other components on-touch/on-release script and fire a page select statement in the timer script when the variable reaches a certain value.
No action required by the MCU at all.
But if you want to notify the MCU you can also use printh or several other messages from the timer script to trigger a particular callback.

I’ve no Nextion display with me at the moment, so I can’t provide you a working solution, but I might be able to send you one next week.
What type display have you got?

I am using a NX4827T043_011. To increase the timer i would have to setup the variable to increase on touch of every component on screen. Worst part is that the timer has a limit to 65000 ms and i would actually want time out to be over 2 mins. As always i appreciate your advice greatly.

If you have a timer that fires every 1000ms to increase a variable you'd have over four billion seconds to play with :wink:

How do you do the timer resetting currently on the MCU side?
No more work there than here IMHO.

As said, try to see the display as a secondary MCU and use it wherever possible.

there is a timer attached to the above function in the MCU. So the page will goto splash regardless of whether the user touched the screen or not which is not a best practice thing. I am following what you are suggesting though now.

Timer starts the moment page loaded
button press resets the timer
as long as something is pressed or touched timer keeps updating.
Only issue is i would have to put that in all the components now. I have about 24 screens as off now. That is why was looking for a shorter way to do this.

1 Like

for anyone else who tries to do the same thing.

Make 2 global variables one for holding a counter and another for a constant in the Nextion editor

then add a timer component to each page

In my case i wanted the timer to go back to a lock screen on timeout but wanted it to reset the timer if something was touched

the script in the timer events looked like this

if(splash.timerVar.val==splash.timerRepeat.val)
{
  splash.timerVar.val=0
  page splash
}else
{
  tm0.tim=60000
  splash.timerVar.val=splash.timerVar.val+1
}

timerVar is a counter that can be reset and timerRepeat is a constant in case i want to increase the timer length by 60 secs later on.

On every button enter timerVar.val=0 to reset the counter.
This way you can completely offload sleep events or page transitions off the MCU.

Thank you @ScruffR for your amazing suggestions as always.

1 Like

No need for that, just mark the one Timer and Variable component as global in the properties pane.

And hence you can omit the constant component too.

And I'd rather use

if(splash.timerVar.val>=splash.timerRepeat.val)

Just in case the one crucial check was missed or the variable got incremented otherwise.

You won't need to set tm0.tim=60000 again. The timer will just keep counting up to that, fire, reset and start again till you disable it via tm0.en=0

i could not get a global timer to work from another page. Page and timer are both declared global. Timer is enabled and i also tried to run

page0.tm0.en=1

in the pre and post initialization of the second page

I’ll have to double check with my devices.
I’m convinced I had this working with Nextion Editor 0.22 but not tried it since.

Let me know when you can. Using the >= operator does make sense. Its doing what i intended it to do otherwise.

On another note
I also cannot get the screen out of sleep on reset of the MCU. What is the correct way to send a system command like “rest” or sleep=0 using the MCU?

Btw here’s a confirmed bug. Every single get from text or number returns a null in the HMI file i have been working on. The MCU program is fine and runs on another sample file. Set works fine. But i cannot get any value text or otherwise back from the Nextion. All i get is null.

What version of my library are you using?
I've tested this code with v0.0.11

#include "ITEADLIB_Nextion/Nextion.h"

void t0PopCallback(void *ptr);

NexText   t0 = NexText(0, 8, "t0");
NexNumber n0 = NexNumber(0, 6, "ss"); 

char     buffer[100] = {0};
uint32_t sec;

NexTouch *nex_listen_list[] = 
{
    &t0,
    NULL
};

void t0PopCallback(void *ptr)
{
    t0.getText(buffer, sizeof(buffer));
    n0.getValue(&sec);
    Serial.printlnf("%s (%d)", buffer, sec);
}


void setup(void)
{
    nexInit();
    t0.attachPop(t0PopCallback);
    dbSerialPrintln("setup done");
}

void loop(void)
{
    nexLoop(nex_listen_list);

    // this works just the same
    //t0.getText(buffer, sizeof(buffer));
    //n0.getValue(&sec);
    //Serial.printlnf("%s (%d)", buffer, sec);
}

and my output looks as expected (don't care about the wrong date/time ;-))

[536871700:0,8,t0,(null)]
recvRetString[19,14.01.2000 04:11:00]
recvRetNumber :0
14.01.2000 04:11:00 (0)
[536871700:0,8,t0,(null)]
recvRetString[19,14.01.2000 04:11:04]
recvRetNumber :4
14.01.2000 04:11:04 (4)
[536871700:0,8,t0,(null)]
recvRetString[19,14.01.2000 04:11:08]
recvRetNumber :8
14.01.2000 04:11:08 (8)
[536871700:0,8,t0,(null)]
recvRetString[19,14.01.2000 04:11:10]
recvRetNumber :10
14.01.2000 04:11:10 (10)

Can you send me your HMI file and code as a ZIP file?

I am using the same version. So i tested a few combinations yesterday and found out the reason i was getting a null. The button object also had a page change code in its last line. I am guessing the page changed before the MCU completed the getText and the text object was a local object so it always returned a null. I was using the same code with different test HMI files which did not have that page change on the button code that is why all the other tests worked. I am now calling the page change code for just those buttons in the MCU after get is completed.

How come the code you show here shows different from the code on my iphone mail.

That comes from a bad habit of mine :blush:
I tend to press Reply to quickly, then go back and reread what I just wrote and find errors which I then correct, but the mail notification on your phone will only give you the first version - which happened to be the code I tested against a more OOP-like v0.1.0 of that lib which is not yet public.

It is indeed a better implementation of the library. When will you be making it public. Having a getter for the screen object would make is easier to set constantly changing variables. Ex- i have temp and humidity values that are displayed on certain screens. I use a timer right now to update all of them every 30s.

I’m currently busy with other things, so not much progress there and I had some issues with templating the methods properly, so I got a bit stuck :blush:
You can have a look tho’ (but it’s rather messy at the moment)

I will try the lib out when i get some time. Thank you as always.

Hello again,

I have a Float Switch that i want to connect to the Photon directly. I was thinking of hooking up the “+” to D1" pin and the " - " to D2. Set D1 to digitalwrite HIGH and then make a listener method that runs digitalread(D1) in the loop. When the switch is activated it will return a boolean.

I am a bit confused with the power ratings on the spec sheet of the switch. I only wish to run 3v through the switch coming from D1 and read if its received by D2. Is this possible or am i completely missing the point?

Thank you guys for all the help as always.

Hi @Ali

In the picture on that web page, it looks like the float switch is a magnetically operated reed switch so your plan should be fine. The data sheet for the switch seems to be saying that is can switch 0.28A (280mA) so you should be fine.