NodeMCU ESP8266 
For
HOME AUTOMATION
 (IoT Application Development)




The best way to quickly develop an IoT application with less Integrated circuits to add is to choose this circuit “NodeMCU”. It is an open-source firmware and development kit that plays a vital role in designing a proper IoT product using a few script lines.
            The module is mainly based on ESP8266 that is a low-cost Wi-Fi microchip incorporating both a full TCP/IP stack and microcontroller capability. It is introduced by manufacturer “Expressif Systems”.
            The ESP8266 NodeMCU is a complex device, which combines some features of the ordinary Arduino board with the possibility of connecting to the internet.
            Arduino Modules and Microcontrollers have always been a great choice to incorporate automation into the relevant project. But these modules come with a little drawback as they lack built-in WiFi capability, and hence external WiFi protocol is to be added to these devices to make them compatible with the internet channel.
            This is the famous  NodeMCU is also an open-source firmware and development kit that

Introduction to NodeMCU ESP8266:

            NodeMCU is based on ESP8266 WiFi SoC which is version 3 and it is based on ESP-12E (An ESP8266 based WiFi module). It is an open-source firmware and development kit that plays a vital role in designing an IoT product using a few script lines and it can always be programmed with Arduino IDE.
            Multiple GPIO pins on the board allows to connect the board with other peripherals and are capable of generating PWM, I2C, SPI, and UART serial communications.
            The interface of the module is mainly divided into two parts including both Firmware and Hardware where former runs on the ESP8266 Wi-Fi SoC and latter is based on the ESP-12 module.

NodeMCU ESP8266 Pinout:

            NodeMCU ESP8266 comes with a number of GPIO Pins. Following figure shows the Pinout of the board.

The power voltage for USB that must be kept around 5 V.

Features:
1. Open-source
2. Arduino-like hardware
3. Status LED
4. MicroUSB port
5. Reset/Flash buttons
6. Interactive and Programmable
7. Low cost
8. ESP8266 with inbuilt wifi
9. USB to UART converter
10. GPIO pins
11. Arduino-like hardware IO
12. Advanced API for hardware IO, which can dramatically reduce the redundant work for configuring and manipulating hardware.
13. Code like arduino, but interactively in Lua script.
14. Nodejs style network API
15. Event-driven API for network applicaitons, which faciliates developers writing code running on a 5mm*5mm sized MCU in Nodejs style.
16. Greatly speed up your IOT application developing process.
17. Lowest cost WI-FI
18. Less than $2 WI-FI MCU ESP8266 integrated and esay to prototyping development kit.
19. We provide the best platform for IOT application development at the lowest cost.

As mentioned above, a cable supporting micro USB port is used to connect the board. As you connect the board with a computer, LED will flash. You may need some drivers to be installed on your computer if it fails to detect the NodeMCU board.

How to Power NodeMCU ESP8266:
We can see from the pinout image above, there are five ground pins and three 3ESP8266 pins on the board. The board can be powered up using the following three ways.

USB Power. It proves to an ideal choice for loading programs unless the project you aim to design requires separate interface i.e. disconnected from the computer.

Provide 3.3V. This is another great option to power up the module. If you have your own off-board regulator, you can generate an instant power source for your development kit.

Power Vin. This is a voltage regulator that comes with the ability to support up to 800 mA. It can handle somewhere between 7 to 12 V. You cannot power the devices operating at 3.3 V, as this regulator unable to generate as low as 3.3V.

Programming:

Step 1: Installing the Firmware
            Following are the steps to install the Firmware on the NodeMCU board:

1. Open the NodeMCU flasher master folder and then open the win32/win64 folder (whichever is in your computer). Now, open the folder Release and then double click ESP8266Flasher to run.
2. Select the COM Port.
3. Goto config tab.
4. Click on the small gear and open up the firmware which you have downloaded
5. Go to the advanced tab and select the desired Baud rate

6. Goto the Operation tab and click on Flash Button.

Step 2: Preparing the Arduino IDE
After Installing the firmware you are ready to do the programming with the ESP8266

1. Install the Arduino IDE

2. open the Arduino IDE from the desktop icon
3. Click on File tab and than open preferences

