#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <LoRa.h>
#define BUTTON_PIN 12 // Brightness button
#define RECONNECT_PIN 14 // Reconnection button (for LoRa)
#define PWM_PIN 13 // PWM pin for adjustable brightness
// OLED ST7735 settings
#define TFT_CS 5
#define TFT_RST 17
#define TFT_DC 16
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
// Define the I2C address for SSD1306 OLED
#define SSD1306_I2C_ADDRESS 0x3C // This is typically the address of the display
// Brightness control variables
int brightnessLevel = 0;
// LoRa settings
#define LORA_SSID "YOUR_SSID"
#define LORA_PASS "YOUR_PASSWORD"
// Initialize LoRa
void setup() {
Serial.begin(9600);
// Initialize OLED display
tft.initR(INITR_BLACKTAB);
tft.setRotation(3);
tft.fillScreen(ST77XX_BLACK);
// Set button pins
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(RECONNECT_PIN, INPUT_PULLUP);
pinMode(PWM_PIN, OUTPUT);
// Initialize LoRa
if (!LoRa.begin(915E6)) {
Serial.println("LoRa initialization failed!");
while (1);
}
LoRa.setSyncWord(0xF3); // Set Helium sync word
LoRa.setTxPower(20); // Set transmission power
Serial.println("LoRa Initialized");
}
void loop() {
// Brightness button press detection
if (digitalRead(BUTTON_PIN) == LOW) {
brightnessLevel = (brightnessLevel + 1) % 6; // Cycle through brightness levels
adjustBrightness(brightnessLevel);
delay(500); // Debounce delay
}
// LoRa reconnection button press detection
if (digitalRead(RECONNECT_PIN) == LOW) {
reconnectLoRa();
delay(500); // Debounce delay
}
// LoRa receive data (RSSI, SNR, etc.)
if (LoRa.parsePacket()) {
String gatewayData = "";
while (LoRa.available()) {
gatewayData += (char)LoRa.read();
}
displayLoRaData(gatewayData);
}
}
// Adjust brightness based on the button press
void adjustBrightness(int level) {
int pwmValue = map(level, 0, 5, 0, 255); // Map brightness level to PWM value
analogWrite(PWM_PIN, pwmValue);
}
// Reconnect to LoRa module and Helium network
void reconnectLoRa() {
Serial.println("Reconnecting to LoRa network...");
LoRa.end(); // End the current LoRa session
delay(1000); // Wait for 1 second
if (!LoRa.begin(915E6)) {
Serial.println("LoRa reconnect failed!");
return;
}
Serial.println("LoRa reconnected successfully");
// Optionally, you can reset the SNR and RSSI measurements here if needed
}
// Display LoRa data (SNR, RSSI, etc.) on the OLED screen
void displayLoRaData(String data) {
tft.fillScreen(ST77XX_BLACK); // Clear the display
tft.setTextColor(ST77XX_WHITE);
tft.setTextSize(1);
tft.setCursor(0, 0);
tft.println("LoRa Gateway Info:");
tft.print("Data: ");
tft.println(data);
tft.print("Brightness Level: ");
tft.println(brightnessLevel);
}