LoginSignup
1
1

ArduinoIDEでM5StackのATOMS3Liteを使う

Last updated at Posted at 2024-03-20

ATOMS3 Lite【M5STACK-C124】

Wifiを使ってTCPとUDPで通信したい

IDEの設定

バージョン:2.3.2
日付:2024-02-20T10:04:35.814Z
CLIバージョン:0.35.3

ボードマネージャ
[ツール][ボード][ボードマネージャ]=[M5Stack]
ボード
[ツール][ボード][M5Stack][M5AtomS3]
ライブラリマネージャ
[ファイル][基本設定][追加のボードマネージャ]=以下のURL
https://m5stack.oss-cn-shenzhen.aliyuncs.com/resource/arduino/package_m5stack_index.json
参考
https://qiita.com/penguinprogrammer/items/774d4b719464e41cdcf3
ライブラリ
[スケッチ][ライブラリをインクルード]=[M5Stack]
以下をビルドするとエラーになるので

#include <M5Stack.h>
static int l_cnt = 0;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}
void loop() {
  // put your main code here, to run repeatedly:
  Serial.printf("%d\n",l_cnt);
  l_cnt++;
  delay(1000);
}
In file included from c:\Users\masumi\Documents\Arduino\libraries\M5Stack\src/M5Display.h:8,
                 from c:\Users\masumi\Documents\Arduino\libraries\M5Stack\src/M5Stack.h:111,
                 from C:\Users\masumi\Documents\Arduino\sketch_mar19a\sketch_mar19a.ino:1:
c:\Users\masumi\Documents\Arduino\libraries\M5Stack\src/utility/In_eSPI.h:633:20: error: 'VSPI' was not declared in this scope
     uint8_t port = VSPI;
                    ^~~~
c:\Users\masumi\Documents\Arduino\libraries\M5Stack\src/utility/In_eSPI.h:633:20: note: suggested alternative: 'SPI'
     uint8_t port = VSPI;
                    ^~~~
                    SPI
「SD.h」に対して複数のライブラリが見つかりました
  使用済:C:\Users\masumi\AppData\Local\Arduino15\packages\m5stack\hardware\esp32\2.1.1\libraries\SD
  未使用:C:\Users\masumi\AppData\Local\Arduino15\libraries\SD
exit status 1

Compilation error: exit status 1

M5CoreS3に変更

「there is a separate library for M5CoreS3」なので
https://github.com/m5stack/M5Stack/issues/316
以下のリポジトリの別ライブラリ「M5CoreS3」を使う
https://github.com/m5stack/M5CoreS3
[スケッチ][ライブラリをインクルード]=[M5CoreS3]

PlatformIOと共通化のためにM5Unifiedに変更

[スケッチ][ライブラリをインクルード]=[提供されたライブラリ/M5Unified]

LEDの点灯

[スケッチ][ライブラリをインクルード]=[提供されたライブラリ/FastLED]
参考
https://winchans.com/archives/603
https://fastled.io/docs/class_c_fast_l_e_d.html

UDP送受信

本家
https://www.arduino.cc/reference/en/libraries/wifi/
参考
https://note.com/khe00716/n/n65a479049078

実装例

#include <M5Unified.h>
#include <WiFi.h>
#include <FastLED.h>

//#define Serial USBSerial

#if 1
static const char *ssid = "RPiAP";
static const char *password = "1234567890123456";
#endif
#if 0
static const char *ssid = "aterm-0356f5-g";
static const char *password = "0eb31244d6e34";
#endif
static WiFiUDP c_udp;
static const int l_txport = 55556;
static const int l_rxport = 55555;

enum {
  NUM_LEDS = 1,
  LED_DATA_PIN = 35,
};
static CRGB leds[NUM_LEDS];

void setup() {
  // put your setup code here, to run once:
#if 1
  M5.begin();
  M5.Power.begin();
#else
  auto cfg = M5.config();
  M5.begin(cfg);
#endif
  Serial.begin(9600);
  Serial.println("hello world");
  FastLED.addLeds<WS2811, LED_DATA_PIN, GRB>(leds, NUM_LEDS);
  leds[0] = CRGB::Green;
  FastLED.setBrightness(0);
  FastLED.show();
  WiFi.disconnect();
  if (true) {
    int n = WiFi.scanNetworks();
    if (n == 0) {
      Serial.println("no networks found");
    } else {
      int i;
      for (i = 0; i < n; i++) {
        Serial.printf("%d:", i + 1);
        Serial.print(WiFi.SSID(i));
        Serial.printf("(%d)", WiFi.RSSI(i));
        Serial.println(WiFi.encryptionType(i) == WIFI_AUTH_OPEN ? " " : "*");
      }
    }
  }
  if (true) {
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);
    delay(1000);
    while (WiFi.status() != WL_CONNECTED) {
      Serial.print(".");
      FastLED.setBrightness(32);
      FastLED.show();
      delay(500);
      FastLED.setBrightness(0);
      FastLED.show();
      delay(500);
    }
    FastLED.setBrightness(32);
    FastLED.show();
    Serial.println("connected");
    Serial.print("IP address:");
    Serial.println(WiFi.localIP());
    c_udp.begin(l_rxport);
  }
  c_udp.begin(l_rxport);
}

void loop() {
  // put your main code here, to run repeatedly:
  const int len = c_udp.parsePacket();
  if (0 < len) {
    char recvbuf[80];
    IPAddress addr = c_udp.remoteIP();
    const uint16_t port = c_udp.remotePort();
    const int rd = c_udp.read((uint8_t *)recvbuf, sizeof(recvbuf));
    if (0 < rd) {
      recvbuf[rd] = '\0';
      char *ptr;
      if ((ptr = strchr(recvbuf, '\r')) != NULL) { *ptr = '\0'; }
      if ((ptr = strchr(recvbuf, '\n')) != NULL) { *ptr = '\0'; }
      Serial.print("udprecv ");
      Serial.print(addr);
      Serial.printf(":%d %d [%s]\r\n", port, rd, recvbuf);
      static int l_cnt = 0;
      char sendbuf[80];
      sprintf(sendbuf, "%d\r\n", l_cnt++);
      const int len = strlen(sendbuf);
      const int bgn = c_udp.beginPacket(addr, l_txport);
      const size_t wr = c_udp.write((const uint8_t *)sendbuf, len);
      const int end = c_udp.endPacket();
      if ((ptr = strchr(sendbuf, '\r')) != NULL) { *ptr = '\0'; }
      if ((ptr = strchr(sendbuf, '\n')) != NULL) { *ptr = '\0'; }
      Serial.print("udpsend ");
      Serial.print(addr);
      Serial.printf(":%d %d [%s]\r\n", l_txport, len, sendbuf);
    }
  }
  static bool l_pressed = false;
  M5.update();
  if (!l_pressed && M5.BtnA.isPressed()) {
    l_pressed = true;
    char sendbuf[80];
    strcpy(sendbuf, "name1\r\n");
    IPAddress server(192, 168, 249, 1);
    const int len = strlen(sendbuf);
    const int bgn = c_udp.beginPacket(server, l_txport);
    const size_t wr = c_udp.write((uint8_t *)sendbuf, len);
    const int end = c_udp.endPacket();
    char *ptr;
    if ((ptr = strchr(sendbuf, '\r')) != NULL) { *ptr = '\0'; }
    if ((ptr = strchr(sendbuf, '\n')) != NULL) { *ptr = '\0'; }
    Serial.print("udpsend ");
    Serial.print(server);
    Serial.printf(":%d %d [%s]\r\n", l_txport, len, sendbuf);
    delay(1000);
  }
}
1
1
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
1
1