Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda - How to get the topic name of data coming from AWS IOT

I'm testing AWS Lambda with an AWS IOT source. My mqtt clients are publishing in different topics : device A publish data into streaming/A, device B publish data into streaming/B so in AWS Lambda I defined a SQL rule selecting all devices coming from the topics streaming/+. The thing is that now I didn't have the information of the device source because I only have a Array[Byte]] with extra informations. If anyone has a solution to access to the mqtt payload with the topic information, I will take it !

import java.io.{ByteArrayOutputStream, InputStream, OutputStream}
import com.amazonaws.services.lambda.runtime.{Context, RequestStreamHandler}
/**
  * Created by alifirat on 24/04/17.
  */
class IOTConsumer extends RequestStreamHandler {

  val BUFFER_SIZE = 1024 * 4

  override def handleRequest(input: InputStream, output: OutputStream, context: Context): Unit = {
    val bytes = toByteArray(input)
    val logger= context.getLogger
    logger.log("Receive following thing :"  + new String(bytes))
    output.write(bytes)
  }

   /**
     * Reads and returns the rest of the given input stream as a byte array.
     * Caller is responsible for closing the given input stream.
     */
   def toByteArray(is : InputStream) : Array[Byte] = {
     val output = new ByteArrayOutputStream()
     try {
       val b = new Array[Byte](BUFFER_SIZE);
       var n = 0
       var flag = true
       while(flag) {
         n = is.read(b)
         if(n == -1) flag = false
         else {
           output.write(b, 0, n)
         }
       }
       output.toByteArray();
     } finally {
       output.close();
       Array[Byte]()
     }
   }
}
like image 724
alifirat Avatar asked Apr 24 '17 14:04

alifirat


People also ask

How do I get data from AWS IoT?

In order for your web service to receive data from AWS IoT, you first need to create a topic rule with a new HTTP action. HTTP action will POST message payloads as well as any headers specified in the action to the designated HTTPS endpoint every time the action is executed.

How do I get my AWS IoT endpoint?

The AWS IoT device service endpoints support device-centric access to security and management services. To learn your account's device data endpoint, you can find it in the Settings page of your AWS IoT Core console.

Can I get a history of AWS IoT core API calls made on my account for security analysis and operational troubleshooting purposes?

Q. Can I get a history of AWS IoT Core API calls made on my account for security analysis and operational troubleshooting purposes? Yes, to receive a history of AWS IoT Core API calls made on your account, you simply turn on CloudTrail in the AWS Management Console.


1 Answers

I was looking for the same thing, there is a way to achieve that. While constructing your SQL you can use the topic() function to get the topic the message was sent to. That way you could put in the attribute section

*, topic() as topic

so your final SQL will look like:

SELECT *, topic() as topic FROM one/of/my/+/topics

your payload will then contain a new attribute topic that you can parse within your lambda function. More on this https://docs.aws.amazon.com/iot/latest/developerguide/iot-sql-functions.html

like image 195
Mattia Procopio Avatar answered Oct 17 '22 15:10

Mattia Procopio