User Tools

Site Tools


iot:aws:json

JSON formatted payload

It is widely used MQTT payload format JSON. Let’s write a small script which will use inbuild library to encode any WebHMI register values to JSON formatted strings. Here is the code example to encode a string type message with Amazon Web Services (AWS) Thing Report Shadow JSON format.

- AWS_thing_report.lua
function main (userId)
    local decoded = 
    {
    state = 
        {
        reported =
            { 
            timestamp       =   os.date("%Y-%m-%dT%H:%M:%S", os.time() - 3 * 60 * 60),
            message         =   "Hello, WebHMI!"            
            }
        }
    }
    encoded = cjson.encode( decoded )
                                        DEBUG(encoded)
    WriteReg("mqtt_publish", encoded); -- Test Publish (iot-test@MQTT WebHMI Client)
end

To check the result of script work let’s check “iot-test” subscribed topic in MQTT.fx.

Hint: There is drop-down list with MQTT payload format selector in the lower right corner.