How to make a static ip

can someone fill in the details (fake ip an key etc) so i can see a real example

is so confusing it just sais ip is there where i put the ip instead of ip or after it i dont know its unclear if you going to give example put fake details in so its an actual working ish example as i have no clue what it means there is no ay of knowing unless you already know!

I got an issue where its on the wrong subnet :confused: please someone clarify!

plsu way too many catagories i dont know what 1 to pick just make a couple cats not a mmillions!

Whilst I agree that there are many categories in the forum, your topic was certainly not an introductory tutorial and it has been moved here

Ok dw I fixed it on my own!

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

#define OLED_RESET 4

const String intname = "doorlock";
byte mac[6];          // MAC Address
const char* ssid="AP-NAME-HERE";
const char* password="PASSWORD-HERE";

IPAddress ip(192, 168, 1, 53);
IPAddress gateway(192, 168, 1, 254);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(192, 168, 1, 254); //primaryDNS
//IPAddress secondaryDNS(1, 1, 1, 1); // optional


ESP8266WebServer server(80);

const int output1=14; //14
const int output2=12; //12
const int output3=13; //13
const int output4=15; //15

boolean device1=false;
boolean device2=false;
boolean device3=false;
boolean device4=false;

void handleRoot() {
  //digitalWrite(led, 1);
  //server.send(200, "text/plain", "hello from esp8266!");
  //digitalWrite(led, 0);

  String cmd;     
      cmd += "<!DOCTYPE HTML>\r\n";
      cmd += "<html>\r\n";
      //cmd += "<header><title>ESP8266 Webserver</title><h1>\"ESP8266 Web Server Control\"</h1></header>";
      cmd += "<head>";
      cmd += "<meta http-equiv='refresh' content='5'/>";
      cmd += "</head>";
      
      if(device1){
        cmd +=("<br/>Device1  : ON");
      }
      else{
        cmd +=("<br/>Device1  : OFF");
      }
      
      if(device2){
        cmd +=("<br/>Device2  : ON");
      }
      else{
        cmd +=("<br/>Device2  : OFF");
      }
           
       if(device3){
        cmd +=("<br/>Device3  : ON");
      }
      else{
        cmd +=("<br/>Device3  : OFF");
      }
      
      if(device4){
        cmd +=("<br/>Device4  : ON");
      }
      else{
        cmd +=("<br/>Device4  : OFF");
      }
           
      cmd += "<html>\r\n";
      server.send(200, "text/html", cmd);
}

void handleNotFound(){
  //digitalWrite(led, 1);
  String message="File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET)?"GET":"POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i=0; i<server.args(); i++){
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
  //digitalWrite(led, 0);
}

void setup(void){
  pinMode(output1, OUTPUT);
  pinMode(output2, OUTPUT);
  pinMode(output3, OUTPUT);
  pinMode(output4, OUTPUT);

  digitalWrite(output1, HIGH); 
  digitalWrite(output2, HIGH);
  digitalWrite(output3, HIGH);
  digitalWrite(output4, HIGH);
   
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.config(ip, dns, gateway, subnet); 

  WiFi.begin(ssid, password);
  Serial.println("");

  // by default, we'll generate the HIGH voltage from the 3.3v line internally! (neat!)
  //display.begin(SSD1306_SWITCHCAPVCC, 0x3D);      // initialize with the I2C addr 0x3D (for the 128x64)

  delay(2000);


  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");

  }
 
  ip = WiFi.localIP();

  Serial.println("");
  Serial.print("Connected to ");
  Serial.print(intname);
  Serial.println("");
  Serial.print("Connected To ");
  Serial.println(ssid);
  Serial.print("IP Address: ");
  Serial.println(ip);
  WiFi.macAddress(mac);
  Serial.print("MAC: ");
  Serial.print(mac[5],HEX);
  Serial.print(":");
  Serial.print(mac[4],HEX);
  Serial.print(":");
  Serial.print(mac[3],HEX);
  Serial.print(":");
  Serial.print(mac[2],HEX);
  Serial.print(":");
  Serial.print(mac[1],HEX);
  Serial.print(":");
  Serial.println(mac[0],HEX);

  

  if (MDNS.begin("esp8266")) {
    Serial.println("MDNS responder started");
  }

  server.on("/", handleRoot);

  server.on("/status1=1", [](){
    server.send(200, "text/plain", "device1=ON");
    digitalWrite(output1, LOW);
    device1=true;
    // also you can lock door automatically after 5 secs!
    delay(4000);                  // waits for a second    
    digitalWrite(output1, HIGH);
    device1=false;
  });

  server.on("/status1=0", [](){
    server.send(200, "text/plain", "device1=OFF");
    digitalWrite(output1, HIGH); 
    device1=false;
  });

  server.on("/status2=1", [](){
    server.send(200, "text/plain", "device2=ON");
    digitalWrite(output2, LOW); 
    device2=true;
  });

  server.on("/status2=0", [](){
    server.send(200, "text/plain", "device2=OFF");
    digitalWrite(output2, HIGH); 
    device2=false;
  });

  server.on("/status3=1", [](){
    server.send(200, "text/plain", "device3=ON");
    digitalWrite(output3, LOW); 
    device3=true;
  });

  server.on("/status3=0", [](){
    server.send(200, "text/plain", "device3=OFF");
    digitalWrite(output3, HIGH); 
    device3=false;
  });

  server.on("/status4=1", [](){
    server.send(200, "text/plain", "device4=ON");
    digitalWrite(output4, LOW); 
    device4=true;
  });

  server.on("/status4=0", [](){
    server.send(200, "text/plain", "device4=OFF");
    digitalWrite(output4, HIGH); 
    device4=false;
  });

  server.onNotFound(handleNotFound);
  server.begin();
  Serial.println("HTTP server started");
}

void loop(void){
  server.handleClient();
}

does anyone know how to also include a pincode option for extra security? so that ?statuts1=1&pincode=1234

that would be aswome if someone could help me figure that last part out?

but anyway i hope this post helps someone who needs a static ip and my code works so, if you got any use out ofit or know about how to put in a poincode let me know in reply please, thanks! ps il buy you a beer if you can help with the pincode part!

WiFiLocalIP example is about retrieving the local IP, not about setting the local IP

I suggest there is a better way to do this: Instead of setting a static IP address in your code allow your code to get an IP address by DHCP in the usual manner. Configure your router to supply the required IP address to the device. Most routers allow you to lock a specific IP address to the MAC address of a device, so the particular device always gets the specified IP address.

If you do it this way you can manage and change all your allocated IP addresses in one place, which is the router.

If you don't do this you still have to configure your router so as to make sure the allocated IP addresses are outside the range allocated to DHCP.

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.