Bluetooth HC-05 AT Commands (English)

Bluetooth is one of the simplest communication modules which can be bought at a relatively cheap price. HC-05 Bluetooth Module can be easily found in many online shop websites around fifty thousand rupiahs. Before we further out discussion, I’ll talk about several Bluetooth basic concepts

Introduction

Bluetooth is a wireless technology standard for short-distance communication. Bluetooth uses 2.4 GHz frequency, which is a free-to-use frequency channel, with a low power limitation. Bluetooth standard encompasses quite a lot of protocols, one of which is RFCOMM Protocol. The Bluetooth Module which we usually find normally uses this protocol. RFCOMM Protocol is pretty popular because there is a lot of APIs available using this protocol. In addition, this protocol, commonly called as Emulation Port, can be integrated to many applications using RS-232 as its communication interface

Generic Bluetooth RFCOMM Modules

After knowing several of Bluetooth basic concepts, it’s time to see how Bluetooth works directly. For this demonstration, I’ll try using Bluetooth Module: Tokopedia (Disclaimer: No intention of promoting).

Bluetooth
Picture  1. Bluetooth HC 05 Module

The Bluetooth Module as depicted in Picture 1 is a Module based on Bluetooth Chip BC417. This module has several output pins such as STATE, RX, TX, GND, +5V, and EN. To use this module, it’s enough to connect the RX, TX, GND, and +5V pins.

The 5V pin, or VCC Pin, in this module is actually connected to the LDO Voltage Regulator in this Bluetooth Module Board. I tried to see what kind of Regulator is being used, but it’s pretty hard to find a datasheet for this SMD component. Before I talked about how AT programming commands are, I’ll test the serial port using this Bluetooth Module first

Connect the Bluetooth TX pin with RX Raspberry Pi, RX Bluetooth with TX Raspberry Pi, VCC with +5V Raspberry Pi and GND with GND Raspberry Pi. I’ll turn on the minicom to open the serial port (/dev/ttyS0) using baudrate 9600. The test display result should be as shown in Picture 2

Capture
Picture 2. Serial Port Receive Outcome via Bluetooth

AT Commands Bluetooth

After finished testing the Serial communication, the next thing to try is the AT Commands. Several configurations such as Bluetooth name and PIN and also baud rate could be done through AT Commands. To do that, I made a short script in Python to execute this function. Before trying this script, make sure Raspberry Pi had been installed with Python and PySerial package (python-serial)

# apt-get install python-serial

Before I give the source code from AT Command Configuration, it’s important to know that entering AT commands can be done in three ways. The first is to give 3.3 Volt input to the 34th pin in the Bluetooth Module. This will make the module enter a Full AT Commands mode, where all AT Commands can be accessed and baudrate from the module is 38400. The second method is by pressing a button before the module turned on and make sure the button is still pressed as the configuration process continues (this is effectively similar to the first method), which makes the module enter a Full AT Commands mode. The third method is that after the module is turned on, press a button on the module and keep it pressed when doing the configuration. The module will then enter a Semi AT Command mode where the baudrate is still the normal serial communication baudrate (9600 baud default). If you forgot what baudrate you had set for your serial communication baudrate, it’s better to program your Bluetooth in Full AT Command mode

# attest.py
# Sending AT commands to Bluetooth HC-05

import serial
import time

bluetooth = serial.Serial("/dev/ttyS0", baudrate=9600, timeout=5)

bluetooth.write('AT+NAME=JosefMTD\r\n')
result = bluetooth.read(100)
print result

bluetooth.write('AT+UART=38400,0,0\r\n')
result = bluetooth.read(100)
print result

To use the program above, change “/dev/ttyS0” into “/dev/ttyACM0” if using PL011 Hardware UART from Raspberry Pi. Only use “/dev/ttyS0” if you’re using mini UART. Set baudrate according to the module’s baudrate or put in 38400 if you want to enter Full AT Command mode

To change the name and baudrate, Bluetooth module can be configured in a Semi AT Command mode, which is why my set of processes are to press a button, run attest.py script, and let go of the button when it finished

Capture1.PNG
Picture 3. Output from program attest.py

That’s all from me this time, leave a comment if you have a problem in setting up the Bluetooth

Raspberry Pi Zero Headless Setup (English)

Do you want to try playing around with Raspberry Pi? Maybe you have encountered several problems such as no monitor to configure Raspberry Pi, no keyboard to write the commands, or no LAN cable. Maybe you have the same problem as mine, owning Raspberry Pi Zero W which can only be accessed through Wireless, USB using micro USB, or mini HDMI. If you have Raspberry Pi Zero and want to start playing around with Raspberry, please read this guide to start installing your Raspberry Pi!

IMG_20180401_222102.jpg
Picture 1. Raspberry Pi Zero Wireless

Instalasi Operating System Raspbian Stretch Lite

Several things you need to start are as followed:

  1. Raspberry Pi Zero Wireless (make sure it’s not a Raspberry Pi without WiFi)
  2. Micro SD Card (Class 10 is better so the Read/Write Disk operations are better)
  3. 2A Handphone Charger
  4. Laptop with SD Card Reader (Micro-SD Adapter, if necessary)
  5. Wireless Router or Mobile WiFi Hotspot

The first thing you need to do is to put in your SD Card into your computer, then format it if you have installed Raspbian on that SD Card. You can format it using SD Card Formatter (sdcard.org).

For a comparison, Picture 2 shows the formatting using quick format Windows dan SD Card Formatter. Windows Formatter can only format the F:\ partition in SD Card, while SD Card Formatter can format all of SD Card.

Screenshot 2018-04-01 22.40.45
Picture 2. Formatting Comparison between Windows Formatter and SD Card Formatter

After doing Quick Format using SD Card Formatter, you can add Raspbian OS in SD Card. First thing you need to do is to download ZIP File from Raspbian OS Stretch Lite. Download from the Raspberry Pi official website for Raspbian Stretch Lite, with kernel Linux 4.9. There are two variants of Raspbian Stretch, which are the Desktop and Lite variants. Lite is the version without GUI, while the Desktop one had GUI so it can be connected to a monitor and keyboard and used like a normal computer.

After you have Raspbian Stretch Lite and a formatted SD Card, the next process is to flash Raspbian Stretch Lite on SD Card Formatter. My most favorable method is by using the Etcher program. This program can be downloaded from Etcher.io and it’s easy to use

Screenshot 2018-04-01 23.00.17

Picture 3. Choosing Raspbian Stretch Lite to flash on SD Card using Etcher program

SSH Server dan WiFi Setup

To access Raspberry Pi without monitor or keyboard, you need an SSH Server and a WiFi connection. On the computer you’re using to configure Raspberry Pi, make sure you have an SSH client so you can use PuTTY.

To turn on SSH, you need to create an “ssh” file on the boot partition in SD Card that you flashed using Raspbian OS. Make sure you made the ssh file without any extension (not even .txt). It didn’t need to contain anything, just ensure that there is a file named “ssh”

Screenshot 2018-04-01 23.16.44

Picture 4. Installing SSH Server using SSH file without any extension in the SD Card for Raspbian

After adding the SSH server, the second thing you need to do is to create a “wpa_supplicant.conf” file on the boot partition. The content of this file is as follows:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="NAMA_WIFI_ANDA"
    psk="PASSWORD_ANDA"
    priority=1
}

Use Notepad to write those, and make sure you saved it as “wpa_supplicant.conf”, not in .txt extension. Picture 5 shows the result of creating “wpa_supplicant” file which is placed in the boot partition (F:\). Change NAMA_WIFI_ANDA with the SSID your WiFi is using, and PASSWORD_ANDA according to the WiFi password.

ConfigureWPASupplicant
Picture 5. wpa_supplicant file configuration for the WiFi connection

After installing the Raspbian OS and adding the necessary files according to the instructions above, eject the SD Card and plug it into Raspberry Pi Zero.

Accessing SSH Raspberry Pi from PC

After ensuring that the created files are according to this tutorial, turn on Raspberry Pi Zero for the first time using the SD Card which had been installed with Raspbian OS and configured to be accessed using SSH through WiFi. Check the IP Address of your Raspberry Pi to access it using Raspberry Pi. You can use your Router’s DHCP Table to know which IP address is your Raspberry Pi’s. An example of that is shown in picture 6.

Router
Picture 6. IP Address from Raspberry Pi Zero

After that, you can open PuTTY to access SSH from Raspberry Pi with the IP Address you obtained through your DHCP Router. You can login using

login as: pi
pi@192.168.0.7's password: raspberry

Make sure you follow the instructions above, using “pi” as your login name and “raspberry” for your password. After that, you can access Raspberry Pi Zero through SSH to change your password. You can do that using the command:

$ passwd

You’ll be asked to fill the old password (raspberry) and fill in the new one. After you filled it, make sure the SSH Server from Raspberry Pi is turned on permanently. You can do that by using this command as root (sudo):

# raspi-config

By accessing raspi-config, you’ll be shown the interface below like in Picture 7 on your PuTTY window screen. Direct the pointer to the “Interfacing Options” and press Enter.

RaspiConfig
Picture 7. Raspberry Configuration Tool Interface (Raspi-Config)

After accessing Interfacing Options, choose P2 SSH to access SSH Server configuration. You’ll be asked “Would you like the SSH server to be enabled?” Choose Yes and press Enter. You’ll then see “The SSH server is enabled”. After you’re redirected to the front page, press Finish to exit. You’ll be able to access Raspberry Pi via SSH without using Monitor or Keyboard after this, but ensure that you have WiFi with the same SSID and Password as the configuration you had done

Still have more questions? Please ask in this blog post. Good luck experimenting with your Raspberry Pi.

Install Python 3.6. On Raspbian (English)

Do you want to use Python 3.6 on Raspberry Pi? When you run Python in Raspberry Pi, the version called is usually v2.7. This blog post will discuss how to change the default Python 2.7 into Python 3.6.

Before I begin the tutorial, I want to explain one thing which is commonly seen in Linux tutorials, the ‘$’ and ‘#’ annotations. When writing a command in Linux Terminal’s common documents, both annotations are commonly used as prefixes. The reason for that is in Linux Terminal, when the user isn’t accessing root, will show the ‘$’ annotation. If the Terminal is showing ‘#’, then the user is accessing root. In the document which showed Linux commands, ‘#’ means the command must be accessed as root (or with sudo)

The first thing that can be done is to check if your default Python is on version 3.x or 2.x. You can use this command to make sure of that

$ python --version
PythonVersionStart
Picture 1. Python version prior to upgrading to 3.6.4

There are a few things that must be done to change Python 2.7 into Python 3.6. The first thing is to ensure that several package dependencies must have been installed in Raspberry Pi. To make sure, you can use the command below

# apt-get update
# apt-get install build-essential tk-dev libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev

After executing those programs, you can download the source code for Python 3.6 from Python’s official website. Python 3.6 source code is provided in tar.xz, which means you have to unpack it. But before you download it with wget, make sure your file is in the /tmp/ directory, so it will be deleted after you reboot

$ cd /tmp/
$ wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz
$ tar xf Python-3.6.4.tar.xz
DownloadPythonSource
Picture 2. Result after downloading and extracting the source code from Python 3.6.4

After you finished extracting as in Picture 2, you can see that the source code is at /tmp/Python-3.6.4. To continue with the installation, access Python-3.6.4 directory

$ cd Python-3.6.4

After opening the directory, follow the process below to install Python 3.6.4:

$ ./configure
$ make
# make altinstall

After installing Python 3.6, you’ll see that Python default is still the same as the previous Python (in my case, still 2.7.13). To change the default Python that is being used, you have to use update-alternatives. First, make sure that you know where to install Python 3.6. and that the installed Python 3.6 can be accessed. Use both these commands to know where Python 3.6 is

$ ls /usr/bin/python*
$ ls /usr/local/bin/python*

After knowing the position of executable Python 3.6, do update-alternatives to add Python 2.7.13 and Python 3.6.4 as Python alternatives. Picture 3 shows the output from command to find out where the executable files from Python2.7, Python3.5 dan Python3.6. Add all those executable Python-s as alternatives from Python

FindingPythonExecutable
Picture 3. Python executable locations from root OS Raspbian directory

You will then find out the positions, which are /usr/bin/ for Python2.7 and Python3.5 and /usr/local/bin/ for Python3.6 which is just installed. Include the alternative /usr/bin/python using these commands:

# update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
# update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2
# update-alternatives --install /usr/bin/python python /usr/local/bin/python3.6 3

After adding it as the order above, Python 3.6 will automatically become the default for Python. If you run call python –version, it will show the output like in Picture 4

UpdateAlternativesPython

Picture 4. Automatic version change for Python into Python 3.6.4

If you want to reconfigure your Python back into Python 2.7.13, you can run the command as follows:

# update-alternatives --list python
# update-alternatives --config python

After executing the commands above, you’ll be asked to input the number you want to use as your default Python. Type 1 if you want to use Python 2.7.13 because of one reason or another (for example, the library you’re using still uses Python 2.7). After selecting which version you want, you can check the default version using ‘python –version’ command dan the result will show like in Picture 5.

RevertPythonTo27From36.PNG
Picture 5. Changing back the default version of Python into Python 2.7.13

You can use the configuration commands below to select which version you want to use. You can do this if the newest Python version is available and you want to add it into Raspberry Pi. If you have any questions, don’t hesitate to ask them in this blog. Have fun experimenting!

A Diary of My Electronics Engineering Hobby Begins

Hai semuanya!

Ini adalah percobaan pertama saya untuk membuat sebuah blog khusus untuk tema Teknik Elektronika. Saya seorang Sarjana Teknik Elektro. Saya akan mencoba untuk menuliskan segala percobaan yang saya lakukan untuk belajar lebih tentang Teknik Elektro.

Hey everyone!

This is my first attempt to making a blog specialized for Electronics Engineering. I am an undergraduate in Electronics Engineering. I will be posting all my endeavors to learn more about Electronics Engineering.

Keep It Simple, Stupid — Kelly Johnson