Creating a Web Server: Multiplexer & Ethernet Shield

Hi All,

I am trying to send the data from a bunch of sensors to a web server that I've written using Node.js. I have written the following code but it doesn't work. Can someone help me identify the problem?

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

int IO1DigitalVals[16];
int IO2DigitalVals[16];
int IO3DigitalVals[16];

MuxShield muxShield;

byte arduinoMac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress arduinoIP(192, 168, 1, 177); 
unsigned int arduinoPort = 8888;     

IPAddress receiverIP(192, 168, 0, 197); 
unsigned int receiverPort = 8000;      

EthernetUDP Udp;


void setup() {
  Ethernet.begin(arduinoMac,arduinoIP);
  Udp.begin(arduinoPort);
  muxShield.setMode(1,DIGITAL_IN_PULLUP);
  muxShield.setMode(2,DIGITAL_IN_PULLUP);
  muxShield.setMode(3,DIGITAL_IN_PULLUP);
  
}

void loop() {
  
    for (int i=0; i<16; i++)
  {
    //Digital read on all 16 inputs on IO1, IO2, and IO3
    IO1DigitalVals[i] = muxShield.digitalReadMS(1,i);
    IO2DigitalVals[i] = muxShield.digitalReadMS(2,i);
    IO3DigitalVals[i] = muxShield.digitalReadMS(3,i);
  
 byte valueInBytes [i]= (byte) IO1DigitalVals[i];
 
  Udp.beginPacket(receiverIP, receiverPort); 
  Udp.write(valueInBytes[i]);
  Udp.endPacket();
 } 
}

I have written the following code but it doesn't work.

Assuming that the code you pasted doesn't actually look like that, it DOES work. It just doesn't do what you want. But, without knowing what it ACTUALLY does, and without knowing what you want it to do, we don't have a snowballs chance in hell of helping you.

hsayah:
I am trying to send the data from a bunch of sensors to a web server that I've written using Node.js.

The term 'web server' usually means something that handles HTTP(S) protocol requests. Your client appears to be sending an arbitrary UDP packet which is definitely NOT an HTTP request.

What protocol do you actually intend to use between your client and server?

PaulS:

I have written the following code but it doesn't work.

Assuming that the code you pasted doesn't actually look like that, it DOES work. It just doesn't do what you want. But, without knowing what it ACTUALLY does, and without knowing what you want it to do, we don't have a snowballs chance in hell of helping you.

Hey Paul,

So what I'm trying to do is to send data from some sensors connected to digital pins of my Arduino. They send 0 and 1 only. I'm trying to send them to a localhost created on my computer. When I run Nodejs I don't receive any data nor I receive the specific IP address. I tried passing the data to serial monitor as well just to test and see if I can pass the data but didn't work. So that's why I'm stuck.

PeterH:

hsayah:
I am trying to send the data from a bunch of sensors to a web server that I've written using Node.js.

The term 'web server' usually means something that handles HTTP(S) protocol requests. Your client appears to be sending an arbitrary UDP packet which is definitely NOT an HTTP request.

What protocol do you actually intend to use between your client and server?

Hi Peter,

I'm trying to use UDP packet from my Arduino. That's the only way I'm aware of to send data from my Arduino and Ethernet Shield.
And on the other end (node.js) I am not creating a web server using HTTP. (Sorry for confusion). Here's the code I have for the Node.js application:

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

var crlf = new Buffer(2);
crlf[0] = 0xD; //CR - Carriage return character
crlf[1] = 0xA; //LF - Line feed character

server.on("message", function (msg, rinfo) { //every time new data arrives do this:
  console.log("server got: " + msg.readUInt16LE(0) + " from " + rinfo.address + ":" + rinfo.port); // you can comment this line out
  fs.appendFile('mydata.txt', msg.readUInt16LE(0) + crlf, encoding='utf8');//write the value to file and add CRLF for line break
});

server.on("listening", function () {
  var address = server.address();
  console.log("server listening " + address.address + ":" + address.port);
});

server.bind(8000); //listen to udp traffic on port 8000

Server and client are not in the same subnet. There is a router in between?

zoomx:
Server and client are not in the same subnet. There is a router in between?

Yes there is. How can I fix this problem?

What IP address has your UDP server actually bound to?

Have you confirmed that devices in the subnet your Arduino is in can reach the subnet that your server is in?

hsayah:
I'm trying to use UDP packet from my Arduino. That's the only way I'm aware of to send data from my Arduino and Ethernet Shield.

You can use any TCP/IP or UDP/IP protocol. UDP can work (assuming you have provided IP connectivity and implement the client and server correctly) but it's far from the only option.

UDP is a protocol without any check. Arduino send the packet without any control, so it may arrive or may not. Firts check that communication between the two subnet, using UDP, is working.
If there is not a particular reason for using UDP, use TCP/IP because in this case there is a check about packet sending and there is a first check than connection is established.