Skip to content

Instantly share code, notes, and snippets.

@moritzmhmk
Last active February 22, 2017 19:01
Show Gist options
  • Select an option

  • Save moritzmhmk/61acd2996c2b974cb0288119ddaab6b5 to your computer and use it in GitHub Desktop.

Select an option

Save moritzmhmk/61acd2996c2b974cb0288119ddaab6b5 to your computer and use it in GitHub Desktop.
Bluetooth LE Temperature Sensor Setup
hciconfig hci0 up
btmgmt -i hci0 le on
hcitool -i hci0 lescan
gatttool -i hci0 -b XX:XX:XX:XX:XX:XX --characteristics
const noble = require('noble')

const serviceUUIDs = ["1809", "180f"]

noble.on('stateChange', state => {
  if (state === 'poweredOn') {
    console.log('scanning...')
    noble.startScanning(serviceUUIDs, true)
  } else {
    noble.stopScanning()
  }
})

noble.on('discover', peripheral => {
  if (peripheral.id === "84dd20ef106e") {
    noble.stopScanning()
    peripheral.on('connect', () => console.log("connected!"))
    let connect = () => {
      console.log("connecting...")
      peripheral.connect(err => {
        peripheral.discoverServices(serviceUUIDs, (err, services) => {
          services.forEach(service => {
            service.discoverCharacteristics([], (err, characteristics) => {
              characteristics.forEach(characteristic => {
                if (characteristic.uuid === "2a1c") {
                  getTemperature(characteristic)
                }
                if (characteristic.uuid === "fff1") {
                //  console.log(characteristic)
                }
                if (characteristic.uuid === "2a19") {
                  getBatterylevel(characteristic)
                }
              })
            })
          })
        })
      })
    }
    peripheral.on('disconnect', () => {
      console.log("disconnected!")
      setTimeout(connect, 15 * 60 * 1000)
    })
    connect()
  } else {
    console.log('ignoring device with local name: ' + peripheral.advertisement.localName)
  }
})

let getTemperature = (characteristic) => {
  characteristic.subscribe(err => {})
  characteristic.on('data', (data, isNotification) => {
//    console.log(data, isNotification)
    if (data[0] === 0 && data[1] === 1) {
      console.log("done.")
      console.log(`${new Date().toLocaleTimeString()} \t ${data[3] - 40}°C \t ${data[4]}% humidity`)
    }
  })
}
let getBatterylevel = (characteristic) => {}
const noble = require('noble')
const serviceUUIDs = ["1809", "180f"]
let Service, Characteristic
module.exports = function (homebridge) {
Service = homebridge.hap.Service
Characteristic = homebridge.hap.Characteristic
homebridge.registerAccessory('homebridge-lonometer', 'Lonometer', Lonometer)
}
class Lonometer {
constructor (log, config) {
if (config.name === undefined) { return log('Name missing from configuration.') }
noble.on('stateChange', state => {
if (state === 'poweredOn') {
console.log('scanning...')
noble.startScanning(serviceUUIDs, true)
} else {
noble.stopScanning()
}
})
noble.on('discover', peripheral => {
if (peripheral.id === "84dd20ef106e") {
noble.stopScanning()
peripheral.on('connect', () => console.log("connected!"))
let connect = () => {
console.log("connecting...")
peripheral.connect(err => {
peripheral.discoverServices(serviceUUIDs, (err, services) => {
services.forEach(service => {
service.discoverCharacteristics([], (err, characteristics) => {
characteristics.forEach(characteristic => {
if (characteristic.uuid === "2a1c") { getTemperature(characteristic) }
if (characteristic.uuid === "fff1") { }
if (characteristic.uuid === "2a19") { getBatterylevel(characteristic) }
})
})
})
})
})
}
peripheral.on('disconnect', () => {
console.log("disconnected!")
setTimeout(connect, 15 * 60 * 1000)
})
connect()
} else {
console.log('ignoring device with local name: ' + peripheral.advertisement.localName)
}
})
var informationService = new Service.AccessoryInformation()
informationService
.setCharacteristic(Characteristic.Name, 'lonometer')
.setCharacteristic(Characteristic.Manufacturer, 'moritzmhmk')
.setCharacteristic(Characteristic.Model, 'v0.0.1')
.setCharacteristic(Characteristic.SerialNumber, '0000000001')
let temperatureService = new Service.TemperatureSensor(config.name)
// temperatureService.setCharacteristic(Characteristic.StatusActive, false)
let humidityService = new Service.HumiditySensor(config.name)
// temperatureService.setCharacteristic(Characteristic.StatusActive, true)
// temperatureService.setCharacteristic(Characteristic.CurrentTemperature, temp++)
let getTemperature = (characteristic) => {
characteristic.subscribe(err => {})
characteristic.on('data', (data, isNotification) => {
if (data[0] === 0 && data[1] === 1) {
console.log("received data.")
console.log(`${data[3] - 40}°C \t ${data[4]}% humidity`)
temperatureService.setCharacteristic(Characteristic.CurrentTemperature, data[3] - 40)
humidityService.setCharacteristic(Characteristic.CurrentRelativeHumidity, data[4])
}
})
}
let getBatterylevel = (characteristic) => {}
this.services = [ informationService, temperatureService, humidityService ]
}
getServices () {
return this.services
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment