Jump to content

ESP32 project question


bxdobs
 Share

Recommended Posts

Sorry, this is probably a real basic problem but struggling with how or what question to ask ... essentially need to trigger an IOT web server to turn on|off a led

Not sure if the generic ESP32 firmware provides AJAX functionality so was trying to use KISS as much as possible.

Task: I have written an HTML script which runs on a client ... it has 3 buttons which provide the ability to;

Clear atmr (a global client side variable)

Increment atmr

Decrement atmr

What I am looking for is a way to trigger the ESP32 to:

a) turn off a led when either atmr is zero OR the client disconnects

b) turn on a led when atmr is NOT zero

The HTML script works great on the Client but I am stumped with how to link/simulate the led ON/OFF process per the example given for the ESP32 "without using a button"

ESP32 example uses Buttons to do this

HTML Button Example Client Side

<p><a href="/led/on"><button>Turn Led ON</button></a></p>
or
<p><a href="/led/off"><button>Turn Led Off</button></a></p>

This is coupled with the Server Side code:

if (header.indexOf("GET /led/on") >= 0) {
 digitalWrite(LED_BUILTIN, HIGH); // led on
} else if (header.indexOf("GET /led/off") >= 0) {
 digitalWrite(LED_BUILTIN, LOW);
}      

Basically when a button is pressed, the buttons href modifies the URL ... this is a bonus feature because both pressing a button OR creating a url with someurl.com/led/on or someurl.com/led/off can be used ... BUT ... expecting this won't work if changing the URL forces a reload of the client (resetting the global variables)

The following client side function, both sets and checks a client side global variable atmr with the expectation that when 0 the url changes to /led/off and when != 0 it changes to /led/on

My expectation was to just add some on/off code in the Zero test at the end of this function which currently just changes the color of this screen element stmr

function fs2t() {
  var hrs = 0;
  var hd = 0;
  var mns = 0;
  var md = 0;
  var scs = 0;
  var dsp = '';
  atmr = atmr - 1;
  if (atmr < 0) {
   atmr = 0;
  }else if (atmr > 600) {
    atmr = 600;
  }
  hd = atmr / 3600;
  hrs = Math.trunc(hd); 
  md = (hd - hrs) * 60;
  mns = Math.trunc(md);
  scs = Math.trunc((md - mns) * 60);
  dsp = hrs.toString().padStart(2,'0')
         + ':' 
         + mns.toString().padStart(2,'0') 
         + ':' 
         + scs.toString().padStart(2,'0');
  stmr.innerText = dsp;
  if (atmr != 0) {
   stmr.style.backgroundColor = 'red';
    // expected to add a /led/on here
  } else {
    stmr.style.backgroundColor = 'green';
    // expected to add a /led/off here
  }
}

Discussions that pertain to changing urls suggest this may cause a reload of the client ... if this is the case ... as in ... if the client reloads when the led is turned on, then all global vars would reset resulting in this process failingĀ 

Another solution might be that the client stays connected only while atmr > 0 and automatically disconnects when zero

Thanks in advance for any help you can provide

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...