ODIN-W2(技適付きWiFiモジュール)でTCP通信の実験。mbed-os-example-tcp-ping-pongを試してみる。

ODIN-W2でtcp通信の実験としてmbed-os-example-tcp-ping-pongを試してみました。ボードとしては、EVK-ODIN-W2(技適付き WiFi モジュール)を使用します。

1.動作環境

ソースコードは、下記から入手します。ここで試す動作としては、Linux/Windows で Node.js を動作させて、ODIN-W2とパケットを ping-pong します。

モジュール、開発環境などは下記を参照してください。

2. Linux(ubuntu20) でのNode.js を動作させる

Linux に Nodeをインストール。

$ sudo apt install npm
$ sudo npm install -g n

ダウンロードした、server.js を実行します。

$ node server.js 

実際の動作はこうなります。

$ node server.js 
enp2s0 192.168.2.200
wlxcce1d5407f61 192.168.2.106
Listening on port 1885
New socket connected
New data on socket ping
New data on socket ping

3.EVK-ODIN-W2で ping-pong の動作をさせる。

Build環境は、下記をご参照ください。

[+New Project] から下記のように新規プロジェクトを作成します。

画像1

mbed-os-example-tcp-ping-pongのそのものの問題として、せっかくの WiFiが活用されてませんでした。下記のように修正しました。IPアドレスは合わせてください。

#include "mbed.h"
#include <stdio.h>
#include <cstring>
#include <fstream>
#define HOST_IP			"192.168.2.106"
#define HOST_PORT		1885
WiFiInterface *wifi;
TCPSocket socket;
const char *sec2str(nsapi_security_t sec)
{
   switch (sec) {
       case NSAPI_SECURITY_NONE:
           return "None";
       case NSAPI_SECURITY_WEP:
           return "WEP";
       case NSAPI_SECURITY_WPA:
           return "WPA";
       case NSAPI_SECURITY_WPA2:
           return "WPA2";
       case NSAPI_SECURITY_WPA_WPA2:
           return "WPA/WPA2";
       case NSAPI_SECURITY_UNKNOWN:
       default:
           return "Unknown";
   }
}
int scan_demo(WiFiInterface *wifi)
{
   WiFiAccessPoint *ap;
   printf("Scan:\n");
   int count = wifi->scan(NULL,0);
   if (count <= 0) {
       printf("scan() failed with return value: %d\n", count);
       return 0;
   }
   /* Limit number of network arbitrary to 15 */
   count = count < 15 ? count : 15;
   ap = new WiFiAccessPoint[count];
   count = wifi->scan(ap, count);
   if (count <= 0) {
       printf("scan() failed with return value: %d\n", count);
       return 0;
   }
   for (int i = 0; i < count; i++) {
       printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(),
              sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
              ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
   }
   printf("%d networks available.\n", count);
   delete[] ap;
   return count;
}
// Receive data from the server
void receiveTCP() {
   // Allocate 2K of data
   char* data = (char*)malloc(2048);
   while (1) {
       // recvfrom blocks until there is data
       nsapi_size_or_error_t size = socket.recv(data, 2048);
       if (size <= 0) {
           if (size == NSAPI_ERROR_WOULD_BLOCK) {  // Would block... that's fine (no data on the line)
               thread_sleep_for(100);
               continue;
           }
           printf("Error while receiving data from TCP socket, probably it's closed now? (%d)\r\n", size);
           // @todo: re-connect
           break;
       }
       // turn into valid C string
       data[size] = '\0';
       printf("Received %d bytes from TCP socket (%s)\r\n", size, data);
   }
}
int main() {
   printf("WiFi example\n");
   mbed_stats_sys_t stats;
   mbed_stats_sys_get(&stats);
   printf("Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
   printf("CPU ID: 0x%x \n", (int)stats.cpu_id);
   printf("Compiler ID: %d \n", stats.compiler_id);
   printf("Compiler Version: %d \n", (int)stats.compiler_version);
   /* RAM / ROM memory start and size information */
   for (int i = 0; i < MBED_MAX_MEM_REGIONS; i++) {
       if (stats.ram_size[i] != 0) {
           printf("RAM%d: Start 0x%lx Size: 0x%lx \n", i, stats.ram_start[i], stats.ram_size[i]);
       }
   }
   for (int i = 0; i < MBED_MAX_MEM_REGIONS; i++) {
       if (stats.rom_size[i] != 0) {
           printf("ROM%d: Start 0x%lx Size: 0x%lx \n", i, stats.rom_start[i], stats.rom_size[i]);
       }
   }
   /* WiFi Code Start */
   wifi = WiFiInterface::get_default_instance();
   if (!wifi) {
       printf("ERROR: No WiFiInterface found.\n");
       return -1;
   }
   int count = scan_demo(wifi);
   if (count == 0) {
       printf("No WIFI APs found - can't continue further.\n");
       return -1;
   }
   printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID);
   int ret = wifi->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
   if (ret != 0) {
       printf("\nConnection error: %d\n", ret);
       return -1;
   }
   printf("Success\n\n");
   printf("MAC: %s\n", wifi->get_mac_address());
   printf("IP: %s\n", wifi->get_ip_address());
   printf("Netmask: %s\n", wifi->get_netmask());
   printf("Gateway: %s\n", wifi->get_gateway());
   printf("RSSI: %d\n\n", wifi->get_rssi());
   printf("Welcome to Mbed TCP ping pong\n");
   nsapi_error_t rt = socket.open(wifi);
   if (rt != NSAPI_ERROR_OK) {
       printf("Could not open TCP Socket (%d)\r\n", rt);
       return 1;
   }
   printf("Opened a TCP socket\n");
   socket.set_blocking(false);
   rt = socket.connect(HOST_IP	, HOST_PORT); 
   if (rt != NSAPI_ERROR_OK) {
       printf("Could not connect TCP socket (%d)\r\n", rt);
   }
   Thread socket_thread;
   socket_thread.start(&receiveTCP);
   while (1) {
       char buffer[] = "ping";
       socket.send(buffer, strlen(buffer));
       printf("Sent %d bytes over TCP socket (%s)\n", strlen(buffer), buffer);
       thread_sleep_for(2000);
   }
   thread_sleep_for(osWaitForever);
}

