     ledcWriteNote(0,NOTE_D,8);              // 送信中の音
     delay(200);                             // 送信待ち時間
     ledcWrite(0, 0);
-    pAdvertising->stop();
     esp_deep_sleep(SLEEP_P);                // Deep Sleepモードへ移行
 }
-
-void setBleAdvData(byte *data, int data_n){
-    BLEAdvertisementData oAdvertisementData = BLEAdvertisementData();
-    BLEAdvertisementData oScanResponseData = BLEAdvertisementData();
-    oAdvertisementData.setName(BLE_DEVICE);
-    oAdvertisementData.setFlags(0x06);      // LE General Discoverable Mode | BR_EDR_NOT_SUPPORTED
-    
-    std::string strServiceData = "";
-    strServiceData += (char)(data_n+3);     // Len
-    strServiceData += (char)0xFF;           // Manufacturer specific data
-    strServiceData += (char)0x01;           // Company Identifier(2 Octet)
-    strServiceData += (char)0x00;
-    for(int i=0;i<data_n;i++) strServiceData += (char)(data[i]);
-
-    oAdvertisementData.addData(strServiceData);
-    pAdvertising->setAdvertisementData(oAdvertisementData);
-    pAdvertising->setScanResponseData(oScanResponseData);
-
-    Serial.print("data            = ");
-    int len=strServiceData.size();
-    if(len != (int)(strServiceData[0]) + 1 || len < 2) Serial.println("ERROR: BLE length");
-    for(int i=2;i<len;i++) Serial.printf("%02x ",(char)(strServiceData[i]));
-    Serial.println();
-    Serial.print("data length     = ");
-    Serial.println(len - 2);
-}

    if(wake<3) morseIp0(PIN_BUZZER,50,IP);  // IPアドレス終値をモールス信号出力
}

int d_append(byte *array,int i, byte d){
    array[i]=d;
    return i+1;
}

int d_append_int16(byte *array,int i, int16_t d){
    array[i] = (byte)(d & 0xFF); 
    array[i+1] = (byte)(d >> 8);
    return i+2;
}

int d_append_int24(byte *array,int i, int32_t d){
    array[i] = (byte)(d & 0xFF); 
    array[i+1] = (byte)((d >>8)&0xFF);
    array[i+2] = (byte)((d >>16)&0xFF);
    return i+3;
}

void sensors_log(float *val){
    Serial.print("Temperature    =  ");
    Serial.print(val[0]);
    Serial.println(" [degrees Celsius]");
    Serial.print("Pressure        = ");
    Serial.print(val[1]);
    Serial.println(" [hPa]");
    Serial.write("Accelerometer X = ");
    Serial.print(val[2]);
    Serial.println(" [g]");
    Serial.write("Accelerometer Y = ");
    Serial.print(val[3]);
    Serial.println(" [g]");
    Serial.write("Accelerometer Z = ");
    Serial.print(val[4]);
    Serial.println(" [g]");
    Serial.print("Geomagnetic X   = ");
    Serial.print(val[5], 3);
    Serial.println("[uT]");
    Serial.print("Geomagnetic Y   = ");
    Serial.print(val[6], 3);
    Serial.println("[uT]");
    Serial.print("Geomagnetic Z   = ");
    Serial.print(val[7], 3);
    Serial.println("[uT]");
}

void loop() {
    WiFiUDP udp;                            // UDP通信用のインスタンスを定義
    byte d[DAT_N]; 
    float val[VAL_N];                       // センサ用の浮動小数点数型変数
    long v;
    int l=0;

    bm1383aglv.get_val(&val[1],val);        // 気圧と温度を取得
    v=(long)((val[0] + 45.) * 374.5);       // 温度
    l=d_append_int16(d,l,v);
    v=(long)(val[1] * 2048);
    l=d_append_int24(d,l,v);                // 気圧
    
    l=d_append(d,l,SEQ_N);                  // 送信番号
    
    kx224.get_val(&val[2]);                 // 加速度を取得
    for(int i=0;i<3;i++){
        v=(long)(val[2+i] * 4096);
        l=d_append_int16(d,l,v);
    }
    
    bm1422agmv.get_val(&val[5]);            // 地磁気を取得
    for(int i=0;i<3;i++){
        v=(long)(val[5+i] * 10);
        l=d_append_int16(d,l,v);
    }
    
    sensors_log(val);
    
    // UDP
    if(wifi_enable){
        udp.beginPacket(IP_BC, PORT);       // UDP送信先を設定
        udp.print(DEVICE);                  // デバイス名を送信
        for(int i=0; i<VAL_N; i++){
            udp.print(val[i],0);            // 変数tempの値を送信
            if(i < VAL_N-1)udp.print(", "); // 「,」カンマを送信
        }
        udp.endPacket();                    // UDP送信の終了(実際に送信する)
    }
    // BLE Advertizing
    pAdvertising = BLEDevice::getAdvertising();
    setBleAdvData(d,l);
    pAdvertising->start();                  // Start advertising
    SEQ_N++;
    sleep();
}


#define PORT 1024                           // 送信のポート番号
#define SLEEP_P 50*1000000ul                // スリープ時間 50秒(uint32_t)
#define DEVICE "rohme_1,"                   // デバイス名(5文字+"_"+番号+",")

#include <Wire.h>
#include "BM1383AGLV.h"
