Building a Simple Server with NodeMCU ESP8266 Board
NodeMCU ESP8266 board is a versatile platform for building Internet of Things (IoT) projects. It is widely used by makers and hobbyists to build smart home systems, weather stations, and remote monitoring systems. In this article, we will explore how to use the NodeMCU ESP8266 board to build a simple server that controls the on and off functionalities.
One of the main advantages of the NodeMCU ESP8266 board is its support for programming in C++. This allows developers to leverage their existing programming skills and use the vast library of C++ functions to program the board.
To demonstrate how to use the NodeMCU ESP8266 board, we will build a simple server that has two endpoints. These endpoints will control the on and off functionalities of the server when called. The code for the server can be found here.
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
WiFiClient wifiClient;
const char* ssid = "Your SSID";
const char* password = "Your Password";
int ledPin2 = D2;
int ledPin1 = D0;
ESP8266WebServer server(80);
void handleRoot() {
server.send(200, "text/plain", "Hello World");
}
void handleOn(){
digitalWrite(ledPin1, HIGH);
server.send(200,"text/plain", "LED turned ON");
digitalWrite(ledPin2, HIGH);
server.send(200,"text/plain", "LED turned ON");
}
void handleOff(){
digitalWrite(ledPin1, LOW);
server.send(200,"text/plain", "LED turned OFF");
digitalWrite(ledPin2, LOW);
server.send(200,"text/plain", "LED turned OFF");
}
void setup() {
pinMode(ledPin2, OUTPUT);
pinMode(ledPin1, OUTPUT);
Serial.begin(9600);
delay(10);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.on("/",handleRoot);
server.on("/on",handleOn);
server.on("/off",handleOff);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
}
In this example, we will use the built-in WiFi support of the NodeMCU ESP8266 board to connect to a WiFi network and communicate with other devices over the internet. This allows us to build IoT projects that can be accessed and controlled remotely from anywhere in the world.
To get started, we first need to set up the NodeMCU ESP8266 board by installing the required libraries and configuring the WiFi settings. Once this is done, we can start building the server by defining the two endpoints for the on and off functionalities.
In the code, we define two endpoints “/on” and “/off” that will trigger the on and off functionalities respectively. When a user sends a GET request to either of these endpoints, the server will turn on or off the LED connected to the board.
With this simple server, we have demonstrated how to use the NodeMCU ESP8266 board to build a basic IoT project. However, the possibilities with this board are endless. For example, as mentioned earlier, the NodeMCU ESP8266 board can be used to build smart home systems, weather stations, and remote monitoring systems.
In fact, the NodeMCU ESP8266 board can even be used for more advanced IoT projects such as the Automated Smart Railway E-Ticketing System with IoT Integration, which is my final year project at the university. Integrating IoT with railway ticketing systems can significantly improve the overall user experience and enhance the efficiency of the system. With IoT sensors and devices, it’s possible to automate many of the processes involved in railway ticketing, such as ticket validation, seat allocation, and tracking train movements.
In conclusion, the NodeMCU ESP8266 board is a powerful and flexible platform for building IoT projects. Whether you are a beginner or an experienced developer, the NodeMCU ESP8266 board provides a great opportunity to explore the possibilities of IoT and build exciting projects.