Advertisement
ross_s

ESP32_TTGO_LoRa_BMP280

Aug 7th, 2021
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.08 KB | None | 0 0
  1. /*********
  2. Rui Santos
  3. Complete project details at https://RandomNerdTutorials.com/ttgo-lora32-sx1276-arduino-ide/
  4. *********/
  5. //Libraries for LoRa
  6. #include <SPI.h>
  7. #include <LoRa.h>
  8. #include <Wire.h>
  9. #include <SPI.h>
  10. #include <Adafruit_Sensor.h>
  11. #include <Adafruit_BME280.h>
  12. //Libraries for OLED Display
  13. #include <Wire.h>
  14. #include <Adafruit_GFX.h>
  15. #include <Adafruit_SSD1306.h>
  16. #define BUFF_SIZE 100 // define buffer size for BME280 data
  17. // define used to calculate relative atmospheric pressure readings
  18. #define SEALEVELPRESSURE_HPA (1013.25)
  19. //define the pins used by the LoRa transceiver module
  20. #define SCK 5
  21. #define MISO 19
  22. #define MOSI 27
  23. #define SS 18
  24. #define RST 14
  25. #define DIO0 26
  26. //433E6 for Asia
  27. //866E6 for Europe
  28. //915E6 for North America
  29. #define BAND 915E6
  30. //OLED pins
  31. #define OLED_SDA 4
  32. #define OLED_SCL 15
  33. #define OLED_RST 16
  34. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  35. #define SCREEN_HEIGHT 64 // OLED display height, in pixels
  36. //packet counter
  37. int counter = 0;
  38. Adafruit_BME280 bme; // I2C BME atmospheric sensor
  39. unsigned long delayTime;
  40. char BME280_buff[BUFF_SIZE]; // buffer for BME280 data
  41. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RST);
  42. void setup() {
  43. //initialize Serial Monitor
  44. Serial.begin(115200);
  45. //reset OLED display via software
  46. pinMode(OLED_RST, OUTPUT);
  47. digitalWrite(OLED_RST, LOW);
  48. delay(20);
  49. digitalWrite(OLED_RST, HIGH);
  50. //initialize OLED
  51. Wire.begin(OLED_SDA, OLED_SCL);
  52. if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3c, false, false)) { // Address 0x3C for 128x32
  53. Serial.println(F("SSD1306 allocation failed"));
  54. for(;;); // Don't proceed, loop forever
  55. }
  56. unsigned status;
  57. // default settings
  58. status = bme.begin(0x76); //(0x76);
  59. // You can also pass in a Wire library object like &Wire2
  60. // status = bme.begin(0x76, &Wire2)
  61. if (!status) {
  62. Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
  63. Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(), 16);
  64. Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
  65. Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n");
  66. Serial.print(" ID of 0x60 represents a BME 280.\n");
  67. Serial.print(" ID of 0x61 represents a BME 680.\n");
  68. while (1) delay(10);
  69. }
  70.  
  71. display.clearDisplay();
  72. display.setTextColor(WHITE);
  73. display.setTextSize(1);
  74. display.setCursor(0,0);
  75. display.print("LORA SENDER ");
  76. display.display();
  77.  
  78. Serial.println("LoRa Sender Test");
  79. //SPI LoRa pins
  80. SPI.begin(SCK, MISO, MOSI, SS);
  81. //setup LoRa transceiver module
  82. LoRa.setPins(SS, RST, DIO0);
  83.  
  84. if (!LoRa.begin(BAND)) {
  85. Serial.println("Starting LoRa failed!");
  86. while (1);
  87. }
  88. Serial.println("LoRa Initializing OK!");
  89. display.setCursor(0,10);
  90. display.print("LoRa Initializing OK!");
  91. display.display();
  92. delay(2000);
  93. }
  94. void loop() {
  95.  
  96. Serial.print("Sending packet: ");
  97. Serial.println(counter);
  98. //Send LoRa packet to receiver
  99. LoRa.beginPacket();
  100. LoRa.print("hello sailor! ");
  101. LoRa.print(counter);
  102. LoRa.endPacket();
  103.  
  104. display.clearDisplay();
  105. display.setCursor(0,0);
  106. display.println("LORA SENDER");
  107. display.setCursor(0,20);
  108. display.setTextSize(1);
  109. display.print("LoRa packet sent.");
  110. display.setCursor(0,30);
  111. display.print("Counter:");
  112. display.setCursor(50,30);
  113. display.print(counter);
  114. display.display();
  115. counter++;
  116.  
  117. delay(10000);
  118. }
  119.  
  120. void setBME_Address(uint8_t address) {
  121. unsigned status;
  122. // default settings
  123. status = bme.begin(address); //(0x76);
  124. // You can also pass in a Wire library object like &Wire2
  125. // status = bme.begin(0x76, &Wire2)
  126. if (!status) {
  127. Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
  128. Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(), 16);
  129. Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
  130. Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n");
  131. Serial.print(" ID of 0x60 represents a BME 280.\n");
  132. Serial.print(" ID of 0x61 represents a BME 680.\n");
  133. while (1) delay(10);
  134. }
  135. Serial.println("-- Default Test --");
  136. Serial.print("SensorID is: 0x"); Serial.println(bme.sensorID(), 16);
  137. delayTime = 1000;
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement