VPA
Published © GPL3+

Temperature Monitoring Using RFID Access (LORAWAN & TTN)

Contactless temperature control for employees' RFID access and sending data to the cloud via LORAWAN.

IntermediateFull instructions providedOver 2 days3,923
Temperature Monitoring Using RFID Access (LORAWAN & TTN)

Things used in this project

Hardware components

The Things Uno
The Things Industries The Things Uno
×1
MLX90614
×1
RFID reader RC522
×1
I2C 16x2 Arduino LCD Display Module
DFRobot I2C 16x2 Arduino LCD Display Module
×1
Solid state relay SSR DA
×1
The Things Industries The Things Indoor Gateway
×1

Software apps and online services

Arduino IDE
Arduino IDE
mysql
php
Fritzing

Story

Read more

Code

Step 3. Connecting an IR temperature sensor MLX90614 to an The Things Uno

Arduino
#include <Wire.h>
#include <Adafruit_MLX90614.h>

Adafruit_MLX90614 mlx = Adafruit_MLX90614();

void setup() {
  Serial.begin(9600);
  Serial.println("start");  
  mlx.begin();  
}

void loop() {
  Serial.print("Ambient = "); Serial.print(mlx.readAmbientTempC()); 
  Serial.print("*C");
  Serial.print("\tObject = "); Serial.print(mlx.readObjectTempC()); 
  Serial.print("*C");
  Serial.print("\tObject = "); Serial.print(mlx.readObjectTempF()); 
  Serial.println("*F");
  delay(3000);
}

Step 6. Sending data from the Things Uno by LORAWAN to The Things Network service

Arduino
// Connecting libraries
#include <SPI.h>
#include <MFRC522.h>
#include <TheThingsNetwork.h>
#include <Wire.h>
#include <Adafruit_MLX90614.h>
#include <LiquidCrystal_I2C.h>


// Set your AppEUI and AppKey
const char *appEui = "70B3D57ED00313E5";
const char *appKey = "FAFA472FB54FFE967CE63F5EE056F6D7";

#define loraSerial Serial1
#define debugSerial Serial

// Replace REPLACE_ME with TTN_FP_EU868 or TTN_FP_US915
#define freqPlan TTN_FP_EU868

TheThingsNetwork ttn(loraSerial, debugSerial, freqPlan);
MFRC522 mfrc522(10, 9);
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
LiquidCrystal_I2C lcd(0x27,16,2);  

byte cardUID[4] = {0,0,0,0};
byte payload[6] = {0,0,0,0,0,0};
float mytemp=0.0;
int pin_relay=12;
    
void setup() {
  //
  loraSerial.begin(57600);
  debugSerial.begin(9600);
  // display
  lcd.init();
  lcd.backlight();
  lcd.clear();
  //  SPI
  SPI.begin();
  //  MFRC522
  mfrc522.PCD_Init();
  // starting the MLX90614 sensor
  mlx.begin();
  // relay
  pinMode(pin_relay, OUTPUT);
  digitalWrite(pin_relay, LOW);
  // Wait a maximum of 10s for Serial Monitor
  while (!debugSerial && millis() < 10000)
    ;

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

  debugSerial.println("-- JOIN");
  ttn.join(appEui, appKey);
  // Print a message to the LCD.
  lcd.setCursor(0,0);
  lcd.print("Wait card....");
}

void loop() {
  if ( mfrc522.PICC_IsNewCardPresent()) {
     //    .
     if ( mfrc522.PICC_ReadCardSerial()) {   
        //  UID
        Serial.print(F("Card UID:")); 
        for (byte i = 0; i < 4; i++) {
           cardUID[i]=mfrc522.uid.uidByte[i];
           debugSerial.print(cardUID[i],HEX);
        }
     debugSerial.println();
     debugSerial.println("-- LOOP");
     // Prepare payload of 1 byte to indicate LED status
     //byte payload[6];
     payload[0] = cardUID[0];
     payload[1] = cardUID[1];
     payload[2] = cardUID[2];
     payload[3] = cardUID[3];
     // 
     // Print a message to the LCD.
     lcd.setCursor(0,0);
     lcd.print("Gett temp ..");
     if(get_temperature()) {
        send_ttn(mytemp);
        if(mytemp>37.0) {
           // close 
           digitalWrite(pin_relay, LOW);
           lcd.setCursor(0,0);
           lcd.print("CLOSE !!!!!!! ..");
           delay(2000);
        }
        else {
           // open
           digitalWrite(pin_relay, HIGH);
           lcd.setCursor(0,0);
           lcd.print("OPEN !!!!!!! ..");
           delay(5000);
           digitalWrite(pin_relay, LOW);
        }
     }
     debugSerial.print("t=");
     debugSerial.println(mytemp);
     // Print a message to the LCD.
     lcd.setCursor(0,0);
     lcd.clear();
     lcd.print("Wait card....");
     delay(2000);
     }
  mfrc522.PICC_HaltA();
  mfrc522.PCD_StopCrypto1();
  }

}

