Advertisement
fingy

LoRaWan BME280

Feb 14th, 2021
648
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.39 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. #include <Adafruit_Sensor.h>
  10. #include <Adafruit_BME280.h>
  11.  
  12. //Libraries for OLED Display
  13. #include <Wire.h>
  14. #include <Adafruit_GFX.h>
  15. #include <Adafruit_SSD1306.h>
  16.  
  17. //define the pins used by the LoRa transceiver module
  18. #define SCK 5
  19. #define MISO 19
  20. #define MOSI 27
  21. #define SS 18
  22. #define RST 14
  23. #define DIO0 26
  24.  
  25. #define SEALEVELPRESSURE_HPA (1013.25)
  26. Adafruit_BME280 bme ; // = Adafruit_BME280(0x76, &Wire1)
  27. float temperature, humidity, pressure, altitude;
  28.  
  29. //433E6 for Asia
  30. //866E6 for Europe
  31. //915E6 for North America
  32. #define BAND 915E6
  33.  
  34. //OLED pins
  35. #define OLED_SDA 4
  36. #define OLED_SCL 15
  37. #define BME_SDA 21
  38. #define BME_SCL 22
  39. #define OLED_RST 16
  40. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  41. #define SCREEN_HEIGHT 64 // OLED display height, in pixels
  42.  
  43.  
  44.  
  45.  
  46. //packet counter
  47. int counter = 0;
  48.  
  49. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RST);
  50.  
  51. void setup() {
  52.  
  53.   //reset OLED display via software
  54.   pinMode(OLED_RST, OUTPUT);
  55.   digitalWrite(OLED_RST, LOW);
  56.   delay(20);
  57.   digitalWrite(OLED_RST, HIGH);
  58.  
  59.  Wire1.begin(BME_SDA, BME_SCL);
  60.   bme.begin();
  61.  //
  62.  
  63.   //initialize OLED
  64.   Wire.begin(OLED_SDA, OLED_SCL);
  65.   if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3c, false, false)) { // Address 0x3C for 128x32
  66.     Serial.println(F("SSD1306 allocation failed"));
  67.     for(;;); // Don't proceed, loop forever
  68.  
  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.   //initialize Serial Monitor
  79.   Serial.begin(9600);
  80.  
  81.   Serial.println("LoRa Sender Test");
  82.  
  83.   //SPI LoRa pins
  84.   SPI.begin(SCK, MISO, MOSI, SS);
  85.   //setup LoRa transceiver module
  86.   LoRa.setPins(SS, RST, DIO0);
  87.  
  88.   if (!LoRa.begin(BAND)) {
  89.     Serial.println("Starting LoRa failed!");
  90.     while (1);
  91.   }
  92.   Serial.println("LoRa Initializing OK!");
  93.   display.setCursor(0,10);
  94.   display.print("LoRa Initializing OK!");
  95.   display.display();
  96.   delay(2000);
  97. }
  98.  
  99. void loop() {
  100.  
  101.   temperature = bme.readTemperature();
  102.   humidity = bme.readHumidity();
  103.   pressure = bme.readPressure() / 100.0F;
  104.   altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);
  105.   Serial.print("Temperature = ");
  106.   Serial.println(temperature);
  107.   Serial.print("Humidite = ");
  108.   Serial.println(humidity);
  109.   Serial.print("Pression = ");
  110.   Serial.println(pressure);
  111.   Serial.print("Altitude = ");
  112.   Serial.println(altitude);
  113.    
  114.   Serial.print("Sending packet: ");
  115.   Serial.println(counter);
  116.  
  117.   //Send LoRa packet to receiver
  118.   LoRa.beginPacket();
  119.   LoRa.print("hello ");
  120.   LoRa.println(counter);
  121.   LoRa.print("Temperature ; ");
  122.   LoRa.println(temperature);
  123.   LoRa.endPacket();
  124.  
  125.   display.clearDisplay();
  126.   display.setCursor(0,0);
  127.   display.println("LORA SENDER");
  128.   display.setCursor(0,20);
  129.   display.setTextSize(1);
  130.   display.print("LoRa packet sent.");
  131.   display.setCursor(0,30);
  132.   display.print("Counter:");
  133.   display.setCursor(50,30);
  134.   display.print(counter);      
  135.   display.display();
  136.   display.setCursor(0,40);
  137.   display.print("Temperature :");
  138.   display.setCursor(80,40);
  139.   display.print(temperature);      
  140.   display.display();
  141.  
  142.   counter++;
  143.  
  144.   delay(10000);
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement