【问题标题】:Arduino WebSocket Client connection issue with node.js ws websocket serverArduino WebSocket客户端与node.js ws websocket服务器的连接问题
【发布时间】:2016-06-28 20:10:19
【问题描述】:

我正在尝试使用 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 解码,所以我恢复到默认示例,但我仍然收到相同的消息,连接失败。

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

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

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

知道是什么原因造成的吗?

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

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

【问题讨论】:

  • 删除 var io = require('socket.io')(http);线。令人困惑。
  • 删除它,这是一个错字:) 之前我尝试过使用它。
  • 在错误情况下,服务器端显示哪些调试行?
  • 可能是版本不匹配,但两个库都是最新的。

标签: node.js websocket arduino esp8266


【解决方案1】:

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

【讨论】:

  • 请提供一个小例子来提供一些背景信息。
  • 它已经在使用:var WebSocketServer = require('ws').Server;
  • 我已经在用WS了,io是不小心放在那里的,因为我也试过用。
猜你喜欢
  • 2015-06-18
  • 1970-01-01
  • 1970-01-01
  • 2019-11-29
  • 2016-05-21
  • 2018-04-11
  • 2013-12-09
  • 2022-06-27
  • 1970-01-01
相关资源
最近更新 更多