Skip to content

Instantly share code, notes, and snippets.

@GOROman
Last active January 18, 2025 07:20
Show Gist options
  • Save GOROman/5839164cc89938536ec6400a3d510ffe to your computer and use it in GitHub Desktop.
Save GOROman/5839164cc89938536ec6400a3d510ffe to your computer and use it in GitHub Desktop.
M5 Module LLM で、Wifiに繋いでネットワーク経由で指示を与える例
/*
* SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
*
* SPDX-License-Identifier: MIT
*/
#include <Arduino.h>
#include <M5Unified.h>
#include <M5ModuleLLM.h>
#include <WiFi.h>
// ネットワーク経由でLLMに指示を出すサンプル
// > nc 192.168.0.100 5656
// みたいな感じで接続(telnetは日本語が化けるのでncを使う)
// WiFi設定
const char *WIFI_SSID = "<<<<<YOUR SSID>>>>>";
const char *WIFI_PASS = "<<<YOUR PASSWORD>>>";
// 接続するポート
const int PORT = 5656;
#define CommSerialPort Serial
M5ModuleLLM module_llm;
String llm_work_id;
String received_question;
WiFiServer server(PORT);
// クライアント変数の正しい宣言方法
WiFiClient client; // 参照ではなく通常のオブジェクトとして宣言
void setup()
{
M5.begin();
M5.Display.setTextSize(1);
M5.Display.setTextScroll(true);
M5.Display.setFont(&fonts::efontJA_16); // 日本語フォント
// M5.Display.setFont(&fonts::efontCN_12); // Support Chinese display
/* Init usb serial */
CommSerialPort.begin(115200);
/* Init module serial port */
Serial2.begin(115200, SERIAL_8N1,
M5.getPin(m5::pin_name_t::port_c_rxd),
M5.getPin(m5::pin_name_t::port_c_txd)); // Auto detection
/* Init module */
module_llm.begin(&Serial2);
// Conncet to WiFi
WiFi.begin(WIFI_SSID, WIFI_PASS);
M5.Display.print(">> Connecting to WiFi..");
while (WiFi.status() != WL_CONNECTED)
{
delay(300);
M5.Display.print('.');
}
M5.Display.println("OK!");
/* Make sure module is connected */
M5.Display.print(">> Check ModuleLLM connection.");
while (1)
{
delay(300);
M5.Display.print('.');
if (module_llm.checkConnection())
{
M5.Display.println("OK!");
break;
}
}
/* Reset ModuleLLM */
M5.Display.printf(">> Reset ModuleLLM..\n");
module_llm.sys.reset();
/* Setup LLM module and save returned work id */
M5.Display.printf(">> Setup llm..\n");
m5_module_llm::ApiLlmSetupConfig_t llm_config;
// llm_config.model = "qwen2.5-0.5b";
llm_config.max_token_len = 1023;
llm_work_id = module_llm.llm.setup(llm_config);
M5.Display.printf(">> LLM Work ID:\"%s\"\n", llm_work_id.c_str());
M5.Display.printf("\nTry nc from your terminal.\n> nc %s %d\n", WiFi.localIP().toString(), PORT);
server.begin();
M5.Display.setTextSize(1);
M5.Display.println("Waiting for connection");
}
void LLM_Process()
{
// 入力待ち
if (client.available())
{
received_question = client.readStringUntil('\r');
client.printf("[You]:%s\n", received_question.c_str());
client.flush();
delay(1);
M5.Display.setTextSize(2);
M5.Display.setTextColor(TFT_WHITE);
M5.Display.printf("[You]:%s", received_question.c_str());
M5.Display.setTextColor(TFT_YELLOW);
client.print("[AI]:");
M5.Display.print("[AI]:");
/* Push question to LLM module and wait inference result */
module_llm.llm.inferenceAndWaitResult(llm_work_id, received_question.c_str(), [](String &result)
{
/* Show result on screen and usb serial */
if (result.length() > 0)
{
client.printf("%s", result.c_str());
client.flush();
M5.Display.printf("%s", result.c_str());
} });
/* Clear for next question */
received_question.clear();
client.print("\n");
M5.Display.println();
}
}
void loop()
{
client = server.available();
if (client)
{
M5.Display.setTextSize(1);
M5.Display.println(">> Connected!");
// 接続されている間は処理
while (client.connected())
{
LLM_Process();
delay(20);
}
M5.Display.setTextSize(1);
M5.Display.print("Disconnected\n");
}
}
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:m5stack-cores3]
platform = espressif32
board = m5stack-cores3
framework = arduino
monitor_speed = 115200
lib_deps =
m5stack/M5Unified
https://github.com/m5stack/M5Module-LLM#dev
; M5Module-LLM は devブランチ の方を使うと良い
@GOROman
Copy link
Author

GOROman commented Jan 18, 2025

使用例

> nc 192.168.0.203 3232

hey
[You]:hey

[AI]:Hello! How can I assist you today?

おっす
[You]:おっす

[AI]:おっす!何かお手伝いできることがありますか?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment