- #include <DFRobot_LWNode.h>
- #include "DFRobot_Atmospherlum.h"
- // 创建对象
- DFRobot_Atmospherlum_I2C atm(0x42,&Wire);
- #define FREQ 914900000
- DFRobot_LWNode_IIC node(1);//设置节点地址为1
-
- void setup( void ) {
- Serial.begin(115200);
- delay(5000);
- node.begin(/*communication IIC*/&Wire,/*debug UART*/&Serial);
-
-
- const uint32_t loraConfig[] = {FREQ, DBM16, 125000, 12}; //配置LoRa通信参数
- while(!node.setFreq(loraConfig[0]) ||
- !node.setEIRP(loraConfig[1]) ||
- !node.setBW(loraConfig[2]) ||
- !node.setSF(loraConfig[3]) ||
- !node.start()) {
- Serial.println("LoRa init failed");
- delay(2000);
- }
- while(atm.begin()!=0){delay(1000);}
- Serial.println("yunque ok");
- }
-
- void loop( void ){
-
- String data=atm.getValue("Speed")+","+atm.getValue("Dir")+","+atm.getValue("Temp")+","+atm.getValue("Humi")+","+atm.getValue("Pressure");
- delay(2000);
- node.sendPacket(2, data); //发送温湿度数据给地址为2的节点
- node.sleep(5000);
- }
复制代码
2.接收端(Receiver Node)- #include <DFRobot_LWNode.h>
- #include <DFRobot_Iot.h>
- // 静态常量
- const String topics[5] = {"siot/pres","siot/speed","siot/dir","siot/humi","siot/tem"};
- // 创建对象
- DFRobot_Iot myIot;
- #define FREQ 914900000
- DFRobot_LWNode_IIC node(2);//设置节点地址为2
-
- void rxCBFunc(uint8_t from, void *buffer, uint16_t size, int8_t rssi, int8_t snr){
- char *p = (char *)buffer;
- Serial.print("recv from: ");
- Serial.println(from, HEX);
- Serial.print("recv data: ");
- for(uint8_t i = 0; i < size; i++){
- Serial.print(p[i]);
- }
- Serial.println();
- Serial.println("Text:");
- Serial.println((char *)buffer);
- Serial.print("rssi=");Serial.println(rssi);
- Serial.print("snr=");Serial.println(snr);
- String input = (char *)buffer;
- String values[5]; // 存放结果
- int index = 0;
-
- // 先补一个逗号,方便统一处理
- input += ",";
-
- while (input.length() > 0) {
- int commaIndex = input.indexOf(',');
- if (commaIndex == -1) break;
- values[index] = input.substring(0, commaIndex);
- input = input.substring(commaIndex + 1);
- index++;
- }
-
-
- myIot.publish(topic_1, values[0], 1);//风速
- myIot.publish(topic_2, values[1], 1);//风向
- myIot.publish(topic_4, values[2], 1);//温度
- myIot.publish(topic_3, values[3], 1);//湿度
- myIot.publish(topic_0, values[4], 1);//气压
-
- }
-
- void setup( void ){
- Serial.begin(115200);
- pinMode(D9, OUTPUT);
- myIot.wifiConnect("***", "********");
- while (!myIot.wifiStatus()) {}
- digitalWrite(D9, HIGH);
- myIot.init("192.168.31.11","siot","5490164669696611","dfrobot", topics, 1883);
- myIot.connect();
- while (!myIot.connected()) {}
-
- delay(5000);
- node.begin(/*communication IIC*/&Wire,/*debug UART*/&Serial);
- const uint32_t config[] = {FREQ, DBM16, 125000, 12}; //配置LoRa通信参数
- while(!node.setFreq(config[0]) ||
- !node.setEIRP(config[1]) ||
- !node.setBW(config[2]) ||
- !node.setSF(config[3]) ||
- !node.start()) {
- Serial.println("LoRa init failed, retrying...");
- delay(2000);
- }
- node.setRxCB(rxCBFunc);
- }
-
- void loop( void ){
- node.sleep(5000);
- }
-
复制代码