I have a program to run 3leds from webpage. the command is posted to the serial port but the lights do go on. I have posted the code. grateful for your help
`#define MAX_TOKENS 40
#define MAX_INPUT_LENGTH 30
#include <ESP8266HTTPClient.h>
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <WebSerial.h>
#include "html.h"
AsyncWebServer server(80);
#define ARRSIZE(x) sizeof(x)/sizeof(x[0])
#define MAX_TOKENS 40
#define MAX_INPUT_LENGTH 30
const char* ssid = ""; // Your WiFi SSID
const char* password = ""; // Your WiFi Password
//const char* ssid = "" // Write here your router's username
//const char* password = ""; // Write here your router's password
const char* validCommands[] = { "zhag", "pri", "ya" };
size_t maxCommandLength; // size of the longest command, here it will be 4 for goat
const byte commandCount = sizeof validCommands / sizeof validCommands[0];
const char* separators = " ,.;:/+-&\t";
void zhagCommand();
void priCommand();
void yaCommand();
void (*funcPtrs[])() = { &zhagCommand, &priCommand, &yaCommand };
static_assert((commandCount == sizeof funcPtrs / sizeof funcPtrs[0]), "Error: command list and functoin list not matching");
char inputBuffer[MAX_INPUT_LENGTH];
char* tokens[MAX_TOKENS];
byte tokenIndex = 0;
//const char* validCommands[] = { "zhag", "pri", "ya",};
//void (*funcPtrs[ARRSIZE(validCommands)])() = { &zhagCommand, &priCommand, &yaCommand,};
#define blueLed 12
#define whiteLed 14
#define redLed 16
void getInput() {
while (!Serial.available());
Serial.readBytesUntil('\n', inputBuffer, MAX_INPUT_LENGTH);
}
void getCommand(AsyncWebServerRequest *request) {
// GET request for "/process?command=<message>"
if (request->hasParam("command")) {
parseCommands(request->getParam("command")->value().c_str());
request->send(200, "text/html", webPage);
}
}
void notFound(AsyncWebServerRequest *request) {
request->send(404, "text/plain", "Not found");
}
void setup() {
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
pinMode(12, OUTPUT);
pinMode(14, OUTPUT);
pinMode(16, OUTPUT);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.printf("WiFi Failed!\n");
while (true) yield();
}
Serial.println("IP Address: ");
Serial.println(WiFi.localIP());
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send(200, "text/html", webPage);
});
server.on("/process", HTTP_GET, getCommand);
server.onNotFound(notFound);
server.begin();
Serial.println("recognized commands: ");
for (auto && s : validCommands) {
maxCommandLength = max(maxCommandLength, strlen(s));
Serial.print(" - "); Serial.println(s);
}
Serial.println("Web site ready.");
}
void tokeniseInput() {
char* token = strtok(inputBuffer, " ,.-");
while (token != NULL && tokenIndex < MAX_TOKENS) {
tokens[tokenIndex++] = token;
token = strtok(NULL, " ,.-");
if (tokenIndex >= MAX_TOKENS)
Serial.println(" ERR! Max Tokens Reached");
}
}
void printTokens() {
for (byte i = 0; i < tokenIndex; i++) {
Serial.print("Token Stored: ");
Serial.println(tokens[i]);
}
}
void reset() {
for (byte i = 0; i < MAX_INPUT_LENGTH; i++)
inputBuffer[i] = NULL;
for (byte i = 0; i < MAX_TOKENS; i++)
tokens[i] = nullptr;
tokenIndex = 0;
}
void processCommand(const char* aCommand) {
size_t commandIndex;
if (strlen(aCommand) > 0) {
for (commandIndex = 0; commandIndex < commandCount; commandIndex++) {
if (strcmp(validCommands[commandIndex], aCommand) == 0) {
funcPtrs[commandIndex]();
break;
}
}
if (commandIndex >= commandCount) {
Serial.print("Command not recognized : "); Serial.println(aCommand);
}
}
}
void parseCommands(const char* aCommandList) {
bool error = false;
char commandBuffer[maxCommandLength + 1];
commandBuffer[maxCommandLength] = '\0';
const char * startPtr = aCommandList;
const char * endPtr;
if (strlen(aCommandList) > 0)
do {
endPtr = strpbrk(startPtr, separators);
if (endPtr == nullptr) {
if (strlen(startPtr) > maxCommandLength) error = true;
else strncpy(commandBuffer, startPtr, maxCommandLength);
} else {
size_t nbChars = endPtr - startPtr;
if (nbChars > maxCommandLength) error = true;
else {
strncpy(commandBuffer, startPtr, nbChars);
commandBuffer[nbChars] = '\0';
}
}
if (error) {
Serial.print("Error parsing "); Serial.print(startPtr); Serial.println(". Continue.");
} else processCommand(commandBuffer);
error = false;
startPtr = endPtr + 1;
} while (endPtr != nullptr);
}
void zhagCommand() {
Serial.println("zhag");
digitalWrite(12, HIGH);
delay(1000);
digitalWrite(12, LOW);
delay(1000);
digitalWrite(14, HIGH);
delay(1000);
digitalWrite(14, LOW);
delay(1000);
digitalWrite(16, HIGH);
delay(1000);
digitalWrite(16, LOW);
delay(1000);
}
void priCommand() {
Serial.println("pri");
// digitalWrite(12, HIGH);
// delay(1000);
// digitalWrite(12, LOW);
// delay(1000);
digitalWrite(14, HIGH);
delay(1000);
digitalWrite(14, LOW);
delay(1000);
digitalWrite(16, HIGH);
delay(1000);
digitalWrite(16, LOW);
delay(1000);
}
void yaCommand() {
Serial.println("ya");
digitalWrite(12, HIGH);
delay(1000);
digitalWrite(12, LOW);
delay(1000);
// digitalWrite(14, HIGH);
// delay(1000);
// digitalWrite(14, LOW);
// delay(1000);
digitalWrite(16, HIGH);
delay(1000);
digitalWrite(16, LOW);
delay(1000);
}
void loop() {
if (Serial.available()) {
Serial.write(Serial.read());
}
getInput();
tokeniseInput();
printTokens(); // Uncomment for debugging
//processCommand();
WebSerial.println(" ");
delay(2000);
reset();
}