I'm having issues implementing a web socket on the Arduino/Genuino to procress a string received and do something respectively.
I'm using SimpleWebSocket example, edited for my purpose, from the ArduinoHttpClient...
don't seam to get the hangs of it. don't know what i'm doing wrong, cannot store the received string object on a string variable or use the readString() method on and if statement to compare with a string to decide what flow path to get in to
This is the code im trying:
/*
Simple WebSocket client for ArduinoHttpClient library
Connects to the WebSocket server, and sends a hello
message every 5 seconds
note: WiFi SSID and password are stored in config.h file.
If it is not present, add a new tab, call it "config.h"
and add the following variables:
char ssid[] = "ssid"; // your network SSID (name)
char pass[] = "password"; // your network password
created 28 Jun 2016
by Sandeep Mistry
this example is in the public domain
*/
#include <ArduinoHttpClient.h>
#include <WiFi101.h>
#include "config.h"
char serverAddress[] = "192.168.1.140"; // server address
int port = 8000;
WiFiClient wifi;
WebSocketClient client = WebSocketClient(wifi, serverAddress, port);
int status = WL_IDLE_STATUS;
int count = 0;
void setup() {
pinMode(6, OUTPUT);
Serial.begin(9600);
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid); // print the network name (SSID);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
}
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
}
void loop() {
Serial.println("starting WebSocket client");
client.begin();
while (client.connected()) {
int messageSize = client.parseMessage();
if (messageSize > 0) {
String test = client.readString();
Serial.println(client.readString());
Serial.println(test);
if (client.readString() == "off") {
Serial.println("low");
digitalWrite(6, LOW);
}
else {
Serial.println("high");
digitalWrite(6, HIGH);
}
}
}
Serial.println("disconnected");
}
On the example above, you can see in the void loop, after I check there's incoming data with messsageSize I store the incoming data using readString on a variable test, which I then println, this shows and empty line, although when I directly via println readString it shows some data, so the data is coming in... subsequently when I try to use the data from the incoming buffer on and if statement to compare with a string is wont work.
I've tried everything I can think off, puting the string to compare in to a variable and compare them, using Compare method from the object and I cannot even remember what else.
does anyone have any advice on how to do this, maybe the library is buggie, maybe I'm doing it wrong, any other libraries that anyone can recommend, I've looked around and this is the only one that looked simple enough to get started with and had the most examples.