From ad72c185569a7c46703e6a33300062d5c4c69e94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Fri, 6 Mar 2020 11:15:00 +0100 Subject: [PATCH] add script to configure HC-05 bluetooth module --- hc-05_porg.rb | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100755 hc-05_porg.rb diff --git a/hc-05_porg.rb b/hc-05_porg.rb new file mode 100755 index 0000000..c1330f1 --- /dev/null +++ b/hc-05_porg.rb @@ -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