Help in code html for web server esp8266
Posted: Tue Jul 04, 2017 10:59 pm
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>
)=====";
----------------------------------------------------------------------------------------------------------------------------------------------------------------
#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>
)=====";