NodeMCU Series Tutorial:
-
ESP8266 and the IoT Magic Device NodeMCU
-
Light Up an LED: How to Program NodeMCU Using Arduino IDE
In the previous article, we introduced how to light up an LED using NodeMCU, which is the most basic step in learning any microcontroller. Ideally, we could explain how to drive various sensors and actuators with NodeMCU just like we do with Arduino. However, due to the esp8266 core for Arduino library, most sensor libraries can be directly used, with only minor differences in pin configuration between NodeMCU and Arduino. Therefore, I believe we can gradually introduce sensors and actuators later. In this article, we will discuss the fundamental reason why NodeMCU, or ESP8266, is called the IoT magic device, which is its native support for WiFi.
Three Network Modes of NodeMCU
To understand how NodeMCU or the ESP8266 chip works, we first need to understand the three basic wireless network modes. You can temporarily think of NodeMCU as your home router or your mobile phone.
AP (Access Point) Mode: A router or mobile phone can typically create a wireless WiFi hotspot, allowing other devices to connect.
STA (Station) Mode: A router or mobile phone can act as a WiFi device, connecting to other hotspots.
AP and STA Mixed Mode: This refers to the coexistence of the above two modes.
These are the three basic WiFi network working modes of NodeMCU/ESP8266. If you don’t fully understand these three different modes yet, that’s okay; we will explain them through different examples later.
How to Quickly Turn NodeMCU into a WiFi Hotspot
Transforming into a WiFi hotspot is a typical application of AP (Access Point) mode. NodeMCU can create a custom WiFi hotspot and set a password. Of course, the hotspot created by NodeMCU is a local area network without internet connection. You can establish a hotspot with NodeMCU using just a few lines of code.
Connect NodeMCU to your computer, open Arduino IDE, select NodeMCU 1.0 under Tools > Board, and choose the correct port under Tools > Port.
Input the following code in the IDE:
/* NodeMCU Establish WiFi Access Point */
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
/* Wireless Settings*/
const char *ssid = “test”; //WiFi Name
const char *password = “12345678”; //WiFi Password
ESP8266WebServer server(80);
/* Establish a web server; any device connected to this hotspot
* can access http://192.168.4.1 through a browser.
*/
void handleRoot() {
server.send(200, “text/html”, “<h1>Hello from ESP8266 AP!</h1>”);
}
void setup() {
delay(1000);
Serial.begin(115200);
Serial.println();
Serial.print(“Configuring access point…”);
/* You can remove the password parameter if you want the AP to be open. */
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print(“AP IP address: “);
Serial.println(myIP);
server.on(“/”, handleRoot);
server.begin();
Serial.println(“HTTP server started”);
}
void loop() {
server.handleClient();
}
Save the file and upload it to NodeMCU. After a successful upload, NodeMCU will establish a WiFi hotspot.
You can connect to this WiFi with your laptop or mobile phone and access
http://192.168.4.1
192.168.4.1 is the default IP address of NodeMCU. If you see the following message in your browser, it means the connection is successful, and you can access the web service running on NodeMCU.
Thus, we have successfully established a WiFi hotspot and can access NodeMCU via a specific local area network IP. Although it’s a simple example, don’t underestimate this mode. You can fully customize the specific functions of this webpage; it can display sensor data or serve as a remote control interface for drones. You don’t even need physical buttons or a display. NodeMCU perfectly bridges the web and the embedded world, with limitless potential.
In the next article, we will look at STA mode and learn how to remotely control the LED switch via WiFi. If you’re interested, you can purchase a NodeMCU to play with:
https://item.taobao.com/item.htm?id=580998229338
How to Get Arduino E-books and Video Tutorials
Do you also want a copy of our Arduino e-book and various video tutorials included with our kit?
Scan the QR code below to follow us, then reply “Benefits”
Click to read the original text and purchase the Arduino beginner learning kit