Wemos D1 mini static IP issue

Hi All,

I’m new to the Wemos platform… I’m working on a simple project that functions as expected with the exception of receiving a DHCP address instead of static IP.

Here’s my full code:

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


#define NUM_PIXELS 20
Adafruit_NeoPixel pixels(NUM_PIXELS, D2, NEO_GRB | NEO_KHZ800);

MDNSResponder mdns;

os_timer_t osTimer;

// network credentials
const char* ssid     = "my_ssid_name";
const char* password = "my_wifi_pass";
////////////////
IPAddress ip(192, 168, 1, 140);
IPAddress gateway(192, 168, 1, 1); 
IPAddress subnet(255, 255, 255, 0);
//WiFi.config(ip, gateway, subnet);
/////////////////

int R[13] = {255,255,227,255,255,128,075,000,127,000,127,255,000};
int G[13] = {194,165,066,000,000,000,000,000,255,255,255,255,000};
int B[13] = {000,000,052,000,255,128,130,255,212,000,000,000,000};
int countColors = 0;
int numColors = 12;

// Buttons text colors and initial values
String buttonColor [2] = {"white", "black"};
boolean ColorState [13] = {1,1,1,1,1,1,1,1,1,1,1,1,1}; // initial colors
boolean buttonSparkle  = 1; 
boolean buttonBrighter = 1; 
boolean buttonFaster   = 1;
boolean buttonDarker   = 1;
boolean buttonSlower   = 1; 
boolean buttonReset    = 1; 

int brightValue = 4;
int brightMax   = 16; // brightness (1..16)

int waitTimes [16]= {50, 100, 150, 200, 250, 500, 750, 1000, 1500, 2000, 2500, 3000, 5000, 10000, 30000, 60000}; // millis()
int waitTime     = 5; // default values
int lastColor    = 0;
int foundColor   = 0;
int currentColor = 0;
int beginSparkle = 0;
int currentLed   = 0;

ESP8266WebServer server(80);

String webPage = "";

void setColor(int Rs, int Gs, int Bs) 
{
   for (int i = 0; i < NUM_PIXELS; i++) 
   {
      pixels.setPixelColor (i, (Rs * brightValue)/brightMax, (Gs * brightValue)/brightMax, (Bs * brightValue)/brightMax);
   }
   pixels.show();
}

int nextColor (int lastColor)
{
   foundColor = numColors; // nothing found return value
   countColors = 0;        // count number of searches inside the loop

   do
   { 
      currentColor += 1;
      countColors  += 1;
      if (currentColor>numColors)   {currentColor=0;}
      if (ColorState[currentColor]) {foundColor=currentColor;}
   } 
   while (currentColor != lastColor && foundColor == numColors && countColors < numColors+1);
   return (foundColor);
}

// interrupt os-timer
void timerCallback(void *pArg) 
{   
   if (!buttonSparkle)
   {
      // Sparkle Off
      currentColor = nextColor (lastColor);
      if (lastColor != currentColor)
      {
         setColor (R[currentColor], G[currentColor], B[currentColor]);
         lastColor = currentColor;
      }
   }
   else
   {
      // Sparkle
      beginSparkle += 1;                            // Startposition for first color
      if (beginSparkle>numColors-1) {beginSparkle=0;}
      
      currentColor = 0;                             // Begin with first color in array
      for (int x = 0; x < NUM_PIXELS; x++) 
      {
         currentColor = nextColor (lastColor);
         lastColor = currentColor;
         currentLed = beginSparkle + x;
         if (currentLed >= NUM_PIXELS) 
         {
            currentLed = currentLed - NUM_PIXELS;

         }
      pixels.setPixelColor (currentLed, (R[currentColor] * brightValue)/brightMax, (G[currentColor] * brightValue)/brightMax, (B[currentColor] * brightValue)/brightMax);
      }
      pixels.show();
   }
} 

void showPage() 
{
   webPage  = "<h2>**Merry Chistmas**</h2>";                                                                                                                   
   webPage += "<p><a href=\"socketAmb\"><button style=\"background:#FFC200;font-size:200%;width:33%;padding:3%;color:" + buttonColor [ColorState[0]]  + "\">amber     </button></a>";   
   webPage += "   <a href=\"socketRes\"><button style=\"background:#7F7F7F;font-size:200%;width:33%;padding:3%;color:" + buttonColor [buttonReset]    + "\">reset     </button></a></p>";

   server.send(200, "text/html", webPage);
   yield (); 
}

