7 Replies Latest reply on Aug 13, 2015 6:14 PM by Levitate

    node_app_slot node app won't start up

    joshuajnoble

      My node app which lives in the node app slot doesn't start up properly on reboot. I'm guessing this is something to do with needing start up Bluetooth and find a device with a specific characteristic on startup. I use Noble (the NodeJS lib for BLE) to scan devices and find a particular device, connect to the characteristic, and then write to it in response to certain URLs. It works fine when I log in and run it by hand, but it doesn't seem to work when it's left in the node_app_slot. Any suggestions on how to get this to run automatically on startup would much appreciated. Server is shown below:

       

      var mraa = require('mraa');
      var async = require('async');
      var noble = require('noble');
      var sys = require('sys');
      var exec = require('child_process').exec;
      
      var loggedData = [];
      //////////////////////////////////////////////////////////////////
      // BLUETOOTH
      //////////////////////////////////////////////////////////////////
      // do the system stuff to turn on bluetooth
      var e = exec("rfkill unblock bluetooth");
      e = exec("killall bluetoothd");
      e = exec("hciconfig hci0 up");
      var peripheralUuid = "ca61ba97dd3f4f60a393b96a9122b3af";
      var readCharacteristic = null;
      noble.on('stateChange', function(state) {
        if (state === 'poweredOn')
        {
          console.log("noble started scanning");
          noble.startScanning();
        }else {
          noble.stopScanning();
        }
      });
      
      
      noble.on('discover', function(peripheral) {
        console.log(peripheral.uuid);
        console.log(peripheral.advertisement.localName);
        if(peripheral.advertisement.localName == "RFduino") {
          noble.stopScanning();
          console.log('peripheral with UUID ' + peripheralUuid + ' found');
          var advertisement = peripheral.advertisement;
          peripheral.connect(function(err) {
            console.log(' connecting , now look for services ');
            peripheral.discoverServices(['2220'], function(err, services) {
              services.forEach(function(service) {
                console.log('found service:', service.uuid);
                service.discoverCharacteristics([], function(err, characteristics) {
                  characteristics.forEach(function(characteristic) {
                    //
                    // Loop through each characteristic and match them to the
                    // UUIDs that we know about.
                    //
                   console.log('found characteristic:', characteristic.uuid);
                    if (characteristic.uuid == '2222') {
                      console.log(" found characteristic, now subscribe ");
                      readCharacteristic = characteristic;
                      var buf = new Buffer("OK!");
                      readCharacteristic.write(buf, false); //function(err){ console.log("err");});
                    }
                  })
                })
              })
            })
          });
        }
      });
      
      var http = require("http");
      
      function getRead(){
        if(!readCharacteristic){
        setTimeout(getRead, 1000);
        console.log(" getting read char ");
        }
        if(readCharacteristic){
        var buf = new Buffer("OK!");
        readCharacteristic.write(buf, false);
        }
      }
      
      http.createServer(function(request, response) {
        response.writeHead(200, {"Content-Type": "text/plain", 'Access-Control-Allow-Origin':'*'});
      
      console.log("request");
          if( request.url == '/unlock')
        {
        var buf = new Buffer("OK!");
        if(!readCharacteristic)
        {
        getRead();
        }
        else
        {
        readCharacteristic.write(buf, false);
              response.write("unlocked!");
              response.end();
        }        
        }
        else
        {
           response.write(JSON.stringify(loggedData));
           response.end();
        }
        loggedData = [];
      }).listen(8888);
      

       

       

      Any suggestions much appreciated.

        • 1. Re: node_app_slot node app won't start up
          CMata_Intel

          Hi joshua,

           

          Are you having problems with the code only when you reboot the board? Or always? Is the code working fine?

          Which image version do you have (configure_edison --version)? Are you using the XDK to build the project?

          After you reboot the board, what is the outcome of running node main.js?

           

          If the code is working completely fine, the reason of the behavior you are having may be that the exec calls are running faster that it should, Linux is not a real-time OS so it may take some time to run a task. You can debug this by adding delays between these calls.

           

           

          Regards,

          Charlie

          • 2. Re: node_app_slot node app won't start up
            joshuajnoble


            Is the code working fine?


            Yes. When I run node server.js it runs correctly


            Which image version do you have (configure_edison --version)?

            68

             

            Are you using the XDK to build the project?

             

            No.

             

            After you reboot the board, what is the outcome of running node main.js?

             

            It runs fine.

            • 3. Re: node_app_slot node app won't start up
              joshuajnoble

              Alright, I've changed my server to check whether the device and characteristic have been found and if not, call the system commands to setup BLE on a timer loop:

               

               

               

              function getRead(){

                      if(!readCharacteristic){

              //              noble.startScanning();

               

                              var e = exec("rfkill unblock bluetooth");

                              e = exec("killall bluetoothd");

                              e = exec("hciconfig hci0 up");

               

                              setTimeout(getRead, 5000);

                              console.log(" getting read char ");

                      }

                      if(readCharacteristic){

                              var buf = new Buffer("OK!");

                              readCharacteristic.write(buf, false);

               

                      }

              }


              This still doesn't work, though again, as soon as I actually type in the command "node server.js" everything works. Is this possibly a permissions problem?

              • 4. Re: node_app_slot node app won't start up
                joshuajnoble

                One more quick update, I've given up on node_app_slot and made a service. The service won't start up on boot and hangs with the following error:

                 

                 

                 

                Aug 03 22:47:12 denny libmraa[223]: libmraa version v0.7.3 initialised by user 'root' with EUID 0

                Aug 03 22:47:12 denny libmraa[223]: gpio: Failed to write 214 to export

                Aug 03 22:47:14 denny node[223]: Open port: /dev/ttyMFD1

                Aug 03 22:47:15 denny node[223]: open

                Aug 03 22:47:15 denny node[223]: noble warning: adapter does not support Bluetooth Low Energy (BLE, Bluetooth Smart).

                Aug 03 22:47:15 denny node[223]: Try to run with environment variable:

                Aug 03 22:47:15 denny node[223]: [sudo] NOBLE_HCI_DEVICE_ID=x node ...

                 

                however if I login and then call:

                 

                systemctl restart denny.service

                 

                It works perfectly. Again, I think the problem is the bluetooth adapter, is there anyway to start a service when the bluetooth adapter is up and working? I know that this can work as I had it all working on an Edison at one point in time but that board was destroyed in an unfortunate accident and I'm now stuck trying to re-create it.

                • 5. Re: node_app_slot node app won't start up
                  CMata_Intel

                  Hi,

                   

                  First, I suggest you to update your image by downloading it from: IoT - Intel® Edison Board Download. The latest version is 159.

                   

                  Try with that version and let me know if you have updates with node_app_slot.

                  If you want to create a service for this, take a look at this thread: Re: systemd works for python script but not nodejs script . I wrote a little example for a service that uses NodeJS I think you can find it useful.

                   

                  Regards,

                  Charlie

                  • 6. Re: node_app_slot node app won't start up
                    joshuajnoble

                    The hangup is actually with the Bluetooth adapter, not the node application itself. It doesn't seem possible to have a service properly rely on the bluetooth.service. What I'm doing in the Node app now is simply restarting the service with 'exec("systemctl restart denny.service");' if it hasn't connected with the bluetooth peripheral after a few seconds. This is a wretched hack but it does (eventually) work.

                    • 7. Re: node_app_slot node app won't start up
                      Levitate

                      I had a similar problem when using Bleno...

                       

                      A couple of things to try...

                      Because exec is async you should probably chain those commands (rfkill, kill all, hci0) in callbacks to make sure the previous command has finished executing before running the next one.


                      Also try moving

                      1. var noble = require('noble'); 

                      Into a function that runs after the 3 bluetooth commands have finished. I think when you require the noble module it attempts to connect to the Bluetooth adapter and if the adapter is disabled or something it just fails.. I think thats what this error is: "noble warning: adapter does not support Bluetooth Low Energy (BLE, Bluetooth Smart)."