4. In the additional Boards Manager URLs add the following link (http://arduino.esp8266.com/stable/package_esp8266com_index.json) and click OK




5. Goto Tools>Borads>Boards Manager


6. In the search field type esp8266 click the esp8266 by ESP8266 Community option and click Install



Step 3: Code and Automate
Now we can do whatever you want with your NodeMCU board Following is an example for Home Automation with NodeMCU board via Google Assistant

Ø  In arduino IDE goto tools>Boards>select NODEMCU 1.0 (ESP - 12E Module)
Ø  again goto tools and select port.
Ø  Change the Wifi name and password from the following code.
Ø  Now click on Upload button to upload the following code.
Ø  Connect the 5V Relay module to D1, D2, D3 and D4 pins  of board and ground of relay to ground of the board.
Ø  Power up the board and open the serial monitor from arduino IDE
Ø  After power on, NodeMcu will connect to the wifi.
Ø  Then using google assistant we can operate the home appliances using mobile phone.

Sample code:
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#define Relay1            D1
#define Relay2            D2
#define Relay3            D3
#define Relay4            D4
#define WLAN_SSID       "**************"            
#define WLAN_PASS       "*****************"        
/************************* Adafruit.io Setup *********************************/
#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883                   // use 8883 for SSL
#define AIO_USERNAME    #define AIO_KEY         "***************************"  
WiFiClient client;
// or... use WiFiFlientSecure for SSL
//WiFiClientSecure client;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
/****************************** Feeds ***************************************/
// Setup a feed called 'onoff' for subscribing to changes.
Adafruit_MQTT_Subscribe Light1 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME"/feeds/Re1"); // FeedName
Adafruit_MQTT_Subscribe Light2 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME"/feeds/Re2");
Adafruit_MQTT_Subscribe Light3 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME"/feeds/Re3");
Adafruit_MQTT_Subscribe Light4 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME"/feeds/Re4");
void MQTT_connect();
void setup() {
  Serial.begin(115200);
  pinMode(Relay1, OUTPUT);
  pinMode(Relay2, OUTPUT);
  pinMode(Relay3, OUTPUT);
  pinMode(Relay4, OUTPUT);
  // Connect to WiFi access point.
  Serial.println(); Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WLAN_SSID);
  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
   // Setup MQTT subscription for onoff feed.
  mqtt.subscribe(&Light1);
  mqtt.subscribe(&Light2);
  mqtt.subscribe(&Light3);
  mqtt.subscribe(&Light4);
}
void loop() {

  MQTT_connect();
    Adafruit_MQTT_Subscribe *subscription;
  while ((subscription = mqtt.readSubscription(5000)))
  {
    if (subscription == &Light1)
    {
      Serial.print(F("Got: "));
      Serial.println((char *)Light1.lastread);
      int Light1_State = atoi((char *)Light1.lastread);
      digitalWrite(Relay1, !(Light1_State));
    }
    if (subscription == &Light2)
    {
      Serial.print(F("Got: "));
      Serial.println((char *)Light2.lastread);
      int Light2_State = atoi((char *)Light2.lastread);
      digitalWrite(Relay2, !(Light2_State));
      }
    if (subscription == &Light3)
    {
      Serial.print(F("Got: "));
      Serial.println((char *)Light3.lastread);
      int Light3_State = atoi((char *)Light3.lastread);
      digitalWrite(Relay3, !(Light3_State));    
    }
    if (subscription == &Light4)
    {
      Serial.print(F("Got: "));
      Serial.println((char *)Light4.lastread);
      int Light4_State = atoi((char *)Light4.lastread);
      digitalWrite(Relay4, !(Light4_State));     
    }
  } 
}
void MQTT_connect() {
  int8_t ret;
  // Stop if already connected.
  if (mqtt.connected()) {
    return;
  }
  Serial.print("Connecting to MQTT... ");
  uint8_t retries = 3;
    while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
    Serial.println(mqtt.connectErrorString(ret));
    Serial.println("Retrying MQTT connection in 5 seconds...");
    mqtt.disconnect();
    delay(5000);  // wait 5 seconds
    retries--;
    if (retries == 0) {
      // basically die and wait for WDT to reset me
      while (1);
    }
  }
  Serial.println("MQTT Connected!");
  }








Comments