RI YAN TANNicolas DAILLY
Published

Environment Network Device

A device created to check the environment status, power supply, button status, and network connections.

BeginnerShowcase (no instructions)79
Environment Network Device

Things used in this project

Hardware components

The Things Uno
The Things Industries The Things Uno
×1
Arduino Leonardo
Arduino Leonardo
×1
Arduino Ethernet Shield 2
Arduino Ethernet Shield 2
×1
DHT11 Temperature & Humidity Sensor (3 pins)
DHT11 Temperature & Humidity Sensor (3 pins)
×1
push button
×1
USB-A to B Cable
USB-A to B Cable
×1

Software apps and online services

Arduino IDE
Arduino IDE
The Things Stack
The Things Industries The Things Stack
Node-RED
Node-RED
UwAmp
MySQL
Grafana

Story

Read more

Schematics

Overall Schematic diagram

This shows the overall architecture and process of the whole project

Code

LoRa.ino

C/C++
This code is for The Things Uno to read the data from those sensors and actuators, and send them to TTN
#include <SoftwareSerial.h>
#include <TheThingsNetwork.h>
#include <dht.h>

SoftwareSerial mySerial(9,10);
dht DHT;

// AppEUI and AppKey
const char *appEui = "2001091720010917";
const char *appKey = "97FF0D40044C9CC34D4A8E08E559E1A7";

int analogInputPin = A2;
float vArduino = 0;
int vActual = 0;
float R1 = 30000.0;
float R2 = 7500.0;
int rawValueRead= 0;
const int buttonPin = 13;
int buttonState = 0;
int rawVal = 0;
int val = 0;
byte payload[]={0,0,0,0,0,0};

#define DHT11_PIN 3
#define loraSerial Serial1
#define debugSerial Serial

// define the LoRa frequency for Europe region(868MHz)
#define freqPlan TTN_FP_EU868

TheThingsNetwork ttn(loraSerial, debugSerial, freqPlan);

void setup()
{
  Serial.begin(9600);
  loraSerial.begin(57600);
  debugSerial.begin(9600);
  mySerial.begin(9600);
  pinMode(buttonPin, INPUT);
  pinMode(analogInputPin, INPUT);

  // Wait a maximum of 10s for Serial Monitor
  while (!debugSerial && millis() < 10000)
    ;

  debugSerial.println("-- STATUS");
  ttn.showStatus();

  debugSerial.println("-- JOIN");
  ttn.join(appEui, appKey);
}

void loop()
{

  hourCheck(); // Calling function hourcheck()
  
}

void hourCheck(){

  // continue loop for an hour if no conditions meet
  for(int i = 0; i < 3600; i++){ 
    int chk = DHT.read11(DHT11_PIN); //getting value from DHT-11 sensor
    val = mySerial.read(); //getting value from the master board
    float temp = DHT.temperature;
    
    // USB cable voltage reading
    rawValueRead = analogRead(analogInputPin);   
    vArduino = (rawValueRead * 5.0) / 1024.0;
    vActual = vArduino / (R2/(R1+R2));


    DelayPin(1000);

    Cond(DHT.temperature, DHT.humidity, vActual, val);
  
  }    

  payload[0] = DHT.temperature;
  payload[1] = DHT.humidity;     
  payload[2] = vActual;
  payload[3] = 0;
  payload[5] = 0;
  Serial.println("Periodic message!");
  ttn.sendBytes(payload,sizeof(payload));
}

void DelayPin(int delayTime){ // this will return when the delay time has expired or the conditions have meet.
  
  long startTime = millis();

  while((millis() - startTime < delayTime) && (DHT.temperature == payload[0]) && (vActual == payload[2]) && (val != payload[4])) { 
    
    delay(1);
    
  }
  
}

