๐๐ปhttp ํ๋กํ ์ฝ์ ์ด์ฉํด์ Esp32์์ nodejs์๋ฒ๋ก ๋ฉ์ธ์ง๋ฅผ ๋ณด๋ด๋ ๊ธฐ๋ฅ์ ๊ตฌํํ๋ ๋ฐฉ๋ฒ์ ์ค๋ช
ํฉ๋๋ค.
This explains how to implement the functionality to send messages from ESP32 to a Node.js server using the HTTP protocol.
๐๐ป ๊ทธ๋ฆฌ๊ณ nodejs์๋ฒ์์ Esp32๋ก ๋ฉ์ธ์ง๋ฅผ ๋ณด๋ด๋ ๊ธฐ๋ฅ์ ๊ตฌํํ๋ ๋ฐฉ๋ฒ์ ์ค๋ช
ํฉ๋๋ค.
And I will explain how to implement the functionality to send messages from a Node.js server to ESP32.
๐๐ป IDE์์ ์
๋ก๋ ์๋(11520),๋ชจ๋์ค์ (Esp32 DevModule),ํฌํธ์ค์ (/dev/cu.usbserial-120 ๋๋ 140)์ ๋์ผํฉ๋๋ค.
In the IDE, the upload speed (11520), module setting (Esp32 DevModule), and port setting (/dev/cu.usbserial-120 or 140) are the same.
๐๐ป ์๋์ ์์๋๋ก ์คํํฉ๋๋ค.
Execute in the following order.
1)IDE์์ ์๋์ ์ฝ๋๋ฅผ ์
๋ก๋ ํฉ๋๋ค.
Upload the code below in the IDE.
#include <WiFi.h>
#include <WebServer.h>
#include <HTTPClient.h>
const char* ssid = "YOURID";
const char* password = "YOURPASSWORD";
WebServer server(80);
// ์์ ์ผ์๊ฐ (๋์ค์ ์ค์ ์ผ์ ๊ฐ์ผ๋ก ๋์ฒด)
// Example sensor value (replaced with actual sensor value later)
int sensorValue = 42;
// ํด๋ผ์ด์ธํธ๊ฐ ์ผ์๊ฐ ์์ฒญ
// Client requests sensor value
void handleGetSensor() {
String json = "{\"sensor\":" + String(sensorValue) + "}";
server.send(200, "application/json", json);
}
// ํด๋ผ์ด์ธํธ๊ฐ ESP32์ ๋ช
๋ น ๋ณด๋ด๊ธฐ
// Client sends commands to ESP32
void handlePostControl() {
if (!server.hasArg("plain")) {
server.send(400, "text/plain", "No body");
return;
}
String body = server.arg("plain");
Serial.print("RX control: ");
Serial.println(body);
// ์ฌ๊ธฐ์ body ํ์ฑํด์ ์ ์ด ์ฒ๋ฆฌํ๋ฉด ๋จ
// You can parse the body and handle the control here.
server.send(200, "text/plain", "OK");
}
// nodejs์์ get๋ฐฉ์ ์ ๋ณด ๋ฐ๊ธฐ
// Receiving GET method information in Node.js
void handleControl() {
if (server.hasArg("led")) {
String led = server.arg("led");
Serial.print("LED: ");
Serial.println(led);
}
server.send(200, "text/plain", "OK");
}
// ESP32๊ฐ ์ธ๋ถ ์๋ฒ๋ก ์ผ์ ๋ฐ์ดํฐ ๋ณด๋ด๊ธฐ
// ESP32 sends sensor data to an external server
void postToServer() {
HTTPClient http;
http.begin("http://192.168.0.100:3000/sensor");
http.addHeader("Content-Type", "application/json");
String payload = "{\"sensor\":" + String(sensorValue) + "}";
int code = http.POST(payload);
Serial.print("POST result: ");
Serial.println(code);
http.end();
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) { delay(500); }
Serial.print("IP: ");
Serial.println(WiFi.localIP());
server.on("/sensor", HTTP_GET, handleGetSensor);
// post
//server.on("/control", HTTP_POST, handlePostControl);
// get
server.on("/control", HTTP_GET, handleControl);
server.begin();
}
void loop() {
server.handleClient();
// 5์ด๋ง๋ค ์ธ๋ถ ์๋ฒ๋ก ์ผ์ ๋ฐ์ดํฐ ์
๋ก๋
// Upload sensor data to external server every 5 seconds
static unsigned long lastPost = 0;
if (millis() - lastPost > 5000) {
lastPost = millis();
postToServer();
}
}
2)nodejs ์๋ฒ๋ฅผ ์
ํ
ํฉ๋๋ค.
Set up the Node.js server.
โ๏ธํ๋ก์ ํธ ์์ฑ / Create project
mkdir nodejs-server
cd nodejs-server
npm init -y
npm install express
โ๏ธ server.js
const express = require("express");
const app = express();
app.use(express.json());
// Node 18+๋ fetch ๋ด์ฅ
// Node 18+ has built-in fetch
const esp32Ip = "192.168.0.100"; //YOUR ESP32 IP ADDRESS
app.post("/sensor", (req, res) => {
console.log("sensor data:", req.body);
res.json({ ok: true });
});
// app.post("/control", (req, res) => {
// console.log("control:", req.body);
// res.json({ ok: true });
// });
app.get("/control", async (req, res) => {
console.log("control:", req.query);
try {
const r = await fetch(`http://${esp32Ip}/control?led=${req.query.led}`);
const text = await r.text();
res.json({ ok: true, esp32: text });
} catch (err) {
console.error(err);
res.status(500).json({ ok: false, error: String(err) });
}
});
app.get("/", (req, res) => {
res.send("Server OK");
});
app.listen(3000, () => {
console.log("Server running on http://192.168.0.100:3000");
});
โ๏ธ nodejs ์๋ฒ ์คํ / Run Node.js server
node server.js
3)์คํ / Run
โ๏ธ ESP32์ฝ๋๊ฐ ์ ์์ ์ผ๋ก ์
๋ก๋๋๊ณ nodejs์๋ฒ๋ฅผ ์คํํ๋ฉด ๋ค์๊ณผ ๊ฐ์ด ๋ฉ์ธ์ง๊ฐ 5์ด๋ง๋ค ์ถ๋ ฅ๋๋ ๊ฒ์ ํ์ธ ํ ์ ์์ต๋๋ค.
Once the ESP32 code is successfully uploaded and the nodejs server is run, you can see that the following message is printed every 5 seconds.
— nodejs server

— Arduino IDE

โ๏ธ ๋ธ๋ผ์ฐ์ ์ http://nodejs์๋ฒ์ฃผ์:3000 ์ผ๋ก ์ ์ํ๋ฉด ๋ธ๋ผ์ฐ์ ์ ๋ฉ์ธ์ง๊ฐ ์ถ๋ ฅ๋ฉ๋๋ค.
If you access http://nodejsserveraddress:3000 in your browser, a message will be displayed in the browser.

โ๏ธ get๋ฐฉ์์ผ๋ก ๋ฉ์ธ์ง๋ฅผ ์ ์กํฉ๋๋ค.(control?led=1)
Sends a message using the GET method.(control?led=1)

โ๏ธ ์ ์ก๋ ๋ฉ์ธ์ง๊ฐ Esp32๋ก ์ ์ก๋๋๊ฒ์ ํ์ธ ํ ์ ์์ต๋๋ค.
You can verify that the transmitted message is sent to the Esp32.
