I am trying to incorporate the web socket library and have some questions about how to achieve what i need. First, I downloaded the library from github and viewed the example. Everything installed fine once i changed the shield type from Ethernet to Wifi. Now I run the code and i am connected to the internet (I have a green light on my Wifi Shield). Now I want to listen for messages on a website. I already have the php file and server set up. Basically, the php file turns on and off an LED by writing a 1 or a 0 to a txt file. Currently, I am polling or making an http request to that file every 20 seconds. However I have like 5 or so events going on and i dont want to keep polling every 20 seconds. I just want it to listen for any change while still being connected which is what the web socket library is supposed to be doing im pretty sure. So I guess my question is, ,,,i dont want any actions in my loop and would rather have it in a void. Can I listen for upcoming messages in a void function or setup that will listen for any changes to that txt file that is on the internet server and if that change happens the LED will turn on or off? As I said before, I already have the LED successfully turning on and off from a website using php and txt. I just dont want to poll and would rather stay connected and just listen so my other events in the background can do what they are doing without any possibility of interruption. The web socket code example is posted below. Please, any advice on this matter is most appreciated.
#include <SPI.h>
#include <WiFi.h>
// Enabe debug tracing to Serial port.
#define DEBUG
// Here we define a maximum framelength to 64 bytes. Default is 256.
#define MAX_FRAME_LENGTH 64
#include <WebSocket.h>
char ssid[] = "xx; //SSID Home Network Name
char pass[] = "xx"; //Key or Network Password
// Create a Websocket server
WebSocket wsServer;
void onConnect(WebSocket &socket) {
Serial.println("onConnect called");
}
// You must have at least one function with the following signature.
// It will be called by the server when a data frame is received.
void onData(WebSocket &socket, char* dataString, byte frameLength) {
#ifdef DEBUG
Serial.print("Got data: ");
Serial.write((unsigned char*)dataString, frameLength);
Serial.println();
#endif
// Just echo back data for fun.
socket.send(dataString, strlen(dataString));
}
void onDisconnect(WebSocket &socket) {
Serial.println("onDisconnect called");
}
void setup() {
#ifdef DEBUG
Serial.begin(9600);
#endif
WiFi.begin(ssid, pass); // WPA/WPA2 Connection
wsServer.registerConnectCallback(&onConnect);
wsServer.registerDataCallback(&onData);
wsServer.registerDisconnectCallback(&onDisconnect);
wsServer.begin();
delay(100); // Give Ethernet time to get ready
}
void loop() {
// Should be called for each loop.
wsServer.listen();
// Do other stuff here, but don't hang or cause long delays.
delay(100);
if (wsServer.isConnected()) {
wsServer.send("abc123", 6);
}
}