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.