Wednesday, July 13, 2016

Getting Started with Nodemcu/ESP8266 (Tutorial-1)

I am using the following NodeMCU board for the following tutorial:
Image result for nodemcu
But its your choice you can buy anyone which ever your feel comfortable. 

So let's begin, I first heard about the ESP8266 WiFi chip from my friends but at that time there is no support for this chip on Arduino IDE. At first, I was reluctant to use this chip even though this chip was really very cheap, but it would be difficult to use and I have to use new software's, new scripting language (LUA Script in this case), so I dropped my plan at that time. However, everything changed now with the introduction of a modified version of the Arduino IDE for the ESP8266. With this software, not only it was really easy to use the ESP8266, but it also worked with the on-board processor of the ESP8266, therefore removing the need of an Arduino board next to the chip. This made it the perfect choice for IOT applications.

In this post we are going to start by setting up the ESP8266 chip. We are going to see how to configure & upload code to the ESP8266 chip. For that, we will be using a modified version of the Arduino IDE. This makes using the ESP8266 so much easier, as we will be using a well-known interface & language to configure the chip. We will also be able to use most of the already existing Arduino libraries for our projects.

So let's get started.

CP2102 Installation
In order to install CP2102 (USB to Serial Converter), you will need driver for the same. You may download drivers from the following link. This link offer drivers for 32 and 64 bit operating system.
  • Window
  • Mac
  • Linux
  • Android
You may download CP2102 Drivers from here.
Once you have downloaded drivers as per the operating system your system has got. Connect the NodeMCU to your computer and install the downloaded drivers when your system prompts to install the drivers for the newly connected USB device.

Driver Installation

Go to the device manager of your computer and note down the COM port allocated to the newly connected USB device i.e. You NodeMCU. This comport number will be required while using NodeMCU.


Driver Installation Complete (Note the Com Port Number)

Now your NodeMCU is installed on your computer. Now you need to configure the Arduino IDE.

Now the next step is to download and install the Arduino Core for ESP8266 Chip. This Library project is available for free downloading on the following GitHub page.

This project brings support for ESP8266 chip to the Arduino environment. It lets you write sketches using familiar Arduino functions and libraries, and run them directly on ESP8266, no external micro-controller required.


ESP8266 Arduino core comes with libraries to communicate over WiFi using TCP and UDP, set up HTTP, mDNS, SSDP, and DNS servers, do OTA updates, use a file system in flash memory, work with SD cards, servos, SPI and I2C peripherals.

The following methods can be used to install the libraries in Arduino.
  • Using Boards Manager
  • Using git version
  • Using PlatformIO
  • Building with make

You can see the project page on GitHub for all installation process, i am only going to use the first method "Using Board Manager".

Installing with Boards Manager
Starting with 1.6.4, Arduino allows installation of third-party platform packages using Boards Manager. We have packages available for Windows, Mac OS, and Linux (32 and 64 bit).
  • Install Arduino 1.6.8 from the Arduino website.
  • Start Arduino and open Preferences window.
  • Enter http://arduino.esp8266.com/stable/package_esp8266com_index.json into Additional Board Manager URLs field. You can add multiple URLs, separating them with commas.
  • Open Boards Manager from Tools > Board menu and install esp8266 platform (and don't forget to select your ESP8266 board from Tools > Board menu after installation).
The best place to ask questions related to this core is ESP8266 community forum: http://www.esp8266.com/arduino.

Step 1: Update Additional Boards Manager URL Under File->Preferences and Re-Start IDE

Step 2: Open Board Managers to install ESP8266 Libraries for Arduino

Step 3: Search and Install this ESP8266 Package. After installation restart IDE.
Now you are ready :-) to program your NodeMCU/ESP8266.

Create a new Sketch in Arduino IDE and paste the following code:

 void setup() {  
  pinMode(LED_BUILTIN, OUTPUT);   // Initialize the LED_BUILTIN pin as an output  
 }  
 // the loop function runs over and over again forever  
 void loop() {  
  digitalWrite(LED_BUILTIN, LOW);  // Turn the LED on (Note that LOW is the voltage level  
                   // but actually the LED is on; this is because   
                   // it is acive low on the ESP-01)  
  delay(1000);           // Wait for a second  
  digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making the voltage HIGH  
  delay(2000);           // Wait for two seconds (to demonstrate the active low LED)  
 }  

Select the board and select the Serial Port which we have seen at the top of the post.
Select Board and also Select the COM port on which the board is connected.

Upload the Sketch
Output:
Led On for 2 Seconds

Led Off for 2 Seconds
The above example is just to test the NodeMCU kit which i purchased from Amazon, but as i have purchased this development kit to test the WiFi functionality, which we haven't tested till now.
So i am going to test whether the development kit is working correctly , and will write a program to connect the chip to my local WiFi network (TP-Link Router).

The program i used is as follow:
 #include <ESP8266WiFi.h>  
 // WiFi parameters  
 const char* ssid = "Internet";  
 const char* password = "12344321";  
 void setup(void)  
 {  
  // Start Serial  
  Serial.begin(115200);  
  delay(500);  
  // Connect to WiFi  
  WiFi.begin(ssid, password);  
  while (WiFi.status() != WL_CONNECTED)  
  {  
   delay(500);  
  }  
  Serial.println("");  
  Serial.println("WiFi connected");  
  // Print the IP address  
  Serial.println(WiFi.localIP());  
  pinMode(LED_BUILTIN, OUTPUT);  
 }  
 void loop()  
 {  
  digitalWrite(LED_BUILTIN, LOW);  
  delay(1000);  
  digitalWrite(LED_BUILTIN, HIGH);  
  delay(1000);  
 }  

The output of this program on serial port is as follow:
Ip Address assigned to NodeMCU by WiFi Router
Note: I don't know what these characters are and why they are coming, i will figure out this and update this post.

Clearly from the above snapshot, the NodeMCU is connected to my Router WiFi Network, IP Address 192.168.0.102 is assigned to NodeMCU.
Now to cross verify this, i opened my routers web page and find out the devices connected to it. Its present on 4th number with the same address as we got on serial port.


NodeMCU is listed.

No comments:

Post a Comment