0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ESP32: アクセスポイントにする

Posted at

こちらの記事と同様のことを行いました。
ESP32とPCで通信してみよう!~SoftAP編~

Arduino IDE 2.3.4 を使いました。

image.png

プログラム

ssid01.ino
#include <WiFi.h>

const char *ssid = "ESP32_AP";
const char *password = "1234";

WiFiServer server(8000);

void setup() {
  Serial.begin(115200);
  
  // WiFiネットワークを作成
  WiFi.softAP(ssid, password);
  IPAddress IP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(IP);
  delay(2000);

  server.begin(); // サーバーを起動
  Serial.print("AP IP address: ");
  Serial.println(IP);
  delay(2000);
}

void loop() {
  WiFiClient client = server.available(); // 接続の待機

  if (client) { // 接続されたとき
    Serial.println("New Client.");
    
    while (client.connected()) { // 接続されている間はデータの受信を続ける
      if (client.available()) {
        String request = client.readStringUntil('\r'); // リクエストの読み込み
        Serial.println("Received: " + request);
        
        // レスポンス
        client.println("Received: " + request);
        delay(1);
        client.stop(); // 接続を閉じる
        Serial.println("Client disconnected.");
      }
    }
  }
}

何故か SSID が ESP_660449 になります。

接続テスト

テストに使う python のプログラム

test01.py
#! /usr/bin/python

import socket

# ESP32のIPアドレス
esp32_ip = "192.168.4.1"
port = 8000

# ESP32に接続
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((esp32_ip, port))

# 送信する文字列
message = input("送る文字列を入力\n")

# ESP32にデータを送信
client.sendall(message.encode())

# データを受信して表示
response = client.recv(4096)
print(response.decode())

# 接続を閉じる
client.close()

テスト方法

パソコンを ESP_660449 に接続する

image.png

Python のプログラムを実行する

$ ./test01.py 
送る文字列を入力
Good Morning
Received: Good Morning

ESP32 シリアルモニターの表示

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?