Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. Using code tags and other important information is explained in the How to use this forum post. Please read it.
I fell into the same trap. Strangely enough you have to set the IP address AFTER the call to Wifi.begin()- this seems to be a quirk of the ESP8266 firmware. (Tested on Wemos mini D1)
Hmmm. Depends on WHICH ESP8266 based device you are using, what libraries, I imagine.
With the libraries recommended by Sparkfun, and their ESP8266 "Thing" (their name for it), the code is...
WiFi.config(ip, gateway, subnet);
WiFi.mode(WIFI_STA);
// WiFI.begin([ssid], [passkey]) initiates a WiFI connection
// to the stated [ssid], using the [passkey] as a WPA, WPA2,
// or WEP passphrase.
WiFi.begin(WiFiSSID, WiFiPSK);
(You will have put values in things like "ip" and "gateway", etc before this.)
Details in the full sourcecode provided with ArduServer example at...
Although marked as 'fixed' I would add this solution, as putting the call to config() either before or after the call to begin() did not work for me. Spent ages googling fora to no avail then found this doc:
On reading, the solution(at least for me) was to loop waiting for a connection after begin() is called as the documentation states config() will fail if there is no connection:
// call WiFi.begin() here
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
// now you can call WiFi.config()
This example may be usefull. Its possible to change IP at runtime.
/*
* Helio R. de Freitas Jr.
* This example intend to help get IP from string,
* or string format from IP.
* This is useful to change IP value at runtime
* 28/02/2019
* São Paulo - Brazil
* junior700.eletro@gmail.com
*/
#include <ESP8266WiFi.h> //manipula o wifi
#include <DNSServer.h> //Servidor de DNS permite acesso usando nome do site
#include <ESP8266WebServer.h> //Usado para criação de servidor Web
IPAddress apIP(42, 42, 42, 42); //Defining a static IP address: local & gateway HOTSPOT /softAP
char strAddr[] = "127.0.0.1"; // IP at string format
void StrToIP(char *string, IPAddress *ptrAddress)
{
String octeto="";
char *c;
c = &string[0];
int len = strlen(string);
while(*c!='.')
{
octeto+=*c;
c++;
len--;
}
//Serial.print(octeto);
//Serial.print('.');
(*ptrAddress)[0]=(uint8_t)octeto.toInt();
c++;
len--;
octeto="";
while(*c!='.')
{
octeto+=*c;
c++;
len--;
}
//Serial.print(octeto);
//Serial.print('.');
(*ptrAddress)[1]=(uint8_t)octeto.toInt();
c++;
len--;
octeto="";
while(*c!='.')
{
octeto+=*c;
c++;
len--;
}
//Serial.print(octeto);
//Serial.print('.');
(*ptrAddress)[2]=(uint8_t)octeto.toInt();
c++;
len--;
octeto="";
for(int i=0; i<len; i++)
{
octeto+=*c;
c++;
}
//Serial.println(octeto);
(*ptrAddress)[3]=(uint8_t)octeto.toInt();
//Serial.print(Ip[0]);
//Serial.print(" ");
//Serial.print(Ip[1]);
//Serial.print(" ");
//Serial.print(Ip[2]);
//Serial.print(" ");
//Serial.println(Ip[3]);
}
void IPtoStr(char *string, IPAddress IP)
{
String c="";
c += IP[0] ;
c += '.';
c += IP[1] ;
c += '.';
c += IP[2] ;
c += '.';
c += IP[3] ;
c.toCharArray(string, (c.length()+1) );
}
void setup() {
Serial.begin(9600);
delay(10000);
char strAddr1[20]; //new string
Serial.print("Start value to the string = ");
Serial.println(strAddr);
Serial.print("Start value to the apIP = ");
Serial.println(apIP);
StrToIP(strAddr,&apIP); //conversion: String to IP
Serial.print("Conversion: string to apIP = ");
Serial.println(apIP);
IPtoStr(strAddr1,apIP); //conversion IP to a new string
Serial.print("Conversion: apIP to the new string = ");
Serial.println(strAddr1);
}
void loop() {
}