物聯網網關設計實戰


本文是作者之前做的一個開源項目,用Python及MQTT寫的一個物聯網網關,源碼托管在github上。之前在國外社區發了一個貼,現在轉到博客園來。

 

We always face with some applications which needs to just send some data to a server. To start thinking about MQTT-based framework, here’s the application scenarios: 

arch1What’s MQTT

MQTT is a machine-to-machine (M2M)/”Internet of Things” connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport. It is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium. For example, it has been used in sensors communicating to a broker via satellite link, over occasional dial-up connections with healthcare providers, and in a range of home automation and small device scenarios.

Pub/Sub Model

pub_sub

Figure: Pub/Sub pattern

                          

In a pub/sub model, the producer acts as publisher, and consumer acts as subscriber.  Using the publish – subscribe pattern in application can dramatically simplify its design and improve testability.

In a distribute IoT system, basically you just have some part(s) of your device subscribe to a particular topic and have some other part(s) of your device publish messages with that topic. Messages sent will be delivered to all registered handlders (aka listeners, also receivers) for a given topic(one-to-many broadcast).

 

We implement the following parts:

 

1.1 Generate a heartbeat in zigbee Node

every node can produce data, adc value, temperature etc.

void generateHeartbeat() {
    heartbeat_t heartbeat;
    sendtoAir(heartbeat);
}

The heartbeat format gateway requires is key-value pair string (KVP).

“key:value” + ‘\n’

eg:  “temperature:23 \n”   

Heartbeat is sent with no ack.

topology

                                           Figure: Network Topology

 

GatewayNew

Figure: Gateway software architecture

 

 Gateway is written by pure Python, which makes it more pithy to implement the software components.

It consists of four parts:

1. Exchange component is one of the core components of the gateway project. It acts as a protocol converter between MQTT and PAN;

2. Data filter component provides some static filter, such as LinearFilter, EnumFilter etc. Once a filter is activated by user, each incoming/outgoing data frame will be filtered by it.

3. Plugin usually contains some user-defined business logic, such as sending a heartbeat, processing sensor data etc. Because every single data frame will pass to it, user can do anything they want according to the incoming data. Click here to get a depth understanding of the plugin developing.

4. PAN device Driver is a layer used for communication between Pan Device and Host CPU. Usually, the physical connection is a serial port. Now, it supports MeshBee v1.0.4 firmware and XBee.

 

 Main features:

1. Can be ported to any platform that supports Python.

2. User-defined Qos, which ensure the reliability of data transmission.

3. Message forwarding is handled by Broker. User can focus on their application developing.

4. Pub/Sub model, one-to-many message deliver.

5. light-weight messaging protocol over TCP. It is designed for connections with remote locations where a “small code footprint” is required and/or network bandwidth is limited.

6. Support plugins expanding.

7. Support data filter.

8. Support any device that has a serial port.

9. Build-in Sqlite Database.

 

Getaway running flow

The following flow provides some basic procedures of how a gateway engine run.

 

RunningProcedureNew

 

Gateway configuration

Gateway program will read its configuration files at startup. We create a yaml file in root directory. Modify the following items to meet your request.

1. yaml['pan']['port']

Pan device serial port device ID in /dev.

2. yaml['pan']['baudrate’']

Baudrate of pan device serial port device.

3. yaml['pan']['driver_class']

Specified pan device driver

4. yaml['mqtt’']['client_id']

Gateway client ID

5. yaml['mqtt’']['host']

Broker Url

6. yaml['mqtt’']['port']

Broker TCP port number.

 

An android app can control and monitor every node from the pan network. It runs a background service to server MQTT.

 

a1 a2 a3 a4

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM