Modifying the code
You are now ready to jump into the code. We have provided a working example that can be found at the Edison_Bluetooth repo. Such code contains all modules and functions you'll need to build this project.
First of all, we will uncomment an import line in spp.py
that help us run our main code correctly. Do the following:
root@edison # cd
root@edison # cd Edison_Bluetooth/projects/hospital-assistant/
root@edison # nano spp.py
...
Once opened, go to line and erase the pound sign "#". Save your changes and exit. (Ctrl + X, Y, and [ENTER].
Now let's open the sample code already provided and see how it works:
root@edison cd
root@edison cd Edison_Bluetooth/projects/gardening-system/
root@edison nano example.py
Import section
The first thing you'll find is a block of lines which main purpose is to import all needed python modules.
import pyupm_grove as g # UPM module for Grove sensors/actuators
import pyupm_i2clcd as lcd # UPM module for Grove LCD
from time import sleep # Imports sleep
...
For now, we won't modify these.
Setting up GPIOS
Right beneath the import section, your board's GPIOs setup is done. That is, you'll define the slots where your sensors/actuators will be connected.
NOTE: You must be very careful on the GPIOs setup as the number of the actual slot in the base shield must be the same as the one you define in the code.
light = g.GroveLight(0) # The light sensor is connected to slot A0
display = lcd.Jhd1313m1(0, 0x3E, 0x62) # The LCD can be connected to any 12C slot
relay = g.GroveRelay(4) # The relay module is connected to slot D4
Project functions
Now that you have imported all needed modules and setup the GPIOs, we can jump into the functions that will do your project tasks.
LCD functions
Display light sensor data
def displayLightInfo(): # Name of the function
display.clear() # Clear whatever is being displayed on the LCD
display.setCursor(0,0) # When the cursor is set at a specific position,
# the LCD will start printing info from there
# This case, we are setting it at the top left
# corner
display.write('Light:%s' % str(light.value())) # Print light sensor data
# on LCD
sleep(1) # Delay so data is displayed in a time a human is
# able to read
Display message on LCD
def displayStatus(message): # Name of the function
display.clear() # Clear whatever is being displayed on the LCD
display.setColor(0,0,255) # Set LCD color to blue
display.setCursor(0,2) # Set cursor at bottom left corner
display.write(message) # Write a message on the LCD, where 'message'
# is a variable that will be set when the
# function is called
Change LCD backlight color based on light threshold
def displayLight(): # Name of the function
if light.value() <= 20: # Set a low light threshold
display.setColor(255,0,0) # If the light is below this threshold,
# set the LCD backlight color to RED
elif light.value() in range(21,29): # Set a medium light threshold
display.setColor(255,255,0) # If the light is in range this threshold,
# set the LCD backlight color to YELLOW
elif light.value() >= 30: # Set a high light threshold
display.setColor(0,255,0) # If the light is above this threshold,
# set the LCD backlight color to GREEN
BT functions
Water your plant via BT
def waterPlant(): # Name of the function
displayStatus('Watering') # Display a message when the plant is being watered
# In this case, 'Watering'
relay.off() # Turns the relay off to ensure its starting state
sleep(1) # Wait one second
relay.on() # Turns on the relay so it opens a valve
sleep(5) # Keep the relay turned on for a period of time
# For instance, 5 sec
relay.off() # After the desired time, stop watering, that is
# turn the relay off
Request light sensor data
def requestData(): # Name of the function
return str(light.value()) # When the function is called, it will return the light
# sensor data
BT communication
So you can send commands or receive data, your code must contain these lines at the end of the file. By adding them, you are selecting the function to be executed when a specific command is received. In other words, if you send an 'a' from the BT app, function 'waterPlant' will be executed. This also happens when you send a 'b', but the function to be ran is 'requestData'.
def function (data): # Name of the function
func = functionInUse.get(data) # Selects the function to be used depending
# what the BT sent
func() # Executes the selected function
def dummy(): # If a non-defined character is sent to the system
pass # it won't crash
functionInUse = {'a' : waterPlant, # Declarers which letter correspond
'b' : requestData, } # to each BT controlled function
BT Main Loop
This guide won't cover BT settings, so these lines should remain unchanged.