【问题标题】:Failure to connect to Node.js web server from Arduino Ethernet Shield无法从 Arduino Ethernet Shield 连接到 Node.js Web 服务器
【发布时间】:2014-02-06 16:34:37
【问题描述】:

我打算使用 express 和 Socket.IO 在我的 Arduino(通过 Ethernet Shield)和 Node.js Web 服务器之间创建持久连接——这样我就可以在没有打开并重新打开连接。

不幸的是,我对 Arduino 和 Node 的世界都很陌生,而且我似乎在第一个障碍中落下,我使用了我在互联网上找到的各种代码来创建服务器和客户端,但没有有用。

代码sn-ps请看下面:

Arduino 代码

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x0E, 0x96 };

char server[] = { 192, 168, 1, 2 };

// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);

  delay(5000);

  Serial.println(server);

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac);
  }

  delay(5000);

  Serial.print("My IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print("."); 
  }

  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println();
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 3000)) {
    Serial.println("connected");
    client.write("GET / HTTP/1.1");
  } 
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop()
{
  // if there are incoming bytes available 
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while(true);
  }
}

节点代码

var express = require('express');
var routes = require('./routes');
var http = require('http');

var app = express();
var server = app.listen(3000);
var io = require('socket.io').listen(server); // this tells socket.io to use our express server

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.static(__dirname + '/public'));
  app.use(express.methodOverride());
  app.use(app.router);
});

app.configure('development', function(){
  app.use(express.errorHandler());
});

app.get('/', routes.index);


console.log("Express server listening on port 3000");

io.sockets.on('connection', function (socket) {
    console.log('A new user connected!');
    socket.emit('info', { msg: 'The world is round, there is no up or down.' });
});

执行结果 - 串行监视器 在这张图片上看起来好像盾牌似乎与某物建立了联系,但是连接立即断开(为使用 imgur 道歉,我还没有足够的代表来发布图片!)

执行结果 - 节点控制台 如您所见,节点不共享 Arduino 监视器对正在建立的连接的热情并处于空闲状态。

在此阶段,任何帮助、提示或建议都将不胜感激,过去几天我一直在与此作斗争,并尝试了各种不同类型的服务器,但无论如何都会发生同样的事情。

提前致谢!

【问题讨论】:

    标签: node.js express socket.io arduino


    【解决方案1】:

    请仔细注意 HTTP 中所需的终止符,请参阅

    HTTP protocol at W3

    服务器将严格要求客户端遵循协议。请仔细注意第 5 节和第 5.1 节中的终止:

    整个请求是:

    Request = Request-LineCRLF

    在你的情况下,一行是:

    请求行 = GET / HTTP/1.1CRLF

    组合意味着在一个简单请求的末尾有 2 对 CR LF。

    试试:

    "GET / HTTP/1.1\r\n\r\n"
    

    然后您应该会看到服务器确认请求。

    【讨论】:

    • 感谢您的回复,并为迟到的回复道歉!这里的问题是我试图使用 HTTP 请求与 WebSockets 服务器通信!这显然行不通!
    猜你喜欢
    • 2014-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多