実現したいこと
ESPrDeveloperをArduino化し、アクセスポイントとしてESPrDeveloperのTOUTから読んだアナログ入力の値をWiFiを経由してUnityに渡したいです。
問題点
ESPrDeveloper[ESPr® Developer(ESP-WROOM-02開発ボード]はアクセスポイントとしてPCから接続できるが、識別されていないネットワークとして表示される。
またnetstat -anコマンドで見ても存在を確認できない。
Unity側でUDPClientから何も受信されない。
Arduino
1#include <ESP8266WiFi.h> 2#include <WiFiUDP.h> 3extern "C" { 4#include "user_interface.h" 5} 6 7//Access Point Setting 8const char *APSSID = "ESP_WROOM_02"; 9const char *APPASS = "hoge4444"; 10unsigned int localPort = 22222; 11 12WiFiUDP UDP; 13char packetBuffer[255]; 14 15static const char *udpReturnAddr = "192.168.144.100"; 16static const int udpReturnPort = 22223; 17 18void setup() { 19 20 Serial.begin(115200); 21 Serial.println(); 22 23 WiFi.softAP(APSSID, APPASS); 24 25 IPAddress myIP = WiFi.softAPIP(); 26 Serial.print("AP IP address: "); 27 Serial.println(myIP); 28 UDP.begin(localPort); 29} 30 31void loop() { 32 if(WiFi.softAPgetStationNum() >= 1) 33 { 34 uint ADC_Value = 0; 35 ADC_Value = system_adc_read(); 36 delay(3); 37 String str = String(ADC_Value) ; 38 int a = str.length(); 39 char msg[a+1]; 40 str.toCharArray(msg, a+1); 41 42 UDP.beginPacket(udpReturnAddr, udpReturnPort); 43 UDP.write(msg, a+1); 44 UDP.endPacket(); 45 } 46}
ESPrDeveloperの設定は以下の通りです。また電源はESPrDeveloperを有線でPCに接続したものを使用しています。
Unity側の受信コードは以下の通りです。LOCAL_PORTには22223を入力し実行しています。
C#
1using UnityEngine; 2using System.Collections; 3using System.Net; 4using System.Net.Sockets; 5using System.Text; 6using System.Threading; 7using UnityEditor; 8 9public class UDPReceive : MonoBehaviour 10{ 11 public int LOCAL_PORT; 12 public string ESP_ADDRESS; 13 static UdpClient udp; 14 Thread thread; 15 16 void Start() 17 { 18 udp = new UdpClient(LOCAL_PORT); 19 udp.Client.ReceiveTimeout = 1000; 20 thread = new Thread(new ThreadStart(ThreadMethod)); 21 thread.Start(); 22 } 23 24 void Update() 25 { 26 Debug.Log(udp.Available); 27 } 28 29 void OnApplicationQuit() 30 { 31 thread.Abort(); 32 udp.Close(); 33 } 34 35 private static void ThreadMethod() 36 { 37 while (true) 38 { 39 IPEndPoint remoteEP = null; 40 byte[] data = udp.Receive(ref remoteEP); 41 string text = Encoding.ASCII.GetString(data); 42 Debug.Log(text); 43 } 44 } 45 46}
試したこと
ESP8266のスケッチ例WiFiAccessPointを書き込み実行したところ問題なく機能しました。
wiresharkでのパケットの確認
ESPrDeveloper側のコードを以下のように改変し、wiresharkでWiFiにどんなパケットが流れているか確認しました。
Arduino
1#include <ESP8266WiFi.h> 2#include <WiFiUdp.h> 3extern "C" { 4#include "user_interface.h" 5} 6const char* ssid = "ESP_WROOM_02"; 7const char* password = "hoge4444"; 8 9WiFiUDP UDPTestServer; 10static const char *udpReturnAddr = "192.168.150.64"; 11unsigned int UDPPort = 22222; 12 13const int packetSize = 2; 14byte packetBuffer[packetSize]; 15 16void setup() { 17 Serial.begin(115200); 18 delay(10); 19 20 Serial.println(); 21 Serial.println(); 22 Serial.print("Connecting to "); 23 Serial.println(ssid); 24 25 WiFi.begin(ssid, password); 26 WiFi.config(IPAddress(192, 168, 4, 1), IPAddress(192, 168, 4, 2), IPAddress(255, 255, 255, 0)); 27 28 while (WiFi.softAPgetStationNum() <= 0) { 29 delay(500); 30 Serial.print("."); 31 } 32 33 Serial.println(""); 34 Serial.println("WiFi connected"); 35 Serial.print("IP address: "); 36 Serial.println(WiFi.localIP()); 37 38 IPAddress myIP = WiFi.softAPIP(); 39 Serial.print("AP IP address: "); 40 Serial.println(myIP); 41 42 byte mac[6]; 43 WiFi.macAddress(mac); 44 char buf[20]; 45 Serial.print("MAC address: "); 46 sprintf(buf,"%02X:%02X:%02X:%02X:%02X:%02X",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]); 47 Serial.print(buf); 48 sprintf(buf," %02x%02x%02x%02x%02x%02x",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]); 49 Serial.println(buf); 50 UDPTestServer.begin(UDPPort); 51 52} 53 54int value = 0; 55 56void loop() 57{ 58 if(WiFi.softAPgetStationNum() <= 0) 59 { 60 delay(500); 61 Serial.print("."); 62 } 63 else 64 { 65 handleUDPServer(); 66 } 67 delay(1); 68} 69 70void handleUDPServer() 71{ 72 uint ADC_Value = 0; 73 ADC_Value = system_adc_read(); 74 String str = String(ADC_Value) ; 75 int a = str.length(); 76 char msg[a+1]; 77 str.toCharArray(msg, a+1); 78Serial.print("."); 79 UDPTestServer.beginPacket(udpReturnAddr, UDPPort); 80 UDPTestServer.write(msg, a+1); 81 UDPTestServer.endPacket(); 82 delay(3); 83}
結果は以下のようになりUDPパケットは流れず、ARPパケットやICMPパケットが多くみられました。
![イ
環境
Unity2017.3.0f3
Arduino1.8.5
ESP8266 Ver2.4.1
参考にしたサイト
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。