Sunday, October 14, 2018

Remote Controlling LED Using NodeMCU (ESP8266) Board

Been tinkering around with the NodeMCU (ESP8266) more after I published the original tutorial about Getting Started To Use NodeMCU (ESP8266) With Arduino IDE. Before reading this further, I would recommend you go through it to make sure the board is already setup on your PC with the Arduino IDE (if not already).

I came to notice its quite easy to jump into the interesting world of IoT with this particular board. Since the board has on-board WiFi chip from Espressif, you can control just about anything you want remotely.

I have shared below a simplest possible example of how you can control state of an LED connected to the NodeMCU board remotely (from your phone). Let's start by putting connecting the LED as shown below:


Connect the board to the PC using a/the Micro-USB cable. Go to Tools, Board, select NodeMCU 0.9 (ESP-12 Module). Go to Tools, Port & select COMX port on which the board is connected to PC.

Replace below example code into your Sketch in Arduino IDE. Let me describe what the above code actually does. At startup, it tries to and waits for a successful connection with the SSID & password you have specified. Once connected, it then starts and listens for incoming connections over port 80 (HTTP default). This is what it does in the setup stage.

Now in the main loop, it waits for an incoming HTTP client connection which if found, the checks if the request method is GET and URL contains a specific command like /state/OFF or /state/ON and then reacts accordingly i.e., put the specified pin LOW or HIGH which in turn, sets the LED off or on respectively. In either case, it also responds with a HTML page that shows current state of LED and a link to change the state.

To access the HTML page, you need to be connected to the same WiFi network as the NodeMCU (ESP8266) board and browse the board's local IP address in your favorite browser. To know the IP address easily, the above code writes some useful messages over the serial port it is connected to which you can see by open the Serial Monitor from Tools, Serial Monitor from the Arduino IDE.

Sunday, September 30, 2018

Getting Started To Use NodeMCU (ESP8266) With Arduino IDE

I have recently started getting my hands a bit dirty with the electronics. To start with, I bought a Arduino UNO, Arduino Nano, CubieBoard 3 (CubieTruck), Raspberry Pi Zero W & a couple NodeMCUs. I am still into in-depth learning how these electronics work. When trying to use one of my NodeMCU with the Arduino IDE (they said it was compatible so it's convenient), I found its not very straight forward to get it working in first attempt.


In this blog post, I will try to write the exact steps that were need to get it working along with a minimal example to test. This tutorial assumes you got a Windows (10) PC. Linux/Mac users should be able to adapt most of this write-up to their systems accordingly. Let's start!

First you need to get yourself a ESP8266/NodeMCU board. You can buy one from Amazon (India/International) or any other website/place you like. Along with the board, make sure to get yourself a Micro-USB cable to connect the board with PC. When it comes to cable, I just prefer those from AmazonBasics. Always the top-notch quality and choice of black/white color (am I doing promotions here? :D Nope, I just love it so much). Also buy a breadboard and a simple LED required for the example program to run.

Once it arrives, download and install the Arduino IDE on your PC from here. Before you connect your board to PC, you will also need to download and install USB to UART drivers from here to get the serial/COM port working.

Launch Arduino IDE. Go to File, Preferences, Settings (Tab). Put below URL in additional board manager URLs:

http://arduino.esp8266.com/stable/package_esp8266com_index.json

Click OK. Go to Tools, Board, Board Manager. In the filter box at the top, type ESP8266 and install the esp8266 package by ESP8266 Community.

Not put your NodeMCU in the center of any breadboard end and connect the LED to GND and D8 (GPIO 15) as shown below:


Connect the board to the PC using a/the Micro-USB cable. Go to Tools, Board, select NodeMCU 0.9 (ESP-12 Module). Go to Tools, Port & select COMX port on which the board is connected to PC.

Replace below example code into your Sketch in Arduino IDE.

Hit the Upload button to compile + push your program to the board and run it! You should see the LED blinking or maybe not if anything messed up. Ask me in comments if you encounter issues!