PICマイコンをIOT端末化するための予備知識。
開発環境:vsCode + Platform IO
ライブラリ:
FastLED by Daniel Garcia
M5Atom by M5Stack
パソコン側 Bluetoothアダプター BSBT5D200シリーズ(BUFFALO)
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);
}
}