108 lines
2.1 KiB
C++
108 lines
2.1 KiB
C++
#include <ArduinoJson.h>
|
|
#include <SPI.h>
|
|
#include <LoRa.h>
|
|
#include "DHT.h"
|
|
|
|
#define DHTPIN 3
|
|
#define DHTTYPE DHT11
|
|
DHT dht(DHTPIN, DHTTYPE);
|
|
|
|
int lightPin = A0;
|
|
int counter = 0;
|
|
int LEDPin = 4;
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
while (!Serial);
|
|
|
|
Serial.println("LoRa Sender");
|
|
|
|
if (!LoRa.begin(866E6)) {
|
|
Serial.println("Starting LoRa failed!");
|
|
while (1);
|
|
}
|
|
dht.begin();
|
|
LoRa.crc();
|
|
LoRa.setSyncWord(0x22);
|
|
// register the receive callback
|
|
LoRa.onReceive(onReceive);
|
|
|
|
// put the radio into receive mode
|
|
LoRa.receive();
|
|
// attachInterrupt(digitalPinToInterrupt(2), lora, HIGH);
|
|
pinMode(LEDPin, OUTPUT);
|
|
}
|
|
|
|
void loop() {
|
|
|
|
StaticJsonBuffer<200> jsonBuf;
|
|
|
|
JsonObject& json = jsonBuf.createObject();
|
|
|
|
Serial.print("Sending packet: ");
|
|
Serial.println(counter);
|
|
|
|
float h = dht.readHumidity();
|
|
float t = dht.readTemperature();
|
|
|
|
json["count"] = counter;
|
|
json["temperature"] = t;
|
|
json["humidity"] = h;
|
|
json["light"] = analogRead(lightPin);
|
|
|
|
// send packet
|
|
LoRa.beginPacket();
|
|
json.printTo(LoRa);
|
|
LoRa.endPacket();
|
|
|
|
counter++;
|
|
LoRa.onReceive(onReceive);
|
|
LoRa.receive();
|
|
|
|
delay(1000);
|
|
}
|
|
|
|
void lora() {
|
|
Serial.println("Get Pin High");
|
|
int packetSize = LoRa.parsePacket();
|
|
if (packetSize) {
|
|
// received a packet
|
|
Serial.print("Received packet '");
|
|
|
|
// read packet
|
|
while (LoRa.available()) {
|
|
Serial.print((char)LoRa.read());
|
|
}
|
|
|
|
// print RSSI of packet
|
|
Serial.print("' with RSSI ");
|
|
Serial.println(LoRa.packetRssi());
|
|
}
|
|
}
|
|
|
|
void onReceive(int packetSize) {
|
|
// received a packet
|
|
Serial.print("Received packet '");
|
|
uint8_t d[packetSize];
|
|
// read packet
|
|
for (int i = 0; i < packetSize; i++) {
|
|
// Serial.print((char)LoRa.read());
|
|
d[i] = LoRa.read();
|
|
Serial.print(d[i], HEX);
|
|
Serial.print(" ");
|
|
}
|
|
|
|
// print RSSI of packet
|
|
Serial.print("' with RSSI ");
|
|
Serial.println(LoRa.packetRssi());
|
|
|
|
if (int(d[packetSize - 1]) % 2 == 0) {
|
|
Serial.println("LED on");
|
|
digitalWrite(LEDPin, HIGH);
|
|
} else {
|
|
Serial.println("LED off");
|
|
digitalWrite(LEDPin, LOW);
|
|
}
|
|
}
|
|
|