WiFi なので、mbed_app.jsonも直しました。

{
   "macros": ["MBED_SYS_STATS_ENABLED"],
   "config": {
       "wifi-ssid": {
           "help": "WiFi SSID",
           "value": "\"ssid\""
       },
       "wifi-password": {
           "help": "WiFi Password",
           "value": "\"passwd\""
       }
   },
   "target_overrides": {
       "*": {
           "platform.stdio-convert-newlines": true
       }
   }
}

実際の動作は下記のようになります。

WiFi example
Mbed OS version 5.15.6
CPU ID: 0x410fc241
Compiler ID: 2
Compiler Version: 90200
RAM0: Start 0x20000000 Size: 0x30000
RAM1: Start 0x10000000 Size: 0x10000
ROM0: Start 0x8000000 Size: 0x200000
Scan:
Network: aterm-xxxxxx-g secured: WPA/WPA2 BSSID: XX:XX:XX:XX:XX:XX RSSI: -84 Ch: 1
Network: elecom-xxxxxx secured: WPA2 BSSID: XX:XX:XX:XX:XX:XX RSSI: -57 Ch: 1
2 networks available.
Connecting to elecom-xxxxxx...
Success
MAC: XX:XX:XX:XX:XX:XX
IP: 192.168.2.227
Netmask: 255.255.255.0
Gateway: 192.168.2.1
RSSI: -57
Welcome to Mbed TCP ping pong
Opened a TCP socket
Sent 4 bytes over TCP socket (ping)
Received 4 bytes from TCP socket (pong)
socket (poong)veReceiverfromd 4 b TCP ytes fromsocke TCP t (p
o bytes from TCP socket (pong)Receipt (pved 4
Sent 4 bytes over TCP socket (ping)
Received 4 bytes from TCP socket (pong)
Sent 4 bytes over TCP socket (ping)
Received 4 bytes from TCP socket (pong)
o bytes from TCP socket (pong)ecei(et (pved 4
s froom TCP socket (pong)keReceitcketved 4 byte (p
Sent 4 bytes over TCP socket (ping)
Received 4 bytes from TCP socket (pong)

4. wiresharkでパケットを見てみた。

pingのパケット

画像2

pong のパケット

画像3


いいなと思ったら応援しよう!