Hello, I ran into a problem testing the library WebSerial.h;
The example sketch provided by Rui works perfectly, the problem arises if I insert a call to a function in the code. In this case the compiler gives an error. As if the function was not declared.
I wrote a very simple example with a call to an empty function and it doesn’t compile.
if anyone had any suggestions i would appreciate it very much.
Thanks
Here my code not working:
/*
Rui Santos
Complete project details at https://RandomNerdTutorials.com/esp8266-nodemcu-webserial-library/
This sketch is based on the WebSerial library example: ESP8266_Demo
https://github.com/ayushsharma82/WebSerial
*/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <WebSerial.h>
#define LED 2
AsyncWebServer server(80);
const char* ssid = “REPLACE_WITH_YOUR_SSID”; // Your WiFi SSID
const char* password = “REPLACE_WITH_YOUR_PASSWORD”; // Your WiFi Password
void recvMsg(uint8_t *data, size_t len){
WebSerial.println(“Received Data…”);
String d = “”;
for(int i=0; i < len; i++){
d += char(data[i]);
}
WebSerial.println(d);
if (d == “ON”){
digitalWrite(LED, LOW);
}
if (d==”OFF”){
digitalWrite(LED, HIGH);
}
}
void setup() {
Serial.begin(115200);
pinMode(LED, OUTPUT);
digitalWrite(LED, HIGH);
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);
WebSerial.msgCallback(recvMsg);
server.begin();
}
void loop() {
WebSerial.println(“Hello!”);
delay(2000);
TestVoidFunction();
}
void TestVoidFunction(){
// do nothing
}
I solved my problem.
I have to declare/define my functions TestVoidFunction above loop function before calling it, for my code to recognise it.
I never had this problem using arduino IDE and this happen only including WebSerial.h lib.
Much better should be to use PlatformIO for my projects.
Hi.
Usually, with Arduino IDE, you can define the functions before or after calling them.
With PlatformIO + VS Code, you need to define the function before any function call.
The best practice is to declare the function before calling it.
I’ll mark this issue as resolved.
If you need further help, you just need to open a new question in our forum.
Regards,
Sara