//peripheral_DHT11BLE_ex3 #include #include #include //I2C用ヘッダーファイル #include // R4対応I2C LCD // LCD LiquidCrystal_PCF8574 lcd(0x27); // (0x3F の場合もあり) // DHT11 #define DHTPIN 11 #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); #define LED_PIN 3 // ← セントラルから制御されるLED #define SW_PIN 2 // BLE UUID #define DHT11_SERVICE_UUID "01234567-0123-0123-0123-0123456789a0" //16byte(128bit) #define DHT11_TEMP_UUID "01234567-0123-0123-0123-0123456789a1" #define DHT11_HUM_UUID "01234567-0123-0123-0123-0123456789a2" #define LED_UUID "01234567-0123-0123-0123-0123456789a3" #define SW_UUID "01234567-0123-0123-0123-0123456789a4" #define BLE_LOCAL_NAME "DHT11_float" BLEService DHT11_Service(DHT11_SERVICE_UUID); BLEFloatCharacteristic Temperature_Char(DHT11_TEMP_UUID, BLERead | BLENotify); BLEFloatCharacteristic Humidity_Char(DHT11_HUM_UUID, BLERead | BLENotify); BLEByteCharacteristic Led_Char(LED_UUID, BLEWrite | BLERead); BLEByteCharacteristic Sw_Char(SW_UUID, BLERead | BLENotify); int previousSw = 0;//PreviousSw 追加 void setup() { pinMode(SW_PIN, INPUT_PULLUP); Serial.begin(9600); while (!Serial); pinMode(LED_PIN, OUTPUT); digitalWrite(LED_PIN, LOW); // LCD Wire.begin(); lcd.begin(16, 2); lcd.setBacklight(255); lcd.print("Peripheral Ready"); // DHT init dht.begin(); // BLE init if (!BLE.begin()) { Serial.println("BLE start fail"); while (1); } BLE.setLocalName(BLE_LOCAL_NAME); BLE.setAdvertisedService(DHT11_Service); DHT11_Service.addCharacteristic(Temperature_Char); DHT11_Service.addCharacteristic(Humidity_Char); DHT11_Service.addCharacteristic(Led_Char);//追加 これを忘れていた DHT11_Service.addCharacteristic(Sw_Char);//追加 BLE.addService(DHT11_Service); Temperature_Char.writeValue(0.0f);//float初期値を入れる Humidity_Char.writeValue(0.0f); Led_Char.writeValue((uint8_t)0);//追加 Sw_Char.writeValue((uint8_t)0);//追加 BLE.advertise(); Serial.println("DHT11 Peripheral Started"); Serial.println("Waiting for Central..."); } int cnt=1; void loop() { BLEDevice central = BLE.central(); if (central) { Serial.print("Connected: "); Serial.println(central.address());//MACアドレス 各Bluetooth機器に固有の値 while (central.connected()) { float h = dht.readHumidity(); float t = dht.readTemperature(); if (!isnan(t) && !isnan(h)) {// t と h が正常な値のときだけ送信する // BLEへ書き込む(Notify & Read用) Temperature_Char.writeValue(t); Humidity_Char.writeValue(h); // Serial Serial.print("Temp: "); Serial.print(t, 1); Serial.print(" ℃ "); Serial.print("Humid: "); Serial.print(h, 1); Serial.println(" %"); // LCD lcd.setCursor(0, 0); lcd.print(" "); lcd.setCursor(0, 0); lcd.print("Temp="); lcd.print(t, 1); lcd.print((char)0xDF);//° lcd.print("C "); lcd.print(cnt); lcd.setCursor(0, 1); lcd.print(" "); lcd.setCursor(0, 1); lcd.print("Humid="); lcd.print(h, 1); lcd.print("% "); } else { Serial.println("DHT read error"); } // if the remote device wrote to the characteristic, // use the value to control the LED: if (Led_Char.written()) { if (Led_Char.value()) { // any value other than 0 Serial.println("LED on"); digitalWrite(LED_PIN, HIGH); // will turn the LED on } else { // a 0 value Serial.println(F("LED off")); digitalWrite(LED_PIN, LOW); // will turn the LED off } } // ---- Peripheral Sw → Central LED ---- int nowSw = digitalRead(SW_PIN); if (nowSw != previousSw) { previousSw = nowSw; if (nowSw) { Serial.println("button pressed"); // button is pressed, write 0x01 to turn the LED on Sw_Char.writeValue((byte)0x01); Sw_Char.notify(); // ← これを追加! } else { Serial.println("button released"); // button is released, write 0x00 to turn the LED off Sw_Char.writeValue((byte)0x00); Sw_Char.notify(); // ← これを追加! } } cnt++; //delay(1000); // 1秒に1回更新(DHT11の仕様) delay(100); // 0.1秒に1回更新(DHT11の仕様) } Serial.println("Disconnected"); } }