Skip to content

Instantly share code, notes, and snippets.

@signaleleven
Created January 21, 2018 15:40
Show Gist options
  • Save signaleleven/ffec6d96a6d46a0eaec51e4f99f122f1 to your computer and use it in GitHub Desktop.
Save signaleleven/ffec6d96a6d46a0eaec51e4f99f122f1 to your computer and use it in GitHub Desktop.
Comet blue expect
#!/usr/bin/expect -f
# author: Torsten Tränkner
# version: 0.1
# license: GPLv3
log_user 0
if { $argc < 2 } {
puts "Please add a bluetooth address as first parameter, PIN as second parameter and new temperature as third parameter."
puts "For example:"
puts "$argv0 E0:E5:CF:C1:D4:3F 40e20100"
puts "$argv0 E0:E5:CF:C1:D4:3F 40e20100 20.5"
exit 1
}
set bluetooth_address [lindex $argv 0]
set PIN [lindex $argv 1]
set temperature_string ""
if { $argc > 2 } {
set normal_temperature [lindex $argv 2]
set temperature [expr { int($normal_temperature * 2) }]
set temperature_string 80[string repeat [format "%02x" $temperature] 3]008080
}
# wait some seconds for a response
set timeout 15
set counter 0
while {1} {
exec hciconfig hci0 up
# start gatttool
spawn -noecho gatttool -I -b $bluetooth_address
expect "LE"
send "connect\n"
expect {
"Connection successful" {
break
}
timeout {
send "exit\n"
expect " "
exec hciconfig hci0 down
incr counter
if {$counter > 3} {
exit 2
}
}
}
}
# set a shorter timeout now
set timeout 5
send "char-write-req 0x0048 $PIN\n"
expect {
"Characteristic value was written successfully" {
}
"Characteristic Write Request failed:" {
send "disconnect\n"
send "exit\n"
expect " "
exec hciconfig hci0 down
exit 3
}
}
send "char-read-hnd 0x003d\n"
expect -re "Characteristic value/descriptor.*"
set temperature_value $expect_out(buffer)
set startIndex [string first descriptor: $temperature_value]
set current_temperature_string 0x[string range $temperature_value [expr {$startIndex + 12}] [expr {$startIndex + 13}]]
set old_temperature_string 0x[string range $temperature_value [expr {$startIndex + 15}] [expr {$startIndex + 16}]]
set current_temperature [expr {$current_temperature_string / 2.0}]
set old_temperature [expr {$old_temperature_string / 2.0}]
if {$argc == 2 } {
puts "$current_temperature"
} else {
puts "[lindex $argv 2]"
}
if { $temperature_string ne "" } {
send "char-write-req 0x003d $temperature_string\n"
expect "Characteristic value was written successfully" {}
}
send "disconnect\n"
expect "LE"
send "exit\n"
expect " "
exec hciconfig hci0 down
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment