Raspberry Pi 3 B+

We are going to create a bit more of a advanced BlueDot application using Python 3. If you have never used BlueDot or need help installing it please check out part 1 where I go over installing BlueDot, connecting, and a basic example application. In this example we are going to use the swipe and color functionality to allow us more then 4 choices. I am not 100% sure what they are all going to do yet but I will figure something out.

Python 3 BlueDot Example 2

In this example we will use a combination of swipe, color, and a local text file to make 1 button give us 12 options. The main menu will be a blue color, swiping up will activate mode 1, right will activate mode 2 and so on. On each “sub-menu” you can swipe down to return to the main menu.

from bluedot import BlueDot
from signal import pause

def readMode():
	#Open mode.txt in read mode
	f = open("mode.txt", "r")
	#Read contents of the file
	cont = f.read()
	#Close file
	f.close()
	#Return contents of file
	return cont

def setMode(m):
	#Open mode.txt for writing
	f = open("mode.txt","w+")
	#Write new mode to text file
	f.write(m)
	#Close file
	f.close()
	#Set the button color depending on our new mode
	if m == "mode":
		bd.color = "blue"
	elif m == "one":
		bd.color = "red"
	elif m == "two":
		bd.color = "orange"
	elif m == "three":
		bd.color = "green"
	elif m == "four":
		bd.color = "yellow"

def modeOne(d):
	if d.up:
		print("Mode 1 [Up]")
	elif d.down:
		print("Mode 1 [Down]")
		setMode("mode")
	elif d.right:
		print("Mode 1 [Right]")
	elif d.left:
		print("Mode 1 [Left]")

def modeTwo(d):
	if d.up:
		print("Mode 2 [Up]")
	elif d.down:
		print("Mode 2 [Down]")
		setMode("mode")
	elif d.right:
		print("Mode 2 [Right]")
	elif d.left:
		print("Mode 2 [Left]")

def modeThree(d):
	if d.up:
		print("Mode 3 [Up]")
	elif d.down:
		print("Mode 3 [Down]")
		setMode("mode")
	elif d.right:
		print("Mode 3 [Right]")
	elif d.left:
		print("Mode 3 [Left]")

def modeFour(d):
	if d.up:
		print("Mode 4 [Up]")
	elif d.down:
		print("Mode 4 [Down]")
		setMode("mode")
	elif d.right:
		print("Mode 4 [Right]")
	elif d.left:
		print("Mode 4 [Left]")

def modeMode(d):
	if d.up:
		setMode("one")
		print("Set new mode to [one]")
	elif d.dowm:
		setMode("three")
		print("Set new mode to [three]")
	elif d.left:
		setMode("four")
		print("Set new mode to [four]")
	elif d.right:
		setMode("two")
		print("Set new mode to [two]")

def swiped(d):
	m = readMode()
	if m == "mode":
		modeMode(d)
	elif m == "one":
		modeOne(d)
	elif m == "two":
		modeTwo(d)
	elif m == "three":
		modeThree(d)
	elif m == "four":
		modeFour(d)

setMode("mode")
bd = BlueDot()

bd.when_swiped = swiped

pause()

I am sure there are better ways to code this but as I stated in part 1 I am not a python expert. I know enough to get my hands dirty and write code for my projects. You will notice as you try out the example when you swipe to different menus the colors change. This will show you which menu you are on.

  • Blue = Mode selection menu
  • Red = Mode one menu
  • Orange = Mode two menu
  • Green = Mode three menu
  • Yellow = Mode four menu

We used a few things we have not seen in the previous example. So lets quickly go over what they do. We created 2 functions to read and write the current state of the mode. The setMode function also sets the color of our dot in the application to let us know what menu we are on. Another new function called modeMode this function will call the readMode function and select the proper mode from modeOne, modeTwo, modeThree, and modeFour.

What else can we do?

I know printing out some lines of text will not do much for us. Using python we can make our program do lots of things lets create a few functions to do more useful things.

import os
def restartPi():
	print("Restarting Pi System.")
	os.system("sudo restart 0")

In this example we are using the system function to send the restart command to the system and have it immediately restart the Pi.This could be useful if for example you had put your network card into monitor mode and you want everything back to normal after performing a WiFi attack.

import os
def updatePi():
	print("Updating Pi System.")
	os.system("sudo apt update && sudo apt upgrade -y")

This one is just as simple as the last besides we are using && to put 2 operations together and make our Raspberry Pi run them in order. apt update will check online for any new packages that need to be updated. Apt upgrade will actually download and install the upgraded packages, the -y will make it skip asking if we want to perform the upgrade and just get down to business.

As you can see what you can do is only limited to your imagination and python skills. Thanks for reading part 2! Feel free to drop a comment or subscribe to our newsletter and stay up to date with our latest Raspberry Pi related projects.

A demo of how I use this to make life easier

I will try to keep this example updated as I make changes for my own personal uses.

Main Menu – Blue Button
Swipe Up – System
Swipe Right – WiFi
Swipe Down – Mode 3
Swipe Left – Mode 4

Mode 1 – Red Button – System
Swipe Up – Update Raspberry Pi
Swipe Right – Start SSH Server
Swipe Down – Main Menu
Swipe Left – Reboot Pi

