dwyl/learn-aws-lambda

Lambda Function to Connect to AWS IoT and Publish MQTT Messages

Open

#89 aperta il 9 nov 2016

Vedi su GitHub
 (3 commenti) (0 reazioni) (0 assegnatari)JavaScript (206 fork)batch import
help wantedquestion

Metriche repository

Star
 (1037 star)
Metriche merge PR
 (Nessuna PR mergiata in 30 g)

Descrizione

Hi I was trying to develop a lambda function which will connect to the AWS thing and publish an MQTT message, I want to receive these published messages on a raspberry Pi which is connected to the thing as well, and subscribed to the same topic. I managed to create the thing, certificate and connect to it. But the lambda function seems to not send the MQTT message. I am attaching my sample code here in the index.js. I kept all my certificates and keys in a folder and zipped and uploaded. Since I am testing this lambda function now, I am not using the event to trigger the connection and message publishing, I just did it like whenever the function is called , it will connect and publish the message. The function seems to be running without error, but I dont see any message coming to my Pi when this runs, (I have a python code running at the Pi side which is subscribed to the same topics, and it is connected to AWS as well). Below is the lambda code I am using, could anyone please help me if I am missing something here ?

var awsIot = require('aws-iot-device-sdk'); var deviceName = "Bibin_Pi_3"; // Updated by Bibin from AWS IOT Thing name

var thingShadows = awsIot.thingShadow({ keyPath: './private.pem.key', certPath: './certificate.pem.crt', caPath: './rootCA.pem', clientId: deviceName, region: "eu-east-1", });

var ctx = null;

exports.handler = function (event, context) { ctx = context;

thingShadows.on('connect', function() {
		thingShadows.register(deviceName);
		console.log("Connected...");
        console.log("Registering...");

	});
	
	thingShadows.on('message', function(topic, payload) {
        console.log('message', topic, payload.toString());
    });

thingShadows.publish('qrio/test/pi/voice', 'BedroomLightOn', function(){
		var cardTitle = "Bedroom Lamp on";
		var repromptText = "";
		var sessionAttributes = {};
		var shouldEndSession = false;
		var speechOutput = "Your bedroom light is on. ";
		repromptText = "Your bedroom light is on. ";
		var shouldEndSession = false;
		
	});
	console.log('value1 =', 'Bibin');
	ctx.succeed();

};

Guida contributor