RE: ESP8266 - Send String between two NodeMCU under SAME WiFi

#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))

#define blueLed 12
#define whiteLed 13
#define redLed 16

#define MAX_TOKENS 10
#define MAX_INPUT_LENGTH 30
char inputBuffer[MAX_INPUT_LENGTH];
char* tokens[MAX_TOKENS];
byte tokenIndex = 0;

const char* validCommands = { "dog", "goat", "cat" };

void dogCommand();
void goatCommand();
void catCommand();

void (*funcPtrs[ARRSIZE(validCommands)])() = { &dogCommand, &goatCommand, &catCommand };

void getInput() {
while (!Serial.available());
Serial.readBytesUntil('\n', inputBuffer, MAX_INPUT_LENGTH);
}

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] = '\0';
for (byte i = 0; i < MAX_TOKENS; i++) tokens[i] = nullptr;
tokenIndex = 0;
}

void processCommands() {
for (byte i = 0; i < tokenIndex; i++) {
for (byte j = 0; j < ARRSIZE(validCommands); j++) {
if (strcmp(tokens[i], validCommands[j]) == 0) (funcPtrs[j])();
}
}
}

void dogCommand() {
Serial.println("dog");
digitalWrite(whiteLed, HIGH);
delay (500);
digitalWrite(whiteLed, LOW);
}
void goatCommand() {
Serial.println("goat");
digitalWrite(blueLed, HIGH);
delay (500);
digitalWrite(blueLed, LOW);
}
void catCommand() {
Serial.println("cat");
digitalWrite(redLed, HIGH);
delay (500);
digitalWrite(redLed, LOW);
}

void setup() {
Serial.begin(9600);
//Serial.println 1("Enter Your Commands:");
pinMode(blueLed, OUTPUT);
pinMode(whiteLed, OUTPUT);
pinMode(redLed, OUTPUT);
delay(2000);
}

void loop() {
getInput();
tokeniseInput();
processCommands();
reset();
}

I use the following code for children of a school. It is working fine. I want it to be wireless so they can be anywhere. I am unable to send serial commands as text input to do it. Can you help please.Thanks
Samuel

Wireless?
How?

Please remember to use code tags when posting code

esp8266?

...and what else?

Where does the serial input come from?

THIS IS THE CODE, NOT WORKING
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <ArduinoJson.h>
const char MAIN_page PROGMEM = R"=====(

Circuits4you

HTML Form ESP8266

void handleRoot() {
String s = MAIN_page; //Read HTML contents
server.send(200, "text/html", s); //Send web page
}
//===============================================================
// This routine is executed when you press submit
//===============================================================
void handleForm() {
String firstName = server.arg("firstname");
String lastName = server.arg("lastname");

Serial.print("First Name:");
Serial.println(firstName);

Serial.print("Last Name:");
Serial.println(lastName);
server.sendHeader(Location, /);
server.send(302, text/plain, Updated Press Back Button);

Serial.print("First Name:");
Serial.println(firstName);

Serial.print("Last Name:");
Serial.println(lastName);

//SSID and Password of your WiFi router
const char* ssid = "";
const char* password = "";
const int port = 80;
const String REQUEST_ = " ";
//ESP8266WebServer server(80); //Server on port 80
AsyncWebServer server(80);

void setup() {
Serial.begin(9600);
Serial.println ("Enter Your Commands:");
//WiFi.begin(ssid, password); //Connect to your WiFi router
Serial.println("");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting ");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(300);
}

// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
server.on("/", handleRoot); //Which routine to handle at root
server.begin(); //Start server
}

Serial.println("");
Serial.print("WiFi connected to: ");
Serial.println(WiFi.SSID());
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
pinMode(blueLed, OUTPUT);
pinMode(whiteLed, OUTPUT);
pinMode(redLed, OUTPUT);
delay(2000);

}

void loop() {
getInput();
tokeniseInput();
processCommands();
reset();
}

​ ​ ​ ​

@themakaseh, please edit your post, select all code and click the </> button to apply so-called code tags and next save your post. It makes it easier to read, easier to copy and prevents the forum software from incorrect interpretation of the code (e.g. validCommands vs . validCommands[]).


