【发布时间】: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