void Cond(float temp, float humid, int volt, int internet){ // conditions function

    if((volt != payload[2]) && (volt == 0 || volt == 4)){
      payload[0] = temp;
      payload[1] = humid;
      payload[2] = volt;
      payload[3] = 0;
      payload[4] = internet;
      payload[5] = 3;
      Serial.println("USB status changed!");
      ttn.sendBytes(payload,sizeof(payload));  
    }
    if(temp != payload[0]){
        payload[0] = temp;
        payload[1] = humid;
        payload[2] = volt;
        payload[3] = 0;
        payload[4] = internet;
        payload[5] = 2;
        Serial.println(temp);
        Serial.println(humid);
        Serial.println("Temperature changed!");
        ttn.sendBytes(payload,sizeof(payload));
    }
    if(digitalRead(buttonPin) == LOW){
      payload[0] = temp;
      payload[1] = humid;
      payload[2] = volt; 
      payload[3] = 1;
      payload[4] = internet;
      payload[5] = 1;
      Serial.println("Button pressed!");
      ttn.sendBytes(payload,sizeof(payload));
    }
    if(internet != payload[4] && (internet == 0 || internet == 1)){
      payload[0] = temp;
      payload[1] = humid;
      payload[2] = volt;
      payload[3] = 0;
      payload[4] = payload[4];
      payload[5] = 4;
      Serial.println("Network status changed!");
      ttn.sendBytes(payload,sizeof(payload));
    }

}

ClientConnect.ino

C/C++
This code is for the Arduino to check for internet status and communicate with The Things Uno board.
#include <SoftwareSerial.h>
#include <SPI.h>
#include <Ethernet.h>

SoftwareSerial mySerial(9,10);
byte mac[] = { 0xA8, 0x61, 0x0A, 0xAE, 0x97, 0xEF};

char server[] = "www.google.com";  


IPAddress ip(192, 168, 0, 177);
IPAddress myDns(192, 168, 0, 1);

EthernetClient client;


unsigned long beginMicros, endMicros;
unsigned long byteCount = 0;
bool printWebData = true;  // set to false for better speed measurement

void setup() {

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  mySerial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection:
  Serial.println("Initialize Ethernet with DHCP:");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // Check for Ethernet hardware present
    if (Ethernet.hardwareStatus() == EthernetNoHardware) {
      Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
      while (true) {
        delay(1); // do nothing, no point running without Ethernet hardware
      }
    }
    // try to configure using IP address instead of DHCP:
    Ethernet.begin(mac, ip, myDns);
  } else {
    Serial.print("  DHCP assigned IP ");
    Serial.println(Ethernet.localIP());
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.print("connecting to ");
  Serial.print(server);
  Serial.println("...");


  if (client.connect(server, 80)) {
    Serial.print("connected to ");
    Serial.println(client.remoteIP());
            // Make a HTTP request:
    client.println("GET /search?q=arduino HTTP/1.1");
    client.println("Host: www.google.com");
    client.println("Connection: close");
    client.println();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }


  // if you get a connection, report back via serial:
 
  beginMicros = micros();
}

void loop() {
  int val = 0;
  int cond = 0;

    while(cond == 0){
      cond = 1;
       
          if(Ethernet.linkStatus() == LinkON && client.connected()){
            val = 1;
            mySerial.write(val); 
            cond = 0;
          }
          else{
            val = 0;
            mySerial.write(val); 
            cond = 0;
          }
          delay(1000);

 
    }
  
  
}

home.php

PHP
This code is the home page of the website
<!DOCTYPE html>
<html lang="en">
<head>
<title>Page Title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">


<?php
  include 'css.php';
?>


</head>
<body>

<?php
  include 'navigation.php';
?>

  
<div class = "row">
  <div class = "side">
    <h1>Environment Network Device</h1>
	<iframe src="http://192.168.1.172:3000/d-solo/edgptkjvf2u4gd/dht11?orgId=1&refresh=5s&panelId=1" width="650" height="400" frameborder="0"></iframe>
    <iframe src="http://192.168.1.172:3000/d-solo/edgptkjvf2u4gd/dht11?orgId=1&refresh=5s&panelId=2" width="650" height="300" frameborder="0"></iframe>
    <iframe src="http://192.168.1.172:3000/d-solo/edgptkjvf2u4gd/dht11?orgId=1&refresh=5s&panelId=3" width="650" height="300" frameborder="0"></iframe> 
  </div>
  <div class = "main">
    <h1>Door Sensor</h1>
    <iframe src="http://192.168.1.172:3000/d-solo/cdiw5cln43v28e/door-sensor?orgId=1&panelId=1" width="650" height="400" frameborder="0"></iframe>
	<iframe src="http://192.168.1.172:3000/d-solo/cdiw5cln43v28e/door-sensor?orgId=1&panelId=2" width="650" height="300" frameborder="0"></iframe>
    <iframe src="http://192.168.1.172:3000/d-solo/cdiw5cln43v28e/door-sensor?orgId=1&panelId=3" width="650" height="300" frameborder="0"></iframe>
    
  </div>
</div>

</body>
</html>

navigation.php

HTML
The navigation code for including in websites
<div class = "navbar">
  <a href="index.php">Home</a>
  <a href="weather.php">Weather Dashboard</a>
  <a href="dht11.php">Environment Network Device</a>
  <a href="DoorSensor.php">Door Sensor</a>
  <a href="http://localhost/mysql" target="_blank">My database</a>
  <a href="http://192.168.1.172:3000" target="_blank">Dashboard</a>
  <a href="http://192.168.1.172:1880" target="_blank">Node-Red</a>
  <a href="jsondata.php" target="_blank">Json</a>
</div>

css.php

HTML
This code determines the style of the websites
<style>

* {
  box-sizing: border-box;
}
body {
  font-family: Arial, Helvetica, sans-serif;
  margin: 0;
}

/* navigation style */
.navbar {
  overflow: hidden;
  background-color: grey;
}
.navbar a{
  float: left;
  display: block;
  color: white;
  text-align: center;
  padding: 15px 45px;
  text-decoration: none;
}
.navbar a:hover{
  background-color: lightblue;
  color: black;
}

/* content style */
.row {
  display: flex;
  flex-wrap: wrap;
}
.side {
  flex: 50%; /* Set the width of the sidebar */
  background-color: lightgreen; /* Grey background color */
  padding: 30px; /* Some padding */
}

.main {
  flex: 50%; /* Set the width of the main content */
  background-color: lightblue; /* White background color */
  padding: 30px; /* Some padding */
}

/* Header style */
.header {
  padding: 40px; /* some padding */
  text-align: center; /* center the text */
  background: pink; /* green background */
  color: grey; /* white text color */
}
.header h1 {
  color: black;
  font-size: 30pt;
  font-family: 'Gill Sans', 'Gill Sans MT',' Calibri', 'Trebuchet MS', 'sans-serif';
}
table {
  background-color: white;
  margin: 0 auto;
  font-size: small;
}

td {
  background-color: #E4F5D4;
  border: 1px solid black;
}
th,
td {
  border: 1px solid black;
  padding: 7px;
  text-align: center;
  color: black;
}
td {
  font-weight: lighter;
}

</style>

jsondata.php

PHP
This code print a json array from each table in the database
<?php

$user = 'test';
$password = 'test';
$database = 'test';
$servername='localhost';

$mysqli = mysqli_connect($servername, $user, $password, $database);
if(!$mysqli){
	echo mysqli_connect_error();
	exit;
}

$dht = $mysqli->query("SELECT * FROM `environment` order by time desc limit 1");
$door = $mysqli->query("SELECT * FROM `doorsensor` order by time desc limit 1");
$weather = $mysqli->query("SELECT * FROM `weather` order by time desc limit 1");

$dht_result = $dht ->fetch_assoc();
$door_result = $door->fetch_assoc();
$weather_result = $weather ->fetch_assoc();

$dht_array = array();
$door_array = array();
$weather_array = array();

if($dht && $door && $weather){
	while($dht_row =  $dht->fetch_assoc())
	{
		$dht_array[] = $dht_row;
	}
	while($door_row =  $door->fetch_assoc())
	{
		$door_array[] = $door_row;
	}
	while($weather_row =  $weather->fetch_assoc())
	{
		$weather_array[] = $weather_row;
	}

}

$array = array();
$array["dht_array"] = $dht_array;
$array["door_array"] = $door_array;
$array["weather_array"] = $weather_array;

echo json_encode($array);

asort($array);

?>

Credits

RI YAN TAN
1 project • 0 followers
Student studying in Beijing Institute of Technology
Contact
Nicolas DAILLY
42 projects • 25 followers
Associated Professor at UniLaSalle - Amiens / Head of the Computer Network Department / Teach Computer and Telecommunication Networks
Contact

Comments

Please log in or sign up to comment.