Skip to content

Commit

Permalink
Add .gitignore & images folder
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurockkk committed Aug 2, 2017
1 parent d601322 commit e836c34
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 24 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Mac OS X clutter
*.DS_Store
.DS_Store

52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,54 @@
# Bluetooth-RPi-Python
A Python script running on Raspberry Pi 3 to receive commands (data) from / send executed results to an Android Device over bluetooth. Bluetooth-RPi-Python is used with this Android App: [Bluetooth-RPi](https://github.com/Yurockkk/Bluetooth-RPi)

## Instruction

# Step 0 - Install the bluetooth softwares
Open terminal, install softwares:
`sudo apt-get update`
`sudo apt-get install bluez bluez-utils minicom`

# Step 1 - Setup the SPP (serial port profile):
Open terminal, edit this file
`sudo nano /etc/systemd/system/dbus-org.bluez.service`
Add `-C` at the end of the `ExecStart=` line, to start the bluetooth daemon in 'compatibility' mode. Add a new `ExecStartPost=` immediately after that line, to add the SP Profile. The two lines should look like this:
`ExecStart=/usr/lib/bluetooth/bluetoothd -C`
`ExecStartPost=/usr/bin/sdptool add SP`

# Step 2 - Automatically listen to hci0 channel and run this python code:
Create a new service unit file:
`sudo nano /etc/systemd/system/rfcomm.service`

Edit the file:
```
[Unit]
Description=RFCOMM service
After=bluetooth.service
Requires=bluetooth.service
[Service]
ExecStart=/usr/bin/rfcomm watch hci0 &; /usr/bin/python2 /home/pi/DIRECTORY_TO_YOUR_PYTHON_CODE/bluetooth_terminal_Yubo.py
[Install]
WantedBy=multi-user.target
```

**NOTE:** There is only one line of code in **[Service]** section and there is a space between `bin/python2` and /home

Save it, Enable the service unit file:
`sudo systemctl enable rfcomm`

# Step 3 - Add the bash command `hciconfig hci0 piscan` in the file “/etc/rc.local” (before the last line of “exit 0”):

`sudo nano /etc/rc.local`

![](images/rc.png)

# Step 4 - Set the “discoverableTimeout” to “0” in the file “/etc/bluetooth/main.conf”:
`sudo nano /etc/bluetooth/main.conf`

![](images/main.png)

# Step 5 Reboot your RPi. Now, your RPi is able to receive commands (data) from / send executed results to an Android device.

53 changes: 29 additions & 24 deletions bluetooth_terminal_Yubo_wifi2.py → bluetooth-RPi-wifi.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,34 @@ def is_json(self, mJson):
json_object = json.loads(mJson)
if isinstance(json_object, int):
return False
print("type of json_object: " , type(json_object))
print("length of json: ", len(json_object))

if len(json_object) == 0:
return False
except ValueError, e:
return False
return True

def readExecuteSend(self, shell, ble_comm, ble_line):

json_object = json.loads(ble_line)
ip_address = ble_comm.wifi_connect(json_object['SSID'], json_object['PWD'])
if ip_address == "<Not Set>":
#send back fail to configure wifi
callback_message = {'result': "FAIL", 'IP': ip_address}
callback_json = json.dumps(callback_message)
ble_comm.send_serial(callback_json)
return False

else:
#isConnected = True
#send back configure wifi succesfully
callback_message = {'result': "SUCCESS", 'IP': ip_address}
callback_json = json.dumps(callback_message)
ble_comm.send_serial(callback_json)

return True


def wifi_connect(self, ssid, psk):
# write wifi config to file
f = open('wifi.conf', 'w')
Expand Down Expand Up @@ -114,42 +134,27 @@ def main():
shell = ShellWrapper()

ble_comm = None
isConfigured = False
isConnected = False
while True:
try:
ble_comm = SerialComm()
out = ble_comm.read_serial()
out = ble_comm.read_serial()
for ble_line in out:
print(out)
if ble_comm.is_json(ble_line):
print("json!!")
if not isConnected:
print("is not connected yet!!")
json_object = json.loads(ble_line)
ip_address = ble_comm.wifi_connect(json_object['SSID'], json_object['PWD'])
if ip_address == "<Not Set>":
print("Fail to connect to Internet")
#send back fail to configure wifi
callback_message = {'result': "FAIL", 'IP': ip_address}
callback_json = json.dumps(callback_message)
ble_comm.send_serial(callback_json)
else:
isConnected = True
print("connect to Internet! your ip_address: " + ip_address)
#send back configure wifi succesfully
callback_message = {'result': "SUCCESS", 'IP': ip_address}
callback_json = json.dumps(callback_message)
ble_comm.send_serial(callback_json)

print("callback_json: " + callback_json)
isConnected = ble_comm.readExecuteSend(shell, ble_comm, ble_line)
break
else:
ble_comm.send_serial("Wifi has been configured")
break

shell.execute_command(ble_line)
shell_out = shell.get_output()
for l in shell_out:
print(l)
ble_comm.send_serial(l)


except serial.SerialException:
print("waiting for connection")
Expand Down
Binary file added images/main.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/rc.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e836c34

Please sign in to comment.