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!