Manipulating variables between BLE advertising?

Posted on
  • Hi
    Have a code that sends BLE advertising like this:

      NRF.setAdvertising([{
        0x2a03 : [String(power)], 
        0x2a04 : [String(pulseHour)],
        0x180f : [Puck.getBatteryPercentage()]}],
        {interval: 600}
     //   name: "PM \xE2\x9A\xA1"} // default is 375 - save a bit of power
      );
    

    The "power" variable is instantaneous power that I display between pulses. But the issue that I have is that when there is a delay between pulses that goes for too long (have solar), it keeps retransmitting the last value.
    Is it possible to manipulate it somehow, so that I can put some code in between the interval resends?
    Like if time difference between pulses is over 60 seconds, it will set the "power" variable to 0

  • You can't have code in Espruino that is called each time an advertising packet is sent, but probably the best method is just to use setTimeout?

    var idleTimer;
    
    function updateBluetooth() {
      // setAdvertising...
    }
    
    function newDataPulse() {
      power = ...;
      updateBluetooth();
      if (idleTimer) clearTimeout(idleTimer);
      idleTimer = setTimeout(idleHandler, 60000);
    }
    
    function idleHandler() {
      idleTimer = undefined;
      power = 0;
      updateBluetooth();
    }
    
  • is it possible to stop sending advertisements? I have solar, so during the day the power use is zero (mostly). But because it is resending the last read value, I am currently just sending a 0 (zero) value for power every 30 seconds just to clear it. It is not energy efficient, as I am just sending zero all the time until the next pulse is read, which may be in the afternoon - hours later.
    I would like to be able to check if last power value was zero and if it was, then stop advertising it.

      setInterval(function() {
        power = 0;
        if (sDate.getMinutes()==0) {
          update();
        }
        NRF.setAdvertising({
          0x1809 : [E.getTemperature()],
          0x180f : [Puck.getBatteryPercentage()],
          0x2a03 : [String(power)]
        });
        print("Battery and temp data sent");
      }, 30000);
      
    
  • You can use NRF.sleep() to stop advertising (and wake to wake it), but you're not going to be saving vast amounts of power. If you really want to do that I'd suggest just supplying an interval argument in the second parameter of setAdvertising and choosing a very low (~2 sec?) interval.

    It'll have basically the same effect but it's still connectable, and you'll actually get an update of the data to 0, rather than your last received advertising being the last nonzero value.

  • and choosing a very low (~2 sec?) interval.

    You mean in place of the usual 375/600 ms? I am just worried that ESP32 may not be able to pick it up if it advertises too slow.

  • how does it work with multiple advertisements? If I create one in the main function that it executed on the pulse detection, and then create a different advertisement on timer, do they both exist or whichever was called last?

  • You mean in place of the usual 375/600 ms? I am just worried that ESP32 may not be able to pick it up if it advertises too slow.

    I meant you can advertise slow when there's no power usage, and then fast when there is. It's then nice easy code.

    If I create one in the main function that it executed on the pulse detection, and then create a different advertisement on timer, do they both exist or whichever was called last?

    It's just the one that's called last. If you want them in rotation, you supply an array of them to setAdvertising

  • no, thats fine as is. Was just confirming to make sure that both don't advertise, taking power.
    I know I have been connecting to the puck a number of times to update the code, but its currently sitting around 70% with just about 1 week running.
    Already disabled the LED pulsing and trying to make the most out of the battery. See how I go. Have an idea of replacing the CR2030 with 2 AA lithiums

  • Odd about the battery usage. Definitely being connected uses up a decent amount of power, but apart from that, just advertising, it should last for maybe 6 months.

    Are you following http://www.espruino.com/Smart+Meter for reading the power usage, or something else? The way the LDR works, the more external light there is the higher the power usage, so that could be it? It's best to get it as dark as possible between the LDR and electricity meter

  • yes, I am. I have pasted the whole code (modified now a bit) here:
    http://forum.espruino.com/conversations/­366190/#comment16108564

    Is there a way to test how much power it's using? It's a closed box and I did my best to shield it. It is reading values ok. Signal can hardly get out of the box so had to install esp32 inside it. So dropped the to power to -4

  • If you have a multimeter that can measure current then you could power the Puck from the gnd/3v pin on it and then measure power consumption that way?

    It's not super accurate that way but it should be good enough to give you an idea what's going on.

    Did you connect anything else? I know if D28 is shorted high at boot, it's assumed that is a serial console and then the on-chip serial peripheral is powered up - which can draw ~1mA which sounds like the kind of power draw you're seeing

  • would be a bit hard to measure with multimeter if I need to keep it in the dark.
    Apart from LDR there is nothing else attached. Are you saying that serial console is enabled by default? Would have thought that it would only be needed when someone connects to the client.
    What are your suggestions on saving power? I was planning to leave it as is and when battery runs flat I would just replace with a fresh one and see how long it will last. Maybe then replace with 2x1.5 AA battery pack or just replace with esp32

  • Are you saying that serial console is enabled by default?

    No, only if D28 is pulled high. If floating it's fine.

    What are your suggestions on saving power?

    If it's just the LDR I'd maybe see what happens with a fresh battery for now. I'd take the battery percentage reading with a grain of salt - it's not super accurate since the voltage changes with temperature (and the make of battery!).

    You're definitely running firmware 2v09? With the Puck and LDR I seem to recall I got at least 3 months out of it. Potentially you could even look at adding a rechargeable battery and solar cell.

    Something like a single LiFePo4 or 2x NiMh batteries would be great. There's so little power drawn than even a small solar cell would easily keep it going forever

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Manipulating variables between BLE advertising?

Posted by Avatar for user130485 @user130485

Actions