How To Set up WiFi on a Wemos

Wemos WiFi SetupGet your Wemos online - it's what it was born to do!

A short post today to show you how to set up WiFi on a Wemos D1 Mini.

For me, the whole point of ESP8266 boards (and their variants) is to get them hooked up for networking and IoT fun stuff, so this was a bit of a key one for me to write down!

New to the Wemos? Check out my Wemos setup guide and simple Wemos GPIO LED tutorial.

Initial Setup

I’m going to assume the use of a Wemos D1 Mini here. It may work for other variations of the Wemos, and maybe even other ESP-based boards like the Node MCU.

I’m also going to assume you’ve set up your Wemos board and have the Arduino IDE installed and ready to go. If not, follow my Wemos D1 Mini setup guide here.

Wemos D1 Mini

The Wemos D1 Mini – small and reliable

No Settings Required

If like me, you’re used to the Raspberry Pi way of doing things, the way WiFi is configured on the Wemos will throw you for a second. There’s no separate files or settings for WiFi – it’s just added as part of your sketch. I know right – mind blown!

For example, if you make an LED sketch and want to control that with WiFi, you add the WiFi code to your sketch.

On the flip side, if your project doesn’t need WiFi, you leave that part out and pretend it’s 1996 again (and I reckon it’ll save you some power as well).

WiFi Code

There are two ways I like to do this:

Basic Connectivity

This super-simple option gets you connected with just a few lines of code, but gives you no info on the connection.

It’s great for a project where you’ve already tested WiFi and know everything works. You can always check if it’s connected by using a tool such as Fing (Android/iOS) or Advanced IP Scanner (Windows).

Here’s the code you add to your sketch to enable WiFi in this very basic fashion. You need to update lines 4 and 5 with your SSID (WiFi name) and password:

#include <ESP8266WiFi.h>

// Network SSID
const char* ssid = "YOUR-SSID-HERE";
const char* password = "YOUR-PASSWORD-HERE";
 
void setup() {

  // Connect WiFi
  WiFi.hostname("Name");
  WiFi.begin(ssid, password);
  
}
 
void loop() {
  
//Add project code here  

  }

Connectivity with Serial Monitor Output

This slightly more code-calorific version offers serial printing to let you know if things are working as they should. Just like this:

Arduino IDE Serial Monitor IP Address

Check your connection and IP address with the Arduino IDE Serial Monitor

Open up the serial monitor before you upload the code, and again, change the details in lines 4 and 5 with your SSID and password:

#include <ESP8266WiFi.h>

// Network SSID
const char* ssid = "YOUR-SSID-HERE";
const char* password = "YOUR-PASSWORD-HERE";
 
void setup() {
  
  Serial.begin(115200);
  delay(10);
 
  // Connect WiFi
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.hostname("Name");
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
 
  // Print the IP address
  Serial.print("IP address: ");
  Serial.print(WiFi.localIP());
}
 
void loop() {
  
//Add your project's loop code here

  }

That’s all for now, short one today!

Rich

2 Comments on "How To Set up WiFi on a Wemos"

  1. Try this: https://github.com/tzapu/WiFiManager

    It lets you start up your Wemos and if it can’t connect to an AP it starts up a local AP (SSID=”Wemos” password=”ConfigureMe”) which scans your neighbourhood and looks for available SSIDs. You enter the credentials, the Wemos restarts and connects to the infrastructure network. It’s seriously clever stuff.

    #include <FS.h>                   //this needs to be first, or it all crashes and burns...
    #include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino
    #include <DNSServer.h>
    #include <ESP8266WebServer.h>
    #include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager
    
    void setup() {
    
      Serial.begin(115200);
      Serial.println();
    
      //WiFiManager
    
      WiFiManager wifiManager;
    
      //reset settings - for testing
      wifiManager.resetSettings();
    
      IPAddress _ip = IPAddress(192, 168, 1, 35);
      IPAddress _gw = IPAddress(192, 168, 1, 254);
      IPAddress _sn = IPAddress(255, 255, 255, 0);
    
      wifiManager.setSTAStaticIPConfig(_ip, _gw, _sn);
      if (!wifiManager.autoConnect("WemosAP", "ConfigureMe")) {
        Serial.println("failed to connect, we should reset as see if it connects");
        delay(3000);
        ESP.reset();
        delay(5000);
      }
      //if you get here you have connected to the WiFi
      Serial.println("connected...yeey :)");
      Serial.println("local ip");
      Serial.println(WiFi.localIP());
    }
    void loop() {
    }
    
  2. mark donners | 26/11/2020 at 21:18 | Reply

    This is some serious shit indeed…loving it!

Leave a comment

Your email address will not be published.


*


This website uses cookies. Please visit our privacy policy page for more information. Privacy policy

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close