QuestionsCategory: General Questionhow to change ip address/on or off push button press
eabulislam Staff asked 5 years ago

how to change ip address/on or off push button press.

actually i want to control led on off  press push button when not present wifi  device .

when press push button change link ip

192.168.0.1/light1on

or

192.168.0.1/light1onoff

here my code

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
WiFiServer server(80); //Initialize the server on Port 80
//io output pin configaration
int fan = 5;
int light1=4;
int light2=0;
int light3=2;
int pwmValue;
//////////////////////////////////////////////////
//////////////////////////////////////////////////
/*====-===-TEST SWITCH-==========*/
//int pin_LED = 4;
int pin_switch = 12;

// variables to hold the new and old switch states
boolean oldSwitchState = LOW;
boolean newSwitchState = LOW;

boolean LEDstatus = LOW;

/*========-TEST SWITCH-========*/

//////////////////////////////////////////////////
//////////////////////////////////////////////////
void setup() {
WiFi.mode(WIFI_AP); //Our ESP8266-12E is an AccessPoint
WiFi.softAP(“Hello_IoT”, “12345678”); // Provide the (SSID, password); .
server.begin(); // Start the HTTP Server

Serial.begin(115200); //Start communication between the ESP8266-12E and the monitor window
IPAddress HTTPS_ServerIP= WiFi.softAPIP(); // Obtain the IP of the Server
Serial.print(“Server IP is: “); // Print the IP to the monitor window
Serial.println(HTTPS_ServerIP);

pinMode(fan, OUTPUT); //GPIO16 is an OUTPUT pin;
pinMode(light1, OUTPUT); //GPIO16 is an OUTPUT pin;
pinMode(light2, OUTPUT); //GPIO16 is an OUTPUT pin;
pinMode(light3, OUTPUT); //GPIO16 is an OUTPUT pin;

//////////////////////////////////////////////////
/////////////////////TEST SWITCH/////////////////////////////
pinMode(pin_switch, INPUT);
//////////////////////////////////////////////////
//////////////////////////////////////////////////
}

void loop() {

WiFiClient client = server.available();
if (!client) {
return;
}

//Looking under the hood
Serial.println(“Somebody has connected :)”);
String request = client.readStringUntil(‘\r’);

if (request.indexOf(“/fanoff”) != -1){
pwmValue=1023;
analogWrite(fan, pwmValue);
delay(1);
}
if (request.indexOf(“/fanon”) != -1){
pwmValue=0;
analogWrite(fan, pwmValue);
delay(1);
}

if (request.indexOf(“/down”) != -1){
pwmValue = pwmValue + 100;
if(pwmValue>=1022){pwmValue=1021;}
analogWrite(fan, pwmValue);
delay(1);
}

if (request.indexOf(“/up”) != -1){
if(pwmValue<=10){pwmValue=10;}
pwmValue = pwmValue – 100;
analogWrite(fan, pwmValue);
delay(1);
}

/////////////////////////////////////////////////
////////////////////////////////////////////////
/*=================-TEST SWITCH -==================*/
newSwitchState = digitalRead(pin_switch);

if ( newSwitchState != oldSwitchState )
{
// has the button switch been closed?
if ( newSwitchState == HIGH )
{
if ( LEDstatus == LOW ) { digitalWrite(light1, HIGH); LEDstatus = HIGH; }
else { digitalWrite(light1, LOW); LEDstatus = LOW; }
}
oldSwitchState = newSwitchState;
}
/*=================-TEST SWITCH-==================*/
////////////////////////////////////////////////
////////////////////////////////////////////////
if (request.indexOf(“/light1off”) != -1){
digitalWrite(light1, HIGH);
}
if (request.indexOf(“/light1on”) != -1){
digitalWrite(light1, LOW);
}
if (request.indexOf(“/light2off”) != -1){
digitalWrite(light2, HIGH);
}
if (request.indexOf(“/light2on”) != -1){
digitalWrite(light2, LOW);
}
if (request.indexOf(“/light3off”) != -1){
digitalWrite(light3, HIGH);
}
if (request.indexOf(“/light3on”) != -1){
digitalWrite(light3, LOW);
}

