与node.js的Arduino WebSocket客户端连接问题ws websocket server

时间:2016-03-13 22:11:04

标签: node.js websocket arduino esp8266

我正在尝试使用ESP8266 ESP-201板制作一个小型WiFi控制板。

我使用了为Arduino的WebSocket提供的示例,并进行了一些修改以便能够处理JSON消息。这是我得到的代码:

/*
 * 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();
}

这是我以前的服务器端:

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');
});

在我开始使用JSON编码之前,它已经工作了一段时间,但现在却没有。我收到以下错误:

[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!

我认为它一定是JSON解码,所以我恢复了默认示例,但我仍然收到相同的消息,连接失败。

所以我尝试使用echo websocket服务器,Arduino代码开始工作。

所以我发现它一定是服务器端问题,所以我使用纯WebSocket客户端测试了我的node.js websocket服务器,但这也没有问题。

所以基本上我有两组独立的代码,它们彼此隔离运行没有问题,但是它们不想一起玩。

知道可能导致这种情况的原因是什么?

使用的WebSocket服务器:https://github.com/websockets/ws

ardruino上使用的WebSocket客户端:https://github.com/Links2004/arduinoWebSockets

1 个答案:

答案 0 :(得分:0)

您需要使用WS module而不是socket.io。