David Elias Flores Escalante
Published © GPL3+

FindMe System

FindMe connects an IoT device "SmartBag" to Artik cloud to be located using a loud sound just pushing a button by an alzheimer patient.

AdvancedFull instructions provided4 days2,278
FindMe System

Things used in this project

Hardware components

Raspberry Pi 1 Model B+
Raspberry Pi 1 Model B+
These extraordinary embedded computer are power and cheap. Is for the Base
×1
SparkFun Wall Adapter Power Supply - 5.25V DC 2.4A (USB Micro-B)
Power adapter for the Raspberry Pi of Base
×1
Radio Receiver - RLP434A
Receive RF signals from the wearable button
×1
SparkFun Logic Level Converter - Bi-Directional
SparkFun Logic Level Converter - Bi-Directional
Interface between the Raspberry Pi B+ (3.3V) as Base, and the PIC16F628A (5V)
×1
Microchip pic microcontroller 16f628A
This will be connected to the transmitter to send the ON/OFF status to Base
×2
Jumper wires (generic)
Jumper wires (generic)
Allow temporary connections for prototyping and testing
×1
RF Link Transmitter - 434MHz
This will be used as wireless link between patient and the Raspberry Pi B+ Base so this can send ON action to Artik Cloud
×1
SparkFun Big Dome Pushbutton - Red
button for the wearable button for patient ON status
×1
ESP8266 ESP-01
Espressif ESP8266 ESP-01
This is the brain an WiFi link for the SmartBag Device
×1
SparkFun Power Cell - LiPo Charger/Booster
SparkFun Power Cell - LiPo Charger/Booster
Charges the LiPo Batteries of the SmargBag
×1
3.7 V LiPo Battery
Power the SmartBag
×2
Male-Header 36 Position 1 Row- Long (0.1")
Male-Header 36 Position 1 Row- Long (0.1")
×1
Solderless Breadboard Full Size
Solderless Breadboard Full Size
Temporary platform for testing circuits
×1
Female Header 8 Position 1 Row (0.1")
Female Header 8 Position 1 Row (0.1")
Interface the TLP434 and RLP434
×4
Android device
Android device
This will run FindMe app
×1

Software apps and online services

ARTIK Cloud for IoT
Samsung ARTIK Cloud for IoT
Arduino IDE
Arduino IDE
Raspberry Pi Raspbian Jessie with PIXEL

Story

Read more

Schematics

FindMe Base

Base connected to Internet router. It will sniff the RF signal from the myRFButton wearable device, then send to Artik cloud the action ON/OFF.

SmartBag IoT Device

Device installed in the bag which inform its location by sound when the patient put the myRFButton in ON position

MyRFButton

Device with RF signal which transmits a signal received in Base and launch an action through the Artik Cloud ( STATE : ON )

Base Fritzing

Base of the FindMe System

SmartBag Fritzing

SmartBag device emits a LOUD SOUND so it can be located by an alzheimer patient who forget continuously because the memory losing.

myRFButton Fritzing

This wearable device emits an RF signal when the alzheimer patient push the button

FindMe System Schematics

Here, all the schematics needed to build FindMe Systems, and aid for alzheimer patients

Proteus RF Link simulation

Proteus 7.7 simulation of the RF Link between the myRFButton and the PIC in Base

Code

FindMe_Base.js

JavaScript
This node js source code is used in the Raspberry Pi B+ as BASE of the system. This open the serial port, a socket and the gpio. This base is connected to the PIC16F628 which listen the RF signal from the myRFButton controlled by alzheimer patient. When the patient push the button, this sends a signal to thorugh the 434 MHz RF Link, the PIC16F628 listen this signal and sends the ON message to the Raspberry Pi B+ by the serial port; that simple. Then the Base sends data to Artik Cloud.
//FindMe System
//FindMe_Base.js
//Programmer : David E. Flores Escalante
//Project: FindMe System for Artik Cloud
//Date: Sep 2016
//Description: This listen the serial port for data which indicates an RF Signal was received by the PIC16F628A

var webSocketUrl = "wss://api.artik.cloud/v1.1/websocket?ack=true";
var device_id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // device myMomRFButton
var device_token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";// device myMomRFButton

var WebSocket = require('ws');
var isWebSocketReady = false;
var ws = null;

var gpio = require('rpi-gpio');
var myPin = 13;			// LED RF signal received indicator
var myFindMeState = 0;

var SerialPort = require("serialport");
var port = new SerialPort("/dev/ttyAMA0", {
  baudRate: 9600
});

//var sleep = require('sleep');

/**
 * Gets the current time in millis
 */
function getTimeMillis(){
    return parseInt(Date.now().toString());
}

/**
 * Create a /websocket connection and setup GPIO pin
 */
function start() {
	
	console.log("\nFindMe System - Base");
	
	
    //Create the WebSocket connection
    isWebSocketReady = false;
    ws = new WebSocket(webSocketUrl);
    ws.on('open', function() {
        console.log("\nWebSocket connection is open ....");
        register();
    });
    
    ws.on('close', function() {
        console.log("WebSocket connection is closed ....");
	exitClosePins();
    });
	
	port.on('open', function(err) {
		if (err) {
		  return console.log('port not open', err.message);
		}
		console.log('port open');
	});
	
	port.on('data', function (data) {
		//console.log('Data: ' + data);
		
		
		var newState=0;
		var i = data.indexOf("ON"); // Split data 
		//console.log(i);
		
		if(i>0)
		{
			console.log('myRFButton ON\n');
			console.log('SmartBag Missing...\n');
		
			newState=1;
			toggleFindMeLED(newState);
			
		}	
		else
		{
			i = data.indexOf("OFF"); // Split data 
			if(i>0)
			{
				console.log('myRFButton OFF\n');
				console.log('SmartBag founded...\n');
		
				newState=0;
				toggleFindMeLED(newState);
				
			}	
		}
		
	});


    gpio.setup(myPin, gpio.DIR_OUT, function(err) {
        if (err) throw err;
        myFindMeState = false; // default to false after setting up
        console.log('Setting pin ' + myPin + ' to out succeeded! \n');
     });	 		
	 
}

/**
 * Sends a register message to /websocket endpoint
 */
function register(){
    console.log("Registering device on the WebSocket connection");
    try{
        var registerMessage = '{"type":"register", "sdid":"'+device_id+'", "Authorization":"bearer '+device_token+'", "cid":"'+getTimeMillis()+'"}';
        //console.log('Sending register message ' + registerMessage + '\n');
		console.log('Sending register message to Artik Cloud\n');
        ws.send(registerMessage, {mask: true});
        isWebSocketReady = true;
    }
    catch (e) {
        console.error('Failed to register messages. Error in registering message: ' + e.toString());
    }    
}

function toggleFindMeLED(value) {
    gpio.write(myPin, value, function(err) {
        if (err) throw err;
        myFindMeState = value;
        console.log('toggleLED: wrote ' + value + ' to pin #' + myPin);
        sendStateToArtikCloud();
    });

}

/**
 * Send one message to ARTIK Cloud
 */
function sendStateToArtikCloud(){
    try{
        ts = ', "ts": '+getTimeMillis();
        var data = {
              "STATE": myFindMeState
            };
        var payload = '{"sdid":"'+device_id+'"'+ts+', "data": '+JSON.stringify(data)+', "cid":"'+getTimeMillis()+'"}';
        //console.log('Sending payload ' + payload + '\n');
		
        ws.send(payload, {mask: true});
        console.log('Sending STATE=ON to Artik Cloud\n');
    } catch (e) {
        console.error('Error in sending a message: ' + e.toString() +'\n');
    }    
}

/** 
 * Properly cleanup the pins
 */
function exitClosePins() {
    gpio.destroy(function() {
        console.log('Exit and destroy all pins!');
        process.exit();
    });
}

/**
 * All start here
 */

start();

process.on('SIGINT', exitClosePins);

SmartBag.ino

Arduino
This is the program I flashed in the ESP8266. This is able to send ON/OFF status and also SNIFF/READ messages "actions" to/from Artik Cloud
/****************************************************************************
 * contest  : Artik Cloud
 * Project  : FindMe System
 * Device   : SmartBag
 * Date     : 03 October 2016
 * Email    : davemaster99@gmail.com
 * 
 *****************************************************************************/

#include <MQTTClient.h>

#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <string.h>

const char* _SSID     = "xxxxxx"; //Wi-Fi SSID 
const char* _PASSWORD = "xxxxxxxxxx"; // Wi-Fi Password 

// MQTT - Artik Cloud Server params
char Artik_Cloud_Server[]     = "api.artik.cloud"; // Server 
int  Artik_Cloud_Port         = 8883; // MQTT Port
char Client_Name[] = "FindMe - SmartBag"; // Any Name 
// DEVICE ID mySmartBag 
char Device_ID[]   = "21xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; 
// DEVICE TOKEN mySmartBag
char Device_TOKEN[] = "6xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";  
// DEVICE ID myRFButton
char Device_ID_2[]   = "95xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; 
// (/v1.1/messages/"DEVICE ID")
char MQTT_Publish[] = "/v1.1/messages/21xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; 
// (/v1.1/actions/"DEVICE ID") listen messages for mySmartBag device
char MQTT_GetMessage[] = "/v1.1/actions/21xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; 

char txBuffer[200]; // Json Data to Artik Cloud 
char rxBuffer[300]; // Json Data from Artik Cloud 

// status of SmartBag 
boolean bSmartBagState=false;

// pinouts for button and sound (louder buzzer)
const int buttonPin = 0;  
const int soundPin = 14;  //EXTRA GPIO for ESP8266-01

WiFiClientSecure SSL_ESP; // ESP SSL Connection to Artik cloud 

MQTTClient MQTT_Artik_Client; // MQTT Protocol

/********************************************************************************
 
 messageReceived : receives message from MQTT_GetMessage

********************************************************************************/
void messageReceived(String topic, String payload, char * bytes, unsigned int length) {
  Serial.print("incoming: ");
  Serial.print(payload);  
  
  if(topic == MQTT_GetMessage)
  {
    parseRxBuffer(payload);
  }
  
}

/********************************************************************************
 parseRxBuffer  parse messages from mySmartBag
{"actions":[{"name":"setOff"},{"name":"setOn"}]}

********************************************************************************/
void parseRxBuffer(String payload) 
{
  StaticJsonBuffer<300> JsonBuffer;
  String Json = payload;
  
  Json.toCharArray(rxBuffer,300);

  //*****************************
  JsonObject& root = JsonBuffer.parseObject(rxBuffer);
  
  if (!root.success()) 
  {
    Serial.println("parseObject() failed");
  }
  //****************************
  
  //const char* 
  String nameparam = root["actions"][0]["name"];

  Serial.print("action: ");
  Serial.println(nameparam);

  if(nameparam.equals("setOn"))
  { 
    bSmartBagState=true;
    digitalWrite(soundPin, HIGH);  //turns SOUND ON
    sendToArtikCloud(bSmartBagState); 
    
  }
  
  if(nameparam.equals("setOff"))  
  { 
    bSmartBagState=false;
    digitalWrite(soundPin, LOW); //turns SOUND OFF
    sendToArtikCloud(bSmartBagState);
  }
  
  if(nameparam.equals("setOnButton"))
  { 
    bSmartBagState=true;
    digitalWrite(soundPin, HIGH);  //turns SOUND ON
    
  }
}


/********************************************************************************
 
 setup : initial setup of ESP8266
 
********************************************************************************/
void setup() 
{ 
  delay(1000);
  Serial.begin(9600);  

  pinMode(buttonPin, INPUT);
  pinMode(soundPin, OUTPUT); 
  
  digitalWrite(buttonPin, HIGH);
  digitalWrite(soundPin, LOW); 
     
  bSmartBagState = false;  
  
  
  // Wifi Setting
  WiFi.begin(_SSID, _PASSWORD);
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  
  // Connect to Artik Server
  MQTT_Artik_Client.begin(Artik_Cloud_Server, Artik_Cloud_Port, SSL_ESP); 
  
  TryToConnect();
}

/********************************************************************************
 
 putDataInBuffer : charge txBuffer with message to publish
 
********************************************************************************/
void putDataInBuffer(boolean bState) 
{
  StaticJsonBuffer<200> jsonBuffer;
  JsonObject& dataPair = jsonBuffer.createObject();
  
  dataPair["STATE"] = bState ;
    
  dataPair.printTo(txBuffer, sizeof(txBuffer));
  Serial.println(txBuffer);
}


/********************************************************************************

sendToArtikCloud: sends message with status of SmartBag

********************************************************************************/

void sendToArtikCloud(boolean bState) 
{  
    // push data in txBuffer 
    putDataInBuffer(bState);  
    delay(1000);
    
    // Publishing data to the
    MQTT_Artik_Client.publish(MQTT_Publish, txBuffer); Artik Cloud 
    delay(1000);

    Serial.println("\nMessage sent!");
}


/********************************************************************************
 
 loop : main execution loop code

********************************************************************************/

void TryToConnect() 
{
  // Loop until we're reconnected
  while (!MQTT_Artik_Client.connected()) 
  {
    Serial.print("Attempting Artik Cloud connection...");
    // Attempt to connect
    
    // Connect to Artik IOT Device
    if(MQTT_Artik_Client.connect(Client_Name, Device_ID, Device_TOKEN))  
    { 
      MQTT_Artik_Client.subscribe(MQTT_GetMessage);
      Serial.println("Artik Cloud connected...!");     
    }
    else 
    {
      Serial.println("failed...");      
      Serial.println(" try again in 3 seconds");
      // Wait 3 seconds before retrying
      delay(3000);
    }    
    
  }
}

/********************************************************************************
 
 loop : main execution loop code

********************************************************************************/
void loop() 
{  

  int reading = digitalRead(buttonPin);
  
  if (bSmartBagState)
  {
    if (reading == LOW)
    {
    
    bSmartBagState=false;
    //turns SOUND OFF
    digitalWrite(soundPin, LOW);     
    
    Serial.println("\nButton OFF pressed!");
    
    sendToArtikCloud(bSmartBagState);
    
    }
  }
  
  //if client is disconnected, try to connect again
  if(! MQTT_Artik_Client.connected()) 
  {
    TryToConnect();
  }
  
  //read messages from cloud
  MQTT_Artik_Client.loop();  
  delay(100);  
  
}

myRFButton.c

C/C++
CCS PIC C Code for PIC16F628A for send a RF Signal to Base, so this activate an action using Artik Cloud
#include <16F628A.h>

#fuses INTRC_IO, 
#fuses NOWDT, NOPROTECT, NOLVP, NOPUT
#fuses NOBROWNOUT, NOMCLR

#use delay(clock=4MHz)
#use rs232(baud=1200, xmit=PIN_A0, bits=8, parity=N)     

void main()
{
   char msg[]="FindMe";
   
   while(true)
   {
      putc('$');
      for(int i=0;i<6;i++)
      {
         putc(msg[i]);   
      }
      
      delay_ms(1000);
   }
}

base.c

C/C++
CCS PIC C Code for PIC16F628A used in Base so the Raspberry Pi B+ receive the alert from myRFButton and activate an action (ON) for make the SmartBag SOUND so it can be found by alzheimer patient.
#include <16F628A.h>

#fuses INTRC_IO, 
#fuses NOWDT, NOPROTECT, NOLVP, NOPUT
#fuses NOBROWNOUT, NOMCLR


#use delay(clock=4MHz)
#use rs232(baud=1200, rcv=PIN_A0, bits=8, parity=N) 
#use rs232(baud=9600, xmit=PIN_B2, bits=8, parity=N,stream=RaspberryPi) 
          
#include <string.h>

char msgToFind[]="FindMe";

void main()
{
   char msg[10]="\0";
   
   char c;

   while(true)
   {
      c=getc();
      if(c=='$')
      {
        for(int i=0;i<6;i++)
        {
            msg[i]=getc();
        }
        msg[i]="\0";
        
        if(stricmp(msg,msgToFind)==0)
        {
           fprintf(RaspberryPi,"myRFButton,ON");
            delay_ms(1000);
        }                
      }          
   }
}

ALL CODE for FindMe System

Here you find all firmware/code you need to build a FindMe System

Credits

David Elias Flores Escalante

David Elias Flores Escalante

4 projects • 1 follower
Electronic Engineer

Comments