本期任务

使用LoRaUNO采集BMP180气压和温度数据通过是sx1278 LoRa模组发送到ESPLoRa单项网关,ESPLoRa单项网关获取到温湿度数据上传到MQTT服务器并通过Noderd平台进行展示。

视频:

点击查看【bilibili】

框架:

image.png

ESPLoRa接收端

  1. /*
  2. * 2021年2月19日11:25:18
  3. -----------------
  4. | 时间 wifi|
  5. |---------------|
  6. | LoRa MQTT |
  7. |RS:-128 Online|
  8. | |
  9. -----------------
  10. *
  11. * 0.96OLED
  12. */
  13. #include <U8g2lib.h>
  14. #include <TimeLib.h>
  15. #include <WiFi.h>
  16. #include <NTPClient.h>
  17. #include <Ticker.h>
  18. #include <PubSubClient.h>
  19. #include <LoRa.h>
  20. #include <SPI.h>
  21. U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R2, /* reset=*/U8X8_PIN_NONE, /* clock=*/22, /* data=*/21); // ESP32使用SCL:D22 SDA:D21
  22. //开机画面---显示ESPLoRa图片
  23. 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 };
  24. //MQTT连接成功图片
  25. 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 };
  26. //MQTT连接失败图片
  27. 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 };
  28. //Lora连接成功图片
  29. 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 };
  30. //wifi图标
  31. 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 };
  32. //WiFi初始化
  33. #define ssid "WiFi名称"
  34. #define password "WiFi密码"
  35. //MQTT初始化
  36. #define mqttServer "MQTT服务器ip"
  37. #define Pre_topic "Pressure"//大气
  38. #define Tem_topic "Temperature"//温度
  39. #define RSSI_topic "RSSI"//信号强度
  40. //Lora初始化
  41. #define SS 5
  42. #define RST 14
  43. #define DI0 2
  44. #define BAND 433E6
  45. int updateTimeInterval = 0; //更新时间时间间隔
  46. Ticker updateTimeTicker; //更新时间定时器
  47. int PublishInterval = 0; //更新主题实践间隔
  48. Ticker PublishTicker; //更新主题定时器
  49. //MQTT服务端连接用户名密码
  50. const char* mqttUserName = "lora";
  51. const char* mqttPassword = "123456";
  52. //传感器初始化
  53. String temperature;
  54. String pressure;
  55. String RSSI;
  56. WiFiClient wifiClient;
  57. PubSubClient mqttClient(wifiClient);
  58. WiFiUDP ntpUDP;
  59. NTPClient timeClient(ntpUDP, "ntp1.aliyun.com", 60 * 60 * 8, 30 * 60 * 1000);
  60. String nowTime = "";
  61. void setup() {
  62. Serial.begin(9600);
  63. u8g2.begin();
  64. //检查lora
  65. check_lora();
  66. WiFi.mode(WIFI_STA);
  67. //显示启动界面
  68. showStart();
  69. //连接wifi
  70. connectWifi();
  71. //连接NTP服务器(阿里的NTP服务器)
  72. timeClient.begin();
  73. //开启更新时间定时
  74. updateTimeTicker.attach(1, tickerUpdateTimeInterval);
  75. //3秒上传一次
  76. PublishTicker.attach(3, tickerUpdatePublishInterval);
  77. //可以设置同步字(范围从0到0xFF)。
  78. LoRa.setSyncWord(0xA5);
  79. LoRa.setTxPower(20);
  80. Serial.println("LoRa Initializing OK!");
  81. //连接MQTT服务器
  82. mqttClient.setServer(mqttServer, 1883);
  83. connectMQTTServer();
  84. }
  85. void loop() {
  86. //时间更新显示
  87. time_updata();
  88. //接收lora数据包
  89. Lora_packet();
  90. //MQTT主题更新
  91. Publish_updata();
  92. //检查mqtt连接情况
  93. mqtt_check();
  94. }
  95. //显示启动动画
  96. void showStart() {
  97. u8g2.clearBuffer();
  98. u8g2.drawXBMP(0,0, 128, 64, bmp1);
  99. u8g2.sendBuffer();
  100. delay(3000);
  101. }
  102. //更新时间中断
  103. void tickerUpdateTimeInterval() {
  104. updateTimeInterval++;
  105. }
  106. //订阅主题中断
  107. void tickerUpdatePublishInterval(){
  108. PublishInterval++;
  109. }
  110. //显示时间
  111. void displayTime() {
  112. //设置字体
  113. u8g2.setFont(u8g2_font_t0_17_tr);
  114. u8g2.clearBuffer();
  115. u8g2.setCursor(0,14);//时间的位置
  116. u8g2.print(nowTime);
  117. u8g2.drawXBMP(110,0, 16, 16, bmp4);//wifi图标
  118. u8g2.drawLine(1,16,126,16); //画一条平面直线
  119. u8g2.drawXBMP(78,18, 32, 32, bmp2);//mqtt
  120. u8g2.drawStr(70,63,"Online");//MQTT---原坐标
  121. u8g2.drawXBMP(15,18, 32, 32, bmp3);//lora
  122. u8g2.drawStr(0,63,"Rs:");//LORA---具体显示内容
  123. u8g2.setCursor(28, 63);//确定刷新的位置
  124. u8g2.print(RSSI);//为了不全屏刷新的情况下清空局部显示区域
  125. u8g2.sendBuffer();
  126. }
  127. //连接WIFI
  128. void connectWifi() {
  129. //连接wifi
  130. u8g2.setFont(u8g2_font_t0_17_tr);
  131. u8g2.clearBuffer();
  132. u8g2.drawStr(0, 15, "Connecting to");
  133. u8g2.drawStr(0, 38, "SSID:");
  134. u8g2.drawStr(45, 38, ssid);
  135. Serial.println("Connecting to ");
  136. Serial.println(ssid);
  137. WiFi.begin(ssid, password);
  138. //连接判断
  139. int i = 0;
  140. while (WiFi.status() != WL_CONNECTED) {
  141. delay(1000);
  142. Serial.print(".");
  143. u8g2.drawStr(i, 50, ".");
  144. i = i + 5;
  145. u8g2.sendBuffer();
  146. }
  147. u8g2.clear();
  148. Serial.println(" ");
  149. Serial.println("WiFi Connected!");
  150. Serial.println(" ");
  151. u8g2.drawStr(0, 38, "WiFi Connected!");
  152. u8g2.sendBuffer();
  153. delay(1000);
  154. }
  155. //时间刷新
  156. void time_updata(){
  157. //每隔1s更新一次时间
  158. if (updateTimeInterval >= 1) {
  159. timeClient.update();
  160. nowTime = timeClient.getFormattedTime();
  161. //标志位清零
  162. //重新计时
  163. updateTimeInterval = 0;
  164. //显示时间
  165. displayTime();
  166. }
  167. //Serial.println(nowTime);
  168. }
  169. //每隔三秒上传一次主题的内容
  170. void Publish_updata(){
  171. if(PublishInterval >= 1)
  172. {
  173. //发布主题消息
  174. pubMQTTmsg();
  175. //计时器清零
  176. PublishInterval = 0;
  177. }
  178. }
  179. //连接MQTT云端
  180. void connectMQTTServer(){
  181. String clientId = "ESP_Lora";
  182. Serial.print("Attempting MQTT connection...");
  183. // 连接MQTT服务器
  184. if (mqttClient.connect(clientId.c_str(),mqttUserName, mqttPassword)) {
  185. Serial.println("MQTT Server Connected.");
  186. Serial.println("Server Address: ");
  187. Serial.println(mqttServer);
  188. Serial.println("ClientId:");
  189. Serial.println(clientId);
  190. // --------------------------------------
  191. //添加MQTT连接成功图标
  192. u8g2.clearBuffer();
  193. u8g2.drawXBMP(110,0, 16, 16, bmp4);//wifi图标
  194. u8g2.drawLine(1,16,126,16); //画一条平面直线
  195. u8g2.drawXBMP(78,18, 32, 32, bmp2);//mqtt
  196. u8g2.drawXBMP(15,18, 32, 32, bmp3);//lora
  197. u8g2.drawStr(70,63,"Online");//MQTT---原坐标
  198. u8g2.sendBuffer();
  199. //连接成功后开始订阅主题消息
  200. subscribeTopic(); // 订阅指定主题
  201. // --------------------------------------
  202. } else {
  203. Serial.print("MQTT Server Connect Failed. Client State:");
  204. Serial.println(mqttClient.state());
  205. // --------------------------------------
  206. // MQTT连接失败地方
  207. u8g2.setCursor(70, 63);//确定刷新的位置
  208. u8g2.print(" ");//为了不全屏刷新的情况下清空局部显示区域
  209. u8g2.drawXBMP(78,18, 32, 32, bmp20);//mqtt失败的图标
  210. u8g2.drawStr(78,63,"Fail");//具体显示内容
  211. u8g2.sendBuffer();
  212. // ------------------------------------
  213. delay(800);
  214. }
  215. }
  216. // 订阅指定主题
  217. void subscribeTopic(){
  218. //气压
  219. String topicString = "Sensor/Pressure";
  220. char publishTopic[topicString.length() + 1];
  221. strcpy(publishTopic, topicString.c_str());
  222. String messageString = pressure;
  223. char publishMsg[messageString.length() + 1];
  224. strcpy(publishMsg, messageString.c_str());
  225. // 发布信息
  226. if(mqttClient.publish(publishTopic, publishMsg)){
  227. Serial.print(" ");
  228. Serial.print(F("Pressure = "));
  229. Serial.print(pressure);
  230. Serial.print(" hPa");
  231. } else {
  232. Serial.println("Message Publish Failed.");
  233. }
  234. //温度
  235. String topicString2 = "Sensor/Temperature";
  236. char publishTopic2[topicString2.length() + 1];
  237. strcpy(publishTopic2, topicString2.c_str());
  238. String messageString2 = temperature;
  239. char publishMsg2[messageString2.length() + 1];
  240. strcpy(publishMsg2, messageString2.c_str());
  241. // 发布信息
  242. if(mqttClient.publish(publishTopic2, publishMsg2)){
  243. Serial.print(" ");
  244. Serial.print(F("Temperature = "));
  245. Serial.print(temperature);
  246. Serial.print(" *C");
  247. } else {
  248. Serial.println("Message Publish Failed.");
  249. }
  250. //信号强度
  251. String topicString3 = "Sensor/RSSI";
  252. char publishTopic3[topicString.length() + 1];
  253. strcpy(publishTopic3, topicString3.c_str());
  254. String messageString3 = temperature;
  255. char publishMsg3[messageString3.length() + 1];
  256. strcpy(publishMsg3, messageString3.c_str());
  257. // 发布信息
  258. if(mqttClient.publish(publishTopic3, publishMsg3)){
  259. Serial.print(" ");
  260. Serial.print(" RSSI ");
  261. Serial.println(RSSI);
  262. } else {
  263. Serial.println("Message Publish Failed.");
  264. }
  265. }
  266. // MQTT发布信息
  267. void pubMQTTmsg(){
  268. //气压
  269. String topicString = "Sensor/Pressure";
  270. char publishTopic[topicString.length() + 1];
  271. strcpy(publishTopic, topicString.c_str());
  272. String messageString = pressure;
  273. char publishMsg[messageString.length() + 1];
  274. strcpy(publishMsg, messageString.c_str());
  275. // 发布信息
  276. if(mqttClient.publish(publishTopic, publishMsg)){
  277. Serial.print(" ");
  278. Serial.print(F("Pressure = "));
  279. Serial.print(pressure);
  280. Serial.print(" hPa");
  281. } else {
  282. Serial.println("Message Publish Failed.");
  283. }
  284. //温度
  285. String topicString2 = "Sensor/Temperature";
  286. char publishTopic2[topicString2.length() + 1];
  287. strcpy(publishTopic2, topicString2.c_str());
  288. String messageString2 = temperature;
  289. char publishMsg2[messageString2.length() + 1];
  290. strcpy(publishMsg2, messageString2.c_str());
  291. // 发布信息
  292. if(mqttClient.publish(publishTopic2, publishMsg2)){
  293. Serial.print(" ");
  294. Serial.print(F("Temperature = "));
  295. Serial.print(temperature);
  296. Serial.print(" *C");
  297. } else {
  298. Serial.println("Message Publish Failed.");
  299. }
  300. //信号强度
  301. String topicString3 = "Sensor/RSSI";
  302. char publishTopic3[topicString.length() + 1];
  303. strcpy(publishTopic3, topicString3.c_str());
  304. String messageString3 = RSSI;
  305. char publishMsg3[messageString3.length() + 1];
  306. strcpy(publishMsg3, messageString3.c_str());
  307. // 发布信息
  308. if(mqttClient.publish(publishTopic3, publishMsg3)){
  309. Serial.print(" ");
  310. Serial.print(" RSSI ");
  311. Serial.println(RSSI);
  312. } else {
  313. Serial.println("Message Publish Failed.");
  314. }
  315. }
  316. //检查mqtt连接情况
  317. void mqtt_check(){
  318. // 如果开发板未能成功连接服务器,则尝试连接服务器
  319. if (!mqttClient.connected()) {
  320. connectMQTTServer();
  321. }
  322. // 处理信息以及心跳
  323. mqttClient.loop();
  324. }
  325. // 尝试解析数据包
  326. void Lora_packet(){
  327. int pos1;
  328. int packetSize = LoRa.parsePacket();
  329. if (packetSize) {
  330. // 如果收到包
  331. String LoRaData = LoRa.readString();
  332. //Serial.print(LoRaData);
  333. //读取数据包
  334. while (LoRa.available()) {
  335. Serial.print((char)LoRa.read());
  336. }
  337. //读取后开始拆包
  338. pos1 = LoRaData.indexOf('/');
  339. //解析后赋值变量
  340. temperature= LoRaData.substring(0, pos1);
  341. pressure = LoRaData.substring(pos1 + 1, LoRaData.length());
  342. RSSI = LoRa.packetRssi();
  343. }
  344. delay(500);
  345. }
  346. //检查lora连接是否成功
  347. void check_lora(){
  348. while (!Serial);
  349. Serial.println("LoRa Receiver");
  350. LoRa.setPins(SS, RST, DI0);
  351. if (!LoRa.begin(BAND)) {
  352. Serial.println("Starting LoRa failed!");
  353. u8g2.setFont(u8g2_font_t0_17_tr);
  354. u8g2.clearBuffer();
  355. u8g2.drawLine(1,16,126,16); //画一条平面直线
  356. u8g2.drawXBMP(15,18, 32, 32, bmp3);//lora
  357. u8g2.drawStr(15,63,"Fail");//LORA---具体显示内容
  358. u8g2.sendBuffer();
  359. while (1);
  360. }
  361. }

LoRaUNO发送端

  1. #include <Adafruit_BMP085.h>
  2. #include <LoRa.h>
  3. #include <SPI.h>
  4. #define BAND 433E6//Lora信号
  5. //建立bmp
  6. Adafruit_BMP085 bmp;
  7. String LoRaMessage = "";//声明发送信息
  8. void setup() {
  9. Serial.begin(9600);
  10. //初始化BMP180
  11. if (!bmp.begin()) {
  12. Serial.println("Could not find a valid BMP085 sensor, check wiring!");
  13. while(1);
  14. }
  15. //初始化Lora
  16. while (!Serial);
  17. Serial.println("LoRa Sender");
  18. //以指定的频率初始化收发器模块
  19. //433or434
  20. if (!LoRa.begin(BAND)) {
  21. Serial.println("Starting LoRa failed!");
  22. while (1);
  23. }
  24. LoRa.setSyncWord(0xA5);//可以设置同步字(范围从0到0xFF)。
  25. //LoRa.setTxPower(20);//发射功率
  26. }
  27. void Lora_TX(){
  28. float temperature = bmp.readTemperature();
  29. float pressure = bmp.readPressure() / 100.0F;
  30. LoRa.beginPacket();//准备发送信息
  31. //温度信息
  32. LoRa.print("Temperature: ");
  33. LoRa.print(temperature);
  34. LoRa.print(" *C");
  35. Serial.print(F("Temperature = "));
  36. Serial.print(temperature);
  37. Serial.println(F(" *C"));
  38. //气压信息
  39. LoRa.print("Pressure: ");
  40. LoRa.print(pressure);
  41. LoRa.print(" hPa");
  42. Serial.print(F("Pressure = "));
  43. Serial.print(pressure);
  44. Serial.println(F("hPa"));
  45. Serial.println();
  46. LoRa.endPacket();//发送完毕
  47. delay(3000);
  48. }
  49. void loop(){
  50. Lora_TX();
  51. }

APP界面展示
71FEA2B492A506F5B200E3B0EF2E8F36.jpg

本文附件:

LoRa资料包.zip