-
Ah and one more info, the re-connection works basically but the signals will not be sent to the MDBT42Q. The LED there is not reacting anymore after the reconnection.
I get this message now in the console with BLE task 3:
0 false Uncaught Error: Unhandled promise rejection: Error: BLE task 3 is already in progress Sending command Searching for device... Connecting... Connected 1 true
-
Hi Gordon,
thanks for that help.
What I found out is, if I delete the part with the accelerometer it works. I can connect and disconnect like it should be all the time.
Did I made a mistake in the accelerometer section? I ask for
if connection is true, uart.write.var log = print; var status = 0; var uart; var l; var connected = false; var avrX = 0; var avrY = 0; var accZero = Puck.accel().acc; // get the base value when we start up //BLE Connect function flag(e) { var l = e.time-e.lastTime; if ((l < 0.3) && (connected == false)){ log("Sending command"); log("Searching for device..."); NRF.requestDevice({ filters: [{ namePrefix: 'MDBT42Q' }] }).then(function(device) { log("Connecting..."); device.on('gattserverdisconnected', function() { uart = undefined; connected = false; }); return require("ble_uart").connect(device); }).then(function(u) { log("Connected"); digitalPulse(LED3, 1, 300); uart = u; connected = true; status = 1; log (status); log (connected); });} if ((l < 0.3) && (status == 1)){ if (uart) uart.disconnect(); uart = undefined; connected = false; status = 0; log (status); log (connected); } } setWatch(flag, BTN, {repeat:true, edge:"falling"}); ///Accelerometer Puck.accelOn(); Puck.on('accel', function(data) { avrX = (avrX*0,99) + 0.01*(data.acc.x - accZero.x); avrY = (avrY*0,99) + 0.01*(data.acc.y - accZero.y); ///log (avrX, avrY); if (status == 1) { if (avrX < 90){ uart.write("digitalWrite(LED1, 1), digitalWrite(D31, 1);\n");} if (avrX > 105){ uart.write("digitalWrite(LED1, 1),digitalWrite(D27, 1);\n");} if (avrY < 90){ uart.write("digitalWrite(LED1, 1),digitalWrite(D28, 1);\n");} if (avrY > 105){ uart.write("digitalWrite(LED1, 1),digitalWrite(D29, 1);\n");} if (avrX > 90){ uart.write("digitalWrite(LED1, 0), digitalWrite(D31, 0);\n");} if (avrX < 105){ uart.write("digitalWrite(LED1, 0),digitalWrite(D27, 0);\n");} if (avrY > 90){ uart.write("digitalWrite(LED1, 0),digitalWrite(D28, 0);\n");} if (avrY < 105){ uart.write("digitalWrite(LED1, 0),digitalWrite(D29, 0);\n");} } });
-
I have a little problem that I'm stuck on.
What I basically want to do is:
- with a short press of the Puck.js <0,3s a Bluetooth connection to the MDBT42Q should be established >>> works.
- directly after the connection, the accelerometer should execute its function and switch various pins on the MDBT when the threshold values are exceeded >>> works
- when pressing the Puck.js again < 0,3s the Bluetooth connection shall be disconnected again.
When I upload the code it either doesn't work at all or once and then not anymore.
I get following in the Console:
Uncaught Error: Unhandled promise rejection: Error: BLE task 6 is already in progress Uncaught Error: Unhandled promise rejection: Error: BLE task 6 is already in progress Uncaught Error: Unhandled promise rejection: Error: BLE task 6 is already in progress Uncaught Error: Unhandled promise rejection: Error: BLE task 6 is already in progress Uncaught Error: Unhandled promise rejection: Error: BLE task 6 is already in progress Uncaught Error: BLE task 6 is already in progress at line 1 col 14 k.disconnect() ^ in function "disconnect" called from line 1 col 17 uart.disconnect(); ^ in function called from system Uncaught Error: Unhandled promise rejection: Not connected Uncaught Error: Unhandled promise rejection: Not connected Uncaught Error: Unhandled promise rejection: Not connected Uncaught Error: Unhandled promise rejection: Not connected Uncaught Error: Unhandled promise rejection: Not connected Uncaught Error: Unhandled promise rejection: Not connected Uncaught Error: Unhandled promise rejection: Not connected Uncaught Error: Unhandled promise rejection: Not connecte
Here the code:
var log = print; var uart; var l; var connected = false; var avrX = 0; var avrY = 0; var accZero = Puck.accel().acc; // get the base value when we start up //BLE Connect function flag(e) { var l = e.time-e.lastTime; if ((l < 0.3) && (connected == false)){ log("Sending command"); log("Searching for device..."); NRF.requestDevice({ filters: [{ namePrefix: 'MDBT42Q' }] }).then(function(device) { log("Connecting..."); device.on('gattserverdisconnected', function() { }); return require("ble_uart").connect(device); }).then(function(u) { log("Connected"); digitalPulse(LED3, 1, 300); uart = u; connected = true; });} if ((e.time - e.lastTime < 0.3) && (connected == true) ){ setTimeout(function(device) { uart.disconnect(); log("Disconnected"); connected = false; }, 1000);} } setWatch(flag, BTN, {repeat:true, edge:"falling"}); ///Accelerometer Puck.accelOn(); Puck.on('accel', function(data) { avrX = (avrX*0,99) + 0.01*(data.acc.x - accZero.x); avrY = (avrY*0,99) + 0.01*(data.acc.y - accZero.y); ///log (avrX, avrY); if (connected == true) { if (avrX < 90){ uart.write("digitalWrite(LED1, 1), digitalWrite(D31, 1);\n");} if (avrX > 105){ uart.write("digitalWrite(LED1, 1),digitalWrite(D27, 1);\n");} if (avrY < 90){ uart.write("digitalWrite(LED1, 1),digitalWrite(D28, 1);\n");} if (avrY > 105){ uart.write("digitalWrite(LED1, 1),digitalWrite(D29, 1);\n");} if (avrX > 90 && avrX < 105 && avrY > 90 && avrY < 105){ uart.write("digitalWrite(LED1, 0),digitalWrite(D31, 0), digitalWrite(D27, 0), digitalWrite(D28, 0), digitalWrite(D29, 0) ;\n");} } });
- with a short press of the Puck.js <0,3s a Bluetooth connection to the MDBT42Q should be established >>> works.
-
-
-
-
-
-
-
Is it possible to set different intervals?
I have two operations using setInterval().
One is to read out a sensor via I²C and on the other hand I would like to let a LED blink, depents on the counter value, e. g. counter == 3 >>> LED should blink 3 times all 10 seconds.My problem is that the LED blinks continiously - I tried it with arguments but it does not work.
///Sensor_Data setInterval(() => { i2c.writeTo(0x18, 0xAA, 0x00, 0x00); setTimeout(() => { let output = i2c.readFrom(0x18, 5); var output_bit = output[1] *100 *100 + output[2]*100 + output[3]; var pressure1 = (output_bit-1200000)*(1-(-1)); var pressure2 = pressure1/(1500000-1200000); var pressure_final = pressure2 + (-1) - 0.29; log(pressure_final, counter); ///Counter if (pressure_final < -0.2) { status_counter = 1; } if ((status_counter == 1) && (pressure_final > -0.2) && (pressure_final < 0.2)) { counter = counter + 1; if (counter + 1) { status_counter = 0;} } if (counter > 6){ counter = 1; }
-
And finally the code with the psi-output value:
var i2c = new I2C(); i2c.setup({ scl : D29, sda: D28 }); var output_bit; var output_max; var output_min; var P_max = 1; var P_min = -1; var pressure1; var pressure2; var pressure_final; setInterval(() => { i2c.writeTo(0x18, 0xAA, 0x00, 0x00); setTimeout(() => { let output = i2c.readFrom(0x18, 5); var output_bit = output[1] *100 *100 + output[2]*100 + output[3]; console.log(output_bit); var pressure1 = (output_bit-1200000)*(1-(-1)); var pressure2 = pressure1/(1500000-1200000); var pressure_final = pressure2 + (-1) - 0.4; console.log(pressure_final); }, 100); }, 300);
-
Hi Robin,
also thanks for your answer.
Now with this code works to get a realtime 24-bit value without re-upload:
var i2c = new I2C(); i2c.setup({ scl : D29, sda: D28 }); setInterval(() => { i2c.writeTo(0x18, 0xAA, 0x00, 0x00); setTimeout(() => { let a = i2c.readFrom(MPRL, 5); console.log(a); }, 100); }, 300);
Now I try to calculate the pressure like in the description of the datasheet and the arduino library.
Its strange I just played with the new code and the times of the timeout and interval function. -
-
-
Hi,
can someone help me to get the sensor data of the MPRL sensor?
I wrote some code by using the documentation of the sensor manufacturer:
Find datasheet here:
https://sps.honeywell.com/us/en/products/sensing-and-iot/sensors/pressure-sensors/board-mount-pressure-sensors/micropressure-mpr-seriesvar i2c = new I2C(); i2c.setup({ scl : D29, sda: D28 }); i2c.writeTo(0xAA, 0x00, 0x00); setInterval(function () { var x = i2c.readFrom(0x18, 5); console.log(x); }, 200);
What I get in the console :
new Uint8Array([64, 139, 169, 98, 128])When I reupload the code and give a puff signal to the connected tube on the sensor the value 139 increase. When I do a sip in goes down. The other values 3 & 4 change randomly. the first and the last value is steady.
The value 139 changes just when I reupload to the MDBT42Q module. What I need to do that the value is changing in realtime? Do I need to convert the value as well?
Greetings
-
I've asked about it, but I'm still waiting to hear back.
You can read the hex addresses from the documentation.
Do you think I could try it via a module?
The combination of Bluetooth connection and readout should also work with a module.However, I have not really understood how this works with the modules on a local storage location. I know your documentation but don't know how it is meant to put the module in when you have created a new empty folder via Settings >>> Projects.
-
Hi Gordon,
so I just get zeros as values. I tried it with the hex code 0x04 (attention level).
In the documentation in the link below I saw there are also a hex code to enable the outputs but I don´t konw how to integrate that into the espruino program.I added var buf = 0x04 in the code.
Found this documentation on the thinkgear website with the codes:
http://developer.neurosky.com/docs/doku.php?id=thinkgear_communications_protocol#data_payloadvar buf = 0x04 ; var gatt,characteristic; setWatch(function(e) { NRF.requestDevice({ filters: [{ adress: 'c4:64:e3:e8:d6:e9' }] }).then(function(device) { console.log("Found"); return device.gatt.connect(); }).then(function(g) { console.log("Connected"); gatt = g; return gatt.getPrimaryService( "039afff0-2c94-11e3-9e06-0002a5d5c51b"); }).then(function(service) { return service.getCharacteristic( "039afff8-2c94-11e3-9e06-0002a5d5c51b"); }).then(function (c) { console.log("Got Characteristic"); characteristic = c; characteristic.on('characteristicvaluechanged', function(event) { processBrainData(event.target.value.buffer); }); return characteristic.startNotifications(); }).then(function() { console.log("Connected."); enableOutput(buf1); }); }, BTN, { repeat: true, edge: 'rising' }); function processBrainData(buf) { var d = new Uint8Array(16); setInterval(function() { print(d.slice().map(x=>x.toString(16).padStart(2,0)).join(" ")); }, 500); }
-
-
Hi Gordon,
thanks for your answer.
I have been able to establish a Bluetooth connection with the device. Found a service and characteristics UUID in an Arduino forum.
However, how I should read the data now (with what code), I'm stumped. Had tried it with the combination of the module suggested by you and about the known to me BLE connection setup in the WEB IDE.
var gatt,characteristic; NRF.requestDevice({ filters: [{ adress: 'c4:64:e3:e8:d6:e9' }] }).then(function(device) { console.log("Found"); return device.gatt.connect(); }).then(function(g) { console.log("Connected"); gatt = g; return gatt.getPrimaryService( "039afff0-2c94-11e3-9e06-0002a5d5c51b"); }).then(function(service) { return service.getCharacteristic( "039afff8-2c94-11e3-9e06-0002a5d5c51b"); }).then(function (c) { console.log("Got Characteristic"); characteristic = c; startReading(); }); function startReading() { var busy = false; var i = setInterval(function() { if (!gatt.connected) { clearInterval(i); return; } if (busy) return; busy = true; function processBrainData(data) { console.log(data.field, data.value); } brain.on('data', processBrainData); }, 50); }
-
-
-
-
Hi Gordon,
first of all thank you very much for the information, I didn´t know that.
I have two more questions about this. If I delete this lines:
I don´t get any feedback in the console. If I let it in, I get this message, even if it works with the connecting/disconnecting thing:
The other thing is I try to implement now the button and the part of the code for the button works in another program and also if the Accel is not in that one. What is the issue, when I want to use that additional code? Is it that uart.write is busy in some way? Sometimes the signal from the button receives the MDBT but after 2 or 3 times it stops or the LED on the MDBT does not go off again.
Here the complete Code: