I am using the websocket library with the following code:
#include "Spark-Websockets/Spark-Websockets.h"
WebSocketClient client;
char server[] = "xxx.xxx.xxx.xxx";
int port = 4000;
int sensor0 = 0;
int sensor1 = 1;
int sensor2 = 2;
int sensor3 = 3;
int sensor4 = 4;
int sensor5 = 5;
int sensorState0;
int sensorState1;
int sensorState2;
int sensorState3;
int sensorState4;
int sensorState5;
int bounceDelay = 150;
void setup() {
client.connect(server, port);
//client.onMessage(onMessage);
pinMode(sensor0, INPUT_PULLUP);
pinMode(sensor1, INPUT_PULLUP);
pinMode(sensor2, INPUT_PULLUP);
pinMode(sensor3, INPUT_PULLUP);
pinMode(sensor4, INPUT_PULLUP);
pinMode(sensor5, INPUT_PULLUP);
//Serial.begin(9600);
}
void loop() {
client.monitor();
sensorState0 = digitalRead(sensor0);
sensorState1 = digitalRead(sensor1);
sensorState2 = digitalRead(sensor2);
sensorState3 = digitalRead(sensor3);
sensorState4 = digitalRead(sensor4);
sensorState5 = digitalRead(sensor5);
if(sensorState0 == LOW){
client.send("0");
Serial.println("0");
delay(bounceDelay);
}
if(sensorState1 == LOW){
client.send("1");
Serial.println("1");
delay(bounceDelay);
}
if(sensorState2 == LOW){
client.send("2");
Serial.println("2");
delay(bounceDelay);
}
if(sensorState3 == LOW){
client.send("3");
Serial.print("3");
delay(bounceDelay);
}
if(sensorState4 == LOW){
client.send("4");
Serial.println("4");
delay(bounceDelay);
}
if(sensorState5 == LOW){
client.send("5");
Serial.println("5");
delay(bounceDelay);
}
//delay(100);
// client.send("ALIVE!");
}
void onMessage(WebSocketClient client, char* message) {
// Serial.print("neChimes server returns (echoes): ");
// Serial.println(message);
}
The core connects to the cloud and I can flash it fine. When the Serial port (debugging) is activated I see the pins being read by the core. The only problem is that if the pins are grounded (a button press, basically) in rapid succession the communication to the WebSockets server is severed (or hangs, don’t know which). Yet, I can still talk to the server with wscat, so the server is still up and fine. Again, the pins are being read (per the Serial output) but the client.send() is being ignored.
I see that the client.monitor() function/method checks for .connected() and will try to reconnect if their is any problem. This doesn’t appear to be happening, however.
Also, if two pins are grounded (pressed) at the same time, the Core reboots (?), flashes the red SOS, then 1 flash (fault). Not sure what’s up with that, though it might be indicative of what is happening internally. A reboot (manual, or otherwise) fixes the problem momentarily, but then the problem occurs again.
I am coming from a Processing background with a bit of Arduino in there as well. Is there a better way to poll the pins.
Any ideas would be greatly appreciated.
Jason