[Arduino]ESP32 http ํ†ต์‹ /ESP32 HTTP communication(MacOS)

๐Ÿ‘‰๐Ÿป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

Screenshot

— Arduino IDE

Screenshot

โœ”๏ธ ๋ธŒ๋ผ์šฐ์ €์— http://nodejs์„œ๋ฒ„์ฃผ์†Œ:3000 ์œผ๋กœ ์ ‘์†ํ•˜๋ฉด ๋ธŒ๋ผ์šฐ์ €์— ๋ฉ”์„ธ์ง€๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.
If you access http://nodejsserveraddress:3000 in your browser, a message will be displayed in the browser.

Screenshot

โœ”๏ธ get๋ฐฉ์‹์œผ๋กœ ๋ฉ”์„ธ์ง€๋ฅผ ์ „์†กํ•ฉ๋‹ˆ๋‹ค.(control?led=1)
Sends a message using the GET method.(control?led=1)

Screenshot

โœ”๏ธ ์ „์†ก๋œ ๋ฉ”์„ธ์ง€๊ฐ€ Esp32๋กœ ์ „์†ก๋˜๋Š”๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
You can verify that the transmitted message is sent to the Esp32.

Screenshot

Leave a Reply