Start your IoT journey with NodeMCU
Tags: iot, arduino, esp8266
Published: 2022-06-26 12:01:04

IoT framework aims at transforming everyday objects into intelligent systems by connecting the devices over the internet. Sensor networks, embedded systems, big data platforms, cloud computing and service-oriented architecture make up the ecosystem.

The NodeMCU is the abbreviation of Node MicroController Unit. It is an open-source development environment built on inexpensive SoC chips called the ESP8266. The ESP8266 is developed by Espressif Systems. It can be treated as a microcomputer and contains a CPU, RAM, WiFi module, operating system and SDK. It would be a great option for IoT projects.

In this article, we will learn about NodeMCU and will walk you through several sample projects using it.

## Getting Started
### Table of contents
- NodeMCU Pinout Diagram
- Setup the environment
- Arduino IDE installation
- Install dependencies and Configure the IDE
- First NodeMCU program
- Program 2: Blink an external LED
- Program 3: Remote LED control using a web server

Let’s begin with NodeMCU pinout diagram.

### NodeMCU Pinout Diagram
![NodeMCU Pinout Diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/73us7r9kyijqspppbn27.png)
![NodeMCU Pinout Diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v2jkw93v1fmfld99klem.JPG)
[Download the Datasheet](https://espressif.com/sites/default/files/documentation/0a-esp8266ex_datasheet_en.pdf) to know more about ESP8266.

### Setup the environment
#### Arduino IDE installation
Arduino IDE is an open-source, Java-based development environment that is used to write, compile, and upload programs to the Arduino boards. It is available for download for Windows, Mac, and Linux.

- [Download](https://www.oracle.com/in/java/technologies/javase/javase8u211-later-archive-downloads.html) and install the Java JDK 1.8 or later
![Java installation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ck54ait5nh12llen7xue.JPG)
![Java installation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o9ma47he9i1mm3att0y7.JPG)
![Java installation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2tny58qnwe9f48i67z1w.JPG)
![Java installation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c4n41oj5hg0f1syyva6q.JPG)
- Add the Java JDK bin folder path in the environment variables

- [Download](https://www.arduino.cc/en/software) and install the Arduino IDE
![Arduino installation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q2lldj6hty8e90uo2xyt.JPG)
- Double click on the installer and install the IDE with default configuration

![Arduino installation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qi4j189lg180wk89i2ne.JPG)
![Arduino installation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pyce5rx39huwbw26z8xd.JPG)
![Arduino installation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qwwx47r3gxtgj42wc2r0.JPG)
![Arduino installation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l84lj9y76ebab9gzpl69.JPG)

#### Install dependencies and Configure the IDE
ESP8266 can be programmed just like a normal Arduino board. To do that, we must add a few additional packages to the Arduino IDE.

- Open the Arduino IDE. Select `File -> Preferences` and insert the following URL in the Additional Boards Manager URLs input field.

```
http://arduino.esp8266.com/stable/package_esp8266com_index.json
```
![preferences](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7to7pew9en31u017mll9.png)
- Select `Tools -> Board: [default board name] -> Boards Manager` and search esp8266 in the search box of the newly opened window. Click on the Install button of the listed esp8266 by ESP8266 Community package.
![boards](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oqro1x7oiaxvooh4yed4.png)
![boards](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vjyr98c5833bii4yy0ve.png)
- Select Generic ESP8266 Module from the `Tools -> Board: [default board name] -> ESP8266 Boards (version)` menu list.
![boards](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vppsac35nchosk2vezn9.png)
- Connect the NodeMCU board to the system via USB cable. Select the correct COM port from `Tools -> Port` menu.
![ports](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pdzeiq07mptzc34vs4fm.png)

### First NodeMCU program
Programs written in Arduino IDE are called sketches. A sketch is the unit of code that is uploaded to an Arduino board. These sketches are stored in files with the .ino extension.

[Arduino Integrated Development Environment (IDE) v1 | Arduino Documentation](https://docs.arduino.cc/software/ide-v1/tutorials/arduino-ide-v1-basics)

We will create a LED blinking program using the built-in LED on the NodeMCU board.

- Open the Arduino IDE and add the following code in the sketch window

```
int LED = 2;      // Assign LED to pin GPIO2
void setup() {
    // initialize GPIO2 as an output
    pinMode(LED, OUTPUT);
}
void loop() {
    digitalWrite(LED, LOW);     // turn the LED off
    delay(1000);                // wait for a second
    digitalWrite(LED, HIGH);    // turn the LED on
    delay(1000);
}
```
- Click on `File -> Save` to save the code
- Click on the Verify icon to compile the code
![verify](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bhv2uiyvh3ynz17pj28d.png)
- Click on the Upload button to burn the code on the NodeMCU module
![upload](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xsfvgj4c6w2lu03qxy7s.png)
You will get a screenshot like the below after the successful code upload
![output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tf2qsmqy8y68lxls0d5o.png)
Now, you will see the built-in blue LED blink.
![output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9hsyevvb5i7am4h1be01.gif)

### Program 2: Blink an external LED
We have programmed the built-in LED. Now, it’s time to try the same code on an external LED.

Follow the below diagram to connect the LED to the NodeMCU module. To do that, connect the LED on the D1 (GPIO 5) pin and a GND pin.

![program 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hv9qwmwx8sl7kzmm00s7.png)

- Create a new sketch file from `File -> New` and add the following code to it.

```
int LED = 5; // Assign LED pin (D1 on NodeMCU)
void setup() {
    // initialize GPIO 5 as an output
    pinMode(LED, OUTPUT);
}
void loop() {
    digitalWrite(LED, HIGH);  // turn the LED on
    delay(1000);              // wait for a second
    digitalWrite(LED, LOW);   // turn the LED off
    delay(1000);              // wait for a second
}
```
- To compile the code, click the Verify icon. Click the Upload button to upload the code to the NodeMCU. If the upload is successful, the output will look like as follows.
![program 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/92zzca3h9d837uflttj9.gif)

### Program 3: Remote LED control using a web server
ESP8266 acts as a Web Server when connected to an existing WiFi network. It can be used to build web pages and use those sites to operate sensors. Additionally, we can view the sensor reading in real-time on the page.

In this program, we will control two LED bulbs from a web page.

- Connect two LEDs to digital GPIO D5 and D4 through a 220Ω resistor like the illustration shown below.
![diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/btg12y1lcuq1jkv8aman.png)
- Create a new sketch file from `File -> New` and add the following code to it.

```
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
/* Put your SSID & Password */
const char* ssid = "YOUR_SSID";  // Enter SSID here
const char* password = "YOUR_WIFI_PASSWORD";  //Enter Password here
ESP8266WebServer server(80);
uint8_t LED1pin = 4;
bool LED1status = LOW;
uint8_t LED2pin = 5;
bool LED2status = LOW;
void setup() {
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
   
  Serial.begin(115200);
  pinMode(LED1pin, OUTPUT);
  pinMode(LED2pin, OUTPUT);
  delay(100);
while (WiFi.status() != WL_CONNECTED) {
    delay(200);
    Serial.print(".");
  }
Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  
  server.on("/", handle_OnConnect);
  server.on("/led1on", handle_led1on);
  server.on("/led1off", handle_led1off);
  server.on("/led2on", handle_led2on);
  server.on("/led2off", handle_led2off);
  server.onNotFound(handle_NotFound);
  
  server.begin();
  Serial.println("HTTP server started");
}
void loop() {
  server.handleClient();
  if(LED1status)
    digitalWrite(LED1pin, HIGH);
  else
    digitalWrite(LED1pin, LOW);
  
  if(LED2status)
    digitalWrite(LED2pin, HIGH);
  else
    digitalWrite(LED2pin, LOW);
}
void handle_OnConnect() {
  LED1status = LOW;
  LED2status = LOW;
  Serial.println("GPIO2 Status: OFF | GPIO4 Status: OFF");
  server.send(200, "text/html", SendHTML(LED1status,LED2status)); 
}
void handle_led1on() {
  LED1status = HIGH;
  Serial.println("GPIO4 Status: ON");
  server.send(200, "text/html", SendHTML(true,LED2status)); 
}
void handle_led1off() {
  LED1status = LOW;
  Serial.println("GPIO4 Status: OFF");
  server.send(200, "text/html", SendHTML(false,LED2status)); 
}
void handle_led2on() {
  LED2status = HIGH;
  Serial.println("GPIO2 Status: ON");
  server.send(200, "text/html", SendHTML(LED1status,true)); 
}
void handle_led2off() {
  LED2status = LOW;
  Serial.println("GPIO2 Status: OFF");
  server.send(200, "text/html", SendHTML(LED1status,false)); 
}
void handle_NotFound(){
  server.send(404, "text/plain", "Not found");
}
String SendHTML(uint8_t led1stat,uint8_t led2stat){
  String ptr = "<!DOCTYPE html> <html>\n";
  ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
  ptr +="<title>LED Control</title>\n";
  ptr +="<style>html { font-family: Roboto; display: inline-block; margin: 0px auto; text-align: center;}\n";
  ptr +="body{margin-top: 50px;} h1 {color: #222222;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n";
  ptr +=".button {display: block;width: 80px;background-color: #1abc1a;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
  ptr +=".button-on {background-color: #1abc9c;}\n";
  ptr +=".button-on:active {background-color: #16a085;}\n";
  ptr +=".button-off {background-color: #34495e;}\n";
  ptr +=".button-off:active {background-color: #2c3e50;}\n";
  ptr +="p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
  ptr +="</style>\n";
  ptr +="</head>\n";
  ptr +="<body>\n";
  ptr +="<h1>Smart Home Controller</h1>\n";
  
  if(led1stat)
    ptr +="<p>LED1 Status: ON</p><a class=\"button button-off\" href=\"/led1off\">OFF</a>\n";
  else
    ptr +="<p>LED1 Status: OFF</p><a class=\"button button-on\" href=\"/led1on\">ON</a>\n";
if(led2stat)
    ptr +="<p>LED2 Status: ON</p><a class=\"button button-off\" href=\"/led2off\">OFF</a>\n";
  else
    ptr +="<p>LED2 Status: OFF</p><a class=\"button button-on\" href=\"/led2on\">ON</a>\n";
ptr +="</body>\n";
  ptr +="</html>\n";
  return ptr;
}
```
- Click on the Verify icon to compile the code then click on the Upload button to upload the code on the NodeMCU. If the upload is successful then we can see the output as follows.
![program 3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2d090rnk29g42tjp5os9.gif)

Thanks for reading this article.
To get the article in pdf format: [Start your IoT journey with NodeMCU](https://drive.google.com/file/d/1PuoDp-jaZ8vzpagoWJ9UrIsVHUq9nMvg/view)

The full source code of the programs is available on

[GitHub - codemaker2015/ESP2866-NodeMCU-examples](https://github.com/codemaker2015/ESP2866-NodeMCU-examples)

The article is also available on [Medium](https://medium.com/@codemaker2016/start-your-iot-journey-with-nodemcu-c72457084e7a)

If you enjoyed this article, please click on the heart button ♥ and share to help others find it!