加速度センサの計測結果をBLEで飛ばすっていう試み。XIAO nRF52840と秋月AE-ADXL367を使って。
ではnRF52840でデータを飛ばしてみる。
基本はXIAO nRF5240のexampleのperipheral bleuartをそのまんま使う。で
ADXL367ctrl1.ino
- /*********************************************************************
- This is an example for our nRF52 based Bluefruit LE modules
-
- Pick one up today in the adafruit shop!
-
- Adafruit invests time and resources providing this open source code,
- please support Adafruit and open-source hardware by purchasing
- products from Adafruit!
-
- MIT license, check LICENSE for more information
- All text above, and the splash screen below must be included in
- any redistribution
- *********************************************************************/
- #include <bluefruit.h>
- #include <Adafruit_LittleFS.h>
- #include <InternalFileSystem.h>
-
- #include "ADXL367API.h" //2024.12.28 Sunday Engineer
-
- // BLE Service
- BLEDfu bledfu; // OTA DFU service
- BLEDis bledis; // device information
- BLEUart bleuart; // uart over ble
- BLEBas blebas; // battery
-
- void setup(){
- Serial.begin(115200);
-
- #if CFG_DEBUG
- // Blocking wait for connection when debug mode is enabled via IDE
- while (!Serial)
- yield();
- #endif
-
- Serial.println("Bluefruit52 BLEUART Example");
- Serial.println("---------------------------\n");
-
- // Setup the BLE LED to be enabled on CONNECT
- // Note: This is actually the default behavior, but provided
- // here in case you want to control this LED manually via PIN 19
- Bluefruit.autoConnLed(true);
-
- // Config the peripheral connection with maximum bandwidth
- // more SRAM required by SoftDevice
- // Note: All config***() function must be called before begin()
- Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
-
- Bluefruit.begin();
- Bluefruit.setTxPower(4); // Check bluefruit.h for supported values
- // Bluefruit.setName(getMcuUniqueID()); // useful testing with multiple central connections
- Bluefruit.Periph.setConnectCallback(connect_callback);
- Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
-
- // To be consistent OTA DFU should be added first if it exists
- bledfu.begin();
-
- // Configure and Start Device Information Service
- bledis.setManufacturer("Adafruit Industries");
- bledis.setModel("Bluefruit Feather52");
- bledis.begin();
-
- // Configure and Start BLE Uart Service
- bleuart.begin();
-
- // Start BLE Battery Service
- blebas.begin();
- blebas.write(100);
-
- // Set up and start advertising
- startAdv();
-
- ADXL367setup();//2024.12.28 Sunday Engineer
-
- Serial.println("Please use Adafruit's Bluefruit LE app to connect in UART mode");
- Serial.println("Once connected, enter character(s) that you wish to send");
- }
-
- void startAdv(void){
- // Advertising packet
- Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
- Bluefruit.Advertising.addTxPower();
-
- // Include bleuart 128-bit uuid
- Bluefruit.Advertising.addService(bleuart);
-
- // Secondary Scan Response packet (optional)
- // Since there is no room for 'Name' in Advertising packet
- Bluefruit.ScanResponse.addName();
-
- /* Start Advertising
- * - Enable auto advertising if disconnected
- * - Interval: fast mode = 20 ms, slow mode = 152.5 ms
- * - Timeout for fast mode is 30 seconds
- * - Start(timeout) with timeout = 0 will advertise forever (until connected)
- *
- * For recommended advertising interval
- * https://developer.apple.com/library/content/qa/qa1931/_index.html
- */
- Bluefruit.Advertising.restartOnDisconnect(true);
- Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
- Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
- Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
- }
-
- void loop(){
- // Forward data from HW Serial to BLEUART
- while (Serial.available()){
- // Delay to wait for enough input, since we have a limited transmission buffer
- delay(2);
-
- uint8_t buf[64];
- int count = Serial.readBytes(buf, sizeof(buf));
- bleuart.write(buf, count);
- }
-
- // Forward from BLEUART to HW Serial
- while (bleuart.available()){
- uint8_t ch;
- ch = (uint8_t)bleuart.read();
- Serial.write(ch);
- }
- if(ADXL367task()){bleuart.write(ADXL367sendbuf, 15);}//2024.12.28 Sunday Engineer
- }
- // callback invoked when central connects
- void connect_callback(uint16_t conn_handle){
- // Get the reference to current connection
- BLEConnection *connection = Bluefruit.Connection(conn_handle);
-
- char central_name[32] = {0};
- connection->getPeerName(central_name, sizeof(central_name));
-
- Serial.print("Connected to ");
- Serial.println(central_name);
- }
-
- /**
- * Callback invoked when a connection is dropped
- * @param conn_handle connection where this event happens
- * @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h
- */
- void disconnect_callback(uint16_t conn_handle, uint8_t reason){
- (void)conn_handle;
- (void)reason;
-
- Serial.println();
- Serial.print("Disconnected, reason = 0x");
- Serial.println(reason, HEX);
- }
ADXL367API.h
- #ifndef ADXL367API_H_
- extern char ADXL367sendbuf[16];
- extern void ADXL367setup(void);
- extern uint8_t ADXL367task(void);
- #define ADXL367API_H_
- #endif
ADXL367API.cpp
- #include <SPI.h>
- #define pinMOSI (10)
- #define pinMISO (9)
- #define pinSCK (8)
- #define pinCS (7)
- #define pinDRDY (6)
- #define ADDRESS_INTMAP1_LOWER (0x2A)
- #define ADDRESS_FILTER_CTL (0x2C)
- #define ADDRESS_POWER_CTL (0x2D)
- #define ADDRESS_STATUS (0x0B)
- #define ADDRESS_XDATA_H (0x0E)
- #define ADDRESS_XDATA_L (0x0F)
- #define ADDRESS_YDATA_H (0x10)
- #define ADDRESS_YDATA_L (0x11)
- #define ADDRESS_ZDATA_H (0x12)
- #define ADDRESS_ZDATA_L (0x13)
- #define ADDRESS_SOFT_RESET (0x1F)
- #define VALUE_INTMAP1_LOWER (0x81)
- #define VALUE_FILTER_CTL (0x40)
- #define VALUE_POWER_CTL (0x02)
- #define VALUE_SOFT_RESET (0x52)
- SPISettings mySPISettings=SPISettings(1000000,MSBFIRST,SPI_MODE0);
- void SPI_writeRegister(uint8_t adr,uint8_t data){
- uint8_t txbuf[3];
- uint8_t rxbuf[3];
- txbuf[0]=0x0A;
- txbuf[1]=adr;
- txbuf[2]=data;
- digitalWrite(pinCS,LOW);
- delayMicroseconds(1);
- SPI.transfer(txbuf,rxbuf,3);
- delayMicroseconds(1000);
- digitalWrite(pinCS,HIGH);
- }
- uint8_t SPI_readRegister(uint8_t adr){
- uint8_t txbuf[3];
- uint8_t rxbuf[3];
- txbuf[0]=0x0B;
- txbuf[1]=adr;
- txbuf[2]=0x00;
- digitalWrite(pinCS,LOW);
- delayMicroseconds(1);
- SPI.transfer(txbuf,rxbuf,3);
- delayMicroseconds(1000);
- digitalWrite(pinCS,HIGH);
- return rxbuf[2];
- }
- volatile uint8_t drdy_detected;
- void interruptHandler_pinDRDY(){
- drdy_detected=1;
- }
- void readStatusAndData(uint8_t* buf){
- buf[0]=SPI_readRegister(ADDRESS_STATUS);
- buf[1]=SPI_readRegister(ADDRESS_XDATA_H);
- buf[2]=SPI_readRegister(ADDRESS_XDATA_L);
- buf[3]=SPI_readRegister(ADDRESS_YDATA_H);
- buf[4]=SPI_readRegister(ADDRESS_YDATA_L);
- buf[5]=SPI_readRegister(ADDRESS_ZDATA_H);
- buf[6]=SPI_readRegister(ADDRESS_ZDATA_L);
- }
- const char char_array[]="0123456789ABCDEF";
- void hex2chars(uint8_t src,char* tgt){
- tgt[0]=char_array[(src>>4)&0x0F];
- tgt[1]=char_array[(src>>0)&0x0F];
- }
- void bytes2chars(uint8_t n,char* strbuf,uint8_t* bytedata){
- uint8_t i;
- for(i=0;i<n;i++){
- hex2chars(bytedata[i],&strbuf[i*2]);
- }
- strbuf[n*2]='\n';
- }
-
- uint8_t buf[7];
- char ADXL367sendbuf[16];
-
- void ADXL367setup(){
- uint8_t temp;
- drdy_detected=0;
- pinMode(pinCS,OUTPUT);
- pinMode(pinMOSI,OUTPUT);
- pinMode(pinSCK,OUTPUT);
- pinMode(pinMISO,INPUT);
- digitalWrite(pinCS,HIGH);
- digitalWrite(pinMOSI,LOW);
- digitalWrite(pinSCK,LOW);
- pinMode(pinDRDY,INPUT_PULLUP);
- SPI.begin();
- SPI.beginTransaction(mySPISettings);
- SPI_writeRegister(ADDRESS_SOFT_RESET,VALUE_SOFT_RESET);
- temp=SPI_readRegister(ADDRESS_INTMAP1_LOWER);
- while(temp!=VALUE_INTMAP1_LOWER){
- SPI_writeRegister(ADDRESS_INTMAP1_LOWER,VALUE_INTMAP1_LOWER);
- temp=SPI_readRegister(ADDRESS_INTMAP1_LOWER);
- }
- temp=SPI_readRegister(ADDRESS_FILTER_CTL);
- while(temp!=VALUE_FILTER_CTL){
- SPI_writeRegister(ADDRESS_FILTER_CTL,VALUE_FILTER_CTL);
- temp=SPI_readRegister(ADDRESS_FILTER_CTL);
- }
- temp=SPI_readRegister(ADDRESS_POWER_CTL);
- while(temp!=VALUE_POWER_CTL){
- SPI_writeRegister(ADDRESS_POWER_CTL,VALUE_POWER_CTL);
- temp=SPI_readRegister(ADDRESS_POWER_CTL);
- }
-
- delay(100);
- readStatusAndData(buf);
- attachInterrupt(digitalPinToInterrupt(pinDRDY),interruptHandler_pinDRDY,FALLING);
- }
-
- uint8_t ADXL367task(){
- uint8_t ret=0;
- if(drdy_detected){
- drdy_detected=0;
- readStatusAndData(buf);
- bytes2chars(7,ADXL367sendbuf,buf);
- ret=1;
- }else{
- ret=0;
- }
- return ret;
- }
で、スマホでSerial Bluetooth Terminalってアプリで、見ることができるわけだが、パソコンで見たいってときは、こちら(https://coskxlabsite.stars.ne.jp/html/for_students/iot/XIAOESP32S3/ESP32S3BLEServer_WinPC.html)がイケてる。ページ下の方のBLE_serial_terminalをダウンロードする。windows ble serial terminalとかで検索すると、BLEをCOMに割り当てる方法とか書いてるんだけど、どうもXIAO nRF5240のexampleのperipheral bleuartはその使い方は想定してないらしくて、すぐ切断されるので、COMへの割り当てすらできない。で、上記アプリでやってみると、
すごくいい。ちゃんとBLEでデータが飛んでるってのが簡単にわかるので助かるー。2024年もあと数日、感謝の気持ちとともに、できるだけ、がんばる。、、、できるだけよ。