Mode 2 – Orange Button – WiFi
Swipe Up – Nothing at the moment
Swipe Right – Take down wlan0
Swipe Down – Main Menu
Swipe Left – Bring up wlan0

from bluedot import BlueDot
from signal import pause
import os

#Start bluedot class
bd = BlueDot()

####################
# System functions #
####################
#Function to reboot the raspberry pi
def rebootPi():
	print("Rebooting Pi System")
	os.system("sudo reboot 0")

#Function to update the raspbian system
def updatePi():
	print("Updating Pi System")
	os.system("sudo apt update && sudo apt upgrade -y")

#Function to take down wlan0
def takeDownWlan0():
	print("Taking wlan0 down.");
	os.system("sudo ifconfig wlan0 down")

#Function to bring up wlan0
def bringUpWlan0():
	print("Brining wlan0 up.");
	os.system("sudo ifconfig wlan0 up")

#Function to start SSH server
def startSSHServer():
	print("Start SSH server.");
	os.system("sudo systemctl start ssh")


####################
# Border Functions #
####################
def startTask():
	bd.border = True
	bd.color = "white"

def endTask():
	bd.border = False
	#Set the button color depending on our mode
	m = readMode()
	if m == "mode":
		bd.color = "blue"
	elif m == "one":
		bd.color = "red"
	elif m == "two":
		bd.color = "orange"
	elif m == "three":
		bd.color = "green"
	elif m == "four":
		bd.color = "yellow"

###########################
# File mode.txt functions #
###########################
def readMode():
	#Open mode.txt in read mode
	f = open("mode.txt", "r")
	#Read contents of the file
	cont = f.read()
	#Close file
	f.close()
	#Return contents of file
	return cont

def setMode(m):
	#Open mode.txt for writing
	f = open("mode.txt","w+")
	#Write new mode to text file
	f.write(m)
	#Close file
	f.close()
	#Set the button color depending on our new mode
	if m == "mode":
		bd.color = "blue"
	elif m == "one":
		bd.color = "red"
	elif m == "two":
		bd.color = "orange"
	elif m == "three":
		bd.color = "green"
	elif m == "four":
		bd.color = "yellow"

##################
# Mode functions #
##################
# Mode 1: Red [System]
def modeOne(d):
	if d.up:
		startTask()
		print("Mode [System] [Update Pi]")
		updatePi()
		endTask()
	elif d.down:
		startTask()
		print("Mode [System] [Main Menu]")
		setMode("mode")
		endTask()
	elif d.right:
		startTask()
		print("Mode [System] [Start SSH]")
		startSSHServer()
		endTask()
	elif d.left:
		startTask()
		print("Mode [System] [Reboot Pi]")
		rebootPi()
		endTask()

# Mode 2: Orange [WiFi]
def modeTwo(d):
	if d.up:
		startTask()
		print("Mode 2 [Up]")
		endTask()
	elif d.down:
		startTask()
		print("Mode 2 [Down]")
		setMode("mode")
		endTask()
	elif d.right:
		startTask()
		print("Mode [WiFi] [wlan0 Down]")
		takeDownWlan0()
		endTask()
	elif d.left:
		startTask()
		print("Mode [WiFi] [wlan0 Up]")
		bringUpWlan0()
		endTask()

# Mode 3: Green
def modeThree(d):
	if d.up:
		startTask()
		print("Mode 3 [Up]")
		endTask()
	elif d.down:
		startTask()
		print("Mode 3 [Down]")
		endTask()
		setMode("mode")
	elif d.right:
		startTask()
		print("Mode 3 [Right]")
		endTask()
	elif d.left:
		startTask()
		print("Mode 3 [Left]")
		endTask()

# Mode 4: Yellow
def modeFour(d):
	if d.up:
		startTask()
		print("Mode 4 [Up]")
		endTask()
	elif d.down:
		startTask()
		print("Mode 4 [Down]")
		setMode("mode")
		endTask()
	elif d.right:
		startTask()
		print("Mode 4 [Right]")
		endTask()
	elif d.left:
		startTask()
		print("Mode 4 [Left]")
		endTask()

#Mode Selector: Blue
def modeMode(d):
	if d.up:
		startTask()
		setMode("one")
		print("Set new mode to [one]")
		endTask()
	elif d.down:
		startTask()
		setMode("three")
		print("Set new mode to [three]")
		endTask()
	elif d.left:
		startTask()
		setMode("four")
		print("Set new mode to [four]")
		endTask()
	elif d.right:
		startTask()
		setMode("two")
		print("Set new mode to [two]")
		endTask()

#Function to process our swipe commands
def swiped(d):
	m = readMode()
	if m == "mode":
		modeMode(d)
	elif m == "one":
		modeOne(d)
	elif m == "two":
		modeTwo(d)
	elif m == "three":
		modeThree(d)
	elif m == "four":
		modeFour(d)

#Default the mode to the main menu when starting
setMode("mode")

#Assign swiped function to when_swiped
bd.when_swiped = swiped

pause()

If you want your python script to run when your Raspberry Pi starts up you can do the following:

sudo nano /etc/rc.local

Add the following line above exit 0

sudo python3 /home/pi/your_script_name.py &

By Andrew

I love technology, and when I found Raspberry Pi's I was instantly hooked. Within the first week I had at least 5. I am also a avoid programmer so I made this blog about my creations to help others do cool things with their Pi's.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x