After creating your Node on the ResIOT platform you would probably want to run a scene when the Node receives a message, so go to the Scene mode Payload Parsing dropdown menu
and choose Manual Lua Scene.
Now a scene editor has appeared, you can write your code there. Here's the code for the 4zerobox Demo
-- Reading Payload: SourceOrigin = resiot_startfrom() if SourceOrigin == "Manual" then Payload = resiot_hexdecode("00601835030B") Port = "99" DevEUI = "" AppEUI = "" else Payload, err = resiot_hexdecode(resiot_comm_getparam("payload")) Port = resiot_comm_getparam("port") DevEUI = resiot_payload_getparam("deveui") AppEUI = resiot_payload_getparam("appeui") end -- Parsing Payload: AccelInt = Payload AccelDec = Payload/100 Accel=AccelInt+AccelDec TempInt = Payload TempDec = Payload/100 Temp=TempInt+TempDec ConsInt = Payload ConsDec = Payload/100 Consume=ConsInt+ConsDec -- Setting Tag Values: resiot_setnodevalue(AppEUI, DevEUI, "Accel", tostring(Accel)) resiot_setnodevalue(AppEUI, DevEUI, "Temp", tostring(Temp)) resiot_setnodevalue(AppEUI, DevEUI, "Consume", tostring(Consume))
In order to read the payload received by the 4zerobox we need to determine first if the scene execution originates from manual execution or from other origins. To do so lets use the resiot_startfrom() function. If it's Manual the scene will use the data written in it, otherwise it will use data from the origin using the following two functions: resiot_comm_getparam(param) and resiot_payload_getparam(param). We also need to use resiot_hexdecode(hexstring) in order to parse the hex string of the payload into a byte array.
Now we need to parse the payload into the 3 data that this sensor returns: acceleration, temperature and consume. Based on the 4zerobox documentation we know that each data is written in the payload in the same way: the first byte is the integer part of the value, the second one is the decimal part. So we divide the decimal part by 100 and we sum the result to the integer part.
After creating your Node on the ResIOT platform you need to add some settings in order to use it properly. First, we need to create the 3 tag fields that will contain the data received by the sensor. Inside the node go to the Node fields section, press on the green "Add New" button, in the first text field write the name of the tag, it must be equal to the third parameter of the resiot_setnodevalue function, for example "Accel". As Content Type choose Numeric because the 4zerobox only returns numeric data, in UM you can write the Unit of Measurement, for Accel it will be "G".
Setting Tag Values¶
Now we can set the tag values using the data we parsed from the payload, we use the resiot_setnodevalue(AppEUI, DevEUI, TagName, DataString) function.