Refreshing a Browser.
Posted: Tue Dec 27, 2016 8:23 am
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
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