Help with webserver...
 
Notifications
Clear all

Help with webserver.h

8 Posts
3 Users
7 Likes
740 Views
(@boggiano)
Member
Joined: 3 years ago
Posts: 17
Topic starter  

Hello,
I made a little traffic light with a NodeMCU, an RGB LED strip.

It works using the browser:
http://host/on
http://host/off
http://host/flash

I am using a button on the device and with un interrupt I can turn the flash off.

The problem is that during the flash routine (I am using millis())
the webserver doesn't handle any call from the browser so it's impossibile to
turn off the LED remotly unless the flash finished by itself.

When I call> http://host/off

Do you have any idea, please ?

Thanks

 

 


   
Quote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2504
 

@boggiano

In turnOffByButton() set nBlink to MAX_LAMPEGGI. That should stop the looping and exit the flash() routine.

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
Inq
 Inq
(@inq)
Member
Joined: 2 years ago
Posts: 1900
 

@will 

Will, I don't think that will solve the problem.  I'm guessing that the interrupt (caused by pressing the NodeMCU based button) works as expected.  But because the MPU is in the loop, it can't handle the web request.  

What you'll need to do if you want to stop the loop with both a button (interrupt) and a web call, is you can't have the loop.  

I'm winging this without actually compiling...  (I use the compiler like a spell-checker 😆)

  • You can get rid of your flash() method and use the one below.
  • Add this doFlash() method and the call in the loop().
  • Keep your turnOffByButton() method.

If this doesn't make sense or it doesn't work... let me know.  

void loop() 
{
   server.handleClient();
   MDNS.update();
   if (FLASH)
      doFlash();
}

void flash () { FLASH = 1; }

void doFlash()
{
   static u32 lastToggle = 0;
   u32 now = millis();
   if (now - lastToggle > MAX_LAMPEGGI)
   {
       digitalWrite(pinRosso, !digitalRead(pinRosso));
       lastToggle = now;
    }
}

 

 

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, WiFi Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
Inst-Tech reacted
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2504
 

@inq 

Good, hopefully one or the other methods will help him out 🙂

Anything seems possible when you don't know what you're talking about.


   
Inst-Tech reacted
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2504
 
Posted by: @inq

@will 

Will, I don't think that will solve the problem.  I'm guessing that the interrupt (caused by pressing the NodeMCU based button) works as expected.  But because the MPU is in the loop, it can't handle the web request.  

What you'll need to do if you want to stop the loop with both a button (interrupt) and a web call, is you can't have the loop.  

I got thinking about this again (it's not really insomnia 🙂 ).

What I suggested assumes that the interrupt is, in fact, triggered by the button push. It tries to reset nBlink inside the flash() routine to a value such that the while(nBlink < MAX_LAMPEGGI) loop will exit and the flash() routine will therefore end.

However, I now rthought to check where nBlink is declared and find that it's declared inside flash() and so the interrupt cannot change its value because it's not in the same scope. nBlink would have to be declared globally (and volatile as well) for my solution to work.

Anything seems possible when you don't know what you're talking about.


   
Inq reacted
ReplyQuote
(@boggiano)
Member
Joined: 3 years ago
Posts: 17
Topic starter  

@Will
In the interrupt routine I modify the value of MAX_LAMPEGGI to the max value, and in the while cycle (during the flash) I compare nBlink  (locally declared) with the MAX_LAMPEGGI values (globally declared) and it works.
The problem is that during the while cycle (flashing) the webserver is not responding at all.

I modified the loop() routine, as suggested, and it seems to work ( thanks @inq)!
I have to double check it since the I discovered the problem while soldering the circuit so "I am just in the middle of the river" (no LED and, most of all, no button!) but I am quite sure that it's going to work.

BTW,
MAX_LAMPEGGI keeps only the number of flashes and

tempoIntervallo is the interval time between flashes, so the code should be:

if (now - lastToggle > tempoIntervallo) {}

I think that the spell is working! 😀 
Thanks!


   
Inst-Tech reacted
ReplyQuote
Inq
 Inq
(@inq)
Member
Joined: 2 years ago
Posts: 1900
 
Posted by: @will

I got thinking about this again (it's not really insomnia 🙂 ).

Don't you hate that when it happens... a problem presents itself and you go to sleep and your subconscious wakes you up in the middle of the night with the solution.  At my age, I think my subconscious is smarter than I am.  🤣 

... and it's very rude about my beauty sleep.

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, WiFi Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
Inst-Tech reacted
ReplyQuote
Inq
 Inq
(@inq)
Member
Joined: 2 years ago
Posts: 1900
 
Posted by: @boggiano

BTW,
MAX_LAMPEGGI keeps only the number of flashes and

tempoIntervallo is the interval time between flashes, so the code should be:

if (now - lastToggle > tempoIntervallo) {}

I think that the spell is working! 😀 
Thanks!

Good!  I see you understand the fundamental concept.  That... while the flash() method is flashing, it is starving the web server processing called in the loop().  Both those methods are in the same context and only one or the other will run.

 

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, WiFi Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
Inst-Tech and boggiano reacted
ReplyQuote