Connect the Raspberry Pi to IBM Watson IoT Platform
Create a Watson IoT device
This section describes how to create an IoT device into Watson IoT and generates associated API keys for future access.
If you do not have yet an Organization created for “Internet Of Things”, you first have to connect to Bluemix and reach the “Internet of Things” services section in the Catalog : https://bluemix.net Select “Internet of Things Platform” and then select the Free pricing plan if it is appropriate for your usage, populate the name and credential fields; press Create.
If you have just created your “IoT” service with the previous step, you should be automatically connected to IBM Watson IoT Platform when selecting “Launch the dashboard”. If you already had one, please connect now to Watson IoT https://internetofthings.ibmcloud.com.
Select your Organisation
Open the “Devices” section
Press “+Add Device” button
Create a “Device Type” (if needed)Create device typeName : Raspberry Pi Device
Description : A Raspberry Pi Device
Press Next and optionally select attributes (Serial number, Manufacturer, … ). If not needed, leave empty.
Press Next to submit information
Declare optional metadata. If not needed, leave empty.
The “Raspberry PiDevice” type should be available into the selectable list
Select “Raspberry PiDevice” type and DO NOT press “Create device type” but press Next at theh bottom right of the page
Populate your “DeviceID” with the Raspberry Pi ID for example as a unique Identifier (aka MAC address). As a naming convention, I prefixe with “SFX_” but you can write whatever you want.
Declare optional metadata. If not needed, leave empty
Security : Press “Next” to auto-generate your authentification token
Save your device information (This is very important to copy & paste these information carefully into place where you can retrieve them later on)Organization ID YOURORGHERE
Device Type Raspberry PiDevice
Device ID SFX_73ED4
Authentication Method token
Authentication Token YOURTOKENHERE
Close the window, your device is ready for IBM side.
Now, an API key needs to be generated for future access:
Select “APPS” from the left side menu
Click on “api keys” tab
Click “+ Generate API keys”
Populate “Comment” : Raspberry Pi access API Keys
Copy&Paste the “API Key” and “Authentification token” for later on
Make the servo motor work!
Download the code here: https://github.com/victordibia/tjwave
Watch this Video: https://youtu.be/x808LHcmlpE
About Servo Motors and Pulse Width Modulation (PWM)
Servo motors are used in remote control vehicles and robotics. Most servo motors are not continuous;
that is, they cannot rotate all the way around but rather just over an angle of about 180 degrees.The position of the servo motor is set by the length of a pulse. The servo expects to receive a pulse at least every 20 milliseconds. If that pulse is high for 1 millisecond, the servo angle will be zero; if it is 1.5 milliseconds, it will be at its center position; and if it is 2 milliseconds, it will be at 180 degrees. Depending on the library you chose to use in generation your pulse width modulation signal, there are various ways to control your servo by setting a duty cycle which determines the pulse frequency.
On a raspberry Pi, PWM is available on a dedicated hardware pin GPIO Pin (GPIO18, Pin 12). Sometimes, when you have multiple devices that need PWM (e.g. LEDs), you can have software PWM made available on any other pin. In this instructable, we use the dedicated hardware PWM pin for our RGB LED, and use software PWM for the servo.
Wiring Your Servo Motor
Your servo motor has three wires – Power, Ground and Data in. In this recipe I use the Tower Pro servo motor and the wires are as follows – Red (Power), Brown (Ground), Yellow (Data in).
For this recipe, a software PWM library is used to control the servo motor, and I wire my setup as follows.
Red (+5v, Pin 2)
Brown (Ground, Pin 14)
Yellow (Data in, Pin 26, GPIO7 )
Note: In the code, you can always change the pins used.
Use Node.red to create a flow
Use node.red to create the flow you would like to open and close the package delivery system which you mount on the drone.
Ours looked like the image below.
Create your IoT flow
This section describes how to create the flow that will drive the integration logic.
Create a flow:
Connect to IBM Bluemix : https://console.ng.bluemix.net
Go to CATALOG on to top menu bar
Select “node-red” to filter the offering
Select “Node-Red Starter”
Populate your Application section and press “Create”
After a while, the Node-Red service should be up and running
View your App using the following Menu and selecting your App URL
Click on “Go To your Node-RED flow Editor”
Notice that your App is open based on your URL (http://Raspberrycdrapp.mybluemix.net)
Now, it is time to build the integration flow:
Drag&drop an “IBM IoT” node and configure as followSelect “API Key” for Authentication
Use the Pen to declare an API Key using the one your created before
Populate API Key and API Token fields
Press Add button
Change the Name to “Raspberry IoT” for example
Complete the flow
Drag&drop a “Debug” node and link it
Click on “Deploy”
In the flow, “Debug” should be ON (green) and “Raspberry IoT” should be connected (green). If there is an issue, check you API Key params.
Click on “Debug” tab on the right side to display future log info
Create a web app on Bluemix!
This can be loaded in any web browser and we used it to open and close the package delivery system mounted on the drone.
You can use Node.ui or Freeboard to create a webpage which allows you to create a button to inderact with the flow.
You can get started by installing node-red-ui, as we did, into your node.red instance on Bluemix by following the tutorial here: https://developer.ibm.com/recipes/tutorials/ui-dashboard-for-iot-device-data-using-node-red/
Once it's installed you simply pull the user interface data from the node.red flow (as shown in the image in our previous step).
Execute the code from the Raspberry Pi
Go Fly with your Package Delivery System!