|
| 1 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | +// |
| 3 | +// BleBeacon — advertise the board name and a live temperature reading. |
| 4 | +// |
| 5 | +// The HTS221 temperature is encoded as int16 x100 in the manufacturer- |
| 6 | +// specific data field (company ID 0x0059). The beacon is visible from |
| 7 | +// any BLE scanner app (nRF Connect, LightBlue, etc.). |
| 8 | +// |
| 9 | +// Wiring: no external hookup needed. The HTS221 sits on the STeaMi |
| 10 | +// internal I2C bus. Flash and open the serial monitor at 115200 baud. |
| 11 | + |
| 12 | +#include <Arduino.h> |
| 13 | +#include <HTS221.h> |
| 14 | +#include <STM32duinoBLE.h> |
| 15 | +#include <Wire.h> |
| 16 | + |
| 17 | +TwoWire internalI2C(I2C_INT_SDA, I2C_INT_SCL); |
| 18 | +HTS221 sensor(internalI2C); |
| 19 | + |
| 20 | +void setup() { |
| 21 | + Serial.begin(115200); |
| 22 | + while (!Serial && millis() < 2000) |
| 23 | + ; |
| 24 | + |
| 25 | + // Init sensor |
| 26 | + internalI2C.begin(); |
| 27 | + if (!sensor.begin()) { |
| 28 | + Serial.println("HTS221 not detected — check wiring."); |
| 29 | + while (true) |
| 30 | + ; |
| 31 | + } |
| 32 | + sensor.setContinuous(HTS221_ODR_1_HZ); |
| 33 | + Serial.println("HTS221 ready."); |
| 34 | + |
| 35 | + // Init BLE |
| 36 | + if (!BLE.begin()) { |
| 37 | + Serial.println("BLE init failed!"); |
| 38 | + while (true) |
| 39 | + ; |
| 40 | + } |
| 41 | + |
| 42 | + BLE.setLocalName("STeaMi-Beacon"); |
| 43 | + BLE.setDeviceName("STeaMi-Beacon"); |
| 44 | + BLE.setAdvertisingInterval(160); // 100 ms |
| 45 | + BLE.setConnectable(false); |
| 46 | + |
| 47 | + Serial.println("BLE Beacon ready."); |
| 48 | +} |
| 49 | + |
| 50 | +void loop() { |
| 51 | + // Read temperature and encode as int16 x100 |
| 52 | + float temp = sensor.temperature(); |
| 53 | + int16_t tempRaw = (int16_t)(temp * 100); |
| 54 | + uint8_t data[2] = { |
| 55 | + (uint8_t)(tempRaw >> 8), // MSB |
| 56 | + (uint8_t)(tempRaw & 0xFF), // LSB |
| 57 | + }; |
| 58 | + |
| 59 | + // Update manufacturer data and re-advertise |
| 60 | + BLE.stopAdvertise(); |
| 61 | + BLE.setManufacturerData(0x0059, data, sizeof(data)); |
| 62 | + BLE.advertise(); |
| 63 | + |
| 64 | + Serial.print("Advertising — T = "); |
| 65 | + Serial.print(temp, 2); |
| 66 | + Serial.println(" C"); |
| 67 | + |
| 68 | + BLE.poll(); |
| 69 | + delay(1000); |
| 70 | +} |
0 commit comments