-->
Page 1 of 1

Help in code html for web server esp8266

PostPosted: Wed Jul 05, 2017 5:30 pm
by manarooo
Hi, I'm creating a Web server but there's a problem I want to print the name of the network and the IP address on the web page What way should I follow
---------------------------------------------------------------------------------------------------------------------

#include <WiFiClient.h>
#include <ESP8266WebServer.h>
//#include <DHT.h>
//#include <Adafruit_BMP085.h>
#include "index.h"
#define ssid "MO-Internet" // WiFi SSID
#define password "19881990MO" // WiFi password
//#define DHTTYPE DHT22 // DHT type (DHT11, DHT22)
//#define DHTPIN D4 // Broche du DHT / DHT Pin
//#define LEDPIN D3 // Led
int LEDPIN = 13;
int LEDPIN1 = 12;



ESP8266WebServer server ( 80 );



void handleRoot(){
if ( server.hasArg("LED") ) {
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);
//
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 );
// Attente de la connexion au réseau WiFi / Wait for connection
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 ); Serial.print ( "." );
}
// Connexion WiFi établie / WiFi connexion is OK
Serial.println ( "" );
Serial.print ( "Connected to " ); Serial.println ( ssid );
Serial.print ( "IP address: " ); Serial.println ( WiFi.localIP() );

// On branche la fonction qui gère la premiere page / link to the function that manage launch page
server.on ( "/", handleRoot );

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

}

void loop() {

server.handleClient();
//t = dht.readTemperature();
//h = dht.readHumidity();
// p = bmp.readPressure() / 100.0F;
// delay(1000);
}

-----------------------------------------------------------------
Index.html
-----------------------------------------------------------------
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

const char MAIN_page[] PROGMEM= 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">
<P>ssid;</p>
<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: Wed Jul 05, 2017 11:25 pm
by Abhilash R
[quote="manarooo"]Hi, I'm creating a Web server but there's a problem I want to print the name of the network and the IP address on the web page What way should I follow

Can you not store the name and the IP address in the EEPROM and use it in the main page script? Even I am new to this, but I am guessing. I had a problem reading values submitted in a html form, would have any idea on how to solve that?

Re: Help in code html for web server esp8266

PostPosted: Thu Jul 06, 2017 6:23 am
by jankop
Here is modified example for SSID to html presentation.Look here for more insight.

Code: Select all#include <WiFiClient.h>
#include <ESP8266WebServer.h>
//#include <DHT.h>
//#include <Adafruit_BMP085.h>
//#include index.h
const char* ssid =     "YourSSID";     // Set your router SSID
const char* password = "YourPassword"; // Set your router password
//#define DHTTYPE DHT22 // DHT type (DHT11, DHT22)
//#define DHTPIN D4 // Broche du DHT / DHT Pin
//#define LEDPIN D3 // Led
int LEDPIN = 13;
int LEDPIN1 = 12;
String  MAIN_page;
ESP8266WebServer server (80);

void handleRoot(){
if ( server.hasArg("LED") ) {
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);
//
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 );
// Attente de la connexion au réseau WiFi / Wait for connection
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 ); Serial.print ( "." );
}
// Connexion WiFi établie / WiFi connexion is OK
Serial.println ( "" );
Serial.print ( "Connected to " ); Serial.println ( ssid );
Serial.print ( "IP address: " ); Serial.println ( WiFi.localIP() );

// On branche la fonction qui gère la premiere page / link to the function that manage launch page
server.on ( "/", handleRoot );

server.begin();
Serial.println ( "HTTP server started" );
MAIN_page  = "<!DOCTYPE html>";
MAIN_page += "<html lang=\"en\">";
MAIN_page += "<head>";
MAIN_page += "<title>Bootstrap Example</title>";
MAIN_page += "<meta charset=\"utf-8\">";
MAIN_page += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">";
MAIN_page += "<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css\">";
MAIN_page += "<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js\"></script>";
MAIN_page += "<script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js\"></script>";
MAIN_page += "</head>";
MAIN_page += "<body>";
MAIN_page += "<FORM action=\"/\" method=\"post\">";
MAIN_page += "<P>" + String(ssid) + "</p>";
MAIN_page += "<button name=\"LED\" type=\"submit\" value=\"1\" class=\"btn btn-primary\">ON</button>";
MAIN_page += "<button name=\"LED\" type=\"submit\" value=\"0\" class=\"btn btn-primary\">OFF</button>";
MAIN_page += "</FORM>";
MAIN_page += "</body>";
MAIN_page += "</html>";
}

void loop() {

server.handleClient();
//t = dht.readTemperature();
//h = dht.readHumidity();
// p = bmp.readPressure() / 100.0F;
// delay(1000);
}