Objective-J
Objective-J

Reputation: 319

Arduino with Node.js not working

I'm working on a project which has an Arduino Uno and using a UDP connection, it'll send the data to my Mac running a Node.js module to get that data and print it out.

Here's my Arduino code :

#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);
}

Here's the code for my Node.js module :

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);

When I run the code, there is no value printed in the Terminal. What's going wrong? Thanks.

Upvotes: 1

Views: 359

Answers (1)

Gntem
Gntem

Reputation: 7155

you shouldn't capitalize the events of dgram socket.

  1. Message should be message
  2. Listening should be listening

so a small example of this

var dgram = require("dgram");
var server = dgram.createSocket("udp4");

server.on('listening' /*Correct*/,function(){ 
 console.log("it fires"); 
});

server.on('Listening' /*Wrong*/,function(){
 console.log("it doesn't fire");
});

server.bind(6000);

Upvotes: 1

Related Questions