Skip to content
Bill edited this page Apr 1, 2016 · 15 revisions

Welcome to the lua-azure-iot-hub wiki!

Simple example in using the lua library to send and receive messages via Azure Iot Hub

#!/usr/bin/env lua

-- You will need to set your own device id and shared access key for this code to work
local connectionString = 'HostName=hostname.azure-devices.net;DeviceId=deviceId;SharedAccessKey=????'

-- load in the library
local luaazureiothub = require 'luaazureiothub'

local processRead = function(message)
	print("received a message:")
	print(message)
	return luaazureiothub.messageReceive.ACCEPTED
end

local processSendConfirmation = function(status, message)
	-- called when the message has been sent or has an error
	if status == luaazureiothub.messageSend.OK then
		print('RX message ack')
	else
		print('RX message error:' .. status)
	end
	print(message)
end

local iothub, errorMessage = luaazureiothub.connect(connectionString, 'amqp', processRead, processSendConfirmation)
if iothub then
	-- loop around and send out a test message
	for index = 1, 10 do
		message = {
			text = 'Test message ' .. index,
			property = {
				index = index,
				messageType = 'test',
			}
		}
		-- send out message and wait for ack from server
		local result, errorMessage = iothub:sendMessage(message)
		if not result then
			print('Error in sending message ' .. errorMessage)
		end
		-- now call the worker to see if we have any messages to read
		iothub:listen(1)
	end
	iothub:disconnect()
end

For more information see the online manual

Clone this wiki locally