Not particularly useful.

Please do what sterretje requested in #8. If you are unsure how to do this, the forum instructions may help.

Please read the instructions for posting at the top of each forum and edit your post to correctly display the code. The forum editor doesn't keep the sketch formatting without code tags.

When in your IDE, press CTRL-T to properly indent your code, then do a CTRL-A, CTRL-C to copy the sketch to your clipboard. Then in the forum click on the <> icon to add the code tags, then CTRL-V to paste the code between code tags.

BTW- your project is certainly doable. Do you have a Raspberry Pi that you could use as an MQTT broker? This would make the project almost easy as well as scalable.

Hi Steve
Did as per your advice.
Sorry I do not have Raspberry Pi.
Thanks for your help
Samuel

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <WebSerial.h>

#define LED 2

AsyncWebServer server(80);

const char* ssid = "";          // Your WiFi SSID
const char* password = "";  // Your WiFi Password

#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))

#define blueLed 12
#define whiteLed 13
#define redLed 16

#define MAX_TOKENS 10
#define MAX_INPUT_LENGTH 30
char inputBuffer[MAX_INPUT_LENGTH];
char* tokens[MAX_TOKENS];
byte tokenIndex = 0;

const char* validCommands[] = { "dog", "goat", "cat" };

void dogCommand();
void goatCommand();
void catCommand();

void (*funcPtrs[ARRSIZE(validCommands)])() = { &dogCommand, &goatCommand, &catCommand };

void getInput() {
  while (!Serial.available());
  Serial.readBytesUntil('\n', inputBuffer, MAX_INPUT_LENGTH);
}

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] = '\0';
  for (byte i = 0; i < MAX_TOKENS; i++) tokens[i] = nullptr;
  tokenIndex = 0;
}

void processCommands() {
  for (byte i = 0; i < tokenIndex; i++) {
    for (byte j = 0; j < ARRSIZE(validCommands); j++) {
      if (strcmp(tokens[i], validCommands[j]) == 0) (funcPtrs[j])();
    }
  }
}

void dogCommand() {
  Serial.println("dog");
  digitalWrite(whiteLed, HIGH);
  delay (500);
  digitalWrite(whiteLed, LOW);
}
void goatCommand() {
  Serial.println("goat");
  digitalWrite(blueLed, HIGH);
  delay (500);
  digitalWrite(blueLed, LOW);
}
void catCommand() {
  Serial.println("cat");
  digitalWrite(redLed, HIGH);
  delay (500);
  digitalWrite(redLed, LOW);
}



void setup() {
  Serial.begin(115200);
  Serial.println ("Enter Your Commands:");
  pinMode(blueLed, OUTPUT);
  pinMode(whiteLed, OUTPUT);
  pinMode(redLed, OUTPUT);
  delay(2000);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  if (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.printf("WiFi Failed!\n");
    return;
  }
  Serial.println("IP Address: ");
  Serial.println(WiFi.localIP());
  // WebSerial is accessible at "<IP Address>/webserial" in browser
  WebSerial.begin(&server);

  server.begin();
}

void loop() {

  getInput();
  tokeniseInput();
  processCommands();

  WebSerial.println("ssam ");
  delay(2000);
  reset();



}

I would do this with ESPNow for the wireless communications. It's ambitious but it makes the project wireless and you can add nodes anytime you want.

I have aleady bought 10 esp8266 nodemcu boards. can I use them
Thanks
Samuel

ESPNow works with any ESP8266 board.
Note, however, I haven't actually used ESPNow myself. Yet.

what should I do please. Can you help
Samuel

Get started here.

Thanks. I went through the link. It has limitations of 10 connections. I think that should be good for now.
I will get one esp now and try it.
It wont be a webpage with its advantages I understand.
Thanks for sharing.
Samuel.

Hi
I bought esp wemos board and installed esp-Now slave mode in esp8266. As controller wemos is not accepting the mac
address ' uint8_t broadcastAddress[6] = { EC:FA:BC:5F:6F:17 }; ' error found ':' in nested-name-specifier, expected '::' Can you help please

Samuel

Commas, not colons.