Skip to content

Instantly share code, notes, and snippets.

@andypiper
Created March 26, 2013 19:38
Show Gist options
  • Save andypiper/5248501 to your computer and use it in GitHub Desktop.
Save andypiper/5248501 to your computer and use it in GitHub Desktop.
Sample using Eclipse Mihini and Paho to read modbus values from an Arduino attached to a Raspberry Pi and publish them over MQTT
require 'sched'
require 'shell.telnet'
-- Start a telnet server on port 1234
-- Once this program is started , you can start a Lua VM through telnet
-- using the following command: telnet localhost 1234
local function run_server()
shell.telnet.init {
address = '0.0.0.0',
port = 1234,
editmode = "edit",
historysize = 100 }
end
local modbus = require 'modbus'
local utils = require("utils")
local MQTT = require("mqtt_library")
local function modbus_processing()
local config = { baudRate=9600 }
local modbus_client = modbus.new("/dev/ttyACM0", config, "RTU")
local mqtt_client = MQTT.client.create("m2m.eclipse.org", "1883")
mqtt_client:connect("mihini")
sched.wait(2)
local led_state = 0
while true do
local buffer = modbus_client:readHoldingRegisters(1,0,6)
local _, rawLight, rawTemp = string.unpack(buffer,"h2")
print(rawLight)
mqtt_client:publish("mihini/light", utils.convert(rawLight))
print(rawTemp)
print(utils.convert(rawTemp))
mqtt_client:publish("mihini/temp", utils.convert(rawTemp))
print('----')
led_state = (led_state + 1) % 2
modbus_client:writeMultipleRegisters(1,11,string.pack("h1",led_state))
sched.wait(1)
end
end
local function main()
-- Create a thread to start the telnet server
sched.run(run_server)
sched.run(modbus_processing)
-- Starting the sched main loop to execute the previous task
sched.loop()
end
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment