1
我正在一個項目有一個Arduino Uno和使用UDP連接,它會發送數據到我的Mac運行一個Node.js模塊來獲取該數據並打印它出。Arduino與Node.js不工作
這裏是我的Arduino代碼:
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
//Import the necessary packages.
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; //Arduino's MAC address.
IPAddress IP(192, 168, 1, 152); //Arduino's IP address.
unsigned int arduinoPort = 8888; //Arduino's transmission port.
IPAddress recieverIP(192, 168, 1, 77); //Mac's IP address.
unsigned int recieverPort = 6000; //Mac's transmission port.
EthernetUDP udp;
int sensorPin = 2; //The pin on the Arduino the PIR sensor is connected to.
int sensorStatus; //The PIR sensor's status.
void setup()
{
Serial.begin(9600);
Ethernet.begin(mac, IP); //Starting the Ethernet functionality.
udp.begin(arduinoPort); //Starting the UDP server's functionality.
}
void loop()
{
Serial.println("YES");
udp.beginPacket(recieverIP, recieverPort);
udp.write("YES");
udp.endPacket();
delay(10);
}
下面是我的Node.js模塊的代碼:
var dgram = require('dgram');
var server = dgram.createSocket("udp4");
var fs = require('fs');
var crlf = new Buffer(2);
crlf[0] = 0xD;
crlf[1] = 0xA;
server.on("Message", function(msg, rinfo)
{
console.log("Server got : " + msg.readUInt16LE(0) + " from : " + rinfo.address + " : " + rinfo.port);
});
server.on("Listening", function()
{
var address = server.address();
console.log("Server listening @ " + address.address + " : " + address.port);
});
server.bind(6000);
當我運行的代碼,也沒有在終端打印的值。出了什麼問題?謝謝。