SANJIV KUMAR C A
Published © CC BY-NC

BluTag

It’s estimated that each of us will spend six months of our lives looking for things we’ve lost. Hence 'BluTag' is here to help :)

BeginnerFull instructions provided1,847
BluTag

Things used in this project

Hardware components

TI CC2541 BLE Module
×1
CR2032 Battery holder
×1
LCD Display {20x4} I2C
×1
USB-TTL Converter PL2303
×1
Simcard
×1
LED (generic)
LED (generic)
×1
Buzzer
×1

Story

Read more

Code

F9WG6GHIH90OMC7.ino

C/C++
#include <LGATT.h>
#include <LGATTClient.h>
#include <LGATTServer.h>
#include <LGSM.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F, 20, 4);
static LGATTUUID test_uuid("B4B4B4B4-B4B4-B4B4-B4B4-B4B4B4B4B4B5");

LGATTClient c;
volatile int smsflag = 0;
volatile int disable = 0;
int32_t rssi[2] = { 0, 0 };


void setup() {

	Serial.begin(19200);
	lcd.begin();
	Serial.println("[LGATTC ino]setup.");
	lcd.clear();
	lcd.setCursor(0, 0);
	lcd.print("BluTag is ON!");
	delay(1000);
	lcd.setCursor(0, 2);
	lcd.print("Starting BLE service");

	pinMode(11, OUTPUT);
	pinMode(13, OUTPUT);

	// gatt client register
	if (c.begin(test_uuid))
	{
		Serial.println("[LGATTC ino]begin() success to register.");
		lcd.clear();
		lcd.setCursor(0, 0);
		lcd.print("Registered BLE service");
	}
	else
	{
		Serial.println("[LGATTC ino]begin() failed to register.");
		lcd.setCursor(0, 0);
		lcd.print("Failed to reg. BLE service");
		delay(0xffffffff);
	}
}

