Makerfabs / Lora-Soil-Moisture-Sensor

Lora Soil Moisture Sensor
54 stars 23 forks source link

Code for Lilygo TTGO Lora32 ESP Module as a receiver #10

Open martin3000 opened 2 years ago

martin3000 commented 2 years ago

/***** Rui Santos Complete project details at https://RandomNerdTutorials.com/ttgo-lora32-sx1276-arduino-ide/ needs LoRa library be sandeep mistry

lora32-receiver https://github.com/sandeepmistry/arduino-LoRa/blob/master/API.md https://github.com/sandeepmistry/arduino-LoRa
*****/

//Libraries for LoRa

include

include

//Libraries for OLED Display

include

include

include

//define the pins used by the LoRa transceiver module

define SCK 5

define MISO 19

define MOSI 27

define SS 18

define RST 14

define DIO0 26

//433E6 for Asia //866E6 for Europe //915E6 for North America

define BAND 866E6

//OLED pins

define OLED_SDA 4

define OLED_SCL 15

define OLED_RST 16

define SCREEN_WIDTH 128 // OLED display width, in pixels

define SCREEN_HEIGHT 64 // OLED display height, in pixels

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RST);

String LoRaData;

void setup() {
//reset OLED display via software pinMode(OLED_RST, OUTPUT); digitalWrite(OLED_RST, LOW); delay(20); digitalWrite(OLED_RST, HIGH);

//initialize OLED Wire.begin(OLED_SDA, OLED_SCL); if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3c, false, false)) { // Address 0x3C for 128x32 Serial.println(F("SSD1306 allocation failed")); for(;;); // Don't proceed, loop forever }

display.clearDisplay(); display.setTextColor(WHITE); display.setTextSize(1); display.setCursor(0,0); display.print("LORA RECEIVER "); display.display();

//initialize Serial Monitor Serial.begin(115200);

//SPI LoRa pins SPI.begin(SCK, MISO, MOSI, SS); //setup LoRa transceiver module LoRa.setPins(SS, RST, DIO0);

if (!LoRa.begin(BAND)) { Serial.println("Starting LoRa failed!"); while (1); } LoRa.setSpreadingFactor(9 /SF9/);

//Serial.println("LoRa Initializing OK!"); display.setCursor(0,10); display.println("LoRa Initializing OK!"); display.display();
}

void loop() {

//try to parse packet int packetSize = LoRa.parsePacket();

if (packetSize) { //received a packet Serial.println("Received packet: ");

LoRaData = "";

while (LoRa.available()) {
  LoRaData += (char)LoRa.read();
}

Serial.println("Message: " + LoRaData);
Serial.println("RSSI: " + String(LoRa.packetRssi()));
Serial.println("Snr: " + String(LoRa.packetSnr()));
Serial.println();

// Dsiplay information
display.clearDisplay();
display.setCursor(0,0);
display.print("LORA RECEIVER");
display.setCursor(0,20);
display.print("Received packet:");
display.setCursor(0,30);
display.print(LoRaData);
display.setCursor(0,40);
display.print("RSSI:");
display.setCursor(30,40);
display.print(LoRa.packetRssi());
display.display();   

} }