output temperature with TinyWebServer

hello,
I have my index on the sd (using the ethernet shield + atmega2560) i want to output the temperature to my browser
here is my arduino code (“temp_handler” is the function that I’m asking about, “20” is a value just to try it out )

#include <string.h>
#include <pins_arduino.h>
#include <SPI.h>
#include <Ethernet.h>
#include <Flash.h>
#include <SD.h>
#include <TinyWebServer.h>



//LEDS
const int ledPin =  3;
int ledState = LOW; 

//****************VALUES YOU CHANGE*************/
//byte ip[] = { 192, 168, 0, 12 };// Don't forget to modify the IP to an available one on your home network
byte ip[] = { 192, 168, 0, 10 };// Don't forget to modify the IP to an available one on your home network
//*********************************************/

static uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

 TinyWebServer::PathHandler handlers[] = {
  {"/", TinyWebServer::GET, &index_handler},
  {"/upload/" "*", TinyWebServer::PUT, &TinyWebPutHandler::put_handler },
  {"/RICEVI", TinyWebServer::POST, &riceve_handler},
  {"/" "*", TinyWebServer::GET, &file_handler},
  {"/TEMP", TinyWebServer::GET, &temp_handler },
  {NULL},
};

const char* headers[] = {"Content-Length",NULL};
TinyWebServer web = TinyWebServer(handlers, headers);

boolean has_filesystem = true;
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;

//_________________________________________________________________________________________________________________________________________________________________________
boolean riceve_handler(TinyWebServer& web_server) 
{  
      web_server.send_error_code(200);
      web_server.send_content_type("text/plain");
      web_server.end_headers();
      Client& client = web_server.get_client();
                 
    if (client.available()) 
    { 
      String readString;
      int16_t c;  
      String led1_on="led1_ON#";
      String led1_off="led1_OFF#";
      
       while ((c = client.read())>0)
       {
         readString += (char)c;
         if(c=='#'){           
         Serial.println (readString);  //debug
         //------------------------------------
         //qui elabori la stringa in arrivo
         if(readString==led1_on){
             digitalWrite(ledPin, HIGH);
             Serial.println ("accendo");
           }
          if(readString==led1_off){
             digitalWrite(ledPin, LOW);
             Serial.println ("spengo");
           }
          
         //-----------------------------------
            
         //svuoto la stringa dopo aver incontrato # ... in questo modo la massima lunghezza di  readString
         //sara di 255.255.255.255# quindi 16 char, si evita di crashare arduino a causa di un eventuale 
         //saturazione della ram
         readString="";
         }             
       }
      
        readString=""; //svuota la stringa del client.read  
    } 
   
      client.stop();   
}

//_________________________________________________________________________________________________________________________________________________________________________
void send_file_name(TinyWebServer& web_server, const char* filename) {
//Serial.print ("1");
  if (!filename) {
    web_server.send_error_code(404);
    web_server << F("Could not parse URL");
  } else {
    TinyWebServer::MimeType mime_type
      = TinyWebServer::get_mime_type_from_filename(filename);
    web_server.send_error_code(200);
    web_server.send_content_type(mime_type);
    web_server.end_headers();
    if (file.open(&root, filename, O_READ)) {
      Serial << F("Read file "); 
      Serial.println(filename);
      web_server.send_file(file);
      file.close();
    } else {
      web_server << F("Could not find file: ") << filename << "\n";
    }
  }
}

//__________________________________________________________________________________________________________________________________________________________________________
boolean file_handler(TinyWebServer& web_server) {
//Serial.print ("2");
  char* filename = TinyWebServer::get_file_from_path(web_server.get_path());
  send_file_name(web_server, filename);
  free(filename);
  return true;
}

//__________________________________________________________________________________________________________________________________________________________________________
boolean index_handler(TinyWebServer& web_server) {
//Serial.print ("3");
  send_file_name(web_server, "INDEX.HTM");
  return true;
}