void sosfn() {

	while (!LSMS.ready())
		delay(1000);
	Serial.println("BluTag SMS Function activated!");
	lcd.clear();
	lcd.setCursor(0, 0);
	lcd.print("BluTag SMS Function Activated");
	LSMS.beginSMS("82486007");
	LSMS.print(" ALERT! You just lost your keys! ");
	if (LSMS.endSMS())
	{
		Serial.println("SMS is sent");
	}
	else
	{
		Serial.println("SMS is not sent");
	}
	smsflag = 0;
}
void loop() {

	if (disable == 0){

		Serial.println("[LGATTC ino]loop::start to scan.");

		int num = 0;
		int a = 0;
		LGATTDeviceInfo info = { 0 };

		while (1)
		{
			// num = c.scan(20);
			// Only connect to known Tag addresses
			int found = 0;
			Serial.printf("scan num [%d] a", num);
			Serial.println();
			// for (int i = 0; i < num; i++)
			// {
			//  c.getScanResult(i, info);
			//
			//0:15:83:0:40:b6
			//0:15:83:0:40:91
			for (int j = 0; j < 1; j++)
			{
				LGATTAddress BluTag1;
				BluTag1.addr[0] = 0xb6;
				BluTag1.addr[1] = 0x40;
				BluTag1.addr[2] = 0x00;
				BluTag1.addr[3] = 0x83;
				BluTag1.addr[4] = 0x15;
				BluTag1.addr[5] = 0x00;
				Serial.printf("BluTag1 [%x]", BluTag1);
				lcd.clear();
				lcd.setCursor(0, 2);
				lcd.printf("BLETag1 [%x]", BluTag1);

				LGATTAddress BluTag2;
				BluTag2.addr[0] = 0x91;
				BluTag2.addr[1] = 0x40;
				BluTag2.addr[2] = 0x00;
				BluTag2.addr[3] = 0x83;
				BluTag2.addr[4] = 0x15;
				BluTag2.addr[5] = 0x00;
				Serial.printf("Actual addr [%x]", info.bd_addr);
				Serial.printf("BluTag2 [%x]", BluTag2);
				//LGATTAddress BluTag[2];
				LGATTAddress BluTag[2] = { BluTag1, BluTag2 };


				Serial.println();
				Serial.printf("Current Tag [%x]", BluTag[j]);
				Serial.println();
				//BluTag[2] = BluTag2;
				delay(2000);
				//boolean ret = c.connect(address);
				// Serial.printf(BluTag,"%x%d",BluTag,j);
				//printf("%s",cated_string);
				if (!c.connect(BluTag[j])) // search all services till timeout or searching done.
				{
					Serial.println("[LGATTC ino]begin() failed to connect.");
					lcd.clear();
					lcd.setCursor(0, 0);
					lcd.print("Failed to connect");
					digitalWrite(11, HIGH);
					delay(2000);

				}
				else
					Serial.println("[LGATTC ino]begin() success to connect.");
				lcd.clear();
				lcd.setCursor(0, 0);
				lcd.print("Connected to BluTag1");
				digitalWrite(11, LOW);
				digitalWrite(13, HIGH);
				delay(2000);
				//int32_t rssi[j] = 0;
				while (1)
					//unsigned long Time = millis();
					//while (millis() <  Time + 1000)
				{

					if (c.readRemoteRSSI(BluTag[j], rssi[j]))
					{
						Serial.printf("[LGATTC ino] readRemoteRSSI address : [%x:%x:%x:%x:%x:%x] rssi [%d]",
							BluTag[j].addr[5], BluTag[j].addr[4], BluTag[j].addr[3], BluTag[j].addr[2], BluTag[j].addr[1], BluTag[j].addr[0],
							rssi[j]);
						Serial.println();
						lcd.clear();
						lcd.setCursor(0, 0);
						lcd.printf(">> [%x:%x:%x:%x:%x:%x]", BluTag[j].addr[5], BluTag[j].addr[4], BluTag[j].addr[3], BluTag[j].addr[2], BluTag[j].addr[1]);
						lcd.setCursor(0, 2);
						lcd.printf(">> RSSI %d dBm", rssi[j]);
						delay(500);

						if ((rssi[0] < -85) || (rssi[1] < -85)) {
							a = a + 1;
							if (a >= 6){
								//Serial.printf("rssi0 [%d]; rssi1 [%d]", rssi[0], rssi[1]);
								Serial.printf("Out of range 85 dbm");
								smsflag++;
								lcd.clear();
								lcd.setCursor(0, 0);
								lcd.print("Out of range - >-85 dbm");
								lcd.setCursor(0, 2);
								lcd.print("Alert! Keys lost!");
								delay(2000);
								digitalWrite(13, LOW);
								digitalWrite(11, HIGH);
								for (int i = 0; i < 3; i++)
								{
									lcd.backlight();
									delay(250);
									lcd.noBacklight();
									delay(250);
								}
								lcd.backlight();
								Serial.printf("SMSflag [%d]", smsflag);
								if (smsflag == 1) {
									sosfn();
								}
								a = 0;
							}
						}
						else {

							a = 0;
							smsflag = 0;
							Serial.printf("rssi0 [%d]; rssi1 [%d]", rssi[0], rssi[1]);
							digitalWrite(13, HIGH);
							digitalWrite(11, LOW);
						}

					}
					else
					{
						Serial.println("[LGATTC ino] failed to get rssi");
						lcd.clear();
						lcd.setCursor(0, 0);
						lcd.print("Failed to get RSSI");
						break;
					}
				}

				c.disconnect(BluTag[j]);
				Serial.printf("Disconnecting Tag [%x]", BluTag[j]);
				delay(1000);

			}
			//Serial.println("In While 1");
			delay(1000);
		}
	}
	Serial.printf("BluTag is disabled");
	//delay(0xffffffff);
}

Credits

SANJIV KUMAR C A

SANJIV KUMAR C A

1 project • 2 followers

Comments