Maguette SOWKhadim KaneOusseynouSapiddaRejinthan
Published

Connected-Bee-Hive with an Arduino MKRWAN

In this project, we've developed a complete device that makes it possible to gather data from a hive remotely using an Arduino with LoRaWAN.

IntermediateShowcase (no instructions)1,263
Connected-Bee-Hive with an Arduino MKRWAN

Things used in this project

Hardware components

Arduino MKR WAN 1310
Arduino MKR WAN 1310
Compatible with LoRaWAN
×1
DHT22 Temperature Sensor
DHT22 Temperature Sensor
To measure the temperature outside and inside the hive
×2
DS18B20 Temperature Sensor 1m
HARDWARIO DS18B20 Temperature Sensor 1m
To measure the temperature inside the hive
×2
SparkFun Load Cell Amplifier - HX711
SparkFun Load Cell Amplifier - HX711
Weight sensor
×1
Seeed Studio Lipo Rider Plus
×1
Li-Ion Battery 1000mAh
Li-Ion Battery 1000mAh
×1
SOL2W solar cell
×1
DFRobot_INA219_IIC
×1

Software apps and online services

Arduino IDE
Arduino IDE
Ubidots
Ubidots
The Things Stack
The Things Industries The Things Stack
Beep Monitor
KiCad
KiCad
Otii Arc

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Drill / Driver, Cordless
Drill / Driver, Cordless
Hot glue gun (generic)
Hot glue gun (generic)
Multitool, Screwdriver
Multitool, Screwdriver
3D Printer (generic)
3D Printer (generic)

Story

Read more

Code

Arduino code with LoraWAN configuration and sensors measures

Arduino
/*
  CONNECTED BEE HIVE | Polytech 2022-2023 | GROUP 4
*/

/* Required libraries */
#include <dht.h>
#include <MKRWAN.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <HX711.h>
#include <Wire.h>
#include "DFRobot_INA219.h"
#include "ArduinoLowPower.h"

/* MKRWAN Pins declaration */
#define LOADCELL_DOUT_PIN 5
#define LOADCELL_SCK_PIN 4
#define dht_apin A0       // Analog Pin sensor is connected to
#define dht_apin22 A1     // Analog Pin sensor is connected to
#define dht_apin22ext A2  // Analog Pin sensor is connected to A4 BEFORE
#define analogPin A3      // Pin where the battery is connected

HX711 scale;      // weight sensor
dht DHT;          // DHT22
LoRaModem modem;  // modem Lorawan

/*Link with The Things Network */
String appEui = "0000000000000000";
String appKey = "0DAB3DDAF184926E0347CD274CFEF983";

// Initializing for reading the temperature sensors
//OneWire ds18x20[] = { 6, 7 };
OneWire ds18x20[] = { 6 };
const int oneWireCount = sizeof(ds18x20) / sizeof(OneWire);
DallasTemperature sensor[oneWireCount];

//INA power sensor
DFRobot_INA219_IIC ina219(&Wire, INA219_I2C_ADDRESS4);
// Revise the following two paramters according to actual reading of the INA219 and the multimeter
// for linearly calibration
float ina219Reading_mA = 1;
float extMeterReading_mA = 5000;

/*  Variables */
float calibration_factor = 13350;       // Weight sensor calibration factor
float meas, p, real_meas, temperature;  // variables to calculate the weight
float meas_pow; // Variable for measuring luminosity power

// variables to store the measured values in 16bits
short temp_sonde1, temp_sonde2;
short temp_dht, temp_ext, hum_ext, hum_dht;
short var_poids, var_battery, val_bp;
short var_pow;

float val = 0;                // variable to store the value read
float resultat = 0;           // result in volt
float pourcentage = 0;        //battery pourcentage

bool connected;
int err_count;
int con;

void setup() {
  Serial.println("1");
  Serial.begin(9600);
  Serial.println("Welcome to MKR WAN 1300/1310 connexion");
  modem.begin(EU868);
  delay(1000);  // apparently the murata dislike if this tempo is removed...

  connected = false;
  err_count = 0;
  con = 0;

  // Start up the library on all defined bus-wires
  DeviceAddress deviceAddress;
  for (int i = 0; i < oneWireCount; i++) {
    sensor[i].setOneWire(&ds18x20[i]);
    sensor[i].begin();
    if (sensor[i].getAddress(deviceAddress, 0))
      sensor[i].setResolution(deviceAddress, 8);
  }

  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  scale.set_scale();
  scale.tare();  //Reset the scale to 0

  long zero_factor = scale.read_average();  //Get a baseline reading
  Serial.print("Zero factor: ");            //This can be used to remove the need to tare the scale. Useful in permanent scale projects.
  Serial.println(zero_factor);*/

  //Initialize the sensor
  if (ina219.begin() != true) {
    Serial.println("INA219 begin failed");
  } 
  //Linear calibration
  ina219.linearCalibrate(/*The measured current before calibration*/ ina219Reading_mA, /*The current measured by other current testers*/ extMeterReading_mA);
  Serial.println();

  if (!connected) {
    int ret = modem.joinOTAA(appEui, appKey);
    if (ret) {
      connected = true;
      modem.minPollInterval(60);
      Serial.println("Connected");
      //modem.setPort(3);
      modem.dataRate(5);  // switch to SF7
      //modem.dataRate(2);
      delay(100);  // because ... more stable
      err_count = 0;
    }
  }
  Serial.print("Connexion avec TTN établi ");
}

