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?