Skip to content

Instantly share code, notes, and snippets.

@TomyCesaille
Created October 19, 2023 20:48
Show Gist options
  • Save TomyCesaille/a9188eaf0eb292d1ef9a265f314c5eed to your computer and use it in GitHub Desktop.
Save TomyCesaille/a9188eaf0eb292d1ef9a265f314c5eed to your computer and use it in GitHub Desktop.
Rain alarm - Proto 2 - Base Station Receiver
#include <LoRa.h>
#include <SPI.h>
#define ss 5
#define rst 14
#define dio0 4
int SWITCH_PIN = 15;
int LED_PIN = 2;
struct Payload
{
int rain;
};
void setup()
{
Serial.begin(115200);
while (!Serial)
;
pinMode(LED_PIN, OUTPUT);
Serial.println("LoRa Receiver");
LoRa.setPins(ss, rst, dio0);
while (!LoRa.begin(433E6))
{
Serial.println(".");
delay(500);
}
LoRa.setSyncWord(0x7E);
Serial.println("LoRa Initializing OK!");
}
void loop()
{
int packetSize = LoRa.parsePacket();
if (packetSize)
{
Payload data;
if (packetSize == sizeof(Payload))
{
LoRa.readBytes((uint8_t *)&data, sizeof(Payload));
if (data.rain == 1)
{
digitalWrite(LED_PIN, HIGH);
}
else
{
digitalWrite(LED_PIN, LOW);
}
Serial.print("Raw packet data: ");
uint8_t *dataPtr = (uint8_t *)&data;
for (size_t i = 0; i < sizeof(Payload); ++i)
{
Serial.print(dataPtr[i], HEX);
Serial.print(" ");
}
Serial.println();
}
}
Serial.print(".");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment