diff --git a/IOT/receiver LoRa/include/Program.h b/IOT/receiver LoRa/include/Program.h index 66e4a79..c840657 100644 --- a/IOT/receiver LoRa/include/Program.h +++ b/IOT/receiver LoRa/include/Program.h @@ -1,7 +1,10 @@ #ifndef PROGRAM_H #define PROGRAM_H -#include "Arduino.h" +#include +#include +#include +#include class Program { public: diff --git a/IOT/receiver LoRa/src/Program.cpp b/IOT/receiver LoRa/src/Program.cpp index e412959..82ff6d8 100644 --- a/IOT/receiver LoRa/src/Program.cpp +++ b/IOT/receiver LoRa/src/Program.cpp @@ -3,6 +3,18 @@ Program::Program() { // Startup Serial.begin(MONITOR_SPEED); + Serial.println("initBoard"); + SPI.begin(RADIO_SCLK_PIN, RADIO_MISO_PIN, RADIO_MOSI_PIN); //for LoRa module + Wire.begin(I2C_SDA, I2C_SCL);//for oled screen + // When the power is turned on, a delay is required. + delay(1500); + + //init LoRa + LoRa.setPins(RADIO_CS_PIN, RADIO_RST_PIN, RADIO_DIO0_PIN); + if (!LoRa.begin(LoRa_frequency)) { + Serial.println("Starting LoRa failed!"); + while (1); + } } void Program::loop() { diff --git a/IOT/receiver LoRa/src/main.cpp b/IOT/receiver LoRa/src/main.cpp index 74bf5a7..70fe082 100644 --- a/IOT/receiver LoRa/src/main.cpp +++ b/IOT/receiver LoRa/src/main.cpp @@ -1,11 +1,115 @@ -#include "Program.h" +// #include "Program.h" -Program* program; +// Program* program; -void setup() { - program = new Program(); +// void setup() { +// program = new Program(); +// } + +// void loop() { +// program->loop(); +// } + + +#include +#include "boards.h" + +#include + +void setup() +{ + initBoard(); + // When the power is turned on, a delay is required. + delay(1500); + + Serial.println("LoRa Receiver"); + + LoRa.setPins(RADIO_CS_PIN, RADIO_RST_PIN, RADIO_DIO0_PIN); + if (!LoRa.begin(LoRa_frequency)) { + Serial.println("Starting LoRa failed!"); + while (1); + } } -void loop() { - program->loop(); +void loop() +{ + // try to parse packet + int packetSize = LoRa.parsePacket(); + if (packetSize) { + // received a packet + Serial.print("Received packet '"); + + String recv = ""; + // read packet + while (LoRa.available()) { + recv += (char)LoRa.read(); + } + + Serial.println(recv); + + // print RSSI of packet + Serial.print("' with RSSI "); + Serial.println(LoRa.packetRssi()); +#ifdef HAS_DISPLAY + if (u8g2) { + u8g2->clearBuffer(); + char buf[256]; + u8g2->drawStr(0, 12, "Received OK!"); + u8g2->drawStr(0, 26, recv.c_str()); + snprintf(buf, sizeof(buf), "RSSI:%i", LoRa.packetRssi()); + u8g2->drawStr(0, 40, buf); + snprintf(buf, sizeof(buf), "SNR:%.1f", LoRa.packetSnr()); + u8g2->drawStr(0, 56, buf); + u8g2->sendBuffer(); + } +#endif + } } + + + +// #define HAS_DISPLAY + +// #include +// #include "boards.h" + +// int counter = 0; + +// void setup() +// { +// initBoard(); +// // When the power is turned on, a delay is required. +// delay(1500); + +// Serial.println("LoRa Sender"); +// LoRa.setPins(RADIO_CS_PIN, RADIO_RST_PIN, RADIO_DIO0_PIN); +// if (!LoRa.begin(LoRa_frequency)) { +// Serial.println("Starting LoRa failed!"); +// while (1); +// } +// } + +// void loop() +// { +// Serial.print("Sending packet: "); +// Serial.println(counter); + +// // send packet +// LoRa.beginPacket(); +// LoRa.print("hello "); +// LoRa.print(counter); +// LoRa.endPacket(); + +// #ifdef HAS_DISPLAY +// if (u8g2) { +// char buf[256]; +// u8g2->clearBuffer(); +// u8g2->drawStr(0, 12, "Transmitting: OK!"); +// snprintf(buf, sizeof(buf), "Sending: %d", counter); +// u8g2->drawStr(0, 30, buf); +// u8g2->sendBuffer(); +// } +// #endif +// counter++; +// delay(10000); +// }