Comments (7)
Hi Gil,
You certainly replied very quickly! Do you ever sleep????
Answering your points:
Firstly, I do not believe the server code is running before the completion of the setup. The inline code and the library code each have state machines. This means that with the inline code the first operation in the main loop is to run the client code once (first state) and on success the server code runs (seconds state). Once set the second state is the only code that runs.
The library code is called by a “poll” in the main loop once the setup code has run. This is the only code running in the main loop. Also, the connection to the Wi-Fi is established in the setup code and I would have thought that the library code would fail if it ran before the setup code but it doesn’t since the first operation is to run the client code.
Secondly, I don’t mind the code being public but it will probably need some explanation.
I will ZIP up all the files and send you a copy of them.
Finally, I will run the code again increasing the ESP32 log level and report on what I see.
Thank you for your reply, Regards, Alan.
from arduinowebsockets.
Hi Alan,
Sounds interesting, I can't think off the top of my head of a reason why the server code won't work inside a library. Can you try copy-pasting the server code to it's own sketch just to make sure it works?
Also, when inside the library, is it possible that the server code runs before setup
is called?
I thank you for the kind words. I really appreciate it and glad this library is useful for others. If you don't mind attaching the example project you mentioned, doing it might help. If you rather keep it less-public you could email it to me.
Last thought: running the code with esp32 logs and attaching those here can be helpful. Maybe it will shed some light on any unexpected events occurring while trying to listen for connections.
Thank you again and best wishes,
Gil.
from arduinowebsockets.
//Example code that illustartes the use of the webSockets client and
//webSockets server code in the same sketch
//The client sends a message to a server; that server needs to send a response
//If this is successful, the sketch runs the server code in an endless loop
//replying to each message received.
//A lot of trace code is included with comments to show the path through the sketch
#include <WiFi.h> //for ESP32
#include <ArduinoWebsockets.h>
int my_state = 0; //0==initial client send then 1==server responds
const char* ssid = ""; //change this to your wifi SSID
const char* password = "";//change this to your wifi password
const char* host = "192.168.1.30";//change this to the IP address of a system
//that can received a simple message
using namespace websockets;
websockets::WebsocketsServer socket_server;
websockets::WebsocketsClient socket_client;
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println();
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi SSID: "); Serial.println(ssid);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.print(".");
}
Serial.println(" connected");
Serial.print("LAN Mac Address: "); Serial.println(WiFi.macAddress());
Serial.print("Default Host name: ");
Serial.println(WiFi.getHostname()); //for ESP32
Serial.print("Local IP: "); Serial.println(WiFi.localIP());
socket_server.listen(8085); // server will listen
my_state = 0;//initial state
}
void loop() {
if (my_state == 0) {
//client code
WebsocketsClient myclient;//to send responses to ESP32 sockets Cloud
if (myclient.connect(host, 8086, "/")) {
Serial.print("Connected to: "); Serial.println(WiFi.getHostname());
if (myclient.available())
{
myclient.send("REG");//send initial command
auto msg = myclient.readBlocking();
if (msg.isText()) {
String msg_text = msg.data();
Serial.println("Got message:" + msg.data());
Serial.println(msg_text);
Serial.printf("Message received: %s\n", msg_text);
Serial.printf("Message length: %d\n", msg_text.length());
if (msg.data() == "ACK") {
Serial.println("ACK received");
}
Serial.println("REG received successfully");
} else {
Serial.println("Got something unexpected");
}
myclient.close();//disconnect from cloud
my_state = 1;//now into state 1 - socket server
}
}
} else {
//server code
if (socket_server.available()) { //server is running so wait for connections
// bool client_waiting = socket_server.poll(); //see if a client is waiting to connect
if (socket_server.poll()) {
//there is so accept connection
Serial.println("Client waiting to connect");
auto client = socket_server.accept();
Serial.println("Client connected");
auto message = client.readBlocking();
if (message.isText()) {
String msg_text = (String)message.data();
Serial.printf("Message received: %s\n", msg_text);
Serial.printf("Message length: %d\n", msg_text.length());
if (message.data() == "ping") {
Serial.println("Message received was a ping");
}
client.send("Echo: " + message.data());
}
client.close();
Serial.println("Client connection closed");
}
} else {
Serial.print("In inline code Server not running on: ");
Serial.println(WiFi.localIP());
delay(30000);
}
}
}
from arduinowebsockets.
Hi,
Is it possible that you are missing a call to socket_server.listen
?
Also, if you have using namespace
you don't need the websockets::
prefix 😄
Let me know if I'm missing something or if this solve your issue.
Gil.
from arduinowebsockets.
Hi Gil,
I was indeed missing 'socket_server.listen(8085)' in the library code! I feel somewhat foolish....
This statement is in the setup code.
However, with those statements included in the library code I still get the same symptoms but I think you may be on to something. I suspect there may be a scoping issue or an issue with variables being re-declared on each call of the library.
I'm working on some more testing at the moment - I'll let you know if I make a breakthough.
Alan.
from arduinowebsockets.
I'm glad I was able to help.
No need to feel foolish, feel free to ask anything in a new issue or email!
I'll leave this issue open for a few days just in case you'll have any follow-up questions (you can close it yourself at any point).
Best wishes,
Gil.
from arduinowebsockets.
Gil,
Many thanks for the pointer to something that should have been obvious to me – the missing 'socket_server.listen(8085)'. As I mentioned even after correcting this I got the same symptoms but you pointed me to a scoping issue and that was the problem.
I removed all the inline code from the sketch and still found the same problem. However, when I finally removed all references to the webSockets library in the sketch – even the ‘#include <ArduinoWebsockets.h>’ I started to get some peculiar errors. This eventually pointed to a problem with the declaration ‘WebsocketsServer socket_server;’ in the library code. It would seem the declaration has to be in the ‘private:’ section of the library header file (where it probably ought to be) rather than at the start of the code which used the ‘socket_server’ variable. I have the ‘WebsocketsClient myclient’ declaration at the start of the code where the variable 'myclient' is used so I was convinced my original code should work.
Why it didn’t work in the first place (apart from my foolish error) is a mystery to me but at last the code is now working.
I have to thank you again for your help.
Regards and best wishes, Alan.
from arduinowebsockets.
Related Issues (20)
- Hieromon/AutoConnect & WebSockets Client on ESP8266 - Can't Establish wss
- Secured-ESP8266-Client can't seem to receive any messages while connection is open HOT 1
- Compilation error: 'WebsocketsClient' does not name a type ?? HOT 3
- Getting a error from the TCPServer using the Arduino GIGA R1 WIFI on the ESP-32Client code. HOT 10
- ESP32-CAM WebSocket Server: Failure to Stream Camera Feed after Page Refresh or Multiple Connections
- WSS Connection error
- [ 5236][E][WiFiClient.cpp:313] setSocketOption(): fail on -1, errno: 9, "Bad file number" HOT 2
- Reconnecting after lost connection HOT 2
- Disconnecting almost inmediately
- Random disconnects, fails on binary ping including 0x00 HOT 1
- Issue with WSS and ESP 32 HOT 4
- Esp32 WebSocket Client won't work with WebSocket Node.js server. HOT 3
- Client-Message-Buffer
- ESP32 Server disconnected when receiving messages HOT 1
- Compilation errors with arduino_esp32 version 3.0.0 HOT 6
- wrong username
- Project is dead
- network::TcpServer* _server HOT 1
- Getting my connection closed when on node js wss server HOT 4
- Esp8266-Client example compiles with warnings, maybe others do too. HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from arduinowebsockets.