You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.4 KiB

4 years ago
4 years ago
# LoRa-mqtt
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2FIoTcat%2FLoRa-mqtt.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2FIoTcat%2FLoRa-mqtt?ref=badge_shield)
![size](https://badge-size.herokuapp.com/iotcat/LoRa-mqtt/master/lora-mqtt.h)
4 years ago
在LoRa通信中使用MQTT协议..
[English Version](./README.md)
4 years ago
4 years ago
## 使用案例 - 使用回调函数
4 years ago
```C++
4 years ago
#define LORA_SOCKET_IP "1.0.0.1" //需唯一
4 years ago
#include "lora-mqtt.h"
LoRaMQTT mqtt;
void setup(){
4 years ago
mqtt.ini(); //初始化
mqtt.onReceived(doIfRec); //注册回调函数,当接收到新消息时触发
mqtt.subscribe("主题名称"); //订阅mqtt主题
4 years ago
}
void doIfRec(String subject, String content){
4 years ago
//当收到新消息时执行这个函数中的内容
4 years ago
4 years ago
mqtt.publish("主题", "内容"); //发布mqtt消息
4 years ago
}
void loop(){
4 years ago
//注意loop()全局不得有delay,否则会导致接收不稳定
mqtt.core(); //循环组件
4 years ago
}
```
4 years ago
## 使用案例 - 使用条件触发
4 years ago
```C++
4 years ago
#define LORA_SOCKET_IP "1.0.0.1" //需唯一
4 years ago
#include "lora-mqtt.h"
LoRaMQTT mqtt;
void setup(){
4 years ago
mqtt.ini(); //初始化
mqtt.subscribe("主题名称"); //订阅mqtt主题
4 years ago
}
void loop(){
4 years ago
//注意loop()全局不得有delay,否则会导致接收不稳定
4 years ago
if(mqtt.isNewMsg()){
String subject, content;
mqtt.getNewMsg(subject, content);
Serial.println(subject, content);
}
4 years ago
mqtt.core(); //循环组件
4 years ago
}
```