本期任务
使用LoRaUNO采集BMP180气压和温度数据通过是sx1278 LoRa模组发送到ESPLoRa单项网关,ESPLoRa单项网关获取到温湿度数据上传到MQTT服务器并通过Noderd平台进行展示。
视频:
框架:
ESPLoRa接收端
/*
* 2021年2月19日11:25:18
-----------------
| 时间 wifi|
|---------------|
| LoRa MQTT |
|RS:-128 Online|
| |
-----------------
*
* 0.96OLED
*/
#include <U8g2lib.h>
#include <TimeLib.h>
#include <WiFi.h>
#include <NTPClient.h>
#include <Ticker.h>
#include <PubSubClient.h>
#include <LoRa.h>
#include <SPI.h>
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R2, /* reset=*/U8X8_PIN_NONE, /* clock=*/22, /* data=*/21); // ESP32使用SCL:D22 SDA:D21
//开机画面---显示ESPLoRa图片
const unsigned char bmp1[] U8X8_PROGMEM = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xe0,0x3f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x38,0xe0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0c,0x80,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x00,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xe0,0x3f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x70,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x0f,0x00,0x00,0x00,0x00,0x00,0x00,0xfc,0x0f,0xff,0x3f,0xff,0x3f,0xf0,0x01,0xc0,0x18,0xfe,0x7f,0x00,0x00,0x00,0x00,0xff,0x8f,0xff,0x3f,0xff,0x7f,0xf0,0x01,0x00,0x00,0xfe,0xff,0x00,0x00,0x00,0x80,0xff,0xcf,0xff,0x3f,0xff,0xff,0xf0,0x01,0x00,0x00,0xfe,0xff,0x01,0x00,0x00,0x80,0x1f,0xc0,0x0f,0x00,0x1f,0xf8,0xf0,0x01,0x00,0x00,0x3e,0xf0,0x03,0x00,0x00,0xc0,0x07,0xc0,0x03,0x00,0x1f,0xf0,0xf0,0x01,0xe0,0x0f,0x3e,0xe0,0x03,0x00,0x00,0xc0,0x07,0xc0,0x03,0x00,0x1f,0xf0,0xf1,0x01,0xf0,0x3f,0x3e,0xe0,0xe3,0x7f,0x00,0xc0,0x03,0xc0,0x03,0x00,0x1f,0xf0,0xf1,0x01,0xf8,0x3e,0x3e,0xf0,0xe1,0xff,0x00,0xc0,0xff,0xcf,0x0f,0x00,0x1f,0xf8,0xf0,0x01,0x3c,0x78,0xfe,0xff,0x01,0xf0,0x01,0xe0,0xff,0x8f,0xff,0x0f,0xff,0xff,0xf0,0x01,0x3c,0x78,0xfe,0xff,0x00,0xe0,0x01,0xc0,0xff,0x0f,0xff,0x1f,0xff,0x7f,0xf0,0x01,0x3c,0x78,0xfe,0xff,0x00,0xe0,0x01,0xc0,0x07,0x00,0x00,0x3f,0xff,0x3f,0xf0,0x01,0x3c,0xf8,0x3e,0xfc,0xe0,0xff,0x01,0xc0,0x07,0x00,0x00,0x3c,0x1f,0x00,0xf0,0x01,0x3c,0x78,0x3e,0xf0,0xf1,0xff,0x01,0xc0,0x07,0x00,0x00,0x7c,0x1f,0x00,0xf0,0x01,0x3c,0x78,0x3e,0xf0,0xf1,0xe0,0x01,0x80,0x3f,0x00,0x00,0x3f,0x1f,0x00,0xe0,0x07,0x3c,0x78,0x3e,0xe0,0xf3,0xe0,0x01,0x80,0xff,0xcf,0xff,0x3f,0x1f,0x00,0xe0,0xff,0xf9,0x3e,0x3e,0xe0,0xf3,0xe0,0x01,0x00,0xff,0xcf,0xff,0x1f,0x1f,0x00,0xc0,0xff,0xf9,0x3f,0x3e,0xe0,0xf3,0xff,0x01,0x00,0xfc,0xcf,0xff,0x0f,0x1f,0x00,0x80,0xff,0xe1,0x0f,0x3e,0xe0,0xe3,0xff,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x0f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x70,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xe0,0x3f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x00,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0e,0x80,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x38,0xe0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xe0,0x3f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
//MQTT连接成功图片
const unsigned char bmp2[] U8X8_PROGMEM = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xfc,0x03,0x00,0x00,0xfe,0x07,0x00,0x00,0xff,0x0f,0x00,0x80,0x07,0x1e,0x00,0xc0,0x03,0x3c,0x00,0xc0,0x03,0x3c,0x00,0xc0,0x01,0x3a,0x00,0xc0,0x01,0x3a,0x00,0xe0,0x01,0xfb,0x07,0xf8,0x81,0xff,0x0f,0xfc,0x86,0x07,0x1f,0x1c,0xc7,0x03,0x38,0x1c,0xef,0x01,0x38,0x1c,0xfe,0x01,0x38,0x1c,0xfc,0x00,0x38,0x1c,0x78,0x00,0x38,0x1c,0x30,0x00,0x38,0xf8,0x00,0x00,0x1f,0xf0,0xff,0xff,0x0f,0xe0,0xff,0xff,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
//MQTT连接失败图片
const unsigned char bmp20[] U8X8_PROGMEM = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xfc,0x03,0x00,0x00,0xfe,0x07,0x00,0x00,0xff,0x0f,0x00,0x80,0x07,0x1e,0x00,0xc0,0xf3,0x3c,0x00,0xc0,0xf3,0x3c,0x00,0xc0,0xf1,0x38,0x00,0xc0,0xf1,0x38,0x00,0xe0,0xf1,0xf8,0x07,0xf8,0xf1,0xf8,0x0f,0xfc,0x00,0x00,0x1f,0x1c,0x00,0x00,0x38,0x1c,0xf0,0x00,0x38,0x1c,0xf0,0x00,0x38,0x1c,0xf0,0x00,0x38,0x1c,0xf0,0x00,0x38,0x1c,0x00,0x00,0x38,0xf8,0x00,0x00,0x1f,0xf0,0xff,0xff,0x0f,0xe0,0xff,0xff,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
//Lora连接成功图片
const unsigned char bmp3[] U8X8_PROGMEM = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xe0,0x03,0x00,0x00,0xfc,0x0f,0x00,0x00,0xfe,0x3f,0x00,0x00,0xff,0x7f,0x00,0x80,0x0f,0x7c,0x00,0xc0,0xe3,0xf1,0x00,0xc0,0xf3,0xe3,0x01,0xc0,0xf9,0xe7,0x01,0xe0,0xfd,0xcf,0x01,0xe0,0x3d,0xcf,0x01,0xe0,0x3d,0xce,0x01,0x00,0xc0,0x01,0x00,0x00,0xc0,0x01,0x00,0x00,0xc0,0x01,0x00,0xc0,0xff,0xff,0x03,0xe0,0xff,0xff,0x07,0xe0,0xff,0xff,0x07,0xe0,0xff,0xff,0x07,0xe0,0x39,0xff,0x07,0xe0,0x39,0xff,0x07,0xe0,0xff,0xff,0x07,0xe0,0xff,0xff,0x07,0xc0,0xff,0xff,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
//wifi图标
const unsigned char bmp4[] U8X8_PROGMEM = { 0x00,0x00,0x00,0x00,0xc0,0x03,0xf8,0x1f,0xfe,0x7f,0x3f,0xfc,0xef,0xf7,0xf6,0x6f,0xf8,0x1f,0x38,0x1c,0xc0,0x03,0xe0,0x03,0xc0,0x03,0x80,0x01,0x00,0x00,0x00,0x00 };
//WiFi初始化
#define ssid "WiFi名称"
#define password "WiFi密码"
//MQTT初始化
#define mqttServer "MQTT服务器ip"
#define Pre_topic "Pressure"//大气
#define Tem_topic "Temperature"//温度
#define RSSI_topic "RSSI"//信号强度
//Lora初始化
#define SS 5
#define RST 14
#define DI0 2
#define BAND 433E6
int updateTimeInterval = 0; //更新时间时间间隔
Ticker updateTimeTicker; //更新时间定时器
int PublishInterval = 0; //更新主题实践间隔
Ticker PublishTicker; //更新主题定时器
//MQTT服务端连接用户名密码
const char* mqttUserName = "lora";
const char* mqttPassword = "123456";
//传感器初始化
String temperature;
String pressure;
String RSSI;
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "ntp1.aliyun.com", 60 * 60 * 8, 30 * 60 * 1000);
String nowTime = "";
void setup() {
Serial.begin(9600);
u8g2.begin();
//检查lora
check_lora();
WiFi.mode(WIFI_STA);
//显示启动界面
showStart();
//连接wifi
connectWifi();
//连接NTP服务器(阿里的NTP服务器)
timeClient.begin();
//开启更新时间定时
updateTimeTicker.attach(1, tickerUpdateTimeInterval);
//3秒上传一次
PublishTicker.attach(3, tickerUpdatePublishInterval);
//可以设置同步字(范围从0到0xFF)。
LoRa.setSyncWord(0xA5);
LoRa.setTxPower(20);
Serial.println("LoRa Initializing OK!");
//连接MQTT服务器
mqttClient.setServer(mqttServer, 1883);
connectMQTTServer();
}
void loop() {
//时间更新显示
time_updata();
//接收lora数据包
Lora_packet();
//MQTT主题更新
Publish_updata();
//检查mqtt连接情况
mqtt_check();
}
//显示启动动画
void showStart() {
u8g2.clearBuffer();
u8g2.drawXBMP(0,0, 128, 64, bmp1);
u8g2.sendBuffer();
delay(3000);
}
//更新时间中断
void tickerUpdateTimeInterval() {
updateTimeInterval++;
}
//订阅主题中断
void tickerUpdatePublishInterval(){
PublishInterval++;
}
//显示时间
void displayTime() {
//设置字体
u8g2.setFont(u8g2_font_t0_17_tr);
u8g2.clearBuffer();
u8g2.setCursor(0,14);//时间的位置
u8g2.print(nowTime);
u8g2.drawXBMP(110,0, 16, 16, bmp4);//wifi图标
u8g2.drawLine(1,16,126,16); //画一条平面直线
u8g2.drawXBMP(78,18, 32, 32, bmp2);//mqtt
u8g2.drawStr(70,63,"Online");//MQTT---原坐标
u8g2.drawXBMP(15,18, 32, 32, bmp3);//lora
u8g2.drawStr(0,63,"Rs:");//LORA---具体显示内容
u8g2.setCursor(28, 63);//确定刷新的位置
u8g2.print(RSSI);//为了不全屏刷新的情况下清空局部显示区域
u8g2.sendBuffer();
}
//连接WIFI
void connectWifi() {
//连接wifi
u8g2.setFont(u8g2_font_t0_17_tr);
u8g2.clearBuffer();
u8g2.drawStr(0, 15, "Connecting to");
u8g2.drawStr(0, 38, "SSID:");
u8g2.drawStr(45, 38, ssid);
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
//连接判断
int i = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
u8g2.drawStr(i, 50, ".");
i = i + 5;
u8g2.sendBuffer();
}
u8g2.clear();
Serial.println(" ");
Serial.println("WiFi Connected!");
Serial.println(" ");
u8g2.drawStr(0, 38, "WiFi Connected!");
u8g2.sendBuffer();
delay(1000);
}
//时间刷新
void time_updata(){
//每隔1s更新一次时间
if (updateTimeInterval >= 1) {
timeClient.update();
nowTime = timeClient.getFormattedTime();
//标志位清零
//重新计时
updateTimeInterval = 0;
//显示时间
displayTime();
}
//Serial.println(nowTime);
}
//每隔三秒上传一次主题的内容
void Publish_updata(){
if(PublishInterval >= 1)
{
//发布主题消息
pubMQTTmsg();
//计时器清零
PublishInterval = 0;
}
}
//连接MQTT云端
void connectMQTTServer(){
String clientId = "ESP_Lora";
Serial.print("Attempting MQTT connection...");
// 连接MQTT服务器
if (mqttClient.connect(clientId.c_str(),mqttUserName, mqttPassword)) {
Serial.println("MQTT Server Connected.");
Serial.println("Server Address: ");
Serial.println(mqttServer);
Serial.println("ClientId:");
Serial.println(clientId);
// --------------------------------------
//添加MQTT连接成功图标
u8g2.clearBuffer();
u8g2.drawXBMP(110,0, 16, 16, bmp4);//wifi图标
u8g2.drawLine(1,16,126,16); //画一条平面直线
u8g2.drawXBMP(78,18, 32, 32, bmp2);//mqtt
u8g2.drawXBMP(15,18, 32, 32, bmp3);//lora
u8g2.drawStr(70,63,"Online");//MQTT---原坐标
u8g2.sendBuffer();
//连接成功后开始订阅主题消息
subscribeTopic(); // 订阅指定主题
// --------------------------------------
} else {
Serial.print("MQTT Server Connect Failed. Client State:");
Serial.println(mqttClient.state());
// --------------------------------------
// MQTT连接失败地方
u8g2.setCursor(70, 63);//确定刷新的位置
u8g2.print(" ");//为了不全屏刷新的情况下清空局部显示区域
u8g2.drawXBMP(78,18, 32, 32, bmp20);//mqtt失败的图标
u8g2.drawStr(78,63,"Fail");//具体显示内容
u8g2.sendBuffer();
// ------------------------------------
delay(800);
}
}
// 订阅指定主题
void subscribeTopic(){
//气压
String topicString = "Sensor/Pressure";
char publishTopic[topicString.length() + 1];
strcpy(publishTopic, topicString.c_str());
String messageString = pressure;
char publishMsg[messageString.length() + 1];
strcpy(publishMsg, messageString.c_str());
// 发布信息
if(mqttClient.publish(publishTopic, publishMsg)){
Serial.print(" ");
Serial.print(F("Pressure = "));
Serial.print(pressure);
Serial.print(" hPa");
} else {
Serial.println("Message Publish Failed.");
}
//温度
String topicString2 = "Sensor/Temperature";
char publishTopic2[topicString2.length() + 1];
strcpy(publishTopic2, topicString2.c_str());
String messageString2 = temperature;
char publishMsg2[messageString2.length() + 1];
strcpy(publishMsg2, messageString2.c_str());
// 发布信息
if(mqttClient.publish(publishTopic2, publishMsg2)){
Serial.print(" ");
Serial.print(F("Temperature = "));
Serial.print(temperature);
Serial.print(" *C");
} else {
Serial.println("Message Publish Failed.");
}
//信号强度
String topicString3 = "Sensor/RSSI";
char publishTopic3[topicString.length() + 1];
strcpy(publishTopic3, topicString3.c_str());
String messageString3 = temperature;
char publishMsg3[messageString3.length() + 1];
strcpy(publishMsg3, messageString3.c_str());
// 发布信息
if(mqttClient.publish(publishTopic3, publishMsg3)){
Serial.print(" ");
Serial.print(" RSSI ");
Serial.println(RSSI);
} else {
Serial.println("Message Publish Failed.");
}
}
// MQTT发布信息
void pubMQTTmsg(){
//气压
String topicString = "Sensor/Pressure";
char publishTopic[topicString.length() + 1];
strcpy(publishTopic, topicString.c_str());
String messageString = pressure;
char publishMsg[messageString.length() + 1];
strcpy(publishMsg, messageString.c_str());
// 发布信息
if(mqttClient.publish(publishTopic, publishMsg)){
Serial.print(" ");
Serial.print(F("Pressure = "));
Serial.print(pressure);
Serial.print(" hPa");
} else {
Serial.println("Message Publish Failed.");
}
//温度
String topicString2 = "Sensor/Temperature";
char publishTopic2[topicString2.length() + 1];
strcpy(publishTopic2, topicString2.c_str());
String messageString2 = temperature;
char publishMsg2[messageString2.length() + 1];
strcpy(publishMsg2, messageString2.c_str());
// 发布信息
if(mqttClient.publish(publishTopic2, publishMsg2)){
Serial.print(" ");
Serial.print(F("Temperature = "));
Serial.print(temperature);
Serial.print(" *C");
} else {
Serial.println("Message Publish Failed.");
}
//信号强度
String topicString3 = "Sensor/RSSI";
char publishTopic3[topicString.length() + 1];
strcpy(publishTopic3, topicString3.c_str());
String messageString3 = RSSI;
char publishMsg3[messageString3.length() + 1];
strcpy(publishMsg3, messageString3.c_str());
// 发布信息
if(mqttClient.publish(publishTopic3, publishMsg3)){
Serial.print(" ");
Serial.print(" RSSI ");
Serial.println(RSSI);
} else {
Serial.println("Message Publish Failed.");
}
}
//检查mqtt连接情况
void mqtt_check(){
// 如果开发板未能成功连接服务器,则尝试连接服务器
if (!mqttClient.connected()) {
connectMQTTServer();
}
// 处理信息以及心跳
mqttClient.loop();
}
// 尝试解析数据包
void Lora_packet(){
int pos1;
int packetSize = LoRa.parsePacket();
if (packetSize) {
// 如果收到包
String LoRaData = LoRa.readString();
//Serial.print(LoRaData);
//读取数据包
while (LoRa.available()) {
Serial.print((char)LoRa.read());
}
//读取后开始拆包
pos1 = LoRaData.indexOf('/');
//解析后赋值变量
temperature= LoRaData.substring(0, pos1);
pressure = LoRaData.substring(pos1 + 1, LoRaData.length());
RSSI = LoRa.packetRssi();
}
delay(500);
}
//检查lora连接是否成功
void check_lora(){
while (!Serial);
Serial.println("LoRa Receiver");
LoRa.setPins(SS, RST, DI0);
if (!LoRa.begin(BAND)) {
Serial.println("Starting LoRa failed!");
u8g2.setFont(u8g2_font_t0_17_tr);
u8g2.clearBuffer();
u8g2.drawLine(1,16,126,16); //画一条平面直线
u8g2.drawXBMP(15,18, 32, 32, bmp3);//lora
u8g2.drawStr(15,63,"Fail");//LORA---具体显示内容
u8g2.sendBuffer();
while (1);
}
}
LoRaUNO发送端
#include <Adafruit_BMP085.h>
#include <LoRa.h>
#include <SPI.h>
#define BAND 433E6//Lora信号
//建立bmp
Adafruit_BMP085 bmp;
String LoRaMessage = "";//声明发送信息
void setup() {
Serial.begin(9600);
//初始化BMP180
if (!bmp.begin()) {
Serial.println("Could not find a valid BMP085 sensor, check wiring!");
while(1);
}
//初始化Lora
while (!Serial);
Serial.println("LoRa Sender");
//以指定的频率初始化收发器模块
//433or434
if (!LoRa.begin(BAND)) {
Serial.println("Starting LoRa failed!");
while (1);
}
LoRa.setSyncWord(0xA5);//可以设置同步字(范围从0到0xFF)。
//LoRa.setTxPower(20);//发射功率
}
void Lora_TX(){
float temperature = bmp.readTemperature();
float pressure = bmp.readPressure() / 100.0F;
LoRa.beginPacket();//准备发送信息
//温度信息
LoRa.print("Temperature: ");
LoRa.print(temperature);
LoRa.print(" *C");
Serial.print(F("Temperature = "));
Serial.print(temperature);
Serial.println(F(" *C"));
//气压信息
LoRa.print("Pressure: ");
LoRa.print(pressure);
LoRa.print(" hPa");
Serial.print(F("Pressure = "));
Serial.print(pressure);
Serial.println(F("hPa"));
Serial.println();
LoRa.endPacket();//发送完毕
delay(3000);
}
void loop(){
Lora_TX();
}
APP界面展示