Betz (Customer) asked a question.

Is this disconnecting after every scan? if so, how can I change the coding to just have it connect and stay on until the device is powered off?

#include <Ethernet.h>

#include <P1AM.h>

#include <P1_HSC.h>

 

boolean __proBlocksDigitalRead(int pinNumber)

{

 pinMode(pinNumber, INPUT);

 return digitalRead(pinNumber);

}

 

 

bool _PBVAR_1_PrintModules= false ;

int _PBVAR_2_Slot1 = 0 ;

 

byte mac[] = {0x60, 0x52, 0xD0, 0x06, 0x8F, 0x5E}; 

IPAddress ip(192, 168, 1, 177);

EthernetServer server(80); 

 

void setup() {

  

 Serial.begin(115200);

 while(!Serial);  

 while(!P1.init()); 

 Ethernet.init(5); 

 Ethernet.begin(mac, ip);

 

 if (Ethernet.hardwareStatus() == EthernetNoHardware) {

  Serial.println("Ethernet shield is missing. Please try again");

  while (true) {

   delay(1);

  }

 }

 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() {

 // listen for incoming clients

 EthernetClient client = server.available();

 if (client) {

  Serial.println("new client");

  // an http request ends with a blank line

  boolean currentLineIsBlank = true;

  while (client.connected()) {

   if (client.available()) {

    char c = client.read();

    Serial.write(c);

  

    if (c == '\n' && currentLineIsBlank) {

    

     client.println("HTTP/1.1 200 OK");

     client.println("Content-Type: text/html");

     client.println("Connection: close");  

     client.println("Refresh: 1");  

     client.println();

     client.println("<!DOCTYPE HTML>");

     client.println("<html>"); 

     client.println("<h1>Slot 1 Module Readings</h1>");  

     client.println("<br>");

     client.println("<font size= \" 6 \" >"); 

 

 

 

     if (__proBlocksDigitalRead(31)){

      

  _PBVAR_1_PrintModules = HIGH ;

  _PBVAR_2_Slot1 = P1.readDiscrete(1, 0) ;

      Serial.println("Status");  

      Serial.println(_PBVAR_2_Slot1); 

 

      client.print("Status");  

      client.print(_PBVAR_2_Slot1);}  

     client.println("</font>");

     client.println("</html>");  

     break;

   }

    if (c == '\n') {

      

     currentLineIsBlank = true;

    } else if (c != '\r') {

      

     currentLineIsBlank = false;

    }

   }

  }

  // close the connection:

  client.stop();

  Serial.println("client disconnected");

 }

}

  


  • Macktek (Customer)

    I am new to Arduino and the code used, so I don't know much about the backend, but if you look at his line of code:

    1. server.begin();
    2.  
    3. Serial.print("server is at ");
    4.  
    5. Serial.println(Ethernet.localIP());

    If the server was constantly disconnecting after each loop, you would see multiple lines from Serial saying "Server is at "

     

    Are you seeing multiple lines output or just 1 time?

    Expand Post
  • Tinker (Customer)

    as far as I can tell it might not disconnect after every "scan" (though perhaps what you mean by "scan" could be clarified).

    But the block "if (c == '\n' && currentLineIsBlank) {" has a "break;" in it that will exit the while() loop and send one to the "client.stop();" a bit further down.

    on the other hand, if "(c == '\n' && currentLineIsBlank)" is false, you will stay in the while loop and stay connected, but nothing very interesting happens, if something does happen then the while loop is exited and program will disconnect.

     

    I really don't understand what the program in intended to do, but I suspect that for;

    "how can I change the coding to just have it connect and stay on until the device is powered off?"

    just removing that "break;" might do it?

    Expand Post