module(...,package.seeall) require("xap") require("xap.bsc") info={version="1.1", description="CurrentCost Decoder BBB"} --_DEBUG=1 port="/dev/ttyUSB0" local v, first_packet_received = 0 sensor ={} function update(frame) local data = frame:getValue("Serial.Received","data") if (first_packet_received == 0) then print("First packet...",data) first_packet_received = 1 end print("CC",data) local dataStart, type, id, watts, dataEnd = data:match("([^,]*):([^,]*):([^,]*):([^,]*):([^,]*)") --0 Gas pulse GS: --1858 ???? --2012 new Whole house WH --2377 Kitchen TV and HUB TX --2405 Whole house WH --3431 craft room TX --3985 Computers WH --4073 Living Room TV TX --661027636 Bedroom Mac iAM TRX --588938055 Spare iAM TRX --1232546111 Spare iAM TRX --WH1080:2626:6.9:67:3:5:SW:162:97176:20.96:40:END --{Type: Tx, id: 77 939 Watts} --cc:Tx:77:939:end if (dataStart == "CC") and (dataEnd == "END") then --type = type or 0 --sensor['type']:setText(type) id = id or 0 watts = watts or 0 sensor[id]:setText(watts) for _,v in ipairs(sensor) do v:sendEvent() end end end function init() local serialSetup = [[ xap-header { class=Serial.Comms target=dbzoo.beaglebone.serial } Serial.Setup { port=%s baud=57600 stop=1 databits=8 parity=none flow=none }]] xap.sendShort(string.format(serialSetup, port)) for _,v in ipairs{'0','1858','2012','2377','2405','3431','3985','4073','661027636','588938055','1232546111'} do sensor[v] = bsc.Endpoint{source="dbzoo.beaglebone.sensor:EnergyMonitor."..v, direction=bsc.INPUT, type=bsc.STREAM} end f = xap.Filter() f:add("xap-header","source","dbzoo.beaglebone.Serial") f:add("Serial.Received","port",port) f:callback(update) print("CC Init done..Filters set up") end