Flutter Stuff

NodeMCU: Turn ON/OFF LED using WebSocket on Mobile App

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 (

NodeMCU WebSocket App

Leave a Comment

Scroll to Top