How can I pass a value from Arduino C To HTML?

Archibald
Posts: 110
Joined: Mon Mar 05, 2018 12:44 am

Re: How can I pass a value from Arduino C To HTML?

Postby Archibald » Mon Aug 06, 2018 2:23 pm

Hi Herbie,

I am pleased that you are now seeing the temperature displayed on your web page. The function server.send_P is for when you have pointers to 'char' arrays.

I don't have much expertise in this area, but I see the constructor for your server object can also take two parameters: an IP address (as a IPAddress object) and a port (as an 'int' variable).

So try something like this (placing the #include near the top of your code):

Code: Select all

#include <IPAddress.h>

IPAddress a(192,168,1,20);    // create IPAddress object
ESP8266WebServer server(a,80);     // create server object with IP address and port
You will find code of IPAddress.h, IPAddress.cpp, ESP8266WebServer.h and ESP8266WebServer.cpp on GitHub.

If my suggestion does not work and nobody else steps in to help, I suggest you start a new forum thread.

herbie
Posts: 15
Joined: Sun Jul 01, 2018 9:54 am

Thanks for the help!!!!

Postby herbie » Tue Aug 07, 2018 9:27 am

Hello Archibald,

sadly your solution didn´t work but I found one elsewhere. I really appreciate your help and of course of the others that tried to help too. I enjoy this forum and it´s friendly users very much!!!!! :D :D :D :D :D
Here´s the final code for all people that may be interested.


Code: Select all

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
//#include <ESP8266mDNS.h> /for DNS - comment void setup() first block after "sensors.begin
#include <OneWire.h>
#include <DallasTemperature.h>



#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;
const char before[] {"<!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  </style>\n</head>\n<body>\n\n<center><h1>"};
const char after[] {"<center></h1>\n\n\n</body>\n</html>\n"};
const char* ssid = "DLINK";
const char* password = "C63SxIpdTT";

EspClass ESPm;

ESP8266WebServer server(80);


void handleRoot() {

  temperature_string = "Temperatur: " + String((float)temperature, 1) + " C";
  server.send(200, "text/html", before + temperature_string + after);


}

void handleNotFound() {

  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);

}

void setup() {

  sensors.begin(); /* Initialize Dallas temperature library */
  // config static IP
  IPAddress ip(192, 168, 1, 20); 
  IPAddress gateway(192, 168, 1, 1); 
  IPAddress subnet(255, 255, 255, 0); 
  WiFi.config(ip, gateway, subnet);

  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();
}

Who is online

Users browsing this forum: No registered users and 53 guests