Establish connection between Arduino + Ethernet shield and Node.js web server.

Hi all,

I’m embarking on a project that involves sending data intermittently from an Arduino Uno to a Node.js web server via the Ethernet Shield, though I seem to be falling at the first hurdle.

I’m trying to establish a persistent connection from the Arduino so that when the data is ready it is sent straight away and vice verse as the web server will also be sending data to the Arduino.

I’m new to both Arduino and Node - so any assistance would be greatly appreciated, a lot of the code is chopped and copied from various examples I have found around the internet - hopefully the issue is rather trivial!

Arduno Code

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

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x0E, 0x96 };

char server[] = { 10, 167, 125, 235 };

EthernetClient client;

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

  Serial.println(server);
  
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
  }
  
  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");
  } 
  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);
  }
}

Node Code

Serial Monitor (I’m not sure what the guff is at the beginning, notice it appears to make a connection but disconnects straight away)

Node Console (I would expect the code at the bottom of the Node.js code above to state a client has connected, though the console is completely blank and doesn’t replicate the Arduinos serial monitors optimism in regard to a connection being made)

Tips, pointers and thoughts all welcome and many thanks in advance.

Not quite what you asked for, but take a look at bitlash-commander. It provides some instruction for talking to Arduino with node.js:

If you get it working, you'll be many steps closer to what you are trying to do.

-transfinite

I think You clearly let the Arduino scan for an incoming client request,

so when the Arduino See that there is no client request he closes the whole
client connection,

this happens all in the while loop every cycle so the client has no time to connect because the connection
is already closed in the past couple of seconds,

place the if(!client.connected) in a function and call that function when a client is connected, then when the
client disconnect the connection will be closed!