Ok guys, I realized where the problem is. I modified the code, put some debugging statements. The code looks like this now:
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <MuxShield.h>
int IO1DigitalValues[16];
int IO2DigitalValues[16];
int IO3DigitalValues[16];
byte dataBuff[16];
//Initialize the Mux Shield
MuxShield muxShield;
byte arduinoMac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress arduinoIP(192, 168, 1, 192); // desired IP for Arduino
unsigned int arduinoPort = 8888; // port of Arduino
IPAddress receiverIP(192, 168, 1, 135); // IP of udp packets receiver
unsigned int receiverPort = 6000; // port to listen on my PC
EthernetUDP Udp;
void setup() {
Ethernet.begin(arduinoMac,arduinoIP);
Udp.begin(arduinoPort);
muxShield.setMode(1,DIGITAL_IN_PULLUP); //use DIGITAL_IN in place of DIGITAL_IN_PULLUP if internal pullups are not needed
muxShield.setMode(2,DIGITAL_IN_PULLUP);
muxShield.setMode(3,DIGITAL_IN_PULLUP);
memset(IO1DigitalValues, 0, sizeof(IO1DigitalValues));
memset(IO2DigitalValues, 0, sizeof(IO2DigitalValues));
memset(IO3DigitalValues, 0, sizeof(IO3DigitalValues));
Serial.begin(9600);
}
void loop() {
int dataChanged = readData();
if (dataChanged) {
ProcessData();
}
}
int readData() {
int dataChanged = 0;
for (int i=0; i<16; i++) {
IO1DigitalValues[i] = 1;
IO2DigitalValues[i] = 1;
IO3DigitalValues[i] = 1;
}
for (int i=0; i < 16; i++) {
int v1 = muxShield.digitalReadMS(1, i);
int v2 = muxShield.digitalReadMS(2, i);
int v3 = muxShield.digitalReadMS(3, i);
//Serial.println(v1);
//Serial.println(v2);
//Serial.println(v3);
if ((v1 != IO1DigitalValues[i]) || (v2 != IO2DigitalValues[i]) || (v3 != IO3DigitalValues[i])) {
dataChanged = 1;
IO1DigitalValues[i] = v1;
IO2DigitalValues[i] = v2;
IO3DigitalValues[i] = v3;
Serial.println("Changed");
}
}
return dataChanged;
}
void ProcessData() {
Serial.println("Process");
for(int i=0; i<16; i++) {
dataBuff[i] = (char) IO1DigitalValues[i];
}
Udp.beginPacket(receiverIP, receiverPort);
Udp.write(dataBuff, sizeof(dataBuff));
Udp.endPacket();
}
This gets the sensors data fine, prints out both "Changed" and "Process". The loop works fine only if I comment out the three "UDP" Statements. As soon as I put those three lines in my code the loop stops.
P.s. I am using Node.js to receive the UDP packets (but i don't receive anything). Below is my code for that:
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) {
console.log(msg.toString());
fs.appendFile('mydata.txt', msg.toString() + crlf, encoding='utf8');
return;
var value = '';
for(var k=0; k<msg.length ; k++) {
var v = msg.readUInt8(k);
if (v != 1) { console.log(k) ;};
}
// console.log(msg.toString());
return;
var value = msg.readUInt8(2);
if (value != 1) {
//every time new data arrives do this:
console.log("server got: " , msg.readUInt8(2) , " from " + rinfo.address + ":" + rinfo.port); // you can comment this line out
fs.appendFile('mydata.txt', msg.readUInt8(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(6000); //listen to udp traffic on port 6000