• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Project 1 : need somme help to understand !

Q&A Forum › Project 1 : need somme help to understand !
0 Vote Up Vote Down
Yves BOURDON asked 5 years ago
2 Answers
0 Vote Up Vote Down
Yves BOURDON answered 5 years ago

Could you please comment this part of your code !

Thank you so much !

client.println("<script> function setMode(value) { var xhr = new XMLHttpRequest();"); 
client.println("xhr.open('GET', \"/?mode=\" + value + \"&\", true);"); 
client.println("xhr.send(); location.reload(true); } ");
client.println("function setTimer(value) { var xhr = new XMLHttpRequest();");
client.println("xhr.open('GET', \"/?timer=\" + value + \"&\", true);"); 
client.println("xhr.send(); location.reload(true); } ");
client.println("function setThreshold(value) { var xhr = new XMLHttpRequest();");
client.println("xhr.open('GET', \"/?ldrthreshold=\" + value + \"&\", true);"); 
client.println("xhr.send(); location.reload(true); } ");*/
client.println("function outputOn() { var xhr = new XMLHttpRequest();");
client.println("xhr.open('GET', \"/?state=on\", true);"); 
client.println("xhr.send(); location.reload(true); } ");
client.println("function outputOff() { var xhr = new XMLHttpRequest();");
client.println("xhr.open('GET', \"/?state=off\", true);"); 
client.println("xhr.send(); location.reload(true); } ");
client.println("function updateSensorReadings() { var xhr = new XMLHttpRequest();");
client.println("xhr.open('GET', \"/?sensor\", true);"); 
client.println("xhr.send(); location.reload(true); }</script></body></html>");
0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Hi Yves.

It is easier to understand if we get those lines of JavaScript out of the client.println commands and format it adequately.

So, we end up with something like this:

<script> 
  function setMode(value) { 
    var xhr = new XMLHttpRequest();
    xhr.open('GET', "/?mode=" + value + "&", true); 
    xhr.send(); location.reload(true);
  }
  function setTimer(value) { 
    var xhr = new XMLHttpRequest();
    xhr.open('GET', "/?timer=" + value + "&", true);
    xhr.send(); location.reload(true); 
  }
  function setThreshold(value) { 
    var xhr = new XMLHttpRequest();
    xhr.open('GET', "/?ldrthreshold=" + value + "&", true);
    xhr.send(); location.reload(true); 
  } 
  function outputOn() { var xhr = new XMLHttpRequest();
    xhr.open('GET', "/?state=on", true); 
    xhr.send(); location.reload(true); 
  }
  function outputOff() { var xhr = new XMLHttpRequest();
    xhr.open('GET', "/?state=off", true); 
    xhr.send(); location.reload(true); 
  } 
  function updateSensorReadings() { 
    var xhr = new XMLHttpRequest();
    xhr.open('GET', "/?sensor", true);   
    xhr.send(); location.reload(true); 
  }
</script>

Basically, we have JavaScript code that goes between the <script> and </script> tags in your HTML text.
What we’re doing here is creating Javascript functions that will be automatically executed when we do something on our page.
For example, when you select your mode, it will call the setMode() JavaScript function and pass as argument the mode you select. See it here:

client.println("<p><strong>Mode selected:</strong> " + modes[selectedMode] + "</p>");
client.println("<select id=\"mySelect\" onchange=\"setMode(this.value)\">");
client.println("<option>Change mode");
client.println("<option value=\"0\">Manual");
client.println("<option value=\"1\">Auto PIR");
client.println("<option value=\"2\">Auto LDR");
client.println("<option value=\"3\">Auto PIR and LDR</select>");

The function is this one:

function setMode(value) { 
  var xhr = new XMLHttpRequest(); 
  xhr.open('GET', "/?mode=" + value + "&", true); 
  xhr.send(); location.reload(true);
}

It basically sends a request to your ESP32 on the ?mode=value&true URL, so that the ESP32 knows you need to change mode.

Another example. When you set up your timer, it will call the setTimer() function and pass as argument whatever value you typed:

else if(selectedMode == 1) {
  client.println("<p>Timer (0 and 255 in seconds): <input type=\"number\" name=\"txt\" value=\"" + 
  String(EEPROM.read(2)) + "\" onchange=\"setTimer(this.value)\" min=\"0\" max=\"255\"></p>");
}

The function is this one:

function setTimer(value) { 
  var xhr = new XMLHttpRequest();
  xhr.open('GET', "/?timer=" + value + "&", true);
  xhr.send(); location.reload(true); 
}

And it sends a request on the /?timer=value&true URL, so that the ESP32 knows, we need to change the timer.

The other functions work similarly.

I hope this is clear.

Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.