NodeMCU: Turn ON/OFF LED using WebSocket on Mobile App
Introduction
The NodeMCU is a popular development board that allows users to create a wide range of IoT projects. One of the most exciting features of the NodeMCU is its ability to connect to the internet and communicate with other devices using various protocols, including WebSocket. In this article, we will explore how to use WebSocket to turn an LED on and off using a mobile app.
What is WebSocket
WebSocket is a protocol that allows for real-time communication between a client and a server over the internet. It provides a bi-directional communication channel, allowing the client and server to send and receive data simultaneously. WebSocket is widely used in web development for applications such as live updates, gaming, and real-time analytics.
Setting Up the Hardware
To get started, you will need the following components:
– NodeMCU board
– LED
– Breadboard
– Jumper wires
– Power source
Connect the LED to the NodeMCU board using the breadboard and jumper wires. Make sure to connect the positive leg of the LED to a digital pin on the NodeMCU and the negative leg to a GND pin.
Setting Up the Software
To create the WebSocket server on the NodeMCU, you will need to use the Arduino IDE. First, install the necessary libraries, including the WebSocket library. Then, upload the following code to the NodeMCU:
“`cpp
#include
#include
const char* ssid = “your_ssid”;
const char* password = “your_password”;
WebSocketServer server(81);
const int ledPin = 2;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println(“Connecting to WiFi..”);
}
Serial.println(WiFi.localIP());
server.begin();
pinMode(ledPin, OUTPUT);
}
void loop() {
server.loop();
if (server.available()) {
WebSocketClient client = server.accept();
if (client.isConnected()) {
client.send(“Connected”);
while (client.isConnected()) {
if (client.available()) {
String message = client.readStringUntil(‘\n’);
if (message.equals(“ON”)) {
digitalWrite(ledPin, HIGH);
} else if (message.equals(“OFF”)) {
digitalWrite(ledPin, LOW);
}
}
}
}
}
}
“`
Creating the Mobile App
To create the mobile app, you will need to use a framework such as React Native or Flutter. Create a new project and install the necessary libraries, including the WebSocket library. Then, use the following code to connect to the WebSocket server and send messages:
“`javascript
import React, { useState, useEffect } from ‘react’;
import { View, Button, Text } from ‘react-native’;
import WebSocket from ‘ws’;
const App = () => {
const [connected, setConnected] = useState(false);
const [ws, setWs] = useState(new WebSocket(‘ws://youripaddress:81′));
useEffect(() => {
ws.onopen = () => {
setConnected(true);
};
ws.onmessage = (event) => {
console.log(event.data);
};
ws.onclose = () => {
setConnected(false);
};
ws.onerror = (event) => {
console.log(event);
};
}, [ws]);
const handleOnPress = () => {
if (connected) {
ws.send(‘ON’);
}
};
const handleOffPress = () => {
if (connected) {
ws.send(‘OFF’);
}
};
return (
);
};
export default App;
“`
Conclusion
In this article, we explored how to use WebSocket to turn an LED on and off using a mobile app. We set up the hardware and software, created the WebSocket server on the NodeMCU, and created the mobile app using React Native. With this project, you can create a wide range of IoT projects that require real-time communication between devices.
FAQ
1. Q: What is the advantage of using WebSocket over other protocols?
A: WebSocket provides bi-directional communication, allowing for real-time updates and more efficient communication.
2. Q: Can I use WebSocket with other development boards?
A: Yes, WebSocket can be used with other development boards that support internet connectivity, such as the ESP32 and Arduino.
3. Q: How do I secure my WebSocket connection?
A: You can secure your WebSocket connection by using encryption protocols such as SSL/TLS.
4. Q: Can I use WebSocket with other programming languages?
A: Yes, WebSocket can be used with other programming languages, such as Python and Java.
5. Q: What are some applications of WebSocket in IoT projects?
A: WebSocket can be used in a wide range of IoT projects, including home automation, industrial automation, and wearable devices.