Angular, Ionic and the Texas Instrument SensorTag

Hello everybody, I’m trying to create an app to manage Ti SensorTag 2.
Ti Sensor Tag is a cheap module with a lot of sensor:

http://www.ti.com/ww/en/wireless_connectivity/sensortag2015/?INTC=SensorTag&HQS=sensortag

It can be used for beacon, temperature, tilt detection, light etc etc etc…
You can manage the sensor using a js library created by Evothing,

This is the HTML sample app provided by Evothing:

https://github.com/evothings/evothings-examples/blob/master/examples/ti-sensortag-accelerometer/index.html

I’m trying to create a module to manage the sensor inside Angular and so with Ionic.

I have this code:

angular.module('sensor', [])

.run( (evothings, sensor) => {
  evothings.scriptsLoaded(sensor.initialise)
})

.factory( 'evothings', ()=> {
  // fake global deps
  return {
    tisensortag: {
      createInstance: angular.noop,
      ble: {
        status: {},
        error:{}
      }
    },
    scriptsLoaded: ref => ref()
  }
})

.factory( 'sensor', evothings => {
  
  let sensortag
  let currentDisplayStatus = ''
  let currentAccelerometerValues = {}
  
  function statusHandler(status) {
    setDisplayStatus(status)
 
		if (evothings.tisensortag.ble.status.SENSORTAG_ONLINE === status) {
			// displaySprite()
		}
  }
    
  function errorHandler(error) {
    if (evothings.easyble.error.DISCONNECTED === error)
		{
			setDisplayStatus('Disconnected')
			// sprite.hide()
		}
		else
		{
			setDisplayStatus('Error: ' + error)
		}
  }
    
  function accelerometerHandler(data) {
    var values = sensortag.getAccelerometerValues(data)
    
    currentAccelerometerValues = {
      rawX: values.x,
      rawY: values.y,
      degreeX: values.x/ 10*90,
      degreeY: values.y/ 10*90
    }
  }
  
  function setDisplayStatus(status){
    currentDisplayStatus = status
  }
  
  return {
    
    get displayStatus() {
      return currentDisplayStatus
    },
    
    get accelerometerValues() {
      return currentAccelerometerValues
    },
    
    initialise() {
      // Create SensorTag CC2650 instance.
		  sensortag = evothings.tisensortag
        .createInstance(evothings.tisensortag.CC2650_BLUETOOTH_SMART)
 
  		// Set up callbacks and sensors.
		  sensortag
	  		.statusCallback(statusHandler)
		  	.errorCallback(errorHandler)
			  .accelerometerCallback(accelerometerHandler, 200)
    },
    
    connect() {
      sensortag.connectToNearestDevice()
    },
    
    disconnect() {
      sensortag.disconnectDevice()
		  setDisplayStatus()'Disconnected')
    }
  }
})

I cannot understand how to save in the scope the callbacks, for example the connection status and the accelerometer value.

Can you help me?

Hi.

Nice to see that you’re using Evothings libraries!

I tried running your code but have some troubles getting it parsed correctly, as I haven’t worked with ECMAScript 6 before. Please supply a fully working example including all dependencies and I’ll be able to try to help you.

Eric,
Evothings

I’m refactoring the code using javascript!

@robypez did you get this to work? I’m trying to do the same thing and I’m struggling to convert the evothings code to an angular module