0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PICマイコンと接続して使用するための予備知識、M5 ATOM Lite bluetooth通信

Last updated at Posted at 2024-07-02

PICマイコンをIOT端末化するための予備知識。

今回は、
ATOM Liteのbluetooth通信の基本的な使い方メモ。

通信相手は、パソコンのUSB端子に差し込んだBluetoothアダプタ。

USARTシリアル通信、115200bps。

M5 ATOM Lite(arduino)のライブラリを使用するので、あっという間に通信できます。
全体図.jpg

開発環境:vsCode + Platform IO
ライブラリ:
FastLED by Daniel Garcia
M5Atom by M5Stack

M5 ATOM Lite
IMG_4860.JPG

パソコン側 Bluetoothアダプター BSBT5D200シリーズ(BUFFALO)
IMG_4858.png

パソコン側 Bluetoothアダプタの送受信
出力.png

ATOMのプログラム書き込み(COM7)
Bluetoothアダプタ(COM3)
※COM番号は、環境依存。

ATOM Lite bluetooth通信コード

PICマイコンとはちがい、ATOMはマルチタスク、リアルタイムOSが搭載されています。
bluetoothのシリアル受信は、メインループとは別タスクを立ち上げて対応しています。
タスクの生成も、短い定型文で動作させることができます。

platformIOの環境設定と使用ライブラリ。
[env:m5stack-atom]
platform = espressif32
board = m5stack-atom
framework = arduino
lib_deps =
fastled/FastLED@^3.7.0
m5stack/M5Atom@^0.1.2
lovyan03/LovyanGFX@^1.1.16
upload_port = COM19*

main.cpp
#include <M5Atom.h>
#include "mySerialBT.hpp"
#include "myTask.hpp"

CRGB color[3]={0x000F00,0x00000F,0x000000};

void setup(void)
{
  M5.begin(true,false,true);
  pinMode(22,OUTPUT); //Lチカ用
  //bluetooth初期化開始
  bluetoothBegin();
  SerialBT.println("Start\n");
  //シリアル通信受信タスクスタート
  setUPserialRxTask();
}

int counter;

void loop(void)
{
  //GP22のLEDと、本体LEDの点滅
  digitalWrite(22,HIGH);
  M5.dis.drawpix(0,color[0]);
  SerialBT.printf("cnt=%d\n",counter++);
  delay(500);

  digitalWrite(22,LOW);
  M5.dis.drawpix(0,color[1]);
  SerialBT.printf("cnt=%d\n",counter++);
  delay(500);
}
mySerialBT.hpp
#ifndef MYSERIALBT_HPP
#define MYSERIALBT_HPP

#include <BluetoothSerial.h>

extern BluetoothSerial SerialBT;    //bluetoothインスタンス
extern void bluetoothBegin(void);  

#endif
mySerialBT.cpp
#include "mySerialBT.cpp"

/// @brief bluetoothインスタンス
BluetoothSerial SerialBT;

/// @brief bluetooth初期化関数
/// @param address 
void bluetoothBegin(void)
{
    SerialBT.begin("M5 ATOM01",false);
    Serial.println("begin connect");
}

myTask.hpp
#ifndef MYTASK_HPP
#define MYTASK_HPP

#include <stdint.h>
#include <M5ATOM.h>

#define INTERVAL_TASK 33

extern void setUPserialRxTask(void);
extern void serialRxTask(void *arg);
#endif
myTask.cpp
#include "myTask.hpp"
#include "mySerialBT.hpp"


/// @brief シリアル通信受信タスク初期化
void setUPserialRxTask()
{
    xTaskCreate(serialRxTask, "serialRxTask", 2048, NULL, 1, NULL);
}

/// @brief シリアル通信受信タスク
/// @param arg 
void serialRxTask(void *arg) 
{
  while (1) 
  {
    if(SerialBT.available()>1)
    {
        String str = SerialBT.readStringUntil('\n');
        uint length = str.length();
        SerialBT.print("echo=> ");
        SerialBT.print(str);
        SerialBT.printf("length=%d\r",length);
        Serial.printf("%s\n",str);
    }
    vTaskDelay(INTERVAL_TASK / portTICK_RATE_MS);
  }
}
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?