1

I am developing an Alexa Skill using AWS lambda an ask-sdk. In this skill I need to communicate using mqtt, I added the mqtt library to the project. To do this type of communication we need async communication. I follow the next Amazon guide: https://developer.amazon.com/blogs/alexa/post/4a46da08-d1b8-4d8e-9277-055307a9bf4a/alexa-skill-recipe-update-call-and-get-data-from-external-apisblog

The guide explain a HTTP communication, I don't get it to work with MQTT.

The structure of my project is:

  const Alexa = require('ask-sdk');
    const BienvenidaHandler = {
    canHandle(handlerInput) {
    const request = handlerInput.requestEnvelope.request;
    return request.type === 'LaunchRequest';
    },
    handle(handlerInput) {  
    return handlerInput.responseBuilder
      .speak('Hello MQTT ')
      .reprompt('wait') //Para que no se cierre la sesión tras la bienvenida.
      .withSimpleCard(SKILL_NAME, 'Bienvenido')
      .getResponse();
   },
  };
  const ConversorHandler = {
  canHandle(handlerInput) {
    const request = handlerInput.requestEnvelope.request;
    return request.type === 'IntentRequest'
      && request.intent.name === 'Conversor';
  },
  handle(handlerInput) {
    **//HERE WE MUST SUBSCRIBE/PUBLISH to the BROKER.**
    var mqtt = require('mqtt')
    var client  = mqtt.connect('mqtt://test.mosquitto.org')
    var texto = 'No data'

    client.on('connect', function () {
      client.subscribe('presence', function (err) {
        if (!err) {
          client.publish('presence', 'Hello mqtt')
        }
      })
    })

    client.on('message', function (topic, message) {
      // message is Buffer
      console.log(message.toString())
      texto = message.toString()
      client.end()
    })

    **//ALEXA SPEAK WITH MQTT DATA. (DOES NOT WORK)**
    return handlerInput.responseBuilder
      .speak('test mqtt, '+texto)      
      .reprompt(HELP_REPROMPT)
      .getResponse();
   },
 };
 ...

const skillBuilder = Alexa.SkillBuilders.standard();

exports.handler = skillBuilder
  .addRequestHandlers(
    BienvenidaHandler,
    ConversorHandler,
    HelpHandler,
    ExitHandler,
    SessionEndedRequestHandler
  )
  .addErrorHandlers(ErrorHandler)
  .lambda();

This way to read MQTT does not work. Alexa must speak with the MQTT data, but as for read we need an async function, when Alexa speak we does not have the MQTT data.

Do you know How I can read MQTT data using Alexa Skill?

Thanks.

ramon74
  • 105
  • 7
  • 15

0 Answers0