Files
2025-12-30 23:09:23 +01:00

128 lines
3.1 KiB
C++

#include <heltec.h>
#include "Arduino.h"
#include <Wire.h>
#include <SPI.h>
#include "LoRaWan_APP.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET 21
#define OLED_SDA 17
#define OLED_SCL 18
#define LIGHT_SENSOR_PIN 1
#define RF_FREQUENCY 868100000
#define TX_OUTPUT_POWER 5
#define LORA_BANDWIDTH 0
#define LORA_SPREADING_FACTOR 7
#define LORA_CODINGRATE 1
#define LORA_PREAMBLE_LENGTH 8
#define LORA_SYMBOL_TIMEOUT 0
#define LORA_FIX_LENGTH_PAYLOAD_ON false
#define LORA_IQ_INVERSION_ON false
#define BUFFER_SIZE 50
char txpacket[BUFFER_SIZE];
bool lora_idle = true;
static RadioEvents_t RadioEvents;
void OnTxDone(void);
void OnTxTimeout(void);
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(115200);
Mcu.begin(HELTEC_BOARD, SLOW_CLK_TPYE);
RadioEvents.TxDone = OnTxDone;
RadioEvents.TxTimeout = OnTxTimeout;
Radio.Init(&RadioEvents);
Radio.SetChannel(RF_FREQUENCY);
Radio.SetTxConfig(MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
LORA_SPREADING_FACTOR, LORA_CODINGRATE,
LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
true, 0, 0, LORA_IQ_INVERSION_ON, 3000);
Wire.begin(OLED_SDA, OLED_SCL);
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("Échec de l'initialisation OLED"));
while(true);
}
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(SSD1306_WHITE);
oled.setCursor(0, 10);
oled.println("Hey you ! <3" );
oled.display();
}
void loop() {
if (lora_idle) {
delay(5000);
int sensorValue = analogRead(LIGHT_SENSOR_PIN);
float percentile = (sensorValue * 100.0) / 2430.0;
char percentileLine[30];
sprintf(percentileLine, "%.1f%%", percentile);
char adviceLine[30];
if (percentile > 75) {
strcpy(adviceLine, "Too bright");
} else if (percentile < 25) {
strcpy(adviceLine, "Too dark !");
} else {
strcpy(adviceLine, "I'm fine :)");
}
const char* lines[] = {
"Sensor value",
percentileLine,
adviceLine
};
displayMultipleMessages(lines, 3);
// Préparer le paquet JSON
snprintf(txpacket, BUFFER_SIZE, "{\"lightSensorValue\": %d}", sensorValue);
Serial.printf("\r\nSending packet: %s\n", txpacket);
Radio.Send((uint8_t*)txpacket, strlen(txpacket));
lora_idle = false;
}
Radio.IrqProcess();
}
void displayMultipleMessages(const char* messages[], int numLines) {
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(SSD1306_WHITE);
int lineHeight = 16;
for (int i = 0; i < numLines; i++) {
oled.setCursor(0, i * lineHeight);
oled.println(messages[i]);
}
oled.display();
}
void OnTxDone(void) {
Serial.println("TX done...");
lora_idle = true;
}
void OnTxTimeout(void) {
Radio.Sleep();
Serial.println("TX Timeout...");
lora_idle = true;
}