ross_s

ESP32_TTGO_LoRa_BMP280_DS3231_Sender_LCD

Aug 7th, 2021
87
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*********
  2.   Rui Santos
  3.   Complete project details at https://RandomNerdTutorials.com/ttgo-lora32-sx1276-arduino-ide/
  4. *********/
  5.  
  6. //Libraries for LoRa
  7. #include <SPI.h>
  8. #include <LoRa.h>
  9.  
  10. //Libraries for OLED Display
  11. #include <Wire.h>
  12. #include <Adafruit_GFX.h>
  13. #include <Adafruit_SSD1306.h>
  14.  
  15. // libraries for BME/BMP 280
  16. #include <Adafruit_Sensor.h>
  17. #include <Adafruit_BME280.h>
  18.  
  19. // library for RTC DS3231
  20. #include "RTClib.h"
  21.  
  22. // buffer size for BME280 data
  23. #define BME_BUFF_SIZE 100
  24. char BMEbuff[BME_BUFF_SIZE];
  25. // define sea level pressure - used for relative pressure and altitude
  26. #define SEALEVELPRESSURE_HPA (1013.25)
  27. // delaytime for BME280 prints
  28. unsigned long delayTime;
  29.  
  30. // buffer for RTC data
  31. #define RTC_BUFF_SIZE 100
  32. char RTCbuff[RTC_BUFF_SIZE];
  33.  
  34. //define the pins used by the LoRa transceiver module
  35. #define SCK 5
  36. #define MISO 19
  37. #define MOSI 27
  38. #define SS 18
  39. #define RST 14
  40. #define DIO0 26
  41.  
  42. //433E6 for Asia
  43. //866E6 for Europe
  44. //915E6 for North America
  45. #define BAND 915E6
  46.  
  47. //OLED pins
  48. #define OLED_SDA 4
  49. #define OLED_SCL 15
  50. #define OLED_RST 16
  51. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  52. #define SCREEN_HEIGHT 64 // OLED display height, in pixels
  53.  
  54. //packet counter
  55. int counter = 0;
  56.  
  57. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RST);
  58.  
  59. Adafruit_BME280 bme; // I2C BME280 constructor
  60.  
  61. RTC_DS3231 rtc;
  62. char daysOfTheWeek[7][12] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
  63.  
  64.  
  65. void setup() {
  66.   //initialize Serial Monitor
  67.   Serial.begin(9600);
  68.  
  69.   //reset OLED display via software
  70.   pinMode(OLED_RST, OUTPUT);
  71.   digitalWrite(OLED_RST, LOW);
  72.   delay(20);
  73.   digitalWrite(OLED_RST, HIGH);
  74.  
  75.   // THIS IS THE LINE CAUSING PROBLEMS!                
  76.   Wire.begin(OLED_SDA, OLED_SCL); //initialize OLED
  77.   /*
  78.   if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3c, false, false)) { // Address 0x3C for 128x32
  79.     Serial.println(F("SSD1306 allocation failed"));
  80.     for(;;); // Don't proceed, loop forever
  81.   }
  82.   */
  83.   /*
  84.   display.clearDisplay();
  85.   display.setTextColor(WHITE);
  86.   display.setTextSize(1);
  87.   display.setCursor(0,0);
  88.   display.print("LORA SENDER ");
  89.   display.display();
  90.   */
  91.   Serial.println("LoRa Sender Test");
  92.  
  93.   //SPI LoRa pins
  94.   SPI.begin(SCK, MISO, MOSI, SS);
  95.   //setup LoRa transceiver module
  96.   LoRa.setPins(SS, RST, DIO0);
  97.  
  98.   if (!LoRa.begin(BAND)) {
  99.     Serial.println("Starting LoRa failed!");
  100.     while (1);
  101.   }
  102.   Serial.println("LoRa Initializing OK!");
  103.   //display.setCursor(0,10);
  104.   //display.print("LoRa Initializing OK!");
  105.   //display.display();
  106.   delay(2000);
  107.  
  108.   setBMP280Address(0x76);
  109.  
  110.   if (! rtc.begin()) {
  111.     Serial.println("Couldn't find RTC");
  112.     while (1);
  113.   }
  114.   rtc.adjust(DateTime(__DATE__, __TIME__));
  115. }
  116.  
  117. void loop() {
  118.    
  119.   Serial.print("Sending packet: ");
  120.  
  121.   //Send LoRa packet to receiver
  122.   LoRa.beginPacket();
  123.   LoRa.print(RTCbuff);
  124.   LoRa.print("\r\n");
  125.   LoRa.print(BMEbuff);
  126.   LoRa.endPacket();
  127.   /*
  128.   display.clearDisplay();
  129.   display.setCursor(0,0);
  130.   display.println("LORA SENDER");
  131.   display.setCursor(0,20);
  132.   display.setTextSize(1);
  133.   display.print("LoRa packet sent.");
  134.   display.setCursor(0,30);
  135.   display.print("Counter:");
  136.   display.setCursor(50,30);
  137.   display.print(counter);      
  138.   display.display();
  139. */
  140.   printBME();
  141.   printRTC();
  142.   delay(delayTime);
  143. }
  144.  
  145.  
  146. void setBMP280Address(uint8_t address) {
  147.   unsigned status;
  148.  
  149.   // default settings
  150.   status = bme.begin(address);
  151.   // You can also pass in a Wire library object like &Wire2
  152.   // status = bme.begin(0x76, &Wire2)
  153.   if (!status) {
  154.     Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
  155.     Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(), 16);
  156.     Serial.print("        ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
  157.     Serial.print("   ID of 0x56-0x58 represents a BMP 280,\n");
  158.     Serial.print("        ID of 0x60 represents a BME 280.\n");
  159.     Serial.print("        ID of 0x61 represents a BME 680.\n");
  160.     while (1) delay(10);
  161.   }
  162.  
  163.   Serial.println("-- Default Test --");
  164.   Serial.print("SensorID is: 0x"); Serial.println(bme.sensorID(), 16);
  165.   delayTime = 1000;
  166.  
  167.   Serial.println();
  168. }
  169.  
  170. void printBME() {
  171.   for (int i = 0; i < BME_BUFF_SIZE; i++) {
  172.     snprintf(BMEbuff, sizeof(BMEbuff), "T:%.1f C\r\nH:%.1f %%\r\nP:%.1f HPa\r\n", //A:%.2f\n
  173.     bme.readTemperature(), bme.readHumidity(), (bme.readPressure() / 100.0F));//,
  174.     //bme.readAltitude(SEALEVELPRESSURE_HPA));
  175.   }
  176.   Serial.print(BMEbuff);
  177. }
  178.  
  179. void printRTC(void) {
  180.   DateTime now = rtc.now();
  181.   Serial.print("Current Time: ");
  182.   Serial.print(now.hour());
  183.   Serial.print(":");
  184.   Serial.println(now.minute());
  185.   Serial.print("Todays Date: ");
  186.   Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
  187.   Serial.print(" ");
  188.   Serial.print(now.day());
  189.   Serial.print("/");
  190.   Serial.print(now.month());
  191.   Serial.print("/");
  192.   Serial.println(now.year());
  193.  
  194.  
  195.   for (int i = 0; i < RTC_BUFF_SIZE; i++) {
  196.     snprintf(RTCbuff, sizeof(RTCbuff), "%s, %d/%d/%d %d:%d",
  197.     daysOfTheWeek[now.dayOfTheWeek()], now.day(), now.month(),
  198.     now.year(), now.hour(), now.minute());
  199.   }
  200.   Serial.print(RTCbuff);
  201. }
RAW Paste Data