如何通过 IoT Agent for JSON 将实际(硬件)物联网设备连接到 FIWARE Orion Context

发布于 2025-01-19 07:50:54 字数 425 浏览 1 评论 0原文

是否有任何教程或指南可以通过JSON的IoT代理遵循并连接硬件IoT设备?所有教程在虚拟配置设备上都可以正常工作,但是没有关于如何进行和连接实际设备的引用。

我已经尝试了教程( https://github.com/fiware/tutorials.iots.iot-s.iot-s.iot-s.iot-s.iot-s.iot-s.iot-s.iot-s.iot-s.iot-s.iot-s.iot-s.iot-s.iot-s.iot-s.iot-s.iot-s.iot-coment- Agent-json )成功,但我不知道如何连接,让我们说一个实际的智能灯。

Is there any tutorial or guide to follow and connect a hardware IoT Device via the IoT Agent for JSON? All the tutorial are working just fine with the virtual provisioned devices but there is no reference on how to proceed and connect an actual device.

I have tried the tutorial (https://github.com/FIWARE/tutorials.IoT-Agent-JSON) with success but I do not know how to connect lets say an actual smart lamp.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你与清晨阳光 2025-01-26 07:50:54

本教程应用程序创建能够通过 HTTP 或 MQTT 发送有效负载的虚拟设备。

// measures sent over HTTP are POST requests with params
sendAsHTTP(deviceId, state) {
    const options = {
        method: 'POST',
        url: IOT_AGENT_URL,
        qs: { k: getAPIKey(deviceId), i: deviceId },
        headers: this.headers,
        body: state
    };
    const debugText = 'POST ' + IOT_AGENT_URL + '?i=' + options.qs.i + '&k=' + options.qs.k;

    request(options, (error) => {
        if (error) {
            debug(debugText + ' ' + error.code);
        }
    });
    SOCKET_IO.emit('http', debugText + '  ' + state);
}

// measures sent over MQTT are posted as topics 
sendAsMQTT(deviceId, state) {
    let topic = '/' + getAPIKey(deviceId) + '/' + deviceId + '/attrs';
    if (process.env.MQTT_TOPIC_PROTOCOL !== '') {
        topic = '/' + MQTT_TOPIC_PROTOCOL + topic;
    }
    MQTT_CLIENT.publish(topic, state);
}

任何真实设备也需要遵循这些步骤,例如查看 Arduino 文档 您可以对设备进行编程,使其在选定的 MQTT 主题上发出信号。

相同的技术可用于致动。要么让设备侦听 HTTP:

if (DEVICE_TRANSPORT === 'HTTP') {
    debug('Listening on HTTP endpoints: /iot/bell, /iot/door, iot/lamp');

    const iotRouter = express.Router();

    // The router listening on the IoT port is responding to commands going southbound only.
    // Therefore we need a route for each actuator
    iotRouter.post('/iot/bell:id', Southbound.bellHttpCommand);
    iotRouter.post('/iot/door:id', Southbound.doorHttpCommand);
    iotRouter.post('/iot/lamp:id', Southbound.lampHttpCommand);
    iotRouter.post('/iot/robot:id', Southbound.robotHttpCommand);
    // Dummy ISOBUS/ISOXML endpoint.
    iotRouter.post('/iot/isoxml', Southbound.isobusHttpCommand);

    iot.use('/', iotRouter);
}

要么订阅给定的 MQTT 主题:

if (DEVICE_TRANSPORT === 'MQTT') {
    const apiKeys = process.env.DUMMY_DEVICES_API_KEYS || process.env.DUMMY_DEVICES_API_KEY || '1234';

    MQTT_CLIENT.on('connect', () => {
        apiKeys.split(',').forEach((apiKey) => {
            let topic = '/' + apiKey + '/#';
            if (process.env.MQTT_TOPIC_PROTOCOL !== '') {
                topic = '/' + MQTT_TOPIC_PROTOCOL + topic;
            }
            debug('Subscribing to MQTT Broker: ' + MQTT_BROKER_URL + ' ' + topic);
            MQTT_CLIENT.subscribe(topic);
            MQTT_CLIENT.subscribe(topic + '/#');
        });
    });

    mqtt.connect(MQTT_BROKER_URL);

    MQTT_CLIENT.on('message', function (topic, message) {
        // message is a buffer. The IoT devices will be listening and
        // responding to commands going southbound.
        debug('MQTT message received on', topic.toString());
        Southbound.processMqttMessage(topic.toString(), message.toString());
    });
}

当然,此示例适用于 JSON 或 Ultralight 有效负载,其他协议需要连接到适当的中间件(例如连接到 OPC-UA 的 OPC-UA 设备) UA 服务器),但是一般来说,您的设备将能够下载适当的通信库,并且您需要使用设备本身上找到的语言对设置进行编码并测量速率和/或驱动响应 - Arduino 文档正在使用 C++ 例子。

The tutorial app creates dummy devices which are capable of sending a payload over HTTP or MQTT

// measures sent over HTTP are POST requests with params
sendAsHTTP(deviceId, state) {
    const options = {
        method: 'POST',
        url: IOT_AGENT_URL,
        qs: { k: getAPIKey(deviceId), i: deviceId },
        headers: this.headers,
        body: state
    };
    const debugText = 'POST ' + IOT_AGENT_URL + '?i=' + options.qs.i + '&k=' + options.qs.k;

    request(options, (error) => {
        if (error) {
            debug(debugText + ' ' + error.code);
        }
    });
    SOCKET_IO.emit('http', debugText + '  ' + state);
}

// measures sent over MQTT are posted as topics 
sendAsMQTT(deviceId, state) {
    let topic = '/' + getAPIKey(deviceId) + '/' + deviceId + '/attrs';
    if (process.env.MQTT_TOPIC_PROTOCOL !== '') {
        topic = '/' + MQTT_TOPIC_PROTOCOL + topic;
    }
    MQTT_CLIENT.publish(topic, state);
}

Any real device would also need to follow these steps, so for example looking in the Arduino Documentation you can program a device to emit on a chosen MQTT topic.

The same technique can be used for actuation. Either get the device to listen on HTTP:

if (DEVICE_TRANSPORT === 'HTTP') {
    debug('Listening on HTTP endpoints: /iot/bell, /iot/door, iot/lamp');

    const iotRouter = express.Router();

    // The router listening on the IoT port is responding to commands going southbound only.
    // Therefore we need a route for each actuator
    iotRouter.post('/iot/bell:id', Southbound.bellHttpCommand);
    iotRouter.post('/iot/door:id', Southbound.doorHttpCommand);
    iotRouter.post('/iot/lamp:id', Southbound.lampHttpCommand);
    iotRouter.post('/iot/robot:id', Southbound.robotHttpCommand);
    // Dummy ISOBUS/ISOXML endpoint.
    iotRouter.post('/iot/isoxml', Southbound.isobusHttpCommand);

    iot.use('/', iotRouter);
}

Or subscribe to a given MQTT topic:

if (DEVICE_TRANSPORT === 'MQTT') {
    const apiKeys = process.env.DUMMY_DEVICES_API_KEYS || process.env.DUMMY_DEVICES_API_KEY || '1234';

    MQTT_CLIENT.on('connect', () => {
        apiKeys.split(',').forEach((apiKey) => {
            let topic = '/' + apiKey + '/#';
            if (process.env.MQTT_TOPIC_PROTOCOL !== '') {
                topic = '/' + MQTT_TOPIC_PROTOCOL + topic;
            }
            debug('Subscribing to MQTT Broker: ' + MQTT_BROKER_URL + ' ' + topic);
            MQTT_CLIENT.subscribe(topic);
            MQTT_CLIENT.subscribe(topic + '/#');
        });
    });

    mqtt.connect(MQTT_BROKER_URL);

    MQTT_CLIENT.on('message', function (topic, message) {
        // message is a buffer. The IoT devices will be listening and
        // responding to commands going southbound.
        debug('MQTT message received on', topic.toString());
        Southbound.processMqttMessage(topic.toString(), message.toString());
    });
}

Of course this example is for JSON or Ultralight payloads, other protocols would need to connect to the appropriate middleware (e.g. an OPC-UA device connecting to an OPC-UA server), however in general your device will be able to download an appropriate comms library and you'll need to code up the set-up and measure rate and/or actuation response using the language found on the device itself - the Arduino docs are using C++ for example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文