void loop() {
  //10 min delay before reading the data
  LowPower.deepSleep(600000);
  
  con++;
  Serial.print("Lecture n° : ");
  Serial.println(con);

  /* Temperature and humidity reading of the DHT22*/
  DHT.read22(dht_apin22);
  Serial.print("temperature22 = ");
  Serial.print(DHT.temperature);
  Serial.println("C  ");

  Serial.print("Current humidity22 = ");
  Serial.print(DHT.humidity);
  Serial.print("%  ");

  // Multiplication by 100 then formatting in 16 bits
  temp_dht = (short)((DHT.temperature) * 100);
  hum_dht = (short)((DHT.humidity) * 100);

  DHT.read22(dht_apin22ext);
  Serial.print("temperature22 = ");
  Serial.print(DHT.temperature);
  Serial.println("C  ");

  Serial.print("Current humidity22 = ");
  Serial.print(DHT.humidity);
  Serial.print("%  ");

  // Multiplication by 100 then formatting in 16 bits
  temp_ext = (short)((DHT.temperature) * 100);
  hum_ext = (short)((DHT.humidity) * 100);

  /* Battery */
  val = analogRead(analogPin);  // read the input pin
  Serial.print("Val : ");
  Serial.println(val);
  resultat = (val * 3.3 / 1023);  //(val*3.3/1024) 10 bits with analogRead
  pourcentage = (resultat * 100 / 3.3);
  Serial.print("Pourcentage batterie : ");  // debug value
  Serial.println(pourcentage);
  var_battery = (short)(pourcentage * 100);
  Serial.print("Valeur batterie  : ");
  Serial.println(var_battery);

  /* Temperature reading of the OneWire sensors */
  Serial.print("Requesting temperatures...");
  for (int i = 0; i < oneWireCount; i++) {
    sensor[i].requestTemperatures();
  }
  Serial.println("DONE");
  for (int i = 0; i < oneWireCount; i++) {
    temperature = sensor[i].getTempCByIndex(0);
    Serial.print("Temperature for the sensor ");
    Serial.print(i + 1);
    Serial.print(" is ");
    Serial.println(temperature, 1);
  }
  //delay(100);

  scale.set_scale(calibration_factor);  //Adjust to this calibration factor
  //Serial.print("Reading: ");
  Serial.print((scale.get_units() / 2.2046), 1);  // 1 digit after the decimal point
  Serial.print(" kg");
  real_meas = (scale.get_units() / 2.2046);
  Serial.print(" calibration_factor: ");
  Serial.print(calibration_factor);
  Serial.println();

  /*SEND DATA ON TTN ON 2 BYTES */
  if (connected) {
    int err = 0;
    modem.beginPacket();  // start sending the packet

    // Sending temperature and humidity values to TTN
    modem.write(hum_dht);
    modem.write(temp_dht);
    modem.write(hum_ext);
    modem.write(temp_ext);

    // sending the battery percentage
    modem.write(var_battery);

    for (int i = 0; i < oneWireCount; i++) {
      //Multiplication by 100 then formatting in 16 bits
      temp_sonde1 = (short)(temperature * 100);
      // Sends sensor values
      modem.write(temp_sonde1);
    }
    
    temp_sonde2=temp_sonde1;
    modem.write(temp_sonde2);
    
   Envoi de la valeur du poids
   var_poids = (short)(real_meas * 100);
   modem.write(var_poids);

    // Power measurement

    Serial.print("Power:        ");
    Serial.print(ina219.getPower_mW(), 1);
    Serial.println("mW");
    meas_pow = ina219.getPower_mW();
    meas_pow = (meas_pow*30)/0.0144;
    var_pow = (short)(meas_pow * 100);
    modem.write(var_pow);
   // Serial.print("Var_pow sent:        ");
   // Serial.print(var_pow);
    Serial.print("meas pow:        ");
    Serial.print(meas_pow);

    err = modem.endPacket(true);  // end of sending the packet

    Serial.print(" Valeur erreur: ");
    Serial.println(err);
    if (err <= 0) {
      // Confirmation not received - jam or coverage fault
      err_count++;
      Serial.print("Valeur err_count: ");
      Serial.println(err_count);
      if (err_count > 50) {
        connected = false;
      }
      // wait for 2min for duty cycle with SF12 - 1.5s frame
      for (int i = 0; i < 120; i++) {
        delay(1000);
      }
    } else {
      err_count = 0;
    }
  }
  
}

Connected-beehive

Credits

Maguette SOW
1 project • 4 followers
Contact
Khadim Kane
1 project • 4 followers
Contact
Ousseynou
1 project • 4 followers
Contact
Sapidda
1 project • 4 followers
Contact
Rejinthan
0 projects • 2 followers
Contact

Comments

Please log in or sign up to comment.