Getting Started with IBM Watson Internet of Things and connecting Raspberry Pi as a Gateway – Part 2

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:

  1. Read the LDR values (sending events from the device)
  2. Blink the LED depending on the user input (receiving commands from the cloud and executing some function)

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:

noderedflow1

Node-RED flow:


In the Node-RED flow, we will perform the following functions:

  1. Get the LDR values from Nano using Serial In node, write a function to convert LDR values to lux and filter the values by a threshold value and send it to the cloud using Watson IoT out node.
  2. Get the commands from the cloud using Watson IoT in node and send it to Nano using Serial Out node.
  3. Include the debug node wherever necessary.

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:

noderedflow2

Node-RED flow:

In the Node-RED flow, we will perform the following functions:

  1. Obtain the filtered light intensity (in lux) values from the cloud (IBM IoT Out node), send an email with the lux value and store the values in a cloudant database (light_intensity in this example).
  2. Provide an integer to the IBM IoT In node as a command.
  3. Include the debug node wherever necessary.

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.

Related Articles

loader
Please wait while your application is being created.
Request Callback