//Serial.println(“Client disonnected”); //Looking under the hood

String s= “HTTP/1.1 200 OK\r\n”;
s+=”content-Type:text/html\r\n”;
s+=”<!DOCTYPE HTML>\r\n</html>\r\n”;
//s+=”<br><input type=\”button\” name=\”b1\” value=\”Turn LED ON\” onclick=\”location.href=’/fanon’\”>”;
//s+=”<br><input type=\”button\” name=\”b1\” value=\”Turn LED ON\” onclick=\”location.href=’/light1on’\”>”;
s+=”<br> <br> <br>”;
s+=”</html>\n”;

// Return the response
client.println(“HTTP/1.1 200 OK”);
client.println(“Content-Type: text/html”);
client.println(“”); // do not forget this one
client.println(“<!DOCTYPE HTML>”);
client.println(“<html>”);
client.println(“<head>”);
client.println(“<meta name=’apple-mobile-web-app-capable’ content=’yes’ />”);
client.println(“<meta name=’apple-mobile-web-app-status-bar-style’ content=’black-translucent’ />”);
client.println(“</head>”);
client.println(“<body bgcolor = \”#f7e6ec\”>”);
client.println(“<hr/><hr>”);
client.println(“<h4><center> MICROTECH </center></h4>”);
client.println(“<hr/><hr>”);
client.println(“<br><br>”);
client.println(“<br><br>”);
client.println(“<center>”);
client.println(“Fan”);
client.println(“<a href=\”/fanon\”\”><button>Turn On </button></a>”);
client.println(“<a href=\”/fanoff\”\”><button>Turn Off </button></a><br />”);
client.println(“</center>”);
client.println(“<br><br>”);
client.println(“<center>”);
client.println(“Fan Speed”);
client.println(“<a href=\”/down\”\”><button>DOWN </button></a>”);
client.println(“<a href=\”/up\”\”><button>UP</button></a><br />”);
client.println(“</center>”);
client.println(“<br><br>”);
client.println(“<center>”);
client.println(“Light 1”);
client.println(“<a href=\”/light1on\”\”><button>Turn On </button></a>”);
client.println(“<a href=\”/light1off\”\”><button>Turn Off </button></a><br />”);
client.println(“</center>”);
client.println(“<br><br>”);
client.println(“<center>”);
client.println(“Light 2”);
client.println(“<a href=\”/light2on\”\”><button>Turn On </button></a>”);
client.println(“<a href=\”/light2off\”\”><button>Turn Off </button></a><br />”);
client.println(“</center>”);
client.println(“<br><br>”);
client.println(“<center>”);
client.println(“Light 3”);
client.println(“<a href=\”/light3on\”\”><button>Turn On </button></a>”);
client.println(“<a href=\”/light3off\”\”><button>Turn Off </button></a><br />”);
client.println(“</center>”);
client.println(“<br><br>”);
client.println(“<center>”);
client.println(“<table border=\”5\”>”);
client.println(“<tr>”);
if (digitalRead(5))
{
client.print(“<td>Fan is OFF</td>”);

}
else
{
client.print(“<td>Fan is ON</td>”);

}

client.println(“<br />”);

if (digitalRead(4))
{
client.print(“<td>Light 1 is OFF</td>”);

}
else
{

client.print(“<td>Light 1 is ON</td>”);

}
client.println(“</tr>”);

client.println(“<tr>”);

if (digitalRead(0))

{
client.print(“<td>Light 2 is OFF</td>”);

}

else

{
client.print(“<td>Light 2 is ON </td>”);
}

if (digitalRead(2))

{

client.print(“<td>Light 3 is OFF</td>”);

}

else

{

client.print(“<td>Light 3 is ON</td>”);

}

client.println(“</tr>”);

client.println(“</table>”);

client.println(“</center>”);
client.println(“</html>”);
client.flush(); //clear previous info in the stream
client.print(s); // Send the response to the client
delay(1);
}

void light1on(){ digitalWrite(light1, LOW); }
void light1off(){ digitalWrite(light1, LOW); }