Micro App only working when connected

I have written a short code in order to scan BLE beacons and send it periodically to our platform.
This code is working well when I use the Estimote IDE interface and while I am connected to the LTE beacon … but not at all as soon as I disconnect the beacon.

Any idea of what can happen ?

Below my code:

var SCAN_INTERVAL = 30 * 1000; // 30 seconds in milliseconds
var SCAN_DURATION = 10 * 1000; // 10 seconds in milliseconds
var TIBIB_SCAN = 0; // 0=False 1=True
var TIBIB_MAX=25; // Maximum nber of detected by sync


app.setErrorHandler((type, msg) => print(msg))

var batteryPercentage=sensors.battery.getPerc()
print('Battery level: ' + batteryPercentage);

var deviceId=sys.getPublicId()
print('DeviceId is : ' + deviceId)

/* sync.setSyncPeriod(60 * 3); //synchro every 3 minutes */

io.press(() => {
  TIBIB_SCAN=0;
  print('Stop SCAN ... TIBIB_SCAN = '+TIBIB_SCAN);
  io.led(io.Led.BLE,false); // turn OFF LED with Bluetooth icon
  io.setLedColor(io.Led.BLE,[0,0,0]); // RGB = Blue
   sync.now(); 
}, io.PressType.LONG); // more than 3s

io.press(() => {
  TIBIB_SCAN=1;
  print('Start SCAN ... TIBIB_SCAN = '+TIBIB_SCAN);
   print('Button pressed');
  io.led(io.Led.BLE,true); // turn ON LED with Bluetooth icon
  io.setLedColor(io.Led.BLE,[0,0,1]); // RGB = Blue
  
});

var detectedBeacons = {};

timers.repeat(SCAN_INTERVAL, () => {
  if (TIBIB_SCAN==1) {
    ble.startScan(
        // 1st argument -- callback to process each scan result
        (scanResult) => {
            if (scanResult.name === "ticatag") {
                    var macAddress='';
                    var str=scanResult.addr;
                    /*for (i=str.length; i > 0; i=i-2) {
  	                    macAddress=macAddress+str.substring(i-2, i);
                    }*/
                    macAddress= str.toUpperCase()
                    var data = {estimoteHubId: deviceId, addr: macAddress, timestamp: new Date().toISOString()}
                    detectedBeacons[macAddress]= data
            }
        },
        // 2nd argument -- how long to scan for
        SCAN_DURATION
    ).then(() => {
        if (detectedBeacons.length===0) {
            print('No beacon to sync');
            return;
        }

        var keys = Object.keys(detectedBeacons);
        var beacons = [];
        print('Detected Length =' + keys.length);
        k=keys.length;
        i1=0
        i2=k-1
        if (i2 >= TIBIB_MAX) i2=TIBIB_MAX-1;
        print('k =' + k);
		
		    while (i1 < k) {
		      print(' i1 = '+i1+ ' / i2 = '+i2+ ' k = '+k);
			    for (i=i1; i < i2; i++) {
				    var key = keys[i];
				    var beacon = detectedBeacons[key];
				    //print(JSON.stringify(beacon));
				    beacons.push(beacon);
			    }

			    j=i2-i1+1;
			    print('Sending the events to server: ' + j);
			    cloud.enqueue('assets-update', {'assets': beacons});
			    sync.now();
			    print('Syncing data');

			    beacons=[];
			    i1=i2+1;
		      i2=i2+TIBIB_MAX;
		      if (i2>k) i2=k-1;
		    }
		    
        detectedBeacons = {};
    });
  }
});

I run your code on my LTE Beacon and it runs fine even when disconnected from WebIDE. I can think about two possibilities:

  1. When LTE Beacon is connected in WebIDE it sends events to the cloud using Bluetooth (to save on battery and data plan), when it is disconnected then it uses cellular connection. This clearly suggests that there are some problems with LTE (usually no coverage). Similar issue was mentioned in this topic.
    You can run this simple snippet to check if there if a valid cellular coverage and device was able to register to network.
lte.getStatus().then( status => print("LTE Avalilable: " + status)).catch( e => print("LTE Not available: "+e))
  1. Beacon’s battery is discharged to a level where it is not possible to maintain cellular connectivity. However Bluetooth and other sensors are working and they can collect and store data before battery is completely drained. This data will be transferred to Cloud when beacon will be charged again.
1 Like

Hi pober
Working fine now with the test of the LTE connection …
Thks a lot
Yann

Hi

Just tried to improve by adding a fallback to GSM if LTE not available

Unfortunately does not seems to work as I don’t get the message if I am deconnected

Did I miss something ?

Below my code

io.press(() => {
  print('Button pressed');
  print('Check LTE Network');
  // Check LTE Network
  //lte.getStatus().then( status => print("LTE Available: " + status)).catch( e => print("LTE Not available: "+e))
  lte.getStatus().then( status => print("LTE Available: " + status)).catch( e => { 
    print("LTE Not available: "+e)
    // Fallback to GSM
    lte.setTech(lte.Tech.GSM_LTE).then(() => {
      print("Tech changed to GSM_LTE")
      }).catch((e) => {
      print("Tech set failed " + e)
    });
  
  });
  
  TIBIB_SCAN=1;
  print('Start SCAN ... TIBIB_SCAN = '+TIBIB_SCAN);
  io.led(io.Led.BLE,true); // turn ON LED with Bluetooth icon
  io.setLedColor(io.Led.BLE,[0,0,1]); // RGB = Blue
  
});

You don’t need to implement that fallback this way. Just call lte.setTech(lte.Tech.GSM_LTE)... at the beginning of your code. Modem will try GSM automatically if LTE is not available. Note that lte.setTech is an undocumented function and its behaviour and syntax might change in the future.
You should have two LTE Beacons in the box. Have you tired that on the other device? Did you make sure it is charged? Did you waited long enough for events? (code might start sending data after 40s + time to initialise modem).

OK Thks
Is there a way to have a status of the GSM connection similar to the LTE.getStatus ?

Device can connected either to GSM or LTE. There is no separate method to check GSM status, use lte.getStatus() and it will tell you if beacon is connected to LTE or to GSM.