I am making an Arduino mod-bus device and I want to make a webpage as well that will allow me to reset the unit if there is any issues remotely. To do this I would need the mod-bus to operate on port 502 and the webpage to operate on port 80. is it possible for the Arduino to support two ports? I am using an Uno with a Ethernet shield and the standard ehternet.h file with a Modbus library. Any suggestions or ideas?
#include SPI.h
#include VL53L0X.h
#include Ethernet.h
#include Modbus.h
#include ModbusIP.h
#include VL53L0X.h
#include Wire.h
VL53L0X sensor;
EthernetServer server(80);
//Modbus Registers Offsets (0-9999)
const int SENSOR_Hreg0 = 100;
//ModbusIP object
ModbusIP mb;
short ts;
void setup() {
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 177 };
Wire.begin();
mb.config(mac, ip);
// Add SENSOR_Ireg register - Use addIreg() for analog Inputs
mb.addHreg(SENSOR_Hreg0);
ts = millis();
//Sensor setup
sensor.init();
sensor.setTimeout(500);
sensor.startContinuous();
//remote reset
pinMode(13, INPUT);
digitalWrite(13, LOW);
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Ethernet WebServer Example");
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// start the server
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
mb.task();
//Webpage
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
bool currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// output the value of each analog input pin
client.print("Distance Measures ");
client.print(sensor.readRangeContinuousMillimeters());
client.print("mm");
client.println("<br />");
client.print("Filter Count:");
client.print(-2.2*sensor.readRangeContinuousMillimeters()+404);
client.println("<br />");
}
client.println("</html>");
break;
}
}
//Modbus
if (millis() > ts + 100) {
ts = millis();
mb.Hreg(SENSOR_Hreg0, sensor.readRangeContinuousMillimeters() );
}
}}
Edit: Note: The issue seems to lie somewhere in with the sensor, because when I run it with out have the sensor code in there is no problem but when I add it the issue starts again.