0

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.

  • to test, run two instances of a web server ... one on port 80 and one on port 502 .... access the webpage using two devices so that you are serving a page to two different devices at the same time .... if that works, then try adding some modbus test code (if such a thing exists) instead of a webserver on port 502 – jsotola Nov 1 '18 at 22:46
  • I tried that and it didn't take the ip address for the webpage but took it for the modbus, and when I added a second instance of the ip to see that would work it errored out. – Michael H. Nov 1 '18 at 22:50
  • EthernetServer has nothing to do with IP. Servers should run on a different ports on the same IP. – Juraj Nov 2 '18 at 5:34
  • try to print the IP before server.begin() – Juraj Nov 2 '18 at 13:57
  • Same result when I moved the Serial.print("server is at "); Serial.println(Ethernet.localIP()); before the server.begin() – Michael H. Nov 2 '18 at 14:00
1
  • move the modbus refresh out of the if (client) condition

  • mb.begin() calls Ethernet.begin(). Do not repeat it in setup()

  • move client.println("</html>"); break; inside the if block before it
  • copy the rest of the detection of empty line from the WebServer example

  • stop() the client after response

  • change type of ts to unsigned long

| improve this answer | |
  • I've added to code I am using, I have port 80 assigned and the modbus library sets a port 502 for the modbus. When I use this I can ping the device but Modbus and web communication is not there. – Michael H. Nov 2 '18 at 13:33
  • When i check the serial monitor, I planned to remove it once this was working, it reports the IP address as 0.0.0.0 – Michael H. Nov 2 '18 at 13:47
  • but it is not the IP address of the EthernetServer, it is the IP of the shield. the ip is reported wrong or the shield did not connect. try to connect with DHCP. – Juraj Nov 2 '18 at 13:56
  • when I comment out all the webpage parts it works fine, and I can even ping the device with the code in using the set ip. – Michael H. Nov 2 '18 at 13:57
  • I removed Ethernet.begin() and tried it out and it still didn't work, I can still ping the device but getting Modbus data or the webpage still doesn't work. – Michael H. Nov 2 '18 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.