Monday, 25 September 2017

Bluetooth IoT Solar Battery Voltmeter

A man is like Bluetooth , he's connected to you when you're nearby, but search for others when you're far away.

A woman is like Wi-Fi , she notices all the available ones but connect to the strongest.
But remember, Bluetooth will stick close to you because it's made that way.

The Arduino HC-06 is an amazingly inexpensive Bluetooth module. It may actually be better to buy the HC-05 as it can be set to master or slave. The HC-06 commonly comes in slave configuration (technically the HC-06-S. The master is HC-06-M). It is serial port(UART) to Bluetooth adapter. I bought the HC-06 slave because it has fewer settings to worry about. The master will be my laptop bluetooth module.

Rather inconveniently its TTL level is 3.3V, while my USB RS-485 adapter is 5V TTL. I could have swapped in a 3.3V PIC18LF14K50 for a perfect fit, but it meant junking a perfectly good 5V PIC18F14K50. It is simple enough to use a resistor divider:
Notice that the resistive divider is only necessary for the PIC18F14K50's TX pin. A 5V TTL device will read a 3.3V signal from the HC-06 but the converse will damage the HC-06.

For test purposes I used an unmodified USB to RS485 board. I removed the RS-485 IC, the MAX485E so that it would not interfere with the HC-06. The latter is powered from the 5V line of my USB hub. Connect up 2 lines for 5V and 0V, RX of HC-06 to TX of the PIC18G14K50, and TX of HC-06 to RX of PIC18F14K50 and plug right in:
CH340G USB To TTL Converter

You can buy a USB to TTL serial dongle online for RM12.88.the CH340G USB to TTL adapter . It even has a selector for 5V/3V operation so you don't have to mess about with resistor dividers. 

USB RS485 dongle with Bluetooth HC-06 module. Notice the current is still 10mA.
With Bluetooth active it is 20mA
When powered on successfully, the HC-06 blinks red.

Next we whip out our laptop running Linux (I use Slackware) and invoke hciconfig:
hci0:   Type: BR/EDR  Bus: USB
        BD Address: A4:DB:30:55:9C:AA  ACL MTU: 1022:8  SCO MTU: 183:5
        RX bytes:574 acl:0 sco:0 events:30 errors:0
        TX bytes:368 acl:0 sco:0 commands:30 errors:0

If you get nothing at this stage, you do not have a bluetooth controller, but if you get output like that above you go on to:

root@aspireE1:/home/heong$hciconfig hci up

If you check again your controller should read 'UP':

hci0:   Type: BR/EDR  Bus: USB
        BD Address: A4:DB:30:55:9C:AA  ACL MTU: 1022:8  SCO MTU: 183:5
        UP RUNNING
        RX bytes:1148 acl:0 sco:0 events:60 errors:0
        TX bytes:736 acl:0 sco:0 commands:60 errors:0

Next we use bluetoothctl:

[NEW] Controller C8:FF:28:27:7D:2C BlueZ 5.40 [default]
[NEW] Device 98:D3:32:30:BE:15 HC-06

If you do not see the last line with HC-06, it just means it is your bluetooth module has not been turned on. If your computer does not have bluetooth, a USB bluetooth dongle is very cheap. To turn on your bluetooth, simply do (while still in bluetoothctl). 

Note that some laptops may require to press a button or a keyboard combination (Acer used to have Fn-F3).

[bluetooth]# power on
[CHG] Controller C8:FF:28:27:7D:2C Class: 0x00010c
Changing power on succeeded
[CHG] Controller C8:FF:28:27:7D:2C Powered: yes

If it still does not come up then do:

bluetooth]# scan on
Discovery started
[CHG] Controller C8:FF:28:27:7D:2C Discovering: yes
[CHG] Device 98:D3:32:30:BE:15 LegacyPairing: yes

Next you launch an agent to handle the authentication:

[bluetooth]# agent on
Agent registered
[bluetooth]# default-agent
Default agent request successful

Next you pair your laptop bluetooth module with the HC-06:

[bluetooth]# pair 98:D3:32:30:BE:15
Attempting to pair with 98:D3:32:30:BE:15
[CHG] Device 98:D3:32:30:BE:15 Connected: yes
Request PIN code
[agent] Enter PIN code: 1234
[CHG] Device 98:D3:32:30:BE:15 UUIDs: 00001101-0000-1000-8000-00805f9b34fb
[CHG] Device 98:D3:32:30:BE:15 Paired: yes
Pairing successful

If the pairing has been successful, the HC-06 blinkenlight will be just on, and no longer blinking.

Next I used rfcomm to create a device file /dev/rfcomm0:

rfcomm bind /dev/rfcomm0 98:D3:32:20:BB:7B 1

At this point you are ready for the final test. You need minicom, a serial terminal emulator. Launch 2 instances on 2 consoles (I use KDE's konsole). Aim one minicom at the bluetooth device /dev/rfcomm0 and the other at /dev/ttyUSB0 (usually, but your usage may vary depending on your USB serial dongle). Set the baurate in both minicom to 9600 baud, 8 bits, 1 stop, no parity. Flow control is None.

Type into each minicom console and watch the results appear in the other. Repeat for each console as you need to check both TX and RX.

And there you have it, an HC-06 sending (and receiving) data. In the next post we shall connect the HC-06 to the IoT Solar Battery Voltmeter, so stay tuned.

No comments:

Post a Comment