tdutoisNicolas DAILLYacastrofcaron
Published

The Black Box

Follow your server room's informations from another city with the Black Box

BeginnerShowcase (no instructions)214
The Black Box

Things used in this project

Hardware components

Arduino Leonardo
Arduino Leonardo
×1
Arduino Ethernet Shield 2
Arduino Ethernet Shield 2
×1
Arduino MKR WAN 1300
Arduino MKR WAN 1300
×1
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1
Grove - PIR Motion Sensor
Seeed Studio Grove - PIR Motion Sensor
×1
USB-A to B Cable
USB-A to B Cable
×1

Software apps and online services

The Things Stack
The Things Industries The Things Stack
Arduino IDE
Arduino IDE
KiCad
KiCad
MariaDB
Node-RED
Node-RED
VirtualBox

Hand tools and fabrication machines

Drill / Driver, Cordless
Drill / Driver, Cordless
Soldering Iron Kit, Weller XNT/THM Tips
Soldering Iron Kit, Weller XNT/THM Tips

Story

Read more

Schematics

Diagram of Black Box operating system

This diagram explains how the Black Box works :
-Every collected informations are read by a Arduino Leonardo card.
-With An Arduino MKRWAN card, informations are uploads onto the LoRa network.
-The data is processed on the TTN, the sent to a Node Red server.
-Node Red set up a dashboard with a customized visual.

Code

ConnectLoRa.ino

Arduino
This code is used on the MKR WAN 1300 to receive messages and send them to TTN
#include <MKRWAN.h>
#include <string.h>

LoRaModem modem;

#define appEui "0000000007051998"
#define appKey "746053A507C5FAF7FCC2FA2F34B872C7"

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial1.begin(9600);

  if (!modem.begin(EU868))
  {
    Serial.println("Failed to start module");
    while (1) {}
  }

  int connected = modem.joinOTAA(appEui, appKey);

  if (!connected) {
    Serial.println("Something went wrong; are you indoor? Move near a window and retry");
    while (1) {Serial.println("join error");}
  }

  modem.minPollInterval(60);
  //delay(20000);
}

void loop() {
  static byte ndx =0;
    //Serial.println("Nouveau message");
    ndx=recvWithEndMarker();
    //Serial.println("Nouveau message");

  
    if (newData == true) {
        Serial.println(receivedChars);
        newData = false;
    }
    if (ndx == 0) {
      
    }else{
      sendToTTN();
    }
    
    
    //si j'ai des trucs  envoyer
    
    delay(60000);
}

void sendToTTN() {
  int err;


  /*receivedChars[0]=61;  
  receivedChars[1]=62;
  receivedChars[2]=63;
  receivedChars[3]='\0';
  Serial.println(receivedChars);*/
  
  modem.beginPacket();
  modem.print(receivedChars);

  err = modem.endPacket(true);

  if (err > 0) 
  {
    Serial.println("Message sent correctly!");
  } else {
    Serial.println("Error sending message :(");
    Serial.println("(you may send a limited amount of messages per minute, depending on the signal strength");
    Serial.println("it may vary from 1 message every couple of seconds to 1 message every minute)");
  }

}

static byte recvWithEndMarker() {
    static byte ndx = 0;
    char endMarker = '\n';
    char rc;

    ndx = 0;
    while (Serial1.available() > 0 && newData == false) {
        rc = Serial1.read();

        if (rc != endMarker) {
            receivedChars[ndx] = rc;
            ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
            }
        }
        else {
            receivedChars[ndx] = '\0'; // terminate the string
            //ndx = 0;
            newData = true;
        }
    }
    return ndx;
}

InterpretDonnees.ino

Arduino
This code is used on the Arduino Leonardo card to receive data from sensors
#include <Ethernet.h>
#include <stdlib.h>

#include "DHT.h"

#define DHTPIN A3
#define DHTTYPE DHT11

#define PEOPLESENSOR 3

DHT dht(DHTPIN, DHTTYPE);

byte sensorPin = 3;
byte indicator = 13;
byte usbpin = 5;
byte mac [6] = {0xA8,0x61, 0x0A, 0xAE, 0x94, 0x9B};

void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  dht.begin();
  
  pinMode(sensorPin,INPUT);
  pinMode(usbpin,INPUT);
  pinMode(indicator,OUTPUT); 
}

void loop()
{
  String msg;

  byte state = digitalRead(sensorPin);
  byte onoff = analogRead(usbpin);

  byte ethernet;
  
  byte temp = dht.readTemperature();
  byte hum = dht.readHumidity();

   /*if(!Ethernet.begin(mac)) 
   {
    ethernet = 0;
   }else{
    ethernet = 1;
   }*/

  //ltoa(temp, tempmsg, 16);
  
  msg.concat(temp);
  msg.concat(";");
  msg.concat(hum);
  msg.concat(";");
  msg.concat(state);
  msg.concat(";");
  msg.concat(onoff);
  msg.concat(";");
  msg.concat(ethernet);

  Serial.println(msg);
  Serial1.println(msg); 
  delay(61000);
}

Credits

tdutois

tdutois

1 project • 0 followers
Nicolas DAILLY

Nicolas DAILLY

29 projects • 16 followers
Associated Professor at UniLaSalle - Amiens / Head of the Computer Network Department / Teach Computer and Telecommunication Networks
acastro

acastro

1 project • 0 followers
fcaron

fcaron

12 projects • 1 follower

Comments