MatteoC

Sensor.java

Aug 12th, 2015
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.80 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package iotdevice;
  7.  
  8. import com.pi4j.io.i2c.I2CBus;
  9. import com.pi4j.io.i2c.I2CDevice;
  10. import com.pi4j.io.i2c.I2CFactory;
  11. import java.io.ByteArrayInputStream;
  12. import java.io.DataInputStream;
  13. //import com.pi4j.io.i2c.
  14. import java.io.IOException;
  15.  
  16. /**
  17.  *
  18.  * @author Matteo
  19.  */
  20. public class Sensor {
  21.    
  22.     //Raspberry Pi's I2C bus
  23.     private static final int i2cBus = 1;
  24.     // Device address
  25.     // private static final int address = 0x50;  
  26.     // RFID read address
  27.     private static final int rfidAddrRead = 0xA1;
  28.     private static final int rfidAddrWrite = 0xA0;
  29.     // Read RFID command
  30.     private static final byte getRfidCmd = (byte) 0x01;
  31.     private static final byte firmwareRfidCmd = (byte) 0xF0;
  32.  
  33.     //I2C bus
  34.     I2CBus bus;
  35.     // Device object
  36.     private I2CDevice sl030Read;
  37.     private I2CDevice sl030Write;
  38.     private DataInputStream sl030CalIn;
  39.     private DataInputStream sl030In;
  40.    
  41.     public Sensor() {
  42.        
  43.         try {
  44.             bus = I2CFactory.getInstance(I2CBus.BUS_1);
  45.             System.out.println("Connected to bus OK!!!");
  46.  
  47.             //get device itself
  48.             sl030Read = bus.getDevice(rfidAddrRead);
  49.             sl030Write = bus.getDevice(rfidAddrWrite);
  50.             System.out.println("Connected to device OK!!!");
  51.             //Small delay before starting
  52.            // Thread.sleep(500);
  53.                  } catch (IOException e) {
  54.             System.out.println("Exception: " + e.getMessage());
  55.                  }
  56.     }
  57.    
  58.     public String readRfid() throws IOException {
  59.        
  60.         byte[] bytesTemp = new byte[4];
  61.         byte[] towrite= new byte[2];
  62.         String result="EMPTY";
  63.         try {
  64.             towrite[0]=(byte)0x01;
  65.             towrite[1]=firmwareRfidCmd;
  66.             sl030Write.write(towrite,0,2);
  67.             Thread.sleep(1000);
  68.            // sl030.r
  69.             int readTotal = sl030Read.read(bytesTemp, 0, 4);
  70.             System.out.println("Letti: " +readTotal);
  71. //            if (readTotal < 2) {
  72. //                System.out.format("Error: %n bytes read/n", readTotal);
  73. //                return "EMPTY";
  74. //            }
  75. //            //sl030In = new DataInputStream(new ByteArrayInputStream(bytesTemp));
  76. //            //UT = sl030In.readUnsignedShort();
  77.             result = new String(bytesTemp);
  78.         } catch (IOException e) {
  79.             System.out.println("Error: " + e.getMessage());
  80.         } catch (InterruptedException e) {
  81.             System.out.println("Interrupted Exception: " + e.getMessage());
  82.         }
  83.      //   System.out.println("Id RFID: " + result);
  84.         return result;
  85.     }
  86.    
  87. }
Advertisement
Add Comment
Please, Sign In to add comment