diff --git a/BT_RFID_V3.ino b/BT_RFID_V3.ino new file mode 100644 index 0000000..7921bda --- /dev/null +++ b/BT_RFID_V3.ino @@ -0,0 +1,111 @@ + +#include +#include //this i meant for AVR I guess but everyone seems to use the same library on esp32 so not worried +#include //need to switch to hardware serial just installed extra library so code would build +/* + * BT_RFID_V1.ino + * 11/27/2018 + * By JaydenN117 + * + * BT_RFID_V3.ino + * Last Updated + * 4/1/2020 * + * + * Bluetooth RFID reader for cattle tags + * for rfidRW-E-TTL. It uses an arduino nano as + * well as a 4 pin lcd and HC-05 + * + * Important Links + * ============================================= + * About rfidRW-E-TTL module + * http://www.priority1design.com.au/rfidrw-e-ttl.pdf + * + * RFID code is inspired by code here + * https://forum.arduino.cc/index.php?topic=253703.0 + * + * Code for reading voltaged inspired by + * https://startingelectronics.org/articles/arduino/measuring-voltage-with-arduino/ + * + * Bluetooth tools + * http://www.dsdtech-global.com/2017/11/dsd-tech-bluetooth-config-tool.html + * + * + */ +/* + * Set pins below + * + * + * + * TOOL FOR GETTING I2C ADDRESS for LCD + * https://playground.arduino.cc/Main/sourceblock_1/index.txt?action=sourceblock&num=1 + */ +SoftwareSerial Rfid(3,4); //rfidRW-E-TTL software serial +LiquidCrystal_I2C lcd(0x27 ,16,2);//lcd use tool to get address replace 0x3F with address for your lcd if different +int buzzer = 5; //buzzer pin +int blueLED = 8; //bluetooth led light +int greenLED = 7; // ready led light +int redLED = 6; // scan led light +int btState = 9; //bluetooth state pin //need to remove from code +int vibrate = A1; //vibration motor pin +int battery = A0; //use to detect voltage + +/* + * Other things to change based on wand + * time,name, and wands bluetooth pin + * if not configure it is HC_05 with 1234 as pin + */ +String wandNumber = "3";//wand number in rare case of two wands so theres different name +String btName = "BT_RFID_" + wandNumber; //name displayed on lcd wand number follows +String btPin = ""; //pin displayed on lcd +//custom boot lines +String line1 = "BT RFID V3"; +String line2 = ""; +//Some other things you can configure if necessary +boolean lcdSleepEnabled = true; //set false to keep lcd on +boolean indicators = true; //show bluetooth and battery on screen(really buggy voltage is all over with RFID module and bluetooth is not working quite right +boolean displayVoltage = false; //ahows voltage on lcd +boolean quickBoot = false; //skips fancy startup +boolean bluetoothState = true; // Wand supports bluetooth state +int readings =100; //how many times read to average voltage 1000 more stable 10 updates very frequently +long sleepTime = 30000; // sleep time variable +//other variables controlled by code +boolean clearLCD = true; //lcd clear control during new scan +int state = 0; //bluetooth state value +long previousMillis; //part of sleep timer +char c; +float voltage; //battery voltage +float voltageTotal; // voltage total used in averaging +int voltageReadings = 0; //how times voltage was read +float voltageAverage = -1; //average voltage starts at so program will know to display "?" +void setup() { + + start(); //start code +} + +void loop() { + if (bluetoothState){ + state = digitalRead(btState); //reads state pin + digitalWrite(blueLED, state); //writes state to LED + } + voltage = ((analogRead(battery)*5.0)/1024)*2.0; //voltage + voltageTotal+=voltage; // + voltageReadings++; + scan(); //run scan code + if (sleepTimer() && lcdSleepEnabled){ //checks if time is over 30 and lcd sleep is enabled + sleeplcd(); //if true sleep lcd + } + else{ + //no need to write indicators on lcd without backlight on + if (indicators && clearLCD){ //if it is Ok to clearLCD we also know it is not currently printing so I check clearLCD + batteryAndBluetooth(); //show bluetooth and battery information + } + if (displayVoltage && clearLCD){ //shows voltage + showVoltage(); + } + } + if (voltageReadings>=readings){ + voltageAverage = voltageTotal/voltageReadings; + voltageReadings = 0; + voltageTotal = 0; + } +} diff --git a/Commands.ino b/Commands.ino new file mode 100644 index 0000000..892b8b5 --- /dev/null +++ b/Commands.ino @@ -0,0 +1,292 @@ +//holds commands + +/* + * Arrays for bluetooth logos first one is little logo + * other 4 part of large logo + */ + +byte bluetoothLogo[8] ={ + B00100, + B10110, + B01101, + B00110, + B01101, + B10110, + B00100, +}; + +byte bigLogo1[8] = {//top left + B00001, + B00001, + B00001, + B00001, + B00101, + B00011, + B00001, +}; +byte bigLogo2[8] = {//top right + B00000, + B00000, + B10000, + B01000, + B00100, + B01000, + B10000, + +}; +byte bigLogo3[8] ={//bottom left + B00011, + B00101, + B00001, + B00001, + B00001, + B00001, + B00000, +}; +byte bigLogo4[8] = { //bottom right + B01000, + B00100, + B01000, + B10000, + B00000, + B00000, + B00000, +}; + +byte batteyFull[8] = { //indicates full battery + B01110, + B11111, + B11111, + B11111, + B11111, + B11111, + B11111, +}; +byte batteyGood[8] = { //indicates fair battery + B01110, + B10001, + B10001, + B11111, + B11111, + B11111, + B11111, +}; +byte batteyLow[8] = { //indicates low battery + B01110, + B10001, + B10001, + B10001, + B10001, + B11111, + B11111, +}; +byte batteyEmpty[8] = { //indicates empty battery + B01110, + B10001, + B10001, + B10001, + B10001, + B10001, + B11111, +}; +void start() { + + pinMode(buzzer,OUTPUT); //set buzzer to output + //set LEDS to outputs + pinMode(redLED,OUTPUT); + pinMode(blueLED,OUTPUT); + pinMode(greenLED,OUTPUT); + + pinMode(btState,INPUT); //bluetooth state pin as input + + pinMode(vibrate,OUTPUT); //vibration motor + pinMode(battery,INPUT); + Rfid.begin(9600); + Serial.begin(9600); + lcd.init(); + lcd.setBacklight((uint8_t)1); + //all lights on + if (!quickBoot){ + digitalWrite(blueLED,HIGH); + digitalWrite(greenLED,HIGH); + digitalWrite(redLED,HIGH); + + lcd.print(line1); + lcd.setCursor(0,1); + lcd.print(line2); + delay(1000); + + //all lights off + digitalWrite(blueLED,LOW); + digitalWrite(greenLED,LOW); + digitalWrite(redLED,LOW); + + lcd.clear(); + lcd.setCursor(0,0); + lcd.print(btName); + lcd.setCursor(0,1); + lcd.print("passkey " + btPin); //whatever passkey you set bluetooth module to + bigBtLogo(); //print big bluetooth logo on right side of lcd + + digitalWrite(blueLED,HIGH); + tone(buzzer,500); delay(500); noTone(buzzer); + digitalWrite(blueLED,LOW); + digitalWrite(greenLED,HIGH); + tone(buzzer,500); delay(500); noTone(buzzer); + digitalWrite(greenLED,LOW); + digitalWrite(redLED,HIGH); + digitalWrite(vibrate,HIGH); + tone(buzzer,500); delay(500); noTone(buzzer); + digitalWrite(redLED,LOW); + tone(buzzer,1500); delay(200); noTone(buzzer); + digitalWrite(vibrate,LOW); + } + digitalWrite(greenLED,HIGH); + reset(); +} +void btLogo() { + lcd.createChar(0,bluetoothLogo); + lcd.setCursor(14,0); + lcd.write(byte(0)); +} +void bigBtLogo() { + lcd.createChar(1,bigLogo1); //create lcd character for top left + lcd.createChar(2,bigLogo2); //create lcd character for top right + lcd.createChar(3,bigLogo3); //create lcd character for bottom left + lcd.createChar(4,bigLogo4); //create lcd character for bottom right + + lcd.setCursor(14,0); + lcd.write(byte(1)); //print top left + lcd.write(byte(2)); //print top right + lcd.setCursor(14,1); + lcd.write(byte(3)); //print bottom left + lcd.write(byte(4)); //print bottom right +} +void scan(){ + if (Rfid.available()>0){ + digitalWrite(greenLED,LOW); //green led off + digitalWrite(redLED,HIGH); // red led on + if (clearLCD == true){ + reset(); + clearLCD = false; //dont clear again + lcd.setCursor(0,1); + } + c = Rfid.read(); + if (c == '_') return; //ignore underscore + + Serial.write(c); //write c to Serial(Bluetooth); + + if (c == '\r'){ //if carriage return + Serial.write('\n'); //new line + clearLCD = true;//clear for next reading + + //buzzer to alert user of scan + digitalWrite(vibrate,HIGH); //start vibration motor + tone(buzzer,1000); delay(500); noTone(buzzer); + tone(buzzer,200); delay(200); noTone(buzzer); + digitalWrite(vibrate,LOW); //stop vibration motor + digitalWrite(greenLED,HIGH); //green led on + digitalWrite(redLED,LOW); //red led off + }else{ + lcd.write(c); // write c to lcd dont need extra stuff + } +} + +} +/* + * Resets timer as well as clears screen for next + * numbers + */ +void reset(){ + unsigned long currentMillis = millis(); + previousMillis = currentMillis; + lcd.setBacklight((uint8_t)1); //turn on backlight + lcd.clear(); //clear lcd + lcd.setCursor(0,0); //cursor top left + lcd.print("RFID #"); //print RFID # + if (indicators){ + batteryAndBluetooth(); + } + if (displayVoltage){ + showVoltage(); + } + lcd.setCursor(0,1); //cursor bottom left + +} +/* + * Clears and turns off backlight on lcd + */ +void sleeplcd(){ + //lcd.clear(); //clear lcd + lcd.setBacklight((uint8_t)0); // turn backlight off +} +/* + * Check if time since last scan is over sleeptime + * if it is return true + */ +boolean sleepTimer(){ + unsigned long currentMillis = millis(); + if(currentMillis - previousMillis > sleepTime) { + return true; + } + return false; +} + +void batteryAndBluetooth(){ + + //bluetooth symbol + if (state == HIGH){ + btLogo(); + }else if (state == LOW){ + lcd.setCursor(14,0); + lcd.print(" "); + } + //these voltages work well with poover usb rechargable 9v batteries but not very good with a standard 9v + if(voltageAverage>7.3){ + lcd.createChar(5,batteyFull); + lcd.setCursor(15,0); + lcd.write(byte(5)); //print at top right + return; + } + if(voltageAverage>7.0){ + lcd.createChar(5,batteyGood); + lcd.setCursor(15,0); + lcd.write(byte(5)); //print at top right + return; + } + if(voltageAverage>6.5){ + lcd.createChar(5,batteyLow); + lcd.setCursor(15,0); + lcd.write(byte(5)); //print at top right + return; + } + if (voltageAverage < 0){ + lcd.setCursor(15,0); + lcd.print("?"); + return; + } + else{ + //empty battery + lcd.createChar(5,batteyEmpty); + lcd.setCursor(15,0); + lcd.write(byte(5)); //print at top right + } +} +void showVoltage(){ + //simple command to print voltage on lcd + if(indicators){ + lcd.setCursor(9,0);//go before bluetooth and battery levels + } + else{ + lcd.setCursor(12,0);//go to edge because theres nothing in way + } + if (voltageAverage > 0) { + lcd.print(voltageAverage,1); //print voltage formated like 8.1 + lcd.print("v"); //add v 8.1v + lcd.setCursor(0,0); + } +} +/* + * So I am super new to the esp32 looks like tone doesnt work + * so I am making my own + */