Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*********
- Rui Santos
- Complete project details at https://RandomNerdTutorials.com/ttgo-lora32-sx1276-arduino-ide/
- *********/
- //Libraries for LoRa
- #include <SPI.h>
- #include <LoRa.h>
- //Libraries for OLED Display
- #include <Wire.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- // libraries for BME/BMP 280
- #include <Adafruit_Sensor.h>
- #include <Adafruit_BME280.h>
- // library for RTC DS3231
- #include "RTClib.h"
- // buffer size for BME280 data
- #define BME_BUFF_SIZE 100
- char BMEbuff[BME_BUFF_SIZE];
- // define sea level pressure - used for relative pressure and altitude
- #define SEALEVELPRESSURE_HPA (1013.25)
- // delaytime for BME280 prints
- unsigned long delayTime;
- // buffer for RTC data
- #define RTC_BUFF_SIZE 100
- char RTCbuff[RTC_BUFF_SIZE];
- //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 915E6
- //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
- //packet counter
- int counter = 0;
- //Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RST);
- Adafruit_BME280 bme; // I2C BME280 constructor
- RTC_DS3231 rtc;
- char daysOfTheWeek[7][12] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
- void setup() {
- //initialize Serial Monitor
- Serial.begin(9600);
- //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 SENDER ");
- //display.display();
- Serial.println("LoRa Sender Test");
- //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);
- }
- Serial.println("LoRa Initializing OK!");
- //display.setCursor(0,10);
- //display.print("LoRa Initializing OK!");
- //display.display();
- delay(2000);
- setBMP280Address(0x76);
- if (! rtc.begin()) {
- Serial.println("Couldn't find RTC");
- //while (1);
- }
- rtc.adjust(DateTime(__DATE__, __TIME__));
- }
- void loop() {
- Serial.print("Sending packet: ");
- //Send LoRa packet to receiver
- LoRa.beginPacket();
- LoRa.print(RTCbuff);
- LoRa.print("\r\n");
- LoRa.print(BMEbuff);
- LoRa.endPacket();
- /*
- display.clearDisplay();
- display.setCursor(0,0);
- display.println("LORA SENDER");
- display.setCursor(0,20);
- display.setTextSize(1);
- display.print("LoRa packet sent.");
- display.setCursor(0,30);
- display.print("Counter:");
- display.setCursor(50,30);
- display.print(counter);
- display.display();
- */
- printBME();
- printRTC();
- delay(delayTime);
- }
- void setBMP280Address(uint8_t address) {
- unsigned status;
- // default settings
- status = bme.begin(address);
- // You can also pass in a Wire library object like &Wire2
- // status = bme.begin(0x76, &Wire2)
- if (!status) {
- Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
- Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(), 16);
- Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
- Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n");
- Serial.print(" ID of 0x60 represents a BME 280.\n");
- Serial.print(" ID of 0x61 represents a BME 680.\n");
- while (1) delay(10);
- }
- Serial.println("-- Default Test --");
- Serial.print("SensorID is: 0x"); Serial.println(bme.sensorID(), 16);
- delayTime = 1000;
- Serial.println();
- }
- void printBME() {
- for (int i = 0; i < BME_BUFF_SIZE; i++) {
- snprintf(BMEbuff, sizeof(BMEbuff), "T:%.2fC\r\n H:%.2f%%\r\n P:%.2fHPa\r\n", //A:%.2f\n
- bme.readTemperature(), bme.readHumidity(), (bme.readPressure() / 100.0F));//,
- //bme.readAltitude(SEALEVELPRESSURE_HPA));
- }
- Serial.print(BMEbuff);
- }
- void printRTC(void) {
- DateTime now = rtc.now();
- Serial.print("Current Time: ");
- Serial.print(now.hour());
- Serial.print(":");
- Serial.println(now.minute());
- Serial.print("Todays Date: ");
- Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
- Serial.print(" ");
- Serial.print(now.day());
- Serial.print("/");
- Serial.print(now.month());
- Serial.print("/");
- Serial.println(now.year());
- for (int i = 0; i < RTC_BUFF_SIZE; i++) {
- snprintf(RTCbuff, sizeof(RTCbuff), "%s, %d/%d/%d %d:%d",
- daysOfTheWeek[now.dayOfTheWeek()], now.day(), now.month(),
- now.year(), now.hour(), now.minute());
- }
- Serial.print(RTCbuff);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement