This simple tutorial using the Beagle Bone Black - BBB board will show you some principles to integrate your solution with Tago. More than just connect the BBB to the cloud, you will learn how to easily reuse this code into your own application later.
In this example, let’s send the status of a digital input from a BBB board. We will visualize its status in the dashboard. By using the Actions capability, we will configure the system to send out an e-mail whenever the switch changes to the closed state.
The circuit is pretty simple as we are using only one digital input connected to a normally open switch (connector P8, pin 19). A 2.2k Ohm resistor keeps the signal in state low (0) when the switch is closed.
Log in your account, access the Devices section , then click on ‘Add Device’ blue button. The BBB board will be the device to be added, we will give it the name ‘dev01’. Therefore, enter with the name ‘dev01’ and click on ‘Save’.
All devices should use a valid token when accessing Tago. This token is automatically generated when a device is created. Go to the ‘General information’ session of the device, click on ‘QR Code’ or ‘Tokens’ and copy the token to be added into the BBB code later.
Let’s build a simple dashboard to visualize the data sent by your BBB. Click Dashboards on the left side bar, then the +, type the name of your dashboard, and click on Save. Let’s add one widget to show the variable switch status (open/closed). Click on + right top and pick the widget Display.
Start the configuration of this widget by adding the variable to be displayed. Type the variable name that will be sent by the device as ‘switch’. Select your bucket [dev01], your device [dev01], and click on Select as new option then, click save, and your widget will be ready!
Great! As soon as your device starts to send data, the values will be showed on this display.
Now, let’s add an action to send an e-mail notification when the switch state changes to closed. First, add an action to be executed:
Configure the action to send e-mail, enter with the destination e-mail address and the subject. You can enter with a message that will say something like: Hi,
the
switch
on
your
BBB
is
closed!
.
To make sure that you will receive only one notification each time the switch changes status, we will define values to Set and Reset the trigger. It will create a hysteresis function to prevent the system from sending e-mails continuously. Basically, we just need to configure Set Trigger and Reset Trigger as showed below. Let’s Set trigger to send an e-mail when the sensor is closed and Reset triggerwhen it goes back to open. So, if another data with closed status is sent before it goes back to open, it will not send the e-mail.
Your setup at Tago is ready! Now, you just need to code your BBB to send the data.
When communicating with the devices, Tago uses the JSON format. For example, to send the information that the switch is closed, the device just needs to make a POST in HTTP using the data like:
{ "variable": "switch", "value": "closed" }
Yep! That is all! You can add a lot of more information with the variable, like its location, time, and unit. Several fields can be added with more features of our API’s.
The code developed for this example was done in Python. But, you can also code in other languages, such as C, C# or Node.js. Using Debian distribution installed in the BBB, and Python 2.7.9, we wrote and tested the code below. You should have no problem with a different linux distributions or Python versions.
In case you need some background about how to instal and run Python on a BBB, visit these sites from beaglebone.org and adafruit .
Before running the code, you will need to install Tago library for Python. In your BBB, type the following command:
$
sudo
pip
install
-U
tago
Then, create a file .py with the code below. Make sure you replace the token with that one created for your device.
from tago import Tago
import Adafruit_BBIO.GPIO as GPIO
PIN = "P8_19"
GPIO.setup(PIN, GPIO.IN)
LOW = 0
HIGH = 1
Level = GPIO.input(PIN) and HIGH or LOW
MY_DEVICE_TOKEN = '### INSERT YOUR TOKEN HERE ###'
my_device = Tago(MY_DEVICE_TOKEN).device
send_close = {
'variable' : 'switch',
'value' : 'closed'
}
send_open = {
'variable' : 'switch',
'value' : 'open'
}
def send_data(data_to_insert):
response = my_device.insert(data_to_insert)
print data_to_insert
print response
while True:
if Level == LOW:
if GPIO.input(PIN):
send_data(send_close)
Level = HIGH
elif GPIO.input(PIN) == LOW:
send_data(send_open)
Level = LOW
As we know that you will want to apply this in your own application later, here goes some tips for your code:
1. import the Tago lib for Python. Also, we have libs for several languages to simplify your code, check out ours SDKs from
tago
import
Tago
2. replace MY_DEVICE_TOKEN with the token created for your deviceMY_DEVICE_TOKEN
=
###
INSER
THE
TOKEN
FOR
YOUR
DEVICE
HERE
###
3. prepare a JSON with the data to be sentdata_to_insert = { 'variable' : 'switch', 'value' : 'closed' }
4. send your data to Tago result
=
my_device.insert(data_to_insert)
5. read the API response to treat any error and check the success of the request.
Look at your dashboard at Tago, and run the code in your BBB. Note the widget will display the value of the variable in realtime. Wait few seconds for the Python to start the program and press the button on the switch. You should then receive an e-mail from Tago. Release the button, and you will see the status on the display. Press again, and receive another e-mail ;-) If you have any issue or question about this application, access our Community .
Right, we know... you can do much more with the BBB and Tago! But at least, we hope you got the idea about how to set the ecosystem around Tago and your device. Take a look at the concepts , our API’s and SDK’s to bring the full potential of Tago to your system!