Skip to content

Instantly share code, notes, and snippets.

@tobozo
Last active August 4, 2023 09:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobozo/c7d5436f3bc78f0df0143b5955f441ed to your computer and use it in GitHub Desktop.
Save tobozo/c7d5436f3bc78f0df0143b5955f441ed to your computer and use it in GitHub Desktop.
Heltec Lora32 Wifi + BME POC
#include <SPI.h>
#include <LoRa.h>
#include "SSD1306.h"
#include<Arduino.h>
#include "Adafruit_BME280.h" // from https://github.com/Takatsuki0204/BME280-I2C-ESP32
#define I2C_SDA 4
#define I2C_SCL 15
#define SEALEVELPRESSURE_HPA (1013.25)
#define BME280_ADD 0x76
Adafruit_BME280 bme(I2C_SDA, I2C_SCL);
//OLED pins to ESP32 GPIOs via this connecthin:
//OLED_SDA -- GPIO4
//OLED_SCL -- GPIO15
//OLED_RST -- GPIO16
SSD1306 display(0x3c, 4, 15);
// WIFI_LoRa_32 ports
// GPIO5 -- SX1278's SCK
// GPIO19 -- SX1278's MISO
// GPIO27 -- SX1278's MOSI
// GPIO18 -- SX1278's CS
// GPIO14 -- SX1278's RESET
// GPIO26 -- SX1278's IRQ(Interrupt Request)
#define SS 18
#define RST 14
#define DI0 26
#define BAND 433E6 //915E6
int counter = 0;
void I2Cscan() {
byte error;
byte address;
int numberOfDevices = 0;
String addressMessage = "";
Serial.println("Scanning...\n");
for (address = 1; address < 127; address++) {
// The i2c_scanner uses the return value of
// Write.endTransmisstion to see if
// a device acknowledged at that address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (0 == error) {
addressMessage = "I2C device found at address 0x";
if (address < 16) {
addressMessage += "0";
}
addressMessage += String(address);
addressMessage += "!";
Serial.printf("%s\n", addressMessage.c_str());
Serial.println();
numberOfDevices++;
} else if (4 == error) {
addressMessage = "Unknown error at address 0x";
if (address < 16) {
addressMessage += "0";
}
addressMessage += String(address);
Serial.printf("%s\n", addressMessage.c_str());
Serial.println();
} // if (0 == error)
} // for (address = 1...)
if (0 == numberOfDevices)
Serial.println("No I2C devices found.\n");
else
Serial.println("Done\n");
}
void getValues() {
Serial.print("Temperature = ");
Serial.print(bme.readTemperature());
Serial.println(" ℃");
Serial.print("Pressure = ");
Serial.print(bme.readPressure() / 100.0F);
Serial.println(" hPa");
Serial.print("Approx. Altitude = ");
Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
Serial.println(" m");
Serial.print("Humidity = ");
Serial.print(bme.readHumidity());
Serial.println(" %");
Serial.println();
}
void setup() {
pinMode(25,OUTPUT); //Send success, LED will bright 1 second
pinMode(16,OUTPUT);
digitalWrite(16, LOW); // set GPIO16 low to reset OLED
delay(50);
digitalWrite(16, HIGH);
Serial.begin(115200);
while (!Serial); //If just the the basic function, must connect to a computer
// Initialising the UI will init the display too.
display.init();
I2Cscan();
display.flipScreenVertically();
display.setFont(ArialMT_Plain_10);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.drawString(5,5,"LoRa Sender");
display.display();
SPI.begin(5,19,27,18);
LoRa.setPins(SS,RST,DI0);
Serial.println("LoRa Sender");
if (!LoRa.begin(BAND)) {
Serial.println("Starting LoRa failed!");
while (1);
}
Serial.println("LoRa Initial OK!");
display.drawString(5,20,"LoRa Initializing OK!");
display.display();
delay(2000);
bool status;
status = bme.begin(BME280_ADD);
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
//while (1);
}
}
void loop() {
Serial.print("Sending packet: ");
Serial.println(counter);
display.clear();
display.setFont(ArialMT_Plain_16);
display.drawString(3, 5, "Sending packet ");
display.drawString(50, 30, String(counter));
display.display();
// send packet
LoRa.beginPacket();
LoRa.print("Hello..");
LoRa.print(counter);
LoRa.endPacket();
counter++;
digitalWrite(25, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(25, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
delay(3000);
getValues();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment