首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >node.js ws WebSocket服务器的Arduino websocket客户端连接问题

node.js ws WebSocket服务器的Arduino websocket客户端连接问题
EN

Stack Overflow用户
提问于 2016-03-14 06:11:04
回答 1查看 3.1K关注 0票数 0

我想用ESP8266的ESP-201板做一个小型的WiFi控制板。

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

代码语言:javascript
复制
/*
 * 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();
}

这是我曾经使用的服务器端:

代码语言:javascript
复制
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编码之前,它工作了一段时间,但现在不是了。我得到以下错误:

代码语言:javascript
复制
[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

EN

回答 1

Stack Overflow用户

发布于 2017-01-09 02:15:53

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35976512

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档