Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.37 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <MFRC522.h>
  3. #include <dht.h>
  4. #include <Arduino.h>
  5. #include <U8g2lib.h>
  6. #include <Servo.h>
  7.  
  8.     #ifdef U8X8_HAVE_HW_SPI
  9.     #endif
  10.     #ifdef U8X8_HAVE_HW_I2C
  11.     #include <Wire.h>
  12.     #endif
  13.     U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);   // All Boards without Reset of the Display
  14.  
  15. #define dataPin 8     //Room temp
  16. dht DHT;              //ROOM
  17. #define dataPin2 3    //Rack Temp
  18. dht DHT2;             //RACK
  19. #define SS_PIN 10 //RFID
  20. #define RST_PIN 9 //RFID
  21. MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
  22.  
  23. //GLOBAL VARIABLES
  24. //----------------
  25.  
  26. //IR stuff
  27. int LED = 7; // Use the onboard Uno LED
  28. int isObstaclePin = 7;  // This is our input pin
  29. int isObstacle = HIGH;  // HIGH MEANS NO OBSTACLE
  30. //oled
  31. char taco[4];//variable to send text to the oled display
  32. //misc
  33. int looper=1;
  34. int alarm = 0;
  35. //Alarm
  36. int ledPin = 6;                // LED connected to digital pin 6 (blue LED
  37. int ledPin2 = 5;                // LED connected to digital pin 5 (blue LED
  38. int ledPin3 = 4;                // LED connected to digital pin 4 (blue LED
  39. int ledPin4 = 1;                // WATER detection
  40. bool access = true;
  41. //Servo (fan)
  42. Servo myservo;                  //create servo object to control a servo
  43. int pos = 0;                    //Servo start possition
  44.  
  45.  
  46. void setup()
  47. {
  48.   //Servo
  49.   myservo.attach(0);            // attaches the servo on pin 0 to the servo object
  50.  
  51.  
  52.   //OLED
  53.   SPI.begin();                  // Initiate  SPI bus
  54.   mfrc522.PCD_Init();           // Initiate MFRC522
  55.   pinMode(ledPin, OUTPUT);      // sets the digital pin as output
  56.   u8g2.begin();                 // Oled display begins
  57.  
  58.   //IR
  59.   pinMode(LED, OUTPUT);         //Sets IR pins
  60.   pinMode(isObstaclePin, INPUT);
  61. }
  62.  
  63.  
  64. void loop()
  65. {
  66.     if (access = true)
  67.     {
  68.  
  69.  
  70.     //---------------------------------------------------------
  71.     //IR (Door open?)
  72.     //---------------------------------------------------------
  73.     isObstacle = digitalRead(isObstaclePin);
  74.     if (isObstacle == LOW)
  75.     {
  76.         do{
  77.            digitalWrite(ledPin, HIGH);   // sets the LED on
  78.            delay(1000);                  // waits for a second
  79.            digitalWrite(ledPin, LOW);    // sets the LED off
  80.            delay(1000);    
  81.            alarm++;
  82.            } while (alarm < 3);
  83.  
  84.            }
  85.            else
  86.            {
  87.            //IR clear
  88.            }
  89.         delay(200);
  90.     //---------------------------------------------------------
  91.    
  92.  
  93.  
  94.  
  95.     //---------------------------------------------------------
  96.     //RF ID
  97.     //---------------------------------------------------------                                        
  98.    
  99.     if ( ! mfrc522.PICC_IsNewCardPresent())     //Look for new cards
  100.     {
  101.     return;
  102.     }
  103.    
  104.     if ( ! mfrc522.PICC_ReadCardSerial())       //Select Card
  105.     {
  106.     return;
  107.     }
  108.  
  109.     String content= "";                         //Initalize
  110.     byte letter;
  111.  
  112.     for (byte i = 0; i < mfrc522.uid.size; i++)
  113.     {
  114.         content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
  115.         content.concat(String(mfrc522.uid.uidByte[i], HEX));
  116.     }
  117.  
  118.  
  119.            
  120.     content.toUpperCase();
  121.     if (content.substring(1) == "E5 9F 3E 26")  //ACCESS CODE (HEX)
  122.     {
  123.         for (int z=0; z <= 3; z++)
  124.             {                                   //Flash green LED to show access
  125.             digitalWrite(ledPin3, HIGH);        // sets the LED on
  126.             delay(1000);                        // waits for a second
  127.             digitalWrite(ledPin3, LOW);         // sets the LED off
  128.             delay(1000);  
  129.             }
  130.     //---------------------------------------------------------
  131.  
  132.  
  133.  
  134. //==================================================================================================================
  135. //ENTRY POINT
  136. //==================================================================================================================
  137.             //---------------------------------------------------------
  138.             //OLED
  139.             //---------------------------------------------------------
  140.             sprintf(taco,"Hey.");               //value to string
  141.             u8g2.clearBuffer();                 // clear the internal memory
  142.             u8g2.setFont(u8g2_font_inr38_mf);   // choose a suitable font
  143.             u8g2.drawStr(0,60,taco);            // write something to the internal memory
  144.             u8g2.sendBuffer();                  // transfer internal memory to the display
  145.             delay(2000);
  146.  
  147.             //DHT, displays temp and humidity
  148.              
  149.                 do{
  150.                 looper = 1;
  151.                 int readData=DHT.read11(dataPin);       //ROOM
  152.                 int t=DHT.temperature;
  153.                 int h=DHT.humidity;
  154.  
  155.                 int readData2=DHT2.read11(dataPin2);    //Server Racks
  156.                 int t2=DHT2.temperature;
  157.                 int h2=DHT2.humidity;
  158.  
  159.                 //Water detection
  160.                 digitalWrite(ledPin4, HIGH);    // sets the LED on
  161.                 delay(1000);                    // waits for a second
  162.  
  163.                 //ROOM
  164.  
  165.                 //OLED HUMID
  166.                      
  167.                 sprintf(taco,"RH%02d",h);           //convert DHT value to string
  168.                 u8g2.clearBuffer();                 // clear the internal memory
  169.                 u8g2.setFont(u8g2_font_inr38_mf);   // choose a suitable font
  170.                 u8g2.drawStr(0,60,taco);            // write something to the internal memory
  171.                 u8g2.sendBuffer();                  // transfer internal memory to the display
  172.                 delay(2000);
  173.                                              
  174.                 //OLED TEMP
  175.                                        
  176.                 sprintf(taco,"RT%02d",t);           //convert DHT value to string
  177.                 u8g2.clearBuffer();                 // clear the internal memory
  178.                 u8g2.setFont(u8g2_font_inr38_mf);   // choose a suitable font
  179.                 u8g2.drawStr(0,60,taco);            // write something to the internal memory
  180.                 u8g2.sendBuffer();                  // transfer internal memory to the display
  181.                 delay(2000);
  182.  
  183.                 //RACKS
  184.                 sprintf(taco,"ST%02d",t2);          //convert DHT value to string
  185.                 u8g2.clearBuffer();                 // clear the internal memory
  186.                 u8g2.setFont(u8g2_font_inr38_mf);   // choose a suitable font
  187.                 u8g2.drawStr(0,60,taco);            // write something to the internal memory
  188.                 u8g2.sendBuffer();                  // transfer internal memory to the display
  189.                 delay(2000);
  190.  
  191.                 sprintf(taco,"SH%02d",h2);          //convert DHT value to string
  192.                 u8g2.clearBuffer();                 // clear the internal memory
  193.                 u8g2.setFont(u8g2_font_inr38_mf);   // choose a suitable font
  194.                 u8g2.drawStr(0,60,taco);            // write something to the internal memory
  195.                 u8g2.sendBuffer();                  // transfer internal memory to the display
  196.                 delay(2000);
  197.  
  198.  
  199.  
  200.                 //---------------------------------------------------------
  201.                 //RACK FAN COOLING SYSTEM
  202.                 //---------------------------------------------------------
  203.  
  204.                 if (t2>45)     //START RACK FANS
  205.                 {
  206.                 for (pos = 0; pos <= 180; pos += 1)
  207.                     { // goes from 0 degrees to 180 degrees
  208.                     myservo.write(pos);              //tell servo to go to position in variable 'pos'
  209.                     delay(15);                       //waits 15ms for the servo to reach the position
  210.                     }
  211.                     for (pos = 180; pos >= 0; pos -= 1)
  212.                     { // goes from 180 degrees to 0 degrees
  213.                     myservo.write(pos);              //tell servo to go to position in variable 'pos'
  214.                     delay(15);                       //waits 15ms for the servo to reach the position
  215.                     }
  216.                 }
  217.                 //---------------------------------------------------------
  218.  
  219.  
  220.                 //---------------------------------------------------------
  221.                 //FIRE ALARM SYSTEM
  222.                 //---------------------------------------------------------
  223.  
  224.                 if (t>55 || t2>100)   //FIRE ALARM
  225.                     {
  226.                             do
  227.                             {
  228.                              digitalWrite(ledPin2, HIGH);   // sets the LED on
  229.                              delay(1000);                   // waits for a second
  230.                              digitalWrite(ledPin2, LOW);    // sets the LED off
  231.                              delay(1000);    
  232.                              alarm++;                                                    
  233.                             } while (alarm < 10);
  234.                     }
  235.                 //---------------------------------------------------------
  236.                
  237.                 }while (looper < 20);
  238.     }
  239.  
  240.  
  241.            
  242. //===================================================================================================================================
  243.  
  244.         //---------------------------------------------------------
  245.         //RF ID SHUTDOWN
  246.         //---------------------------------------------------------
  247.  
  248.         else if (content.substring(1) == "50 20 AC A5")
  249.         {
  250.             sprintf(taco,"Bye.");               //value to string
  251.             u8g2.clearBuffer();                 // clear the internal memory
  252.             u8g2.setFont(u8g2_font_inr38_mf);   // choose a suitable font
  253.             u8g2.drawStr(0,60,taco);            // write something to the internal memory
  254.             u8g2.sendBuffer();                  // transfer internal memory to the display
  255.             delay(1000);
  256.  
  257.             for (int a=0; a <= 3; a++)          //Flash green LED to show access
  258.                 {            
  259.                     digitalWrite(ledPin, HIGH); // sets the LED on
  260.                     delay(1000);                // waits for a second
  261.                     digitalWrite(ledPin, LOW);  // sets the LED off
  262.                     delay(1000);  
  263.                 }
  264.         access = false;
  265.         looper = 21;
  266.         }
  267.            
  268.         else  
  269.         {
  270.             sprintf(taco,"No.");                //value to string
  271.             u8g2.clearBuffer();                 // clear the internal memory
  272.             u8g2.setFont(u8g2_font_inr38_mf);   // choose a suitable font
  273.             u8g2.drawStr(0,60,taco);            // write something to the internal memory
  274.             u8g2.sendBuffer();                  // transfer internal memory to the display
  275.             delay(1000);
  276.  
  277.                                                
  278.             for (int g=0; g <= 3; g++)          //Flash intrudor alarm
  279.             {            
  280.                 digitalWrite(ledPin, HIGH);     // sets the LED on
  281.                 delay(1000);                    // waits for a second
  282.                 digitalWrite(ledPin, LOW);      // sets the LED off
  283.                 delay(1000);  
  284.             }
  285.         }
  286.         //---------------------------------------------------------
  287.     }
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement