ESP8266WebServer and Blynk

I’m trying to setup a webserver and Blynk together, but I think they’re some how conflicted. I’ve read the post here Blynk and ESP8266WebServer that it will work, but that leads to couple failures…


#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WebServer.h>
#include <BlynkSimpleEsp8266.h>

ESP8266WebServer server(80);

char auth[] = "auth";
char ssid[] = "";
char pass[] = "";

IPAddress staticIP(192, 168, 1, 15); 
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(1, 1, 1, 1);

void handleRoot() {
  server.send(200, "text/html", "ok");
}

void connectToWifi() {    
  WiFi.disconnect();
  WiFi.config(staticIP, gateway, subnet);
  WiFi.mode(WIFI_STA); 
  WiFi.begin(ssid, pass);
  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());  
}

void startBlynk() {
  Blynk.config(auth);
  Blynk.begin(auth, ssid, pass);  
}

void startServer() {
  server.begin();
  server.on("/", handleRoot);
}

void setup()
{  
  Serial.begin(115200);
  connectToWifi();
  startServer(); 
  startBlynk(); 
}

BLYNK_WRITE(V0) {
  Serial.println("turning on");  
}

void loop()
{
  if (WiFi.status() != WL_CONNECTED) {
    connectToWifi();
  }
  server.handleClient();
  Blynk.run();
}

I think Blynk will have to use the existing wifi, but I’m not sure how. thanks for the help!

1 Like

seems like if I comment out the WiFi.config(...) part, it’ll work… but hmmm, I need the static IP to serve simple info (webserver)

Looks like I’ve found the problem,
the config needs to be like this:
WiFi.config(staticIP, subnet, gateway, dns);

all right, problem solved, I hope this might help someone who got into problem like me.

3 Likes

actually, I think I got it wrong, it should be as the one that I stated.

WiFi.config(ip);
WiFi.config(ip, dns);
WiFi.config(ip, dns, gateway);
WiFi.config(ip, dns, gateway, subnet);
2 Likes