以下の内容はhttps://shuzo-kino.hateblo.jp/entry/2025/01/30/212923より取得しました。


M5stack Basicを対向でUDP通信する

実際のところ

HW的前提

  • M5stack Basic
  • Module13.2 LAN モジュール

スクリプト

#include <M5Stack.h>
#include <SPI.h>
#include <Ethernet.h>

// Device type definition
#define PARENT_DEVICE  // Comment out this line for child device

// Ethernet settings
byte mac[] = {
#ifdef PARENT_DEVICE
    0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
#else
    0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEE
#endif
};

#ifdef PARENT_DEVICE
IPAddress ip(192, 168, 1, 177);
IPAddress target(192, 168, 1, 178);
#else
IPAddress ip(192, 168, 1, 178);
IPAddress target(192, 168, 1, 177);
#endif

IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

// UDP settings
EthernetUDP Udp;
unsigned int localPort = 8888;
unsigned int targetPort = 8888;

// SPI settings
// ピン定義(HSPI)
static const int PIN_SCK  = 18;  
static const int PIN_MISO = 19;  
static const int PIN_MOSI = 23;  

#define W5500_CS 5
#define SLAVE_CS 26
#define MASTER_CS 15

// Message buffers
char lastReceivedMessage[25] = "";
char lastSentMessage[25] = "";


// Function declarations
void initializeSPI();
void initializeEthernet();
void handleButtonA();
void handleUDPReceive();
void sendUDPMessage(const char* message);
void displayMessage(const char* type, const char* message, int y);



void setup() {
  Serial.begin(115200);  // シリアル通信の初期化
  while (!Serial) {
    ; // シリアルポートが接続されるのを待つ
  }
  
  M5.begin(false, false, true);  // LCD無効, SD無効, Serial有効
  M5.Power.begin();
  
  initializeSPI();
  initializeEthernet();
  
  Serial.println("System Ready!");
  #ifdef PARENT_DEVICE
    Serial.printf("Master IP: %s\n", Ethernet.localIP().toString().c_str());
  #else
    Serial.printf("Slave IP: %s\n", Ethernet.localIP().toString().c_str());
  #endif
}


void loop() {
  M5.update();
  
  #ifdef PARENT_DEVICE  
    // 親機は送信のみ
    unsigned long currentMillis = millis();
    uint8_t timeValue = (currentMillis / 10) % 256;
    
    Serial.printf("Sent SPI value: %d\n", timeValue);
    // ボタンアクションを実行
    handleButtonA();
  #else
    // 子機は受信のみ
    handleUDPReceive();
  #endif
}

void initializeSPI() {
  SPI.begin(PIN_SCK, PIN_MISO, PIN_MOSI, -1);  // SCK, MISO, MOSI, SS
  pinMode(W5500_CS, OUTPUT);
  digitalWrite(W5500_CS, HIGH);
  #ifdef PARENT_DEVICE
    pinMode(MASTER_CS, OUTPUT); 
    digitalWrite(MASTER_CS, HIGH);
  #else
    pinMode(SLAVE_CS, OUTPUT);
    digitalWrite(SLAVE_CS, HIGH);
  #endif
}

void initializeEthernet() {
  Ethernet.init(W5500_CS);
  Ethernet.begin(mac, ip, gateway, subnet);
  delay(1000);
  Udp.begin(localPort);
}

void handleButtonA() {
  // 時刻付きメッセージを送信
  char timeMessage[25];
  int seconds = millis() / 1000;
  int milliseconds = millis() % 1000;
  snprintf(timeMessage, 25, "Time: %d.%02d sec", seconds, milliseconds / 10);
  sendUDPMessage(timeMessage);
}


// UDP通信関連の関数
void handleUDPReceive() {
  int packetSize = Udp.parsePacket();
  if (packetSize) {
    memset(lastReceivedMessage, 0, 25);
    Udp.read(lastReceivedMessage, 24);
    lastReceivedMessage[packetSize] = '\0';
    
    Serial.printf("Received UDP message: %s\n", lastReceivedMessage);
  }
}

void sendUDPMessage(const char* message) {
  Udp.beginPacket(target, targetPort);
  Udp.write(message);
  Udp.endPacket();
  
  Serial.printf("Sent UDP message: %s\n", message);
}

void displayMessage(const char* type, const char* message, int y) {
  Serial.printf("%s: %s\n", type, message);
}



以上の内容はhttps://shuzo-kino.hateblo.jp/entry/2025/01/30/212923より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14