-->
Page 1 of 1

Help in code html for web server esp8266

PostPosted: Tue Jul 04, 2017 10:59 pm
by manarooo
Hi I'm creating a web server but there is a problem I want to print the IP address and the name of the HTML web page but do not know how please help this code is
----------------------------------------------------------------------------------------------------------------------------------------------------------------
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include "index.h"
#define ssid "MO-Internet" // WiFi SSID
#define password "19881990MO" // WiFi password
int LEDPIN = 13;
int LEDPIN1 = 12;



ESP8266WebServer server ( 80 );


void handleRoot(){
if ( server.hasArg("LED") ) {
handleSubmit();
} else {
server.send ( 200, "text/html", MAIN_page );
}

if ( server.hasArg("LED1") ) {
handleSubmit();
} else {
server.send ( 200, "text/html", MAIN_page );
}
}

void handleSubmit() {
// Actualise le GPIO / Update GPIO
String LEDValue;
String LED1Value;


LEDValue = server.arg("LED");
if ( LEDValue == "1" ) {
digitalWrite(LEDPIN, 1);

server.send ( 200, "text/html", MAIN_page );
}
else if ( LEDValue == "0" ) {
digitalWrite(LEDPIN, 0);

server.send ( 200, "text/html", MAIN_page );
} else {
Serial.println("Err Led Value");
}

LED1Value = server.arg("LED1");
if ( LED1Value == "1" ) {
digitalWrite(LEDPIN1, 1);
// etatLed = "On";
server.send ( 200, "text/html", MAIN_page );
}
else if ( LED1Value == "0" ) {
digitalWrite(LEDPIN1, 0);
//etatLed = "Off";
server.send ( 200, "text/html", MAIN_page );
} else {
Serial.println("Err Led Value");
}
}

void setup() {


pinMode(LEDPIN, OUTPUT);
pinMode(LEDPIN1, OUTPUT);
digitalWrite(LEDPIN, 0);
digitalWrite(LEDPIN1, 0);
Serial.begin ( 115200 );


WiFi.begin ( ssid, password );

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


server.on ( "/", handleRoot );

server.begin();
Serial.println ( "HTTP server started" );

}

void loop() {

server.handleClient();

}
--------------------------------------------------------------------------------------------------------------------------------------------------------
Index.html

const char MAIN_page[] = R"=====(
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<FORM action="/" method="post">
<button name="LED" type="submit" value="1" class="btn btn-primary">ON</button>
<button name="LED" type="submit" value="0" class="btn btn-primary">OFF</button>
</FORM>

</body>
</html>
)=====";

Re: Help in code html for web server esp8266

PostPosted: Mon Jul 17, 2017 4:02 pm
by Diablillowilly
Though I have never seen sending a webpage in that way, I make them like this:


Code: Select all
String webpage() {
  String htmlPage =
    String() +
    "<!DOCTYPE html>\r\n"
    "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\r\n"
    "<html>\r\n"
    "<head>\r\n"
    "<meta charset=\"UTF-8\"/>\r\n"


   "</head>\r\n"
//WEBPAGE
   "</html>\r\n"




when I put variables into the webpage I do this:
Code: Select all
String webpage() {
  String htmlPage =
    String() +
    "<!DOCTYPE html>\r\n"
    "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\r\n"
    "<html>\r\n"
    "<head>\r\n"
    "<meta charset=\"UTF-8\"/>\r\n"


   "</head>\r\n"
//WEBPAGE
"<p>Variable= " + String(var) + "</p>\r\n"  //(notice the String() function is only necessary when you are not sending a string)

   "</html>\r\n"




or if you just want to send the variable:

Code: Select all
  server.send(String(var).length(), "text/plain", String(var));




Sorry for my english, it's not my mother language

Hope it helps