add script to configure HC-05 bluetooth module
This commit is contained in:
parent
af8aca9218
commit
ad72c18556
|
@ -0,0 +1,49 @@
|
|||
#!/usr/bin/env ruby
|
||||
# encoding: utf-8
|
||||
# ruby: 2.4.1
|
||||
require 'serialport'
|
||||
|
||||
DEBUG = true # show communication
|
||||
|
||||
unless ARGV.length > 0 and File.exist?(ARGV[0]) then
|
||||
puts "this script will configure HC-05 bluetooth modules"
|
||||
puts "provide path to serial port as argument"
|
||||
exit
|
||||
end
|
||||
|
||||
def send(at)
|
||||
puts "< "+at if DEBUG # output request
|
||||
@serial.write(at) # send request
|
||||
@serial.write("\r\n") unless at.end_with?("\r\n")
|
||||
@serial.flush
|
||||
sleep 0.1
|
||||
response = ""
|
||||
until response.end_with?("OK") do
|
||||
activity = IO.select([@serial],nil,nil,3) # wait for response
|
||||
return nil unless activity # no response received
|
||||
response += @serial.read(1) # get response
|
||||
end
|
||||
puts "> "+response if DEBUG # output response
|
||||
return false unless response.end_with?("OK")
|
||||
return response.chomp.gsub(/OK$/, "").chomp
|
||||
end
|
||||
|
||||
@serial = SerialPort.open(ARGV[0],{ baud: 38400, databits: 8, parity: SerialPort::NONE, stop_bit: 1, flow_control: SerialPort::NONE})
|
||||
@serial.baud = 38400 # sometimes the baud rate does not get set
|
||||
@serial.read_timeout = 3000 # don't wait forever for an answer (in ms)
|
||||
|
||||
# commands to send
|
||||
# get some general information
|
||||
commands = ["AT", "AT+VERSION?", "AT+ADDR?", "AT+ROLE?", "AT+UART?", "AT+PSWD?", "AT+BIND?"]
|
||||
#commands << "AT+NAME?" # somehow this does not work
|
||||
commands << "AT+ORGL" # reset of factory settings
|
||||
commands << 'AT+NAME="dachtuer"' # set name
|
||||
commands << "AT+PSWD=1234" # set pin (1234 by default)
|
||||
commands << "AT+UART=115200,0,2" # set usual baud rate (even parity, to be compatible with the bootloader)
|
||||
commands << "AT+RESET" # reset to take effect (goes into non ART command mode)
|
||||
|
||||
# send all commands
|
||||
commands.each do |command|
|
||||
raise "error communicating will HC-05. ensure it is in AT mode by pressing on button while powering up" unless send(command)
|
||||
end
|
||||
@serial.close
|
Loading…
Reference in New Issue