简介
通过lora组网将采集的数据传送到gprs网关,然后实现服务端与lora节点的通讯。
最终目的实现定时亮灯、远程亮灯、亮灯监测、gps等
lora参考资料:
http://www.openedv.com/forum.php?mod=viewthread&tid=288985
http://www.openedv.com/forum.php?mod=viewthread&tid=288951
整体架构
image.png硬件说明
lora模块:正点原子 SX1278 ATK-LORA-01
gprs: luat air202
nodemcu: 安信可
继电器、电流传感器、gps等待
连线:nodemcu lora
Gpio13 TX
Gpio15 RX
Vin Vcc
GND GND
代码
lora节点nodemcu的arduino核心代码
#include <SoftwareSerial.h>
#include <Ticker.h>
SoftwareSerial loraSerial(13, 15);
Ticker ticker1;
String recCMD = "";
int relayPin = 4; //继电器引脚
void ticker1Callback()
{
while (loraSerial.available() > 0)
{
char inByte = loraSerial.read();
char temp[] = " ";
sprintf(temp, "%02x", inByte);
recCMD += temp;
}
// 00 data FF 并且首位是00 末位是FF
if (recCMD.length() >= 6) {
unsigned long startTime = millis();
Serial.println("loop--------" + String(recCMD) + "--elapsed time:" + String(millis() - startTime) + "-------loop");
String _cmd = recCMD.substring(2, 4);
Serial.println(_cmd);
if (_cmd == "01") {
Serial.println("open light");
digitalWrite(relayPin, HIGH);
} else if (_cmd == "02") {
Serial.println("close light");
digitalWrite(relayPin, LOW);
}
recCMD = "";
}
}
void setup() {
// put your setup code here, to run once:
pinMode(relayPin, OUTPUT);
Serial.begin(115200);
loraSerial.begin(115200);
ticker1.attach_ms(1, ticker1Callback);
Serial.println("start lora");
}
void loop() {
Serial.println("send data");
unsigned char hexdata[10] = {0x0, 0x0, 0x0, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17};
loraSerial.write(hexdata, 10);
delay(5000);
}
运行效果:
image.png
网友评论