I'm trying to make a small WiFi control board with the use of an ESP8266 ESP-201 board.
I used the example provided for WebSocket for Arduino, with a little bit of modification to be able to handle JSON messages. This is the code I got to:
/*
* WebSocketClient.ino
*
* Created on: 24.05.2015
*
*/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WebSocketsClient.h>
#include <ArduinoJson.h>
#include <Hash.h>
ESP8266WiFiMulti WiFiMulti;
WebSocketsClient webSocket;
#define USE_SERIAL Serial
ArduinoJson::StaticJsonBuffer<200> jsonBuffer;
void webSocketEvent(WStype_t type, uint8_t * payload, size_t lenght) {
switch(type) {
case WStype_DISCONNECTED:
USE_SERIAL.printf("[WSc] Disconnected!\n");
break;
case WStype_CONNECTED: {
USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload);
// send message to server when Connected
}
break;
case WStype_TEXT: {
USE_SERIAL.printf("[WSc] get text: %s\n", payload);
String text = String((char *) &payload[0]);
USE_SERIAL.println(text);
JsonObject& root = jsonBuffer.parseObject(text);
USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", root["r"]);
USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", root["g"]);
USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", root["b"]);
}
// send message to server
break;
case WStype_BIN:
USE_SERIAL.printf("[WSc] get binary lenght: %u\n", lenght);
hexdump(payload, lenght);
// send data to server
break;
}
}
void setup() {
USE_SERIAL.begin(115200);
USE_SERIAL.setDebugOutput(true);
USE_SERIAL.println();
USE_SERIAL.println();
USE_SERIAL.println();
for(uint8_t t = 4; t > 0; t--) {
USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
USE_SERIAL.flush();
delay(1000);
}
WiFiMulti.addAP("GamersHeavenLow", "nCore4Life");
while(WiFiMulti.run() != WL_CONNECTED) {
delay(100);
}
webSocket.begin("192.168.0.104", 3000);
webSocket.onEvent(webSocketEvent);
}
void loop() {
webSocket.loop();
}
And this is the server side I used to:
var express = require('express');
var app = express();
var url = require('url');
var http = require('http').Server(app);
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({ server: http });
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
app.use(express.static('public'));
wss.on('error', function(error) {
console.log(error);
});
wss.on('connection', function connection(ws) {
var location = url.parse(ws.upgradeReq.url, true);
// you might use location.query.access_token to authenticate or share sessions
// or ws.upgradeReq.headers.cookie (see http://stackoverflow.com/a/16395220/151312)
console.log('connected');
ws.on('message', function incoming(message) {
console.log(message);
wss.clients.forEach(function each(client) {
client.send(JSON.stringify(message));
});
});
//ws.send('something');
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
Before I started to work with the JSON encoding it was working for a while, but now it's not. I get the following error:
[SETUP] BOOT WAIT 4...
scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 2
cnt
connected with GamersHeavenLow, channel 6
dhcp client start...
ip:192.168.0.101,mask:255.255.255.0,gw:192.168.0.1
[SETUP] BOOT WAIT 3...
[SETUP] BOOT WAIT 2...
[SETUP] BOOT WAIT 1...
[WS-Client] connect ws...
[WS-Client] connection to 192.168.0.104:3000 Faild
[WS-Client] client disconnected.
[WSc] Disconnected!
[WS-Client] connect ws...
[WS-Client] connection to 192.168.0.104:3000 Faild
[WS-Client] client disconnected.
[WSc] Disconnected!
I believed it must have been the JSON decoding, so I reverted back to the default example, but I still get the same message, connection failed.
So I tried to use the echo websocket server, with which the Arduino code started working.
So I figured out it must be a server side issue, so I tested my node.js websocket server with a pure WebSocket client, but that is also working without a problem.
So basically I have two separate sets of code, which are running in isolation from one another without a problem, but they don't want to play along together.
Any idea what might be causing this?
WebSocket server used: https://github.com/websockets/ws
WebSocket client used on ardruino: https://github.com/Links2004/arduinoWebSockets