Advertisement
ross_s

TTGO_LORA_ESP#@_BME280_DS3231_Sender

Aug 7th, 2021
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.12 KB | None | 0 0
  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.   //initialize OLED
  76.   //Wire.begin(OLED_SDA, OLED_SCL);
  77.   //if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3c, false, false)) { // Address 0x3C for 128x32
  78.   //  Serial.println(F("SSD1306 allocation failed"));
  79.   //  for(;;); // Don't proceed, loop forever
  80.   //}
  81.  
  82.   //display.clearDisplay();
  83.   //display.setTextColor(WHITE);
  84.   //display.setTextSize(1);
  85.   //display.setCursor(0,0);
  86.   //display.print("LORA SENDER ");
  87.   //display.display();
  88.  
  89.   Serial.println("LoRa Sender Test");
  90.  
  91.   //SPI LoRa pins
  92.   SPI.begin(SCK, MISO, MOSI, SS);
  93.   //setup LoRa transceiver module
  94.   LoRa.setPins(SS, RST, DIO0);
  95.  
  96.   if (!LoRa.begin(BAND)) {
  97.     Serial.println("Starting LoRa failed!");
  98.     while (1);
  99.   }
  100.   Serial.println("LoRa Initializing OK!");
  101.   //display.setCursor(0,10);
  102.   //display.print("LoRa Initializing OK!");
  103.   //display.display();
  104.   delay(2000);
  105.  
  106.   setBMP280Address(0x76);
  107.  
  108.   if (! rtc.begin()) {
  109.     Serial.println("Couldn't find RTC");
  110.     //while (1);
  111.   }
  112.   rtc.adjust(DateTime(__DATE__, __TIME__));
  113. }
  114.  
  115. void loop() {
  116.    
  117.   Serial.print("Sending packet: ");
  118.  
  119.   //Send LoRa packet to receiver
  120.   LoRa.beginPacket();
  121.   LoRa.print(RTCbuff);
  122.   LoRa.print("\r\n");
  123.   LoRa.print(BMEbuff);
  124.   LoRa.endPacket();
  125.   /*
  126.   display.clearDisplay();
  127.   display.setCursor(0,0);
  128.   display.println("LORA SENDER");
  129.   display.setCursor(0,20);
  130.   display.setTextSize(1);
  131.   display.print("LoRa packet sent.");
  132.   display.setCursor(0,30);
  133.   display.print("Counter:");
  134.   display.setCursor(50,30);
  135.   display.print(counter);      
  136.   display.display();
  137. */
  138.   printBME();
  139.   printRTC();
  140.   delay(delayTime);
  141. }
  142.  
  143.  
  144. void setBMP280Address(uint8_t address) {
  145.   unsigned status;
  146.  
  147.   // default settings
  148.   status = bme.begin(address);
  149.   // You can also pass in a Wire library object like &Wire2
  150.   // status = bme.begin(0x76, &Wire2)
  151.   if (!status) {
  152.     Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
  153.     Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(), 16);
  154.     Serial.print("        ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
  155.     Serial.print("   ID of 0x56-0x58 represents a BMP 280,\n");
  156.     Serial.print("        ID of 0x60 represents a BME 280.\n");
  157.     Serial.print("        ID of 0x61 represents a BME 680.\n");
  158.     while (1) delay(10);
  159.   }
  160.  
  161.   Serial.println("-- Default Test --");
  162.   Serial.print("SensorID is: 0x"); Serial.println(bme.sensorID(), 16);
  163.   delayTime = 1000;
  164.  
  165.   Serial.println();
  166. }
  167.  
  168. void printBME() {
  169.   for (int i = 0; i < BME_BUFF_SIZE; i++) {
  170.     snprintf(BMEbuff, sizeof(BMEbuff), "T:%.2fC\r\n H:%.2f%%\r\n P:%.2fHPa\r\n", //A:%.2f\n
  171.     bme.readTemperature(), bme.readHumidity(), (bme.readPressure() / 100.0F));//,
  172.     //bme.readAltitude(SEALEVELPRESSURE_HPA));
  173.   }
  174.   Serial.print(BMEbuff);
  175. }
  176.  
  177. void printRTC(void) {
  178.   DateTime now = rtc.now();
  179.   Serial.print("Current Time: ");
  180.   Serial.print(now.hour());
  181.   Serial.print(":");
  182.   Serial.println(now.minute());
  183.   Serial.print("Todays Date: ");
  184.   Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
  185.   Serial.print(" ");
  186.   Serial.print(now.day());
  187.   Serial.print("/");
  188.   Serial.print(now.month());
  189.   Serial.print("/");
  190.   Serial.println(now.year());
  191.  
  192.  
  193.   for (int i = 0; i < RTC_BUFF_SIZE; i++) {
  194.     snprintf(RTCbuff, sizeof(RTCbuff), "%s, %d/%d/%d %d:%d",
  195.     daysOfTheWeek[now.dayOfTheWeek()], now.day(), now.month(),
  196.     now.year(), now.hour(), now.minute());
  197.   }
  198.   Serial.print(RTCbuff);
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement