-->
Page 1 of 1

Refreshing a Browser.

PostPosted: Tue Dec 27, 2016 8:23 am
by Paul OE8PCK
I have a sketch which reads the temperature via DHT22 and then sends it to my Tablet (android) via the internal wifi hotspot to a browser page. Now this tempewrature Information is on sent when requested from the browser on the Tablet. I want to have this Information refreshed every say 3 seconds. I have asked various People who say I should use a meta refresh, I then copied this to my sketch but it will not compile. I Need some help I think. Please have a look at my sketch and correct what is going wrong. I have to learn a lot more about HTTP etc. because I would like to use the temperature Information in a graph on the browser on the tablet.
Please help me to get at least the temerature Information to be sent every 2 or 3 seconds.



#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <DHT.h>
#define DHTTYPE DHT22
#define DHTPIN 2

const char* ssid = "ESP8266";
const char* password = "";

ESP8266WebServer server(80);

// Initialize DHT sensor
// NOTE: For working with a faster than ATmega328p 16 MHz Arduino chip, like an ESP8266,
// you need to increase the threshold for cycle counts considered a 1 or 0.
// You can do this by passing a 3rd parameter for this threshold. It's a bit
// of fiddling to find the right value, but in general the faster the CPU the
// higher the value. The default for a 16mhz AVR is a value of 6. For an
// Arduino Due that runs at 84mhz a value of 30 works.
// This is for the ESP8266 processor on ESP-01
DHT dht(DHTPIN, DHTTYPE, 11); // 11 works fine for ESP8266

float humidity, temp_t; // Values read from sensor
String webString=""; // String to display
// Generally, you should use "unsigned long" for variables that hold time
unsigned long previousMillis = 0; // will store last temp was read
const long interval = 2000; // interval at which to read sensor

void handle_root() {
server.send(200, "text/plain", "Hello from the weather esp8266, read from /temp or /humidity");
delay(100);
}

void setup(void)
{
// You can open the Arduino IDE Serial Monitor window to see what the code is doing
Serial.begin(115200); // Serial connection from ESP-01 via 3.3v console cable
dht.begin(); // initialize temperature sensor

// Connect to WiFi network
WiFi.begin(ssid);
Serial.print("\n\r \n\rWorking to connect");

// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("DHT Weather Reading Server");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());


// server.on("/", handle_root);

/* server.on("/temp", [](){ // if you add this subdirectory to your webserver call, you get text below ;
gettemperature(); // read sensor
webString="Temperature: "+String((int)temp_t)+" degrees C"; // Arduino has a hard time with float to string

server.send(200, "text/plain", webString); // send to someones browser when asked

});
*/


server.on("/",[](){ // if you add this subdirectory to your webserver call, you get text below ;
gettemperature(); // read sensor
webString="Temperature: "+String((int)temp_t)+" degrees C"; // Arduino has a hard time with float to string
//<meta http-equiv="refresh" content="5"/>;//Wiederholung http


server.send(200, "text/plain", webString); // send to someones browser when asked

});


Many thanks in advance,

Paul

Re: Refreshing a Browser.

PostPosted: Sun Jan 01, 2017 11:32 am
by jeffas
Aside from the fact that your "meta http-equiv" line is commented out in the code that you posted, putting it in a "text/plain" document is not going to work. This is an HTML element, so will work only if you're sending an HTML response.
I'm not familiar with the ESP8266WebServer and its capabilities, but from an HTTP point of view, I would suggest adding a "Refresh" header to the HTTP response, rather than a "meta http-equiv" (which mimics the behaviour of the refresh header via HTML).
If that's hard to do with ESP8266WebServer, change your response to "text/html". You will have to make the response valid HTML, and include the "meta http-equiv" stuff in the <head> element.