Modified the quick and dirty firmware to add the support for a LoRa module (RFM95W). This is a poof of concept and needs to be refined.

This commit is contained in:
Th3maz1ng 2025-10-28 19:27:15 +01:00 committed by anschrammh
parent 628a38d6c2
commit 19d71b8735

View File

@ -9,10 +9,11 @@
#include <PCF8574.h>
#include <HttpClient.h>
#include "RF24.h"
#include <LoRa.h>
#include "definition.h"
#include "credentials.h"
/* NRF pinout definition */
#define NRF_1_CE 15 //chip enable
#define NRF_2_CE 16 //chip enable
@ -24,6 +25,16 @@
#define WIFI_CHECK_TIMEOUT 20000
#define PREVENTIVE_RESET_DELAY 18000000 //Every 5 hours
/* LoRa module pinout definition (using the IO expander) */
#define LORA_CS_PIN (PCF8574::P5)
#define LORA_RST_PIN (PCF8574::P6)
#define LORA_DI0_PIN (PCF8574::P7)
#define LORA_BAND (868E6)
/* PCF8574 IO expander pinout */
#define PCF_INT_PIN (D9)
uint8_t payload[32] = {0};
DataPacket dp;
WeatherStationDataPacket wsdp;
@ -48,21 +59,49 @@ int insertIntoDBError(0);
volatile boolean IRQFlag(false);
boolean waitingForResetSignal(false), resetNow(false);
IRAM_ATTR void NRFIRQsHandler(void *p)
void PCFPinMode(uint8_t pin, uint8_t mode)
{
Serial.println("NRF IRQs detected !");
*(boolean *)p = true;
PCF.pinMode(static_cast<PCF8574::Pin>(pin), static_cast<boolean>(mode));
}
void setup() {
void PCFDigitalWrite(uint8_t pin, uint8_t val)
{
PCF.digitalWrite(static_cast<PCF8574::Pin>(pin), static_cast<boolean>(val));
}
void onLoRaReceive(int payload_size)
{
PCF.digitalWrite(PCF8574::P2, LOW);
Serial.printf("LoRa packet size : %d\n", payload_size);
for(int i = 0; i < payload_size; i++)
{
Serial.printf("%c", LoRa.read());
}
Serial.printf("\nPacket RSSI : %d dBm\n", LoRa.packetRssi());
PCF.digitalWrite(PCF8574::P2, HIGH);
}
IRAM_ATTR void PCFIRQHandler(void *p)
{
Serial.println("PCF IRQs detected !");
bool pinStates[8];
PCF.digitalReadAll(pinStates);
*(boolean *)p = true;
/* If the IRQ pin is the DI0 one, manually call the LoRa IRQ handler */
if(pinStates[7] == HIGH)
{
LoRa.handleDio0Rise();
}
}
void setup()
{
//We do not need to read on the serial bus
Serial.begin(115200, SERIAL_8N1, SERIAL_TX_ONLY);
delay(1000);
Serial.println("\nSetup begin");
//We set the RXD0 as a GPIO
pinMode(D9, FUNCTION_3);
pinMode(D9, INPUT_PULLUP);
attachInterruptArg(D9,&(NRFIRQsHandler), (void *)&IRQFlag, FALLING);
//We set the WiFi part up :
gotIp = WiFi.onStationModeGotIP(&(gotIpFunc));
lostConnection = WiFi.onStationModeDisconnected(&(lostConnectionFunc));
@ -124,6 +163,24 @@ void setup() {
PCF.pinMode(PCF8574::P2, OUTPUT);
PCF.pinMode(PCF8574::P3, OUTPUT);
PCF.pinMode(PCF8574::P4, OUTPUT);
/* Configuring LoRa module and RF settings */
LoRa.setPins(LORA_CS_PIN, LORA_RST_PIN, LORA_DI0_PIN);
LoRa.setCustomGPIOFn(&(PCFPinMode), &(PCFDigitalWrite));
Serial.printf("LoRa module %s\n", LoRa.begin(LORA_BAND) ? "found" : "not found");
Serial.println("######### LoRa regs : #########");
LoRa.dumpRegisters(Serial);
Serial.println("###############################");
LoRa.setSpreadingFactor(10);
LoRa.enableCrc();
LoRa.onReceive(&(onLoRaReceive));
/* Let's start listening to LoRa packets */
LoRa.receive();
//We set the RXD0 as a GPIO
pinMode(PCF_INT_PIN, INPUT_PULLUP);
attachInterruptArg(PCF_INT_PIN, &(PCFIRQHandler), (void *)&IRQFlag, ONLOW /*FALLING*/);
Serial.println("End setup");
}