void setup(void){


   // neopixel
   pixels.begin();
   pixels.show();
   setColor(4,4,4); // wait for IP

   // preparing GPIOs
   delay(1000);
   Serial.begin(115200);
   WiFi.config(ip, gateway, subnet);
   WiFi.begin(ssid, password);
   Serial.println("");

   // Wait for connection
   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());

   // timer
   os_timer_setfn (&osTimer, timerCallback, NULL);
   os_timer_arm   (&osTimer, waitTimes[waitTime], true);

   server.on ("/", [](){showPage(); });
   server.on ("/socketAmb", [](){ColorState [0]  = !ColorState [0];  showPage(); });
   server.on ("/socketYel", [](){ColorState [11] = !ColorState [11]; showPage(); });
   server.on ("/socketBri", [](){brightValue += 1; 
                                 buttonDarker = 1; 
                                 if (brightValue>=16){brightValue=16;
                                                      buttonBrighter=0;}
                                 showPage();  });
   server.on ("/socketFas", [](){waitTime -= 1;
                                 buttonSlower = 1; 
                                 if (waitTime<=0){waitTime=0;
                                                  buttonFaster=0;} 
                                 os_timer_arm (&osTimer, waitTimes[waitTime], true);
                                 showPage(); });
   server.on ("/socketSpa", [](){buttonSparkle = !buttonSparkle;
                                 showPage(); });
   server.on ("/socketDar", [](){brightValue -= 1; 
                                 buttonBrighter = 1;
                                 if (brightValue<=1){brightValue=1;
                                                     buttonDarker = 0;}
                                 showPage(); });
   server.on ("/socketSlo", [](){waitTime += 1;
                                 buttonFaster=1;
                                 if (waitTime>=15){waitTime=15;
                                                   buttonSlower=0;} 
                                 os_timer_arm (&osTimer, waitTimes[waitTime], true);
                                 showPage(); });

   server.on ("/socketRes", [](){brightValue = 4;
                                 waitTime = 5;
                                 os_timer_arm(&osTimer, waitTimes[waitTime], true);

                                 buttonSparkle  = 1;
                                 buttonBrighter = 1;
                                 buttonFaster   = 1;
                                 buttonDarker   = 1;
                                 buttonSlower   = 1; 
    
                                 showPage (); });

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

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

I have tried referencing a few different forum posts both in arduino.cc and github, all recommending similar approaches but the serial monitor still shows DHCP obtained IP.

I added this section into the code for setting the IP and tried placing the Wifi.config in this block:

IPAddress ip(192, 168, 1, 140);
IPAddress gateway(192, 168, 1, 1); 
IPAddress subnet(255, 255, 255, 0);
WiFi.config(ip, gateway, subnet);

and got the error:

xmas_ornament_wifi:23: error: 'WiFi' does not name a type

 WiFi.config(ip, gateway, subnet);

 ^

exit status 1
'WiFi' does not name a type

Placting the code WiFi.config(ip, gateway, subnet); just before the WiFi.begin part doesn’t error out, but also doesn’t set it statically. I’m up to any and all recommendations

Other possibly useful info: I’m running arduino 1.8.2. My board manager for ESP8266 is version 2.3.0

Thank you for any and all assistance you can offer :slight_smile:
Nubstar

try WiFi.config(ip, gateway, subnet, gateway);

Make note that you defined it twice in your code.

Hi mistergreen,

The gateway needs to be defined twice, or should is be dns? Also, where in the code should it go? The section where I typed out the IP info, i did comment it out when testing in various spots.

Thanks Nubstar

right before WiFi.begin. Setting DNS the same as gateway. I got mine to have a static ip.

Hi Mistergreen,

So this would be what is required?

// network credentials
const char* ssid     = "ssidename";
const char* password = "wifipassword";

IPAddress ip(192, 168, 1, 99);
IPAddress gateway(192, 168, 1, 1); 
IPAddress subnet(255, 255, 255, 0);
IPAddress dns1(8, 8, 8, 8);
IPAddress dns2(4, 4, 2, 2);

and

   delay(1000);
   WiFi.config(ip, gateway, subnet, dns1, dns2);
   Serial.begin(115200);
   WiFi.begin(ssid, password);
   Serial.println("");

**Attached when using the code this way just doesn’t connect. The wemos is only 10 feet away from the AP and work’s fine on DHCP. I just get dots on the serial console and the IP in un-pingable.

Thank you
Nubstar

dots.png

look at post #1

got it working!

IPAddress ip(192, 168, 0, 99);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(192, 168, 0, 1);

...

  // preparing GPIOs
   delay(1000);
   Serial.begin(115200);
   WiFi.config(ip, gateway, subnet, dns);
   WiFi.begin(ssid, password);
   Serial.println("");