In part 1 of the tutorial, we created an IoT Platform starter project which created an app and two services. Post that, we moved on to create a device type and added Raspberry Pi to it as a gateway.
Here, we will see how to communicate with a device connected to Raspberry Pi from IBM Watson IoT platform. For this project, we will use Node-RED for communication with the assistance of IBM specific nodes.
Steps to be followed is as given below:
Write a code and upload in Arduino Nano that performs the following functions:
Code for Arduino Nano:
const int LEDPIN = 13; void setup() { Serial.begin(9600); pinMode(LEDPIN,OUTPUT); // LED actuator delay(1000); } void loop() { if (Serial.available()) { int value = Serial.parseInt(); blink(value); } Serial.println(analogRead(A0)); delay(2000); } void blink(int n){ for (int i = 0; i < n; i++) { digitalWrite(LEDPIN, HIGH); delay(1000); digitalWrite(LEDPIN, LOW); delay(1000); } }
Power up your Raspberry Pi.
Connect the Arduino Nano to one of the USB ports of Raspberry Pi.
Open node-red in Raspberry Pi.
Create a Node-red flow as given below or import the node-red flow using the link given below:
Node-RED flow:
In the Node-RED flow, we will perform the following functions:
Deploy the flow and we can observe a blue dot beside the Raspi-01 (Device ID) in the IBM Watson IoT depicting that the gateway is online and connected.
Click on the Raspi-01 and view the recent events.
Go to Dashboard and select RaspberryGatewayNanoDevice Application.
Click on ‘Visit App URL’
Setup Node-RED in IBM Watson IoT.
Create a Node-red flow as given below or import the node-red flow using the link given below:
Node-RED flow:
In the Node-RED flow, we will perform the following functions:
Deploy the flow and observe the functions that were tested.
Device Events: The LDR values from the sensor connected to Arduino Nano were obtained by the Raspberry Pi (which is the gateway) and partial processing was done in the gateway which converts LDR values to lux and filter the values with a threshold. The filtered value is obtained in the cloud, stored in a database and sent as a mail.
Commands: An integer value is sent from cloud to the gateway as a command which in turn is sent to the Arduino Nano. The LED connected to Arduino Nano blinks as per the input given by the user. In this example, the user input is 2. Hence the LED will blink two times.
Note: The IBM Watson UI updates frequently and there might be variation with respect to the images provided in this article.