Can't print values in webpage

I have made a project where I am reading all the parameters from an Energy Meter. I want to publish all the data to a local webpage. I made the webpage and able to send the parameter name. But I am not able to send the Parameter value.

The issue I noticed is that, whenever I am running the code without adding the Ethernet.h library, It's showing all the values correctly in serial monitor. But When I am initializing the library, it's neither showing value in serial monitor nor in webpage. Only throwing Parameter name (i.e Total Active Energy etc.). I am even tried putting constant value but same issue.

  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");
                    client.println();
                    // send web page
                    client.println("<!DOCTYPE html>");
                    client.println("<html>");
                    client.println("<head>");
                    client.println("<title>Monitoring</title>");
                    client.println("</head>");
                    client.print("<meta http-equiv=\"refresh\" content=\"5\">");
                    client.println("<body>");
                    client.println("<h2>Monitoring Page</h2>");
                    client.println("<p>A web page from the Arduino server</p>");
  //--------------------------------------------------------------------------------
                    
      uint16_t TAE, TAE1, TAE2;
      TAE = node.readInputRegisters(0x01, 2);
      
      Serial.print("Total Active Energy: ");
      client.print("Total Active Energy: ");  
      if (TAE == 0)
      {
          TAE1 = node.getResponseBuffer(0x00);
          TAE2 = node.getResponseBuffer(0x01);
          
          long int num_1 = (((unsigned long)TAE1 << 16) | TAE2);
          float numf_1;
          memcpy(&numf_1, &num_1, 4);
          Serial.print(numf_1);
          Serial.println(" kWh");

          client.print(numf_1);
          client.println(" kWh");
      }
      


      uint16_t IAE, IAE1, IAE2;
      
      IAE = node.readInputRegisters(0x03, 2);
      
      Serial.print("Import Active Energy: ");
      client.print("Import Active Energy: ");    
      if (IAE == 0)
      {
          IAE1 = node.getResponseBuffer(0x00);
          IAE2 = node.getResponseBuffer(0x01);
          
          long int num_2 = (((unsigned long)IAE1 << 16) | IAE2);
          float numf_2;
          memcpy(&numf_2, &num_2, 4);
          Serial.print(numf_2);
          Serial.println(" kWh");
          client.print(numf_2);
          client.println(" kWh");
      }

image
image

ESP8266 NodeMCU Web Server using Server-Sent Events (SSE) | Random Nerd Tutorials

ESP8266 Web Server: Display Sensor Readings in Gauges | Random Nerd Tutorials

Visualize ESP32/ESP8266 Sensor Readings from Anywhere in the World | Random Nerd Tutorials

My five cents (suggestions):

  • just do one client.print() : collect all the strings for a response to the Web Browser, generate all as one single string and send it with just ONE client.print();. I would assume: the Web Browser closes the connection after some "pieces" of response came in via all these client.print(). It is also easier to see that MCU has generated the entire response (for one shot) as complete and correct.

  • make sure your entire response is "well formed HTML". I do not see the you close the body and html page (the tail missing?). Assume: if Web Browsers sees an error in HTML response - it might not request again (you do properly this "refresh", but Browser might see errors and do not try again when one response was wrong, not "well formed HTML").

  • verify the correctness (and completeness) with the Web Browser: they have often a tab for "View Source File" or a debug option. Check, if your browser is happy with what you send back.

BTW: (what I have realized)
Web Browsers close all the time the connection after one request (and "a" response received). If "a new" Web Browser is connected as client (again) - the connection must be established again.
This has to be done on server side (MCU side).
What I have learnt: Web Browsers close all the time. And MCU must follow (or realize) that connection was closed in between.

OK, if you use "refresh", no need to press the "refresh" button in browser, it should request the same page again (for updates, here every 5 seconds). But the connection is still closed in between: a Web Browser opens (connects) all the time again, even with "refresh". Your MCU should be aware of it that the connection is released (closed) after browser got one of your response. Maybe, you do not act again on a new connections (from any other browser, even it is the same), you assume the connection is still there (but it is NOT).

I did the test just for one parameter but still the same issue. It's printing sucessfully the parameter name(i.e Total Active Energy ) line but not printing the next line i.e value.

I didn't post complete code. But my html code is correct as it is printing on webpage.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.