NodeMCU nods at wifi?

Hihihi! Welcome back! Ignore my oh-so-cheesy attempt to humourize today's motto. But since my failed humor didn't give it away, today's article will deal with data transmission in IoT.

In the last article, I talked about how the three components of IoT are essential—sensors, Internet, and Microcontrollers.

Today, we will connect a microcontroller to the internet or wifi.

But first, let's learn what a microcontroller is.

A microcontroller is a single integrated circuit that controls tasks in the device or system. A basic microcontroller includes a core processor, memory for storing things, parts for connecting to other devices (i/o peripherals), and ways to talk to the outside world (communication). The core processor controls and executes MCU, memory holds information, and the other parts help it communicate with the world around it.

The MCU we are using today is NodeMCU. Ik that sounds like huh? But....

NodeMCU is an open-source firmware- a fancy schmancy term for software and a development kit.

NodeMCU's development kit has ES8266 (a microchip with TCP/IP protocol) for wifi connection and uses the general i/o ports, communication protocols like I2C, 1 wire and PWM, and A2D techniques. In the last article, I already talked about PWM and A2D techniques.

https://www.nodemcu.com/index_en.html

This website has different ways how you can play around with Nodemcu. I recommend you to check it out.

Apart from HTTP client and server, LUA codes, etc., the website has a code to connect with wifi. But I will do it through a while loop because that is what most of us are familiar with. The interface we will use is Arduino.

Arduino is a microcontroller board. We will use IDE (Integrated Development Environment) software to program on that board. So download Arduino 2.3.2 from this website:

https://www.arduino.cc/en/software

and open a new sketch with the preferred name wifi_request.

There are two things to handle when connecting our nodemcu with wifi.

  1. the connections

  2. the code

We will start with the first. You will need a breadboard, 3 male-male jumper wires, 2 led pins, NodeMCU, and a cable to connect to your Laptop.

On your BreadBoard, insert your NodeMCU; in the D2 and D4 pins, you will attach both your LED pins. You can choose any other slots between D0 and D8. Then, you will ground both the ends of the LED and the ground NODEMCU. Wait to connect the prepared board to the Laptop because you might risk burning your NodeMCU. Here is a reference picture for the connections. Don't worry about the blue thing turning on. We still need to connect our board to the Laptop and write code.

This takes me to the next part, CODE. After installing Arduino ide and opening wifi_request as a new sketch, We will write our code in C++. Follow the below steps:

//1) Import two libraries for wifi and client connection

#include <ESP8266WiFi.h>

#include <WiFiClient.h>

//2) Define your wifi credentials. const is like final in Java. It tells the C++ compiler that this data type is constant and isn't changing. The reason is that Java is OOPS-oriented, but C++ is a hybrid of both OOPS and procedural abstraction, so the compiler needs to know what it is dealing with. It's like a safety precaution it takes so that it knows what has to be done and the handling of the data isn't done in a wrong way.

const char* ssid = "_____________";

const char* password = "________________";

//3) Define your LED pin on where your ESP8266 pin is. In our case, it is at D4. That's where it ends.

const int ledPin = D4;

//4) Creating a wifi server object. As we learned in CN, the HTTP port connection is 80.

WiFiServer server(80);

//5) Now, using the little knowledge we learned in the previous article, we will declare a few things to the compiler. We do that with void setup() from what we have learned in the previous article. What components do we have: ESP8266 (node MCU), Arduino board, LED pin, wifi- ssid and password, server, and client? We need to declare them so the compiler knows we are trying to establish a connection.

void setup() {

//a) Let's connect the Arduino board. A way we can connect it is through ide. In the Arduino ide, you have a magnifying glass-like symbol towards the top right corner, under the close button. If you hover, then you get a cue saying serial monitor click it. This serial monitor is like a terminal in vscode. This is where printing happens. So we need to initialize that. We will use the begin method and give it a standard baud rate. The baud rate is the rate at which your data transmits to a serial monitor. The standard is 115200 bps.

Serial.begin(115200);

//b) Next, we should declare the LED Pin. With our last article, we already know how to. But first, we want the pin not to turn on. So, the state is low. We have connected the nodemcu to led pins. We know an MCU controls, too. So, we want it to control Ledpin's state, for which we give a command output. Also, we want to indicate that I am connecting, like blinking. This is a personal preference. So, I will use delay(how many milliseconds you want the delay).

delay(10);

pinMode(ledPin, OUTPUT);

digitalWrite(ledPin, LOW);

// c) Let's tackle wifi now. We connect it to a serial monitor, and I want it to print what it's connecting to, on the monitor, so I added the print statements.

Serial.println();

Serial.print("Connecting to ");

Serial.println(ssid);

//d) After connecting to the serial monitor, we need the wifi to get initialized to give the credentials. This is the implementation of the parameters.

WiFi.begin(ssid, password);

//e) We have connected and implemented the parameters but need to know the status. We use a loop that continuously checks the status of the wifi connection. It waits until the ESP8266 successfully connects to the wifi network. During this time, it prints dots to the serial monitor to indicate that it attempts to establish the connection.

while (WiFi.status() != WL_CONNECTED) {

delay(500); Serial.print(".");

}

//f) Later, we print everything and begin the connection.

Serial.println("");

Serial.println("WiFi connected");

Serial.println("IP address: ");

Serial.println(WiFi.localIP());

server.begin();

}

//6) We are almost there, I promise. We need to implement everything we have declared. That's right, the MAIN function. We need to fit the logic inside the void loop.

void loop() {

WiFiClient client = server.available(); // Check if a client has connected

if (client) {

Serial.println("New client");

String request = client.readStringUntil('\r');

Serial.println(request);

if (request.indexOf("/H") != -1) {

digitalWrite(ledPin,HIGH);

}

else if (request.indexOf("/L") != -1) {

digitalWrite(ledPin, LOW);

}

client.stop();

// Close the connection Serial.println("Client disconnected"); } }

Now that our code is done. We can connect our board to the computer and select another board and port, and you should see your version of Nodemcu. Click it, and voila, get going.

And here is how the led and nodemcu should work. Led glows, and Nodemcu's blue light glows alternatively. I want to attach the video, but I am struggling with it.

This is it for today. Pat yourselves at the back for a job well done. In the following article, we will create a basic UI and UX for you to operate the turning on and off the LED pin through your phone. Till then, tech talk time ticks! Code connections click!