robak_dVictor LejeuneNicolas DAILLY
Published © GPL3+

MAYA - LoRa Connected Beehive

Have you ever wondered what happens inside a beehive when the beekeeper is away?

IntermediateShowcase (no instructions)Over 1 day91
MAYA - LoRa Connected Beehive

Things used in this project

Hardware components

DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
Temperature & Humidity Sensor
×1
LDR, 5 Mohm
LDR, 5 Mohm
LDR Type Brightness Sensor
×1
Buzzer
Buzzer
Buzzer used for the alarm
×1
HC-SR501
Présence Sensor
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
Micro-servo motor used to open the door of the beehive
×1
Gravity: I2C 1Kg Weight Sensor Kit - HX711
DFRobot Gravity: I2C 1Kg Weight Sensor Kit - HX711
Weight kit HX711 (max 5Kg)
×1

Software apps and online services

Arduino IDE
Arduino IDE
Arduino IDE used to program the Arduino Board
The Things Stack
The Things Industries The Things Stack
Linux Debian 13

Hand tools and fabrication machines

Laser cutter (generic)
Laser cutter (generic)
LaserBox to build the beehive and electronic boxes

Story

Read more

Schematics

BeeHive LaserCut

The BeeHive Model

Code

Arduino Code

Arduino
The Arduino Code uploaded into our SODAQ ExPloRer Board
#include <Sodaq_RN2483.h>
#include "DHT.h"
#include "HX711.h"
#include <Servo.h> // <-- AJOUT DE LA BIBLIOTHEQUE MOTEUR

// --- CONFIGURATION DES BROCHES ---
#define PIN_DHT     13   
#define PIN_BUZZER  12   
#define PIN_BAL_SCK 11   
#define PIN_BAL_DT  10   
#define PIN_PIR     9   
#define PIN_TOUCH   8    
#define PIN_SERVO   7    // <-- NOUVELLE BROCHE POUR LE MOTEUR
#define PIN_LUM     A0   

// --- CONFIGURATION DHT ---
#define DHTTYPE DHT11
DHT dht(PIN_DHT, DHTTYPE);

// --- CONFIGURATION BALANCE ---
HX711 balance;
float facteurCalibration = 492410.0; 

// --- CONFIGURATION SERVO ---
Servo myServo; // <-- CREATION DE L'OBJET MOTEUR

// --- CONFIGURATION LORA (OTAA) ---
#define debugSerial SerialUSB
#define loraSerial  Serial2

const uint8_t devEUI[8]  = {0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x07, 0x66, 0x4F};
const uint8_t appEUI[8]  = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
const uint8_t appKey[16] = {0xD1, 0xB4, 0x40, 0xF8, 0xA3, 0x72, 0x4A, 0x07, 0x35, 0x19, 0x93, 0x45, 0x54, 0xE2, 0x32, 0x43};

// --- FRQUENCES DES NOTES (Pour Miel Pops) ---
#define NOTE_C4  262 
#define NOTE_E4  330 
#define NOTE_G4  392 
#define NOTE_C5  523 

// --- FONCTION JINGLE MIEL POPS  ---
void playMielPops() {
  debugSerial.println(" Lecture du Jingle Miel Pops...");
  int notes[] = { NOTE_G4, NOTE_C5, 0, NOTE_G4, NOTE_G4, NOTE_G4, NOTE_E4, 0, NOTE_G4, NOTE_G4, NOTE_G4, NOTE_E4, 0, NOTE_G4, NOTE_G4, NOTE_G4, NOTE_C5 };
  int durations[] = { 300, 500, 300, 200, 200, 200, 400, 300, 200, 200, 200, 400, 300, 200, 200, 200, 600 };
  for (int i = 0; i < 17; i++) {
    if (notes[i] == 0) { delay(durations[i]); } 
    else { tone(PIN_BUZZER, notes[i], durations[i]); delay(durations[i] * 1.30); }
  }
  noTone(PIN_BUZZER);
}

void setup() {
  debugSerial.begin(57600);
  loraSerial.begin(LoRaBee.getDefaultBaudRate());
  
  debugSerial.println("--- INITIALISATION RUCHE CONNECTEE ---");

  pinMode(PIN_PIR, INPUT);
  pinMode(PIN_TOUCH, INPUT);
  pinMode(PIN_LUM, INPUT);
  pinMode(PIN_BUZZER, OUTPUT);

  dht.begin();

  balance.begin(PIN_BAL_DT, PIN_BAL_SCK);
  balance.set_scale(facteurCalibration);
  balance.tare(); 
  debugSerial.println("Balance taree (mise a zero).");

  // Initialisation du Moteur
  myServo.attach(PIN_SERVO);
  myServo.write(0); // Position ferme par dfaut
  debugSerial.println("Moteur SG90 pret.");

  LoRaBee.setDiag(debugSerial); 
  if (LoRaBee.initOTA(loraSerial, devEUI, appEUI, appKey, true)) {
    debugSerial.println("Connexion OTAA TTN Reussie !");
  } else {
    debugSerial.println("Echec de la connexion OTAA !");
  }
  
  tone(PIN_BUZZER, NOTE_C5, 200);
}

void loop() {
  debugSerial.println("\n--- LECTURE DES CAPTEURS ---");

  float t = dht.readTemperature();
  float h = dht.readHumidity();
  if (isnan(t)) t = 0.0;
  if (isnan(h)) h = 0.0;

  uint8_t etatPir = digitalRead(PIN_PIR);
  uint8_t etatTouch = digitalRead(PIN_TOUCH);
  
  int valeurLumBrute = analogRead(PIN_LUM);
  uint8_t lumPourcent = map(valeurLumBrute, 0, 1023, 0, 100);

  float poids = balance.get_units(5);
  if (poids < 0) poids = 0.0; 
  
  if (etatTouch == HIGH) {
    tone(PIN_BUZZER, NOTE_G4, 100);
  }

  // --- PREPARATION DU PAYLOAD LORA ---
  uint8_t payload[7];
  uint16_t poidsEnvoi = (uint16_t)(poids * 1000.0);

  payload[0] = (int8_t)t;                 
  payload[1] = (uint8_t)h;                
  payload[2] = lumPourcent;               
  payload[3] = etatPir;                   
  payload[4] = etatTouch;                 
  payload[5] = (poidsEnvoi >> 8) & 0xFF;  
  payload[6] = poidsEnvoi & 0xFF;         

  debugSerial.print("Temp: "); debugSerial.print(payload[0]); debugSerial.print("C | ");
  debugSerial.print("Hum: "); debugSerial.print(payload[1]); debugSerial.print("% | ");
  debugSerial.print("Lum: "); debugSerial.print(payload[2]); debugSerial.print("% | ");
  debugSerial.print("Poids: "); debugSerial.print(poids, 3); debugSerial.println(" kg");

  // --- ENVOI ET RCEPTION SUR TTN ---
  debugSerial.println("Envoi LoRaWAN en cours...");
  
  if (LoRaBee.send(1, payload, 7) == NoError) {
    debugSerial.println("Transmission reussie !");
    
    // COUTE DU DOWNLINK
    uint8_t rxBuffer[10]; 
    uint8_t rxLen = LoRaBee.receive(rxBuffer, sizeof(rxBuffer));
    
    if (rxLen > 0) {
      debugSerial.print("Message recu de TTN ! Longueur : ");
      debugSerial.println(rxLen);
      
      // Ordre 0x01 : Alarme
      if (rxBuffer[0] == 0x01) {
        debugSerial.println(" ALARME ACTIVEE PAR LE DASHBOARD !");
        playMielPops(); 
      }
      // Ordre 0x02 : Moteur (Nouveau)
      else if (rxBuffer[0] == 0x02) {
        debugSerial.println(" ACTION MOTEUR : Ouverture trappe...");
        myServo.write(90);  // Ouvre  90 degrs
        delay(3000);        // Reste ouvert 3 secondes
        myServo.write(0);   // Se referme  0 degr
        debugSerial.println(" Trappe referme.");
      }
      
    } else {
      debugSerial.println("Aucun ordre reu en file d'attente.");
    }

  } else {
    debugSerial.println("Erreur de transmission LoRa.");
  }

  delay(60000); 
}

Credits

robak_d
1 project • 1 follower
Victor Lejeune
0 projects • 1 follower
Nicolas DAILLY
46 projects • 33 followers
Associated Professor at UniLaSalle - Amiens / Head of the Computer Network Department / Teach Computer and Telecommunication Networks

Comments