[C/C++] refreshing function to read a value on a web page

Hello.
I have been struggling to solve this out by myself but it has been more than 10 days and nothing is going well. I made a jsp webpage on which a databean value is displayed in tag.
I put ‘888’ as default, and I made it change to ‘1’ or ‘0’ if I click on on/off buttons, respectively.
I checked if the databean value is changed to ‘1’/‘0’ and it DID change(Printed onto eclipse console window).

But the Arduino console window still doesn’t show 1/0. It always shows the default value 888.

In my opinion it is because the Arduino sketch is not refreshing the value.
But I don’t know what to use and how to implement whatever to the coding.

If anybody knows any good function/whatsoever,
Please reply me.

===========

  1. main.jsp file
    ===========
<!-- main.jsp

Main page that shows a simple on/off control and DHT11 sensor value -->

<%@ page language="java" contentType="text/html; charset=utf-8"
 pageEncoding="utf-8"%>
<%@ page import="com.Test.domain.Relay" %>

<%
Relay relay = new Relay();
pageContext.setAttribute("relay", relay);
%>
<!DOCTYPE html> 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Simple sensor value reading app</title>
<link href="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.css" rel="stylesheet" type="text/css"/>
<script src="http://code.jquery.com/jquery-1.6.4.min.js" type="text/javascript"></script>
<script src="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
var timer = setInterval(function(){
$.get("http://192.168.0.32", function(data){
$("#result").text(data);
});
}, 2000);
$("#autoRefresh").click(function(){
alert("interval stopped.");
clearInterval(timer);
});
$("#relayOn").click(function(){
$("#relayState").val("1");
$("#form1").attr("action","Processing.jsp");
$("#form1").submit();
});
$("#relayOff").click(function(){
$("#relayState").val("0");
$("#form1").attr("action","Processing.jsp");
$("#form1").submit();
});
}); 
</script>
</head> 
<body> 

<div data-role="page" id="page">
<div data-role="header">
<h1>DHT11 Sensor temperature</h1>
</div>
<div data-role="content">
<p><span id="result">Current Temperature : Reading data...</span>&nbsp; 
<form method="GET" id="form1" action="" target="haha">
<input type="button" id="relayOn" name="relayOn" value="RelayOn">
<input type="button" id="relayOff" name="relayOff" value="RelayOff">
<input type="text" id="relayState" name="relayState" value="998">
</form>
<span id="relayPower">${relay.relayPower}</span>
</div>
<div data-role="footer">
<h4>Footer</h4>
</div>
</div>

<iframe name="haha">
</iframe>
</body>
</html>

=============
2. Processing.jsp file

<!--
Processing.jsp

A processing file to receive parameters and proceed those files. 

-->

<%@ page language="java" contentType="text/html; charset=utf-8"
 pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="com.Test.domain.Relay" %>
<%
Relay relay = new Relay(); 

request.setCharacterEncoding("utf-8");
String temp = request.getParameter("relayState");
System.out.println("temp: "+temp);
int relayState = Integer.parseInt(temp);

relay.relayPower = relayState;
System.out.println("relay.relayPower: "+relay.relayPower); 

request.setAttribute("haval", relay.relayPower);

%>
<body>
<span id="relayDAT">${relay.relayPower}</span>
</body>

===============
3. Arduino sketch file

// Arduino sketch (c++)


#include <dht11.h>
#include <SPI.h>
#include <Ethernet.h>

/* Set MAC address of ethernet shield
**/
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

/* Set IP address of ethernet shield, 
in case DHCP IP allocation is failed.
**/

IPAddress ip(192, 168, 0, 32);
// Client object that will connect to tomcat server 
// running in eclipse. 
EthernetClient arduinoClient;

// Tomcat project server address
byte server[] = { 192, 168, 0, 55 };

// Client object that will connect to arduino web server from tomcat project server.
EthernetClient tomcatClient;

// Create arduino web server object
EthernetServer arduinoServer(80);

// DHT11 sensor value input
int sensorpin = A0;

// Set relay on/off socket as 2
int relay = 2;

// Create DHT11 sensor object
dht11 dht11;

// Define a variable to save relay on/off state
int relayOnOff;

void setup() {
Serial.begin(9600);

// If DHCP IP allocation fails
if(Ethernet.begin(mac) == 0) {
 Serial.println("DHCP IP allocation failed .....");
 Serial.println("Allocating the set IP...");
 Ethernet.begin(mac, ip);

// Begin arduino web server
 arduinoServer.begin();
 
// Wait for a moment while ethernet is being initialized..
 delay(1000);
}
Serial.print("Set IP : ");
Serial.println(Ethernet.localIP());

Serial.println("Connecting to the tomcat server...");
}

void loop() {

// When a request from arduino web server is received, 
// get the EthernetClient object that sent the request. 
// Here I proceed messages sent to the server, using EthernetClient object.

tomcatClient = arduinoServer.available();

// When a client's request is received to arduino web server,
if(tomcatClient) {
 
// Set a variable to save if HTTP request is over/not over  - HTTP request ends with blank line. 
 boolean current_line_is_blank = true;

// When the web server is connected with the client, 
// analyze the client's request and respond to the client in a loop statement.

 while(tomcatClient.connected()) {
   
// When the client's message is received, 
   if(tomcatClient.available()) {
     
//Read the first character and delete it from the receiving buffer. 
     char c = tomcatClient.read();
     Serial.print(c);

// If the character is a line-feed, and the line is blank, 
// it means HTTP request is over. So write the response data.
     if(c == '\n' && current_line_is_blank) {
       
// Write a standard HTTP response header. 
       tomcatClient.println("HTTP/1.1 200 OK");

       // CORS(Cross-Origin Resource Sharing)을 활용한 크로스도메인 Ajax

// Cross-domain Ajax, using CORS(Cross-Origin Resource Sharing)
       tomcatClient.println("Access-Control-Allow-Origin:*");

// Set MIME type of HTTP response data. 
       tomcatClient.println("Content-Type: text/html; charset=utf-8");
       tomcatClient.println();
       tomcatClient.print("Current Temperature: ");
       tomcatClient.print(temperature());
       tomcatClient.println("℃");
       break;
     }
     
     if(c == '\n') {
       current_line_is_blank = true;
     } else if(c != '\r') {
       current_line_is_blank = false;
     }
   }
 }
 delay(10);

// Stop tomcat client
 tomcatClient.stop();

// Now arduino becomes a client and tomcat is a server.
// Arduino requests tomcat. 

} else {    

// connect() functiion:
// Host name is converted to IP address, through DNS server. 
// If a client can be connected to a server well, it returns 1. 
// 1: connection complete. 
// 0: connection failed. 

 int state = arduinoClient.connect(server, 8080);
 Serial.println("state : " + String(state));

// When connected to tomcat project server,
 if(state) {    
   Serial.println("Tomcat project server connection complete.");

// Requst for the relay on/off state information, to tomcat project server.
   arduinoClient.println("GET /Test/main.jsp HTTP/1.1");
   arduinoClient.println("User-Agent: Arduino 1.6.11");
   
   arduinoClient.println("Host: 192.168.0.55:8080");

// The last line of a web request ends with blank line. 
   arduinoClient.println();
   
 } else {
   Serial.println("Tomcat project server connection failed.");
 }

// When a client requests to a tomcat project server, 
 if(arduinoClient.connected()){
   if(arduinoClient.find("<span id=\"relayPower\">")){ //ask for ${relay.relayPower} in <span> tag
     relayOnOff = arduinoClient.parseInt();
     Serial.println("111111111111111111111111111111");
     Serial.println(relayOnOff);
     if(relayOnOff == 1){
       digitalWrite(relay, HIGH);  
     } else {
       digitalWrite(relay, LOW);  
     }
   }  
 }

// Stop the arduino client
 arduinoClient.stop();
 delay(3000);
}  
}

// A function that returns temperature value, after reading
// DHT11 sensor value. 
float temperature() {

// Read sensor value.
dht11.read(sensorpin);
int dhtInput = dht11.temperature;

// Print the temperature value on the console window.
Serial.print("Dht11 value measured: ");
Serial.println(dhtInput);  

// Return the calculated celcius value.
return dhtInput;
}

**Data bean file is attached. It is Relay.java file.

relay.txt (360 Bytes)

Sounds to me like a bug in your code, but since we're not psychics....

Regards,
Ray L.

Please use code tags for your individual codes. Your post has become a big mess after your edit.

For each individual code (the original attachments)

type
** **[code]** **
before that code
type
** **[/code]** **
after that code

THANKS sterretje.

I edited using

 tag.

Your sketch is quite complex because you are running both a web server and a client which accesses a foreign web server. But if I understand correctly, the problem is only with the client part.
Your arduino client is accessing a web page (a java server page - jsp) on a tomcat server, and irrespective of any changes to the jsp, your arduino client ignores any changes and always returns the original state of the jsp.

What I would probably do for trouble shooting is to reduce the problem to the very simplest which would demonstrate the error and progress from there.

EG:
Step 1. Take the standard ethernet web client sketch.
Step 2 adapt it to contact your tomcat server and do a get on your jsp page.
Step 3 run the sketch
Step 4 From a web browser, access your jsp page and click on one of the buttons.
Step 5 run the sketch again and see what happens.

You may also be able to play a trick like declaring your ethernetClient in the loop instead of as a global to force it to be dropped, if caching is the problem.

Thank you 6v6gt. I think I am going to reconsider the way of doing this.

If you have control of both of the web apps, it might be worthwhile making your jsp look at the 'Accept' header and returning just the reading if the Accept is 'text/plain', or just having it take a parameter.