MadhavKalyani B
Published © GPL3+

Fauna-Flash

Fauna-Flash enhances road safety for both drivers and wildlife by providing an immediate, automated warning system for animal presence.

AdvancedWork in progress14 days28
Fauna-Flash

Things used in this project

Hardware components

Grove Vision AI Module V2
Seeed Studio Grove Vision AI Module V2
×1
Seeed Studio XIAO ESP32S3
Seeed Studio XIAO ESP32S3
×1
RYLR998 transciever module REYAX TECHNOLOGY
×2
Arduino Nano R3
Arduino Nano R3
×1

Story

Read more

Schematics

Receiver unit

Code

Detection (Stack)

C/C++
This code is used for detection and sending of signal
#include <Seeed_Arduino_SSCMA.h>

SSCMA AI;

void setup()
{
    Serial.begin(9600);
    AI.begin();

    pinMode(D0, OUTPUT); // Elephant pin
    pinMode(D1, OUTPUT); // Buffalo pin
    pinMode(D2, OUTPUT); // Target 2 pin

    digitalWrite(D0, LOW);
    digitalWrite(D1, HIGH);
    digitalWrite(D2, LOW);
}

void loop()
{
    if (!AI.invoke())
    {
        Serial.println("invoke success");
        Serial.print("perf: prepocess=");
        Serial.print(AI.perf().prepocess);
        Serial.print(", inference=");
        Serial.print(AI.perf().inference);
        Serial.print(", postpocess=");
        Serial.println(AI.perf().postprocess);

        bool elephantDetected = false;
        bool buffaloDetected = false;
        bool target2Detected = false;

        for (int i = 0; i < AI.classes().size(); i++)
        {
            int target = AI.classes()[i].target;
            int score = AI.classes()[i].score;

            Serial.print("Class[");
            Serial.print(i);
            Serial.print("] target=");
            Serial.print(target);
            Serial.print(", score=");
            Serial.println(score);

            // Check for Elephant (target 0, score > 75)
            if (target == 0 && score > 75)
            {
                Serial.println("Elephant detected!");
                elephantDetected = true;
                digitalWrite(D0, HIGH);
                delay(1000); // Optional delay for elephant detection
            }

            // Check for Buffalo (target 1, score > 75)
            if (target == 1 && score > 75)
            {
                Serial.println("Buffalo detected!");
                buffaloDetected = true;
                digitalWrite(D1,LOW );
            }

            // Check for Target 2 (score > 50)
            if (target == 2 && score > 50)
            {
                Serial.println("Target 2 detected!");
                target2Detected = true;
                digitalWrite(D2, HIGH);
                
            }
        }

        // Reset pins if targets not detected
        if (!elephantDetected) digitalWrite(D0, LOW);
        if (!buffaloDetected) digitalWrite(D1, LOW);
        if (!target2Detected) digitalWrite(D2, LOW);

        // Optionally print other outputs for debugging
        for (int i = 0; i < AI.boxes().size(); i++)
        {
            Serial.print("Box[");
            Serial.print(i);
            Serial.print("] target=");
            Serial.print(AI.boxes()[i].target);
            Serial.print(", score=");
            Serial.print(AI.boxes()[i].score);
            Serial.print(", x=");
            Serial.print(AI.boxes()[i].x);
            Serial.print(", y=");
            Serial.print(AI.boxes()[i].y);
            Serial.print(", w=");
            Serial.print(AI.boxes()[i].w);
            Serial.print(", h=");
            Serial.println(AI.boxes()[i].h);
        }

        for (int i = 0; i < AI.points().size(); i++)
        {
            Serial.print("Point[");
            Serial.print(i);
            Serial.print("]: target=");
            Serial.print(AI.points()[i].target);
            Serial.print(", score=");
            Serial.print(AI.points()[i].score);
            Serial.print(", x=");
            Serial.print(AI.points()[i].x);
            Serial.print(", y=");
            Serial.println(AI.points()[i].y);
        }

        for (int i = 0; i < AI.keypoints().size(); i++)
        {
            Serial.print("keypoint[");
            Serial.print(i);
            Serial.print("] target=");
            Serial.print(AI.keypoints()[i].box.target);
            Serial.print(", score=");
            Serial.print(AI.keypoints()[i].box.score);
            Serial.print(", box:[x=");
            Serial.print(AI.keypoints()[i].box.x);
            Serial.print(", y=");
            Serial.print(AI.keypoints()[i].box.y);
            Serial.print(", w=");
            Serial.print(AI.keypoints()[i].box.w);
            Serial.print(", h=");
            Serial.print(AI.keypoints()[i].box.h);
            Serial.print("], points:[");

            for (int j = 0; j < AI.keypoints()[i].points.size(); j++)
            {
                Serial.print("[");
                Serial.print(AI.keypoints()[i].points[j].x);
                Serial.print(",");
                Serial.print(AI.keypoints()[i].points[j].y);
                Serial.print("],");
            }
            Serial.println("]");
        }
    }
}

