I am trying to control the NodeMCU from the web browser, via HTML buttons on a page served by the device while in Access Point Mode. The Access Point works as does the Web Server but my issue is with the functionality of the HTML buttons.
I would like to call an Arduino void function with each HTML button press.
If I use the HTML buttons for simple operations: just IF statements, they work just fine -- I am able to switch between the different functions. However the problem occurs when running a loop function as I am unable to control the device from the web interface anymore.
In short while in a loop function, the Web Buttons are rendered useless. I believe the issue here is the fact that I don't know how to implement a break call, triggered by the HTML Button action.
I am interested in a looping function as I am running a function to constantly determine the distance to a given object using HC-SR04 and maintain a constant distance to it.
If I call the function keepDistance from the HTML button, it runs fine but only once, not in a loop while anothre button is pressed. If I include a loop under the Button action, then I am unable to exit out of the loop when pressing any of the HTML buttons.
Could you please advise me on the best way to create a break call, that forfeits a looping function, the trigger being a HTML Button press ?
Thank you !
#include <ESP8266WiFi.h>
#define TRIGGER 13
#define ECHO 15
#define greenLED 5
#define yellowLED 12
#define boardLED 16
// Access point network Name and Password
const char* ssid = "TestTest";
const char* password = "test1234";
long distance;
WiFiServer server(80);
void setup() {
pinMode(TRIGGER, OUTPUT);
pinMode(ECHO, INPUT);
pinMode(greenLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(boardLED, OUTPUT);
Serial.begin(115200);
delay(10);
// Start the Access Point
WiFi.softAP(ssid, password);
Serial.println("");
Serial.println("Access Point started");
// Start the server
server.begin();
Serial.println("Server started");
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while (!client.available()) {
delay(1);
}
// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
// Match the request
int value = LOW;
if (request.indexOf("/LED=TOGGLE") != -1) {
value = LOW;
Serial.println(millis());
Serial.println("Calling Led Toggle Function");
ledToggle();
Serial.println(millis());
Serial.println("Exited: Led Toggle Function");
}
if (request.indexOf("/TOPSPEED=LIMIT") != -1) {
value = LOW;
Serial.println(millis());
Serial.println("Calling Set Top Speed Function");
setTopSpeed();
Serial.println(millis());
Serial.println("Exited: Set Top Speed Function");
}
if (request.indexOf("/PILOT=OFF") != -1) {
value = LOW;
Serial.println(millis());
Serial.println("Pilot Off Pressed!");
}
if (request.indexOf("/PILOT=ON") != -1) {
value = HIGH;
Serial.println(millis());
Serial.println("Calling keepDistance function");
keepDistance();
delay(100);
Serial.println(millis());
Serial.println("Function executed : keepDistance");
}
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.print("Auto Pilot is now: ");
if (value == HIGH) {
client.print("On");
} else {
client.print("Off");
}
client.println("<br><br>");
client.println("<a href=\"/LED=TOGGLE\"\"><button>Toggle Led </button></a>");
client.println("<a href=\"/TOPSPEED=LIMIT\"\"><button>Limit Top Speed </button></a>");
client.println("<a href=\"/PILOT=ON\"\"><button>Pilot ON </button></a>");
client.println("<a href=\"/PILOT=OFF\"\"><button>Pilot OFF </button></a><br />");
client.println("</html>");
delay(1);
Serial.println("Client disonnected");
Serial.println("");
}
//Functions to deal with calculating the Distance
long getDistance() {
// Returns distance to an object in Centimeters
long duration;
digitalWrite(TRIGGER, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER, LOW);
duration = pulseIn(ECHO, HIGH);
distance = (duration / 2) / 29.1;
return distance;
}
void printDistance() {
// Prints the value of the distance
distance = getDistance();
Serial.println("");
Serial.print("Distance: ");
Serial.print(distance);
Serial.print(" cm");
Serial.println("");
delay(250);
}
void keepDistance() {
// Mantains the distance to a given object
distance = getDistance();
digitalWrite(greenLED, LOW);
digitalWrite(yellowLED, LOW);
if (distance > 17) {
digitalWrite(greenLED, HIGH);
}
if (distance < 15) {
digitalWrite(greenLED, LOW);
digitalWrite(yellowLED, HIGH);
}
}
void ledToggle(){
digitalWrite(boardLED,!digitalRead(boardLED));
Serial.println(millis());
Serial.println("Currently running ledToggle function");
}
void setTopSpeed(){
Serial.println(millis());
Serial.println("Currently running setTopSpeed function");
}