#include <WiFi.h>
#include <WebServer.h>
#include "index.h"
/* Put your SSID & Password */
const char* ssid = "Badminton"; // Enter SSID here
const char* password = "12345678"; //Enter Password here
/* Put IP Address details */
IPAddress local_ip(192,168,100,100);
IPAddress gateway(192,168,100,100);
IPAddress subnet(255,255,255,0);
WebServer server(80);
uint8_t LED1pin = 4;
bool LED1status = LOW;
uint8_t LED2pin = 5;
bool LED2status = LOW;
void setup() {
Serial.begin(115200);
pinMode(LED1pin, OUTPUT);
pinMode(LED2pin, OUTPUT);
WiFi.softAP(ssid, password);
WiFi.softAPConfig(local_ip, gateway, subnet);
delay(100);
server.on("/", handle_OnConnect);
server.on("/index", handle_OnConnect);
server.on("/names", handle_names);
server.on("/messages", handle_messages);
server.on("/savenames", handle_savenames);
server.on("/savemess", handle_savemess);
server.onNotFound(handle_NotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
}
void handle_OnConnect() {
Serial.println("ON Connect");
//READ SPIFFS HERE
// send the HTTP response body
String html = String(HTML_INDEX);
server.send(200, "text/html",html);
delay(10);
}
void handle_names() {
byte lp;
String currentname;
Serial.println("ON Add Names");
//READ SPIFFS HERE
// send the HTTP response body
String html = String(HTML_NAMES);
for (lp=1;lp<16;lp++) {
currentname="Newname"+String(lp);
html.replace("PN"+String(lp), currentname); // replace the marker by a real value
}
server.send(200, "text/html",html);
delay(10);
}
void handle_messages() {
byte lp;
String currentmess;
Serial.println("ON Add messages");
//READ SPIFFS HERE
// send the HTTP response body
String html = String(HTML_MESSAGES);
for (lp=1;lp<16;lp++) {
currentmess="New_message"+String(lp);
html.replace("PN"+String(lp), currentmess); // replace the marker by a real value
}
server.send(200, "text/html",html);
delay(10);
}
void handle_savenames() {
byte lp;
String currentname;
Serial.println("ON Saving Names");
//SAVE DATA HERE
// send the HTTP response body
String html = String(HTML_COMPLETE);
server.send(200, "text/html",html);
delay(10);
}
void handle_savemess() {
byte lp;
String currentname;
Serial.println("ON Saving MESSAGES");
//SAVE DATA HERE
// send the HTTP response body
String html = String(HTML_COMPLETE);
server.send(200, "text/html",html);
delay(10);
}
void handle_NotFound(){
server.send(404, "text/plain", "Not found");
}
String SendHTML(uint8_t led1stat,uint8_t led2stat){
String ptr = "<!DOCTYPE html> <html>\n";
ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
ptr +="<title>Badminton</title>\n";
ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n";
ptr +=".button {display: block;width: 80px;background-color: #3498db;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
ptr +=".button-on {background-color: #3498db;}\n";
ptr +=".button-on:active {background-color: #2980b9;}\n";
ptr +=".button-off {background-color: #34495e;}\n";
ptr +=".button-off:active {background-color: #2c3e50;}\n";
ptr +="p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
ptr +="</style>\n";
ptr +="</head>\n";
ptr +="<body>\n";
ptr +="<h1>ESP32 Web Server</h1>\n";
ptr +="<h3>Using Access Point(AP) Mode</h3>\n";
if(led1stat)
{ptr +="<p>LED1 Status: ON</p><a class=\"button button-off\" href=\"/led1off\">OFF</a>\n";}
else
{ptr +="<p>LED1 Status: OFF</p><a class=\"button button-on\" href=\"/led1on\">ON</a>\n";}
if(led2stat)
{ptr +="<p>LED2 Status: ON</p><a class=\"button button-off\" href=\"/led2off\">OFF</a>\n";}
else
{ptr +="<p>LED2 Status: OFF</p><a class=\"button button-on\" href=\"/led2on\">ON</a>\n";}
ptr +="</body>\n";
ptr +="</html>\n";
return ptr;
}
// #include <WiFi.h>
// #include <WiFiClient.h>
// #include <WiFiAP.h>
// #include <SPIFFS.h>
// // #define LED_BUILTIN 2 // Set the GPIO pin where you connected your test LED or comment this line out if your dev board has a built-in LED
// Set these to your desired credentials.
// const char *ssid = "Badminton";
// const char *password = "1234";
// bool serverOn=false;
// WiFiServer server(80);
// void startServer() {
// Serial.println();
// Serial.println("Configuring access point...");
// WiFi.softAP(ssid, password);
// IPAddress myIP = WiFi.softAPIP();
// Serial.print("AP IP address: ");
// Serial.println(myIP);
// server.begin();
// Serial.println("Server started");
// }
// void setup() {
// // pinMode(LED_BUILTIN, OUTPUT);
// Serial.begin(115200);
// }
// void showpage() {
// WiFiClient client = server.available(); // listen for incoming clients
// if (client) { // if you get a client,
// Serial.println("New Client."); // print a message out the serial port
// String currentLine = ""; // make a String to hold incoming data from the client
// while (client.connected()) { // loop while the client's connected
// if (client.available()) { // if there's bytes to read from the client,
// char c = client.read(); // read a byte, then
// Serial.write(c); // print it out the serial monitor
// if (c == '\n') { // if the byte is a newline character
// // if the current line is blank, you got two newline characters in a row.
// // that's the end of the client HTTP request, so send a response:
// if (currentLine.length() == 0) {
// // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// // and a content-type so the client knows what's coming, then a blank line:
// client.println("HTTP/1.1 200 OK");
// client.println("Content-type:text/html");
// client.println();
// // the content of the HTTP response follows the header:
// client.print("Click <a href=\"/H\">here</a> to turn ON the LED.<br>");
// client.print("Click <a href=\"/L\">here</a> to turn OFF the LED.<br>");
// // The HTTP response ends with another blank line:
// client.println();
// // break out of the while loop:
// break;
// } else { // if you got a newline, then clear currentLine:
// currentLine = "";
// }
// } else if (c != '\r') { // if you got anything else but a carriage return character,
// currentLine += c; // add it to the end of the currentLine
// }
// // Check to see if the client request was "GET /H" or "GET /L":
// if (currentLine.endsWith("GET /H")) {
// digitalWrite(LED_BUILTIN, HIGH); // GET /H turns the LED on
// }
// if (currentLine.endsWith("GET /L")) {
// digitalWrite(LED_BUILTIN, LOW); // GET /L turns the LED off
// }
// }
// }
// // close the connection:
// client.stop();
// Serial.println("Client Disconnected.");
// }
// }
//void loop() {
// WiFiClient client = server.available(); // listen for incoming clients
// if (client) { // if you get a client,
// Serial.println("New Client."); // print a message out the serial port
// String currentLine = ""; // make a String to hold incoming data from the client
// while (client.connected()) { // loop while the client's connected
// if (client.available()) { // if there's bytes to read from the client,
// char c = client.read(); // read a byte, then
// Serial.write(c); // print it out the serial monitor
// if (c == '\n') { // if the byte is a newline character
// // if the current line is blank, you got two newline characters in a row.
// // that's the end of the client HTTP request, so send a response:
// if (currentLine.length() == 0) {
// // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// // and a content-type so the client knows what's coming, then a blank line:
// client.println("HTTP/1.1 200 OK");
// client.println("Content-type:text/html");
// client.println();
// // the content of the HTTP response follows the header:
// client.print("Click <a href=\"/H\">here</a> to turn ON the LED.<br>");
// client.print("Click <a href=\"/L\">here</a> to turn OFF the LED.<br>");
// // The HTTP response ends with another blank line:
// client.println();
// // break out of the while loop:
// break;
// } else { // if you got a newline, then clear currentLine:
// currentLine = "";
// }
// } else if (c != '\r') { // if you got anything else but a carriage return character,
// currentLine += c; // add it to the end of the currentLine
// }
// // Check to see if the client request was "GET /H" or "GET /L":
// if (currentLine.endsWith("GET /H")) {
// digitalWrite(LED_BUILTIN, HIGH); // GET /H turns the LED on
// }
// if (currentLine.endsWith("GET /L")) {
// digitalWrite(LED_BUILTIN, LOW); // GET /L turns the LED off
// }
// }
// }
// // close the connection:
// client.stop();
// Serial.println("Client Disconnected.");
// }
//}
// /*********
// Rui Santos
// Complete project details at https://RandomNerdTutorials.com/esp32-esp8266-input-data-html-form/
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files.
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// *********/
// #include <Arduino.h>
// #ifdef ESP32
// #include <WiFi.h>
// #include <AsyncTCP.h>
// #include <SPIFFS.h>
// #endif
// #include <ESPAsyncWebSrv.h>
// AsyncWebServer server(80);
// // REPLACE WITH YOUR NETWORK CREDENTIALS
// const char* ssid = "REPLACE_WITH_YOUR_SSID";
// const char* password = "REPLACE_WITH_YOUR_PASSWORD";
// const char* PARAM_STRING = "inputString";
// const char* PARAM_INT = "inputInt";
// const char* PARAM_FLOAT = "inputFloat";
// // HTML web page to handle 3 input fields (inputString, inputInt, inputFloat)
// const char index_html[] PROGMEM = R"rawliteral(
// <!DOCTYPE HTML><html><head>
// <title>ESP Input Form</title>
// <meta name="viewport" content="width=device-width, initial-scale=1">
// <script>
// function submitMessage() {
// alert("Saved value to ESP SPIFFS");
// setTimeout(function(){ document.location.reload(false); }, 500);
// }
// </script></head><body>
// <form action="/get" target="hidden-form">
// inputString (current value %inputString%): <input type="text" name="inputString">
// <input type="submit" value="Submit" onclick="submitMessage()">
// </form><br>
// <form action="/get" target="hidden-form">
// inputInt (current value %inputInt%): <input type="number " name="inputInt">
// <input type="submit" value="Submit" onclick="submitMessage()">
// </form><br>
// <form action="/get" target="hidden-form">
// inputFloat (current value %inputFloat%): <input type="number " name="inputFloat">
// <input type="submit" value="Submit" onclick="submitMessage()">
// </form>
// <iframe style="display:none" name="hidden-form"></iframe>
// </body></html>)rawliteral";
// void notFound(AsyncWebServerRequest *request) {
// request->send(404, "text/plain", "Not found");
// }
// String readFile(fs::FS &fs, const char * path){
// Serial.printf("Reading file: %s\r\n", path);
// File file = fs.open(path, "r");
// if(!file || file.isDirectory()){
// Serial.println("- empty file or failed to open file");
// return String();
// }
// Serial.println("- read from file:");
// String fileContent;
// while(file.available()){
// fileContent+=String((char)file.read());
// }
// file.close();
// Serial.println(fileContent);
// return fileContent;
// }
// void writeFile(fs::FS &fs, const char * path, const char * message){
// Serial.printf("Writing file: %s\r\n", path);
// File file = fs.open(path, "w");
// if(!file){
// Serial.println("- failed to open file for writing");
// return;
// }
// if(file.print(message)){
// Serial.println("- file written");
// } else {
// Serial.println("- write failed");
// }
// file.close();
// }
// // Replaces placeholder with stored values
// String processor(const String& var){
// //Serial.println(var);
// if(var == "inputString"){
// return readFile(SPIFFS, "/inputString.txt");
// }
// else if(var == "inputInt"){
// return readFile(SPIFFS, "/inputInt.txt");
// }
// else if(var == "inputFloat"){
// return readFile(SPIFFS, "/inputFloat.txt");
// }
// return String();
// }
// void setup() {
// Serial.begin(115200);
// // Initialize SPIFFS
// #ifdef ESP32
// if(!SPIFFS.begin(true)){
// Serial.println("An Error has occurred while mounting SPIFFS");
// return;
// }
// #else
// if(!SPIFFS.begin()){
// Serial.println("An Error has occurred while mounting SPIFFS");
// return;
// }
// #endif
// WiFi.mode(WIFI_STA);
// WiFi.begin(ssid, password);
// if (WiFi.waitForConnectResult() != WL_CONNECTED) {
// Serial.println("WiFi Failed!");
// return;
// }
// Serial.println();
// Serial.print("IP Address: ");
// Serial.println(WiFi.localIP());
// // Send web page with input fields to client
// server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
// request->send_P(200, "text/html", index_html, processor);
// });
// // Send a GET request to <ESP_IP>/get?inputString=<inputMessage>
// server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) {
// String inputMessage;
// // GET inputString value on <ESP_IP>/get?inputString=<inputMessage>
// if (request->hasParam(PARAM_STRING)) {
// inputMessage = request->getParam(PARAM_STRING)->value();
// writeFile(SPIFFS, "/inputString.txt", inputMessage.c_str());
// }
// // GET inputInt value on <ESP_IP>/get?inputInt=<inputMessage>
// else if (request->hasParam(PARAM_INT)) {
// inputMessage = request->getParam(PARAM_INT)->value();
// writeFile(SPIFFS, "/inputInt.txt", inputMessage.c_str());
// }
// // GET inputFloat value on <ESP_IP>/get?inputFloat=<inputMessage>
// else if (request->hasParam(PARAM_FLOAT)) {
// inputMessage = request->getParam(PARAM_FLOAT)->value();
// writeFile(SPIFFS, "/inputFloat.txt", inputMessage.c_str());
// }
// else {
// inputMessage = "No message sent";
// }
// Serial.println(inputMessage);
// request->send(200, "text/text", inputMessage);
// });
// server.onNotFound(notFound);
// server.begin();
// }
// void loop() {
// // To access your stored values on inputString, inputInt, inputFloat
// String yourInputString = readFile(SPIFFS, "/inputString.txt");
// Serial.print("*** Your inputString: ");
// Serial.println(yourInputString);
// int yourInputInt = readFile(SPIFFS, "/inputInt.txt").toInt();
// Serial.print("*** Your inputInt: ");
// Serial.println(yourInputInt);
// float yourInputFloat = readFile(SPIFFS, "/inputFloat.txt").toFloat();
// Serial.print("*** Your inputFloat: ");
// Serial.println(yourInputFloat);
// delay(5000);
// }
// /*
// WiFiAccessPoint.ino creates a WiFi access point and provides a web server on it.
// Steps:
// 1. Connect to the access point "yourAp"
// 2. Point your web browser to http://192.168.4.1/H to turn the LED on or http://192.168.4.1/L to turn it off
// OR
// Run raw TCP "GET /H" and "GET /L" on PuTTY terminal with 192.168.4.1 as IP address and 80 as port
// Created for arduino-esp32 on 04 July, 2018
// by Elochukwu Ifediora (fedy0)
// */
// #include <WiFi.h>
// #include <WiFiClient.h>
// #include <WiFiAP.h>
// #define LED_BUILTIN 2 // Set the GPIO pin where you connected your test LED or comment this line out if your dev board has a built-in LED
// // Set these to your desired credentials.
// const char *ssid = "yourAP";
// const char *password = "yourPassword";
// WiFiServer server(80);
// void setup() {
// pinMode(LED_BUILTIN, OUTPUT);
// Serial.begin(115200);
// Serial.println();
// Serial.println("Configuring access point...");
// // You can remove the password parameter if you want the AP to be open.
// WiFi.softAP(ssid, password);
// IPAddress myIP = WiFi.softAPIP();
// Serial.print("AP IP address: ");
// Serial.println(myIP);
// server.begin();
// Serial.println("Server started");
// }
// void loop() {
// WiFiClient client = server.available(); // listen for incoming clients
// if (client) { // if you get a client,
// Serial.println("New Client."); // print a message out the serial port
// String currentLine = ""; // make a String to hold incoming data from the client
// while (client.connected()) { // loop while the client's connected
// if (client.available()) { // if there's bytes to read from the client,
// char c = client.read(); // read a byte, then
// Serial.write(c); // print it out the serial monitor
// if (c == '\n') { // if the byte is a newline character
// // if the current line is blank, you got two newline characters in a row.
// // that's the end of the client HTTP request, so send a response:
// if (currentLine.length() == 0) {
// // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// // and a content-type so the client knows what's coming, then a blank line:
// client.println("HTTP/1.1 200 OK");
// client.println("Content-type:text/html");
// client.println();
// // the content of the HTTP response follows the header:
// client.print("Click <a href=\"/H\">here</a> to turn ON the LED.<br>");
// client.print("Click <a href=\"/L\">here</a> to turn OFF the LED.<br>");
// // The HTTP response ends with another blank line:
// client.println();
// // break out of the while loop:
// break;
// } else { // if you got a newline, then clear currentLine:
// currentLine = "";
// }
// } else if (c != '\r') { // if you got anything else but a carriage return character,
// currentLine += c; // add it to the end of the currentLine
// }
// // Check to see if the client request was "GET /H" or "GET /L":
// if (currentLine.endsWith("GET /H")) {
// digitalWrite(LED_BUILTIN, HIGH); // GET /H turns the LED on
// }
// if (currentLine.endsWith("GET /L")) {
// digitalWrite(LED_BUILTIN, LOW); // GET /L turns the LED off
// }
// }
// }
// // close the connection:
// client.stop();
// Serial.println("Client Disconnected.");
// }
// }