How can I pass a value from Arduino C To HTML?
Posted: Sun Aug 05, 2018 11:58 am
Hello everybody,
I ´ve built a simple WIFi - thermometer with an esp8266 and a DS18B20 sensor. The software produces a simple webpage that can be viewed with a computer, cell phone and so on. I like that much better than using just the "server.send(200, "text/plain"" command but there´s one catch!! I need to display the "temperature_string" variable which contains the actual temperature and don´t no how? Is there a possibillity? I would really appreciate your expert advice!!!!!
Thanks in advance
I ´ve built a simple WIFi - thermometer with an esp8266 and a DS18B20 sensor. The software produces a simple webpage that can be viewed with a computer, cell phone and so on. I like that much better than using just the "server.send(200, "text/plain"" command but there´s one catch!! I need to display the "temperature_string" variable which contains the actual temperature and don´t no how? Is there a possibillity? I would really appreciate your expert advice!!!!!
Thanks in advance
Code: Select all
/*
ESP8266 Temperatur WiFi-Sensor -19.07.2018 - pb -
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <OneWire.h>
#include <DallasTemperature.h>
//Actual webpage - I need the "temperature_string" variable in here
const char index_html[] PROGMEM={"<!DOCTYPE html>\n<html>\n<head>\n<meta http-equiv=\"refresh\" content=\"5\"/>\n<style>\nh1 {\n color: blue;\n font-family: verdana;\n font-size: 300%;\n\n}\np {\n color: red;\n font-family: courier;\n font-size: 160%;\n}\n</style>\n</head>\n<body>\n\n<h1>This should show the temperature</h1>\n\n\n</body>\n</html>\n"};
#define ONE_WIRE_BUS 2 // GPIO of ESP8266 = GPIO2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.
String temperature_string = "";
float temperature;
EspClass ESPm;
const char* ssid = "DLINK";
const char* password = "C63SxIpdTT";
//byte mac[] = { 0xDE, 0x13, 0x02, 0x58, 0xFF, 0x03 };// MAC Adress of Arduino Board
//byte ip[] = { 192, 168, 1, 20 }; // IP Adresse of Arduino Board
//byte gateway[] = { 192, 168, 1, 1 }; // Gateway (optional)
ESP8266WebServer server(80);
//const int led = 13;
void handleRoot() {
//digitalWrite(led, 1);
temperature_string = "Temperature: " + String((float)temperature,1) + " C"; //This is the value that I need to display in the webpage
server.send_P(200, "text/html", index_html);//temperature_string
//digitalWrite(led, 0);
}
void handleNotFound() {
// digitalWrite(led, 1);
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
// digitalWrite(led, 0);
}
void setup(void) {
sensors.begin(); /* Initialize Dallas temperature library */
// pinMode(led, OUTPUT);
// digitalWrite(led, 0);
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
//if (MDNS.begin("esp8266")) {
//Serial.println("MDNS responder started");
// }
server.on("/", handleRoot);
server.on("/inline", []() {
server.send(200, "text/plain", "this works as well");
});
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void get_temperature() {
// Get temperature
sensors.requestTemperatures();
temperature = sensors.getTempCByIndex(0);
Serial.println();
Serial.print(temperature,1);
Serial.print(" Grad Celsius");
Serial.println();
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
delay(2000);
}
void loop() {
server.handleClient();
get_temperature();
}