boolean get_temperature() {
  int count=0;
  float sumtemp=0.0;
  unsigned long millist=millis();
  
  do {
    float t=mlx.readObjectTempC();
    lcd.setCursor(1,0);
    lcd.print("        ");
    lcd.print(t);
    if(t>34.0 && t<42.0) {
       count++;
       sumtemp=sumtemp+t;
    }
  }while(millis()-millist<5000 || count<5);
  if(count<5) {
     mytemp=0.0;
     return false;
  }
  else {
     mytemp=sumtemp/count;
     return true;
  }
}

//
void send_ttn(float temp) {
  int tt=temp*100;
  int tc=tt/100;
  int td=tt%100;
  debugSerial.print("tc=");
  debugSerial.println(tc);
  debugSerial.print("td=");
  debugSerial.println(td);
  payload[4] = (byte)tc;
  payload[5] = (byte)td;
  // Send it off
  ttn.sendBytes(payload, sizeof(payload));
}

Step4. Sketch of UID detection for an RFID card and temperature measurement

Arduino
// Connecting libraries
#include <SPI.h>
#include <MFRC522.h>
#include <Wire.h>
#include <Adafruit_MLX90614.h>


MFRC522 mfrc522(10, 9);
Adafruit_MLX90614 mlx = Adafruit_MLX90614(); 

byte cardUID[4] = {0,0,0,0};
float mytemp=0.0;
    
void setup() {
  //
  Serial.begin(9600);
  //  SPI
  SPI.begin();
  //  MFRC522
  mfrc522.PCD_Init();
  // starting the MLX90614 sensor
  mlx.begin();

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

}

void loop() {
  if ( mfrc522.PICC_IsNewCardPresent()) {
     //    .
     if ( mfrc522.PICC_ReadCardSerial()) {   
        //  UID
        Serial.print(F("Card UID:")); 
        for (byte i = 0; i < 4; i++) {
           cardUID[i]=mfrc522.uid.uidByte[i];
           Serial.print(cardUID[i],HEX);
        }
     Serial.println();
     // Print a message to the LCD.
     get_temperature();
     Serial.print("t=");
     Serial.println(mytemp);
     delay(2000);
     }
  mfrc522.PICC_HaltA();
  mfrc522.PCD_StopCrypto1();
  }

}
// get temperature
boolean get_temperature() {
  int count=0;
  float sumtemp=0.0;
  unsigned long millist=millis();
  
  do {
    float t=mlx.readObjectTempC();
    if(t>34.0 && t<42.0) {
       count++;
       sumtemp=sumtemp+t;
    }
  }while(millis()-millist<5000 || count<5);
  if(count<5) {
     mytemp=0.0;
     return false;
  }
  else {
     mytemp=sumtemp/count;
     return true;
  }
}

Step 9. Page for remote viewing of employee temperature data

PHP
<?php

//
$location="localhost";
$user="**********";
$pass="**********";
$db_name="**********";

// connect db
if(! $db=mysqli_connect($location,$user,$pass,$db_name))
 {echo "connect error";}
else
 {;}
 
$query0=" SELECT * FROM temp WHERE uid='".$_GET['uid']."' ";
$rez0=mysqli_query($db,$query0);

$content1.=date('Y-m-d')."<br><br>";
$content1.="<table>";
$query1="SELECT * FROM temp WHERE day >= CURDATE()  ";
$rez1=mysqli_query($db,$query1);  $i=1;
while($row1=mysqli_fetch_assoc($rez1)) {
   $content1.="<tr>";
   $content1.="<td>".$i."</td>";
   $rez2=mysqli_query($db,"SELECT name FROM users WHERE id =".$row1['id_user']."  ");
   $row2=mysqli_fetch_assoc($rez2);
   $content1.="<td>".$row2['name']."</td>";
   $content1.="<td>".mb_substr($row1['day'],10,9)."</td>"; 
   if($row1['temp']>37.5)  
       $content1.="<td style='background-color:red'>".$row1['temp']."</td>";
   else  
       $content1.="<td>".$row1['temp']."</td>";
   $content1.="</tr>";
   $i++;
   }
$content1.="</table>";
echo $content1;

?>

Credits

VPA

VPA

6 projects • 28 followers
Author of more than 10 books on programming (web, Arduino, Raspberry pi, IoT), developer and teacher.

Comments