Here are the two required scripts to make HS3 work with your Moteinos.
' com port script to send/receive data to/from a COM port
' this script registers a callback script named com_event.txt
' when data is received on the COM port the com_event.txt script is called and the
' data can be processed there
' this script only needs to be called once as the com port will stay open
sub main()
dim e
dim comport
comport = hs.DeviceValue(230)
hs.writelog "COM PORT","Opening COM PORT" & comport & "..."
e=hs.OpenComPort(comport,"115200,n,8,1",1,"com_event.txt","main")
if e <> "" then
hs.writelog "COM PORT","Error opening COM PORT " & comport,e
else
hs.writelog "COM PORT", "COM PORT " & comport & " correctly OPEN."
end if
end sub
I have a virtual device, number 230, that holds the COM port number, so that I can change the com number if necessary without having to edit this script again.
This script fires the com_event.txt every time some data comes trough the COM port, and pushed the incoming data through a variable called "data", which is the argument of the main() function of com_event.txt.
sub main(data)' when opencomport mode is a 1, data is complete string
Dim posNode_init
Dim posNode_end
Dim nodeNumber
Dim posTag_init
Dim posTag_end
Dim tagNumber
Dim infoResult
Dim tagDir
Dim posTemp_init
Dim posTemp_end
Dim posPerc_init
Dim posPerc_end
Dim tempValue
Dim fuelPercent
Dim gatePos_init
Dim gatePos_end
Dim gatePos
Dim posComandId_init
Dim posComandId_end
Dim comandId
Dim virt_device
Dim prev_result
Dim e
Dim comport
comport = hs.DeviceValue(230)
' process the com port data here
' hs.writelog "COM DATA",data
'=============================================================
' Check if incoming data is from RFID reader
'=============================================================
if instr(1,data,"TAGID") then
posTag_init = instr(1,data,"Data 1:")+8
posTag_end = instr(1,data,"Data 2:")-1
'msgbox (Mid(data,posTag_init,posTag_end - posTag_init))
tagNumber = Mid(data,posTag_init,posTag_end - posTag_init)
hs.RunScriptFunc "rfid_tag_processing.vb","Main",tagNumber,False,True
end If
'=============================================================
' Check if incoming data is from the Boiler
'=============================================================
if instr(1,data,"BOILR") then
'#################### Get Temperature #################
posTemp_init = instr(1,data,"Data 2:")+8
posTemp_end = instr(1,data,"Data 3:")-1
tempValue = Mid(data,posTemp_init,posTemp_end - posTemp_init)
virt_device = "135"
'Check if value changed
prev_result = hs.DeviceString(virt_device)
'if value changed, update the value and string on the virtu_device
if tempValue<>prev_result then
hs.SetDeviceString virt_device,tempValue & "ºC",true
hs.SetDeviceValueByRef virt_device,Cint(tempValue),true
end if
'##################### Get Fuel Level ##################
posPerc_init = instr(1,data,"Data 3:")+8
posPerc_end = instr(1,data,"- ACK")-1
fuelPercent = Mid(data,posPerc_init,posPerc_end - posPerc_init)
virt_device="136"
'Check if value changed
prev_result=hs.DeviceString(virt_device)
'if value changed, update the value and string on the virtu_device
if Abs(cInt(fuelPercent)-cInt(instr(1,prev_result,"%")-1))<100 and fuelPercent<>prev_result then
hs.SetDeviceString virt_device,fuelPercent & "%",true
hs.SetDeviceValueByRef virt_device,cInt(fuelPercent),true
end if
end if
'=============================================================
' Check if incoming data is from the Car Remote
'=============================================================
if instr(1,data,"CARMO") then
posComandId_init = instr(1,data,"Data 1:")+8
posComandId_end = instr(1,data,"Data 2:")-1
comandId = Mid(data,posComandId_init,posComandId_end - posComandId_init)
'msgbox (comandId)
if comandId = 1 then 'se o comando for 1, abre o portão de fora
hs.writelog "CARMO", "A abrir o Portão Grande" ' faz um log da abertura do portão grande
hs.SendToComPort comport,"#5/1/" ' abre o portão
elseIf comandId = 2 then 'se o comando for 2, fecha o portão grande da rua
hs.writelog "CARMO", "A fechar o Portão Grande" ' faz um log da abertura do portão grande
hs.SendToComPort comport,"#5/2/" ' fecha o portão
elseIf comandId = 3 then 'se o comando for 3, abre/fecha o Portão da Garagem
hs.writelog "CARMO", "A abrir/fechar o Portão da Garagem" ' faz um log da abertura do portão da garagem
hs.SendToComPort comport,"#4/1/" ' abre o portão da garagem
end if
end if
'=============================================================
' Check if incoming data is a Garage Door Position!
'=============================================================
if instr(1,data,"GDOR!") then
gatePos_init = instr(1,data,"Data 1:")+8
gatePos_end = instr(1,data,"Data 2:")-1
gatePos = Mid(data,gatePos_init,gatePos_end - gatePos_init)
virt_device="195"
'Check if value changed
prev_result=hs.DeviceString(virt_device)
'if value changed, update the value and string on the virtu_device
if Abs(cInt(gatePos)-cInt(instr(1,prev_result,"%")-1))<100 and gatePos<>prev_result then
hs.SetDeviceString virt_device,gatePos & "%",true
hs.SetDeviceValueByRef virt_device,cInt(gatePos),true
end if
end if
'=============================================================
' Check if incoming data is a PING!
'=============================================================
if instr(1,data,"PING!") then
posNode_init = instr(1,data,"[")+1
posNode_end = instr(1,data,"]")
nodeNumber = Mid(data,posNode_init,posNode_end - posNode_init)
hs.writelog "PING!", "A Ping was received from node " & nodeNumber
end if
end Sub
Both this files are in the scripts folder of HS3.