//temp_________________________________________________________________________________________________
boolean temp_handler(TinyWebServer& web_server) {
  web_server.send_error_code(200);
  web_server.send_content_type("text/plain");
  web_server.end_headers();
  Client& client = web_server.get_client();
  client.println("20");
  return true;
}
//_________________________________________________________________________________________________________________________________________________________________________
/////////upload
void file_uploader_handler(TinyWebServer& web_server,
			   TinyWebPutHandler::PutAction action,
			   char* buffer, int size) {
  static uint32_t start_time;
  static uint32_t total_size;

  switch (action) {
  case TinyWebPutHandler::START:
    start_time = millis();
    total_size = 0;
    if (!file.isOpen()) {
      // File is not opened, create it. First obtain the desired name
      // from the request path.
      char* fname = web_server.get_file_from_path(web_server.get_path());
      if (fname) {
	Serial << F("Creating ") << fname << "\n";
	file.open(&root, fname, O_CREAT | O_WRITE | O_TRUNC);
	free(fname);
      }
    }
    break;

  case TinyWebPutHandler::WRITE:
    if (file.isOpen()) {
      file.write(buffer, size);
      total_size += size;
    }
    break;

  case TinyWebPutHandler::END:
    file.sync();
    Serial << F("Wrote ") << file.fileSize() << F(" bytes in ")
	   << millis() - start_time << F(" millis (received ")
           << total_size << F(" bytes)\n");
    file.close();
  }
}


void setup() {
  
  //LEDS///////
  pinMode(ledPin, OUTPUT); 
  ////////////  
  Serial.begin(9600);
  //Serial << F("Free RAM: ") << FreeRam() << "\n";
 
  pinMode(SS_PIN, OUTPUT);   // set the SS pin as an output
  digitalWrite(SS_PIN, HIGH);   // and ensure SS is high
  pinMode(10, OUTPUT);   // Set the CS pin as an output
  digitalWrite(10, HIGH);   // Turn off the W5100 chip! (wait for configuration)
  pinMode(4, OUTPUT);   // Set the SDcard CS pin as an output
  digitalWrite(4, HIGH);   // Turn off the SD card! (wait for configuration)
  // initialize the SD card.
  Serial.println("Setting up SD card...");
  // pass over the speed and Chip select for the SD card
  if (!card.init(SPI_FULL_SPEED, 4)) {
    Serial.println("card failed");
    has_filesystem = false;
  }
  // initialize a FAT volume.
  if (!volume.init(&card)) {
    Serial.println("vol.init failed!");
    has_filesystem = false;
  }
  if (!root.openRoot(&volume)) {
    Serial.println("openRoot failed");
    has_filesystem = false;
  }
  
  if (has_filesystem) {
    // Assign our function to `upload_handler_fn'.
    TinyWebPutHandler::put_handler_fn = file_uploader_handler;
  }

  // Initialize the Ethernet.
  Serial.println("Setting up the Ethernet card...");
  Ethernet.begin(mac, ip);

  // Start the web server.
  Serial.println("Web server starting...");
  web.begin();

  Serial.println("Ready to accept HTTP requests.");
}
 
//_________________________________________________________________________________________________________________________________________________________________________
void loop() {
  if (has_filesystem) {
    web.process();
  }
}

and this is the html code:

<script type="text/javascript">

//temperatura
function temp() {
    $.ajax({type: "GET", cache: false, url: "/TEMP", success: status /*function(status)
				{status = parseInt(status.trim());//<<<<<<<<<<<<<<<<<<<<la variabile status contiene il messaggio di risposta da arduino
				}*/
			});
			
	document.write(status);
	};
</script>

</head>

<body>

Temperatura :
<script type="text/javascript">
temp();	
</script>
°C
</body>

</html>

can you help me to output the temperature (in this case “20”) to my html code? where did I go wrong?

where did I go wrong?

Is this the start of 20 questions, of which you'll answer 8 incompletely?

The code does something. What does it do? What appears in the browser?

Using TinyWebServer to conserve SRAM and then pissing the savings away on the String class just seems wrong.

i'm just missing this part, on how to output from Arduino to the browser. the i see the html , i cam read from the html to arduino but i need help on how to let the browser read the temperature. i said the interested part is the function "temp_handler"
then maybe even the html part is wrong.

up :blush:

If you have the w5100 Ethernet shield, you can try the below test code to show a way to get web based data from the arduino.

// zoomkat's meta refresh data frame test page 4/12/13
// use http://192.168.1.102:84 in your brouser for main page
// http://192.168.1.102:84/data static data page
// http://192.168.1.102:84/datastart meta refresh data page
// for use with W5100 based ethernet shields
// set the refresh rate to 0 for fastest update
// use STOP for single data updates

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

const int analogInPin0 = A0;
const int analogInPin1 = A1;
const int analogInPin2 = A2;
const int analogInPin3 = A3;
const int analogInPin4 = A4;
const int analogInPin5 = A5;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // arduino ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port
unsigned long int x=0; //set refresh counter to 0
String readString; 

//////////////////////

void setup(){
  Serial.begin(9600);
    // disable SD SPI if memory card in the uSD slot
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();
  Serial.println("meta refresh data frame test"); // so I can keep track of what is loaded
}

void loop(){
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
         if (readString.length() < 100) {
          readString += c; 
         } 
        //check if HTTP request has ended
        if (c == '\n') {

          //check get atring received
          Serial.println(readString);

          //output HTML data header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          //generate data page
          if(readString.indexOf("data") >0) {  //checks for "data" page
            x=x+1; //page upload counter
            client.print("<HTML><HEAD>");
            //meta-refresh page every 1 seconds if "datastart" page
            if(readString.indexOf("datastart") >0) client.print("<meta http-equiv='refresh' content='1'>"); 
            if(readString.indexOf("datafast") >0) client.print("<meta http-equiv='refresh' content='0'>"); 
            client.print("<title>Zoomkat's meta-refresh test</title></head><BODY>
");
            client.print("page refresh number ");
            client.print(x); //current refresh count
            client.print("

");
              //output the value of each analog input pin
            client.print("analog input0 ");
            client.print(" is ");
            client.print(analogRead(analogInPin0));
            client.println("
");
            
            client.print("analog input1 ");
            client.print(" is ");
            client.print(analogRead(analogInPin1));
            client.println("
");
                        
            client.print("analog input2 ");
            client.print(" is ");
            client.print(analogRead(analogInPin2));
            client.println("
");
            
            client.print("analog input3 ");
            client.print(" is ");
            client.print(analogRead(analogInPin3));
            client.println("
");
                                    
            client.print("analog input4 ");
            client.print(" is ");
            client.print(analogRead(analogInPin4));
            client.println("
");
            
            client.print("analog input5 ");
            client.print(" is ");
            client.print(analogRead(analogInPin5));
            client.println("
");
            client.print("</BODY></HTML>");
           }
          //generate main page with iframe
          else
          {
            client.print("<HTML><HEAD><TITLE>Zoomkat's frame refresh test</TITLE></HEAD>");
            client.print("Zoomkat's Arduino frame meta refresh test 4/12/13");
            client.print("

Arduino analog input data frame:
");
            client.print("&nbsp;&nbsp;<a href='http://192.168.1.102:84/datastart' target='DataBox' title=''yy''>META-REFRESH</a>");
            client.print("&nbsp;&nbsp;&nbsp;&nbsp;<a href='http://192.168.1.102:84/data' target='DataBox' title=''xx''>SINGLE-STOP</a>");
            client.print("&nbsp;&nbsp;&nbsp;&nbsp;<a href='http://192.168.1.102:84/datafast' target='DataBox' title=''zz''>FAST-DATA</a>
");
            client.print("<iframe src='http://192.168.1.102:84/data' width='350' height='250' name='DataBox'>");
            client.print("</iframe>
</HTML>");
          }
          delay(1);
          //stopping client
          client.stop();
          //clearing string for next read
          readString="";
        }
      }
    }
  }
}

thank you for the answer,
but that isn't using the sd and not even the tinywebserver lib. in that way it's simple.