Skip to content

ehsanulhaq001/IoT_Payload

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Payload Design

Designed in a way to ensure same code can be used for both gateway as well as sensor.

Payload from sensor can be directly passed as data for gateway payload and so on to higher levels.

This ensures that the code used to extract data is reusable at multiple levels.

Gateway Payload

{
    "device": {
        "type": "gateway",
        "id": "001"
    },
    "message": {
        "metadata": {
            "condition": {
                "bat_percentage": 86.7,
                "cycle_count": 226,
                "status": 1,
                "temp": 6,
                "time": 0,
                "voltage": 3.6
            },
            "settings": {
                "property": "value"
            }
        },
        "data_count": 2,
        "data_list": [
            "+++SENSOR_PAYLOAD_1+++",
            "+++SENSOR_PAYLOAD_2+++"
        ],
        "published_at": "2020-01-15T16:02:10.778Z"
    }
}

Sensor Payload

{
    "device": {
        "type": "sensor",
        "id": "sensor_id"
    },
    "message": {
        "metadata": {
            "condition": {
                "bat_percentage": 86.7,
                "cycle_count": 226,
                "status": 1,
                "temp": 6,
                "time": 0,
                "voltage": 3.6
            },
            "settings": {
                "property": "value"
            }
        },
        "data_count": 2,
        "data_list": [
            {
            "sensor_type": "Temperature",
            "value": 31,
            "unit": "C"
        },{
            "sensor_type": "Accelerometer",
            "value": "x: 0.0, y: 0.0, z: 0.0",
            "unit": "m/s^2"
        }
        ]
        "published_at": "2020-01-15T16:02:10.668Z"
    }
}

Example

 const sensor_payload = createPayload({
            type: "sensor",
            id: "0001",
        }, {
            bat_percentage: 86.7,
            cycle_count: 226,
            status: 1,
            temp: 6,
            time: 0,
            voltage: 3.6
        }, {
            property: "value"
        },
        [{
            sensor_type: "Accelerometer",
            value: {
                x: Math.random() * 2 - 1,
                y: Math.random() * 2 - 1,
                z: Math.random() * 4 + 7.801
            },
            unit: "m/s^2",
        }],
        new Date().toISOString()
    )

const gateway_payload = createPayload({
            type: "gateway",
            id: "01",
        }, {
            bat_percentage: 86.7,
            cycle_count: 226,
            status: 1,
            temp: 6,
            time: 0,
            voltage: 3.6
        }, {
            property: "value"
        }, [sensor_payload],
        new Date().toISOString(), )

device.publish(
    iot_topic,
    JSON.stringify(gateway_payload)
);

const createPayload = (device, condition, settings, data, published_at) => {
    return {
        device,
        message: {
            metadata: {
                condition,
                settings
            },
            data_count: data.length,
            data,
            published_at
        }
    }
}

About

MQTT Payload Design

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published