Transmitter (LoRa)

C/C++
This Code receives signal from the stack and transmits the data using LoRa
// Transmitter Code for Xiao nRF52840 with RYLR998 LoRa Module

#include <SoftwareSerial.h>

// Define pins for SoftwareSerial communication with the RYLR998 LoRa module
// LoRa RX pin (connects to LoRa module's TX pin)
#define LORA_RX_PIN 9
// LoRa TX pin (connects to LoRa module's RX pin)
#define LORA_TX_PIN 10

// Define the digital input pin on the Xiao nRF52840
// This pin will be read to determine the signal to send
#define INPUT_PIN 2

// Create a SoftwareSerial object for communication with the LoRa module
// The baud rate for RYLR998 is typically 115200 by default.
SoftwareSerial loraSerial(LORA_RX_PIN, LORA_TX_PIN);

// LoRa Module Configuration Parameters
// These must match on both the transmitter and receiver for communication
const int LORA_ADDRESS = 0;    // Module address (0-65535)
const int LORA_NETWORK_ID = 10; // Network ID (0-16)

// Variable to store the current state of the input pin
int currentInputState = LOW;

void setup() {
  // Initialize the hardware serial for debugging output (optional, but recommended)
  Serial.begin(115200);
  Serial.println("Transmitter (Xiao nRF52840) Initializing...");

  // Initialize the SoftwareSerial communication with the LoRa module
  loraSerial.begin(115200); // Default baud rate for RYLR998

  // Set the input pin mode with internal pull-down resistor
  // This ensures the pin is LOW by default unless actively pulled HIGH.
  pinMode(INPUT_PIN, INPUT_PULLDOWN);

  // --- LoRa Module Configuration ---
  // It's crucial to wait for the module to be ready after power-up.
  // A small delay usually helps, or you can implement a more robust AT command response check.
  delay(1000); // Give the LoRa module time to power up and stabilize

  // Set LoRa Module Address
  // Format: AT+ADDRESS=<address>
  Serial.print("Setting LoRa Address to ");
  Serial.print(LORA_ADDRESS);
  Serial.print("... ");
  loraSerial.print("AT+ADDRESS=");
  loraSerial.print(LORA_ADDRESS);
  loraSerial.print("\r\n"); // CR+LF is required for AT commands
  delay(500); // Wait for module to process command
  // You can add a loop here to read loraSerial and check for "OK" response for robustness.
  while (loraSerial.available()) {
    Serial.write(loraSerial.read());
  }
  Serial.println("Done.");

  // Set LoRa Module Network ID
  // Format: AT+NETWORKID=<ID>
  Serial.print("Setting LoRa Network ID to ");
  Serial.print(LORA_NETWORK_ID);
  Serial.print("... ");
  loraSerial.print("AT+NETWORKID=");
  loraSerial.print(LORA_NETWORK_ID);
  loraSerial.print("\r\n");
  delay(500);
  while (loraSerial.available()) {
    Serial.write(loraSerial.read());
  }
  Serial.println("Done.");

  // You might also want to set AT+BAND, AT+PARAMETER etc.
  // For simplicity, we'll rely on default parameters or pre-configured ones.
  // Example for setting band (e.g., 868000000 for Europe, 915000000 for US):
  // loraSerial.print("AT+BAND=868000000\r\n");
  // delay(500);

  Serial.println("Transmitter setup complete. Ready to send signals.");
}

void loop() {
  // Read the current state of the input pin
  currentInputState = digitalRead(INPUT_PIN);

  // Send a signal if the input pin is HIGH
  if (currentInputState == HIGH) {
    String dataToSend = "H"; // Send 'H' for HIGH
    Serial.println("Input is HIGH. Sending 'H'...");

    // Send data using AT+SEND command
    // Format: AT+SEND=<destination_address>,<data_length>,<data>
    loraSerial.print("AT+SEND=");
    loraSerial.print(LORA_ADDRESS); // Destination address (receiver's address)
    loraSerial.print(",");
    loraSerial.print(dataToSend.length()); // Length of the data string
    loraSerial.print(",");
    loraSerial.print(dataToSend); // The actual data
    loraSerial.print("\r\n"); // CR+LF to terminate the AT command

    // Increased delay to allow the module more time to process the send command and respond
    delay(200);

    // Read and print any response from the LoRa module (e.g., "OK" or "ERR")
    // Added a small delay before reading to ensure the full response is available
    delay(50); // Give a moment for the response to arrive
    while (loraSerial.available()) {
      Serial.write(loraSerial.read());
    }
    Serial.println(); // New line for cleaner output
  }

  // A small delay to debounce the input and prevent rapid transmissions
  // Adjust this value based on how frequently you need to update the signal
  delay(50);
}

Credits

Madhav
2 projects • 2 followers
Kalyani B
2 projects • 1 follower

Comments