Hello everyone,
I'm a little stuck right now, working on a project with an Arduino Nano 33 BLE.
Essentially, I'm trying to read 2 characteristics at the same time from the BLE-Server and use that data afterwards to do some stuff.
I need it this way, because I need to act on the information in real-time.
I have never worked with multithreading libraries before and if there is a better one I would love to use it. I'm not sure posting my existing code would be of any use, but I can provide some snippets:
static struct pt pt1, pt2;
static int protothread1(struct pt *pt) {
static unsigned long lastTimePrint = 0;
PT_BEGIN(pt);
while(1) {
lastTimePrint = millis();
PT_WAIT_UNTIL(pt, millis() - lastTimePrint > 2000);
Serial.println("Task 1 ");
}
PT_END(pt);
}
Now, as you can see I have just used a very simple example here to see if it works. If I create a second thread that is exactly the same as protothread1 and run them in my loop, it works as expected!
But when I actually try to read from a characteristic to get some data, it all comes crashing down and only the second thread seems to get executed.
That would look like so:
static int protothread2(struct pt *pt, BLECharacteristic headingCharacteristic, BLECharacteristic triggerCharacteristic) {
PT_BEGIN(pt);
while(1) {
Serial.println("* Reading value from heading characteristic: ");
// retrieve and print updated value in characteristic
headingCharacteristic.readValue(heading);
Serial.print("Heading is at ");
Serial.println(heading);
}
PT_END(pt);
}
I'm aware that there are additional functions of the protothreads library that could be of use. Like "PT_YIELD" or "PT_SPAWN". I have no idea how to make use of them however.
I highly suspect that the issue lies with not using a "PT_WAIT_UNTIL" in the second thread, therefore blocking execution of the first one. But I could be wrong.
Thank you wizards in advance
There are no "wizards"
especcially nobody has a glas-sphere to look into to see the rest of your code.
You should always post your complete sktech.
The forum-software can take 120.000 character = 117kB
Which is a code-length of estimanted 3000 lines of code
best regards Stefan
Why do you work with a multithreading library?
Hello Stefan,
thank you very much for the revelation that there are in fact no wizards.
And while I am aware that I can post the entirety of my code here, I don't see any use for it.
The problem lies within my lacking knowledge of this library. And the code I posted here is the code refering to that library. The code outside it has nothing to do with my problem.
So unless you want to skim through hundreds of lines of code that have nothing to do with my problem, I don't know why you wouldn't appreciate a distilled excerpt.
Best regards Philip
- If your code is well structured it will not be nescessary to skim through 100dreds of lines of code.
- in 95% of all cases the bug is somewhere else than the user was assuming.
This is the reason why you should post your complete sketch
additionally: what makes you think that you need a multithreading library?
What does "realtime" mean in this application?
what latency = time between event occurs and your code reacts on this event is acceptable for you
1 microsecond?
1 millisecond?
1 second?
Can you reduce the amount of code down to a subset that clearly demonstrates the problem you are having?
First off, I wouldn't recommend protothreads, it's a hack. Under the hood it simply expands to a switch statement that keeps track of the line number where you called PT_WAIT_UNTIL.
Its name is misleading as well: the protothreads library does not actually allow you to create multiple threads of execution in the usual sense, it is simply a way to allow for cooperative multitasking. There is no per-thread stack, local variables are lost, and there is no preemption. The only place where a protothread can be suspended or resumed is at PT_WAIT_UNTIL or similar statements. Unlike actual preemptive threads, protothreads cannot change anything about the behavior of external functions like characteristic.readValue.
Since you're using an Arduino Nano 33 BLE, you're using mbed OS, and you could use its real treads: Thread - API references and tutorials | Mbed OS 6 Documentation
However, I think the overall approach here might be flawed, because reading 2 characteristics at the same time is simply not possible. There is only one wireless radio, and I highly doubt that the Arduino BLE library is thread-safe.
in 95% of all cases the bug is somewhere else than the user was assuming.
This is the reason why you should post your complete sketch
There is no bug in my sketch. I don't know how to achieve multithreading, so this is the library i chose. What you see as my code is only what I came up with so far. The rest of the program does not matter.
What does "realtime" mean in this application?
It means that I don't want one function blocking the other. I wan't to receive data ( BLECharacteristic) from 2 sources and act on them simultaneously.
Thank you Pieter!
I know protothreads is software based multithreading. And I get that you can't read 2 characteristics simultaneously over the radio. But in the code excerpt that I posted that's not even what I'm doing. I'm only reading one characteristic and printing over serial in the other thread.
I will have a look into the Mbed OS Docs!
Would be more likely...
As for "Simultaneous", indeed. Would sound to me like a dual-core processor is in order, then. Is that truly the case?
The arduino is no dual-core processor, I am very aware. However protothreads should allow for multithreading-like behaviour like Pieter already pointed out.
As for the "no known bug" part:
I get that posting the whole sketch can be of value. But it just isn't needed in this case. As a matter of fact the code I posted could be the entire sketch for all I care. So you can treat it like that too.
Again, "simultaneously" is impossible on a single-core processor.
Having two different threads manage the same hardware does seem like a recipe for disaster.
Maybe one thread should handle the radio and somehow distribute the received data to the appropriate handler threads (perhaps using queues?). These will then process it, but not "simultaneously".
How much time between "simultaniously"! is acceptable:
a femto-second? 10^-15
a picosecond? 10^-12
a microsecond? 10^-6
or would be 1 full second still be sufficient?
details versus overview:
We are talking about details. Please give an overview over your whole project.
In mimimum 70% of all cases knowing the whole thing offers completely different and much better working solutions.
This is like
here is an analogon that shall show what can happen if you just ask for details:
Newbee: "I want to do better cutting please help me sharpening. "
Expert: Sure I can help you. What kind of cutting-tool are you using?
(expert has to ask back for details. The asking back slow down to finish the project)
Newbee: a scissor.
Expert: OK take this sharpening tool
Newbee: Yea works great. Next question How can I make it cut faster I need to finish faster.
expert: Motorised scissors.
newbee Yea works great though still not fast enough.
expert: Ok can you give an overview about what you are cutting.
newbee: the green of a football-arena.
expert: Oha! take a big mowing tractor with a seven boom spindel-mower and GPS-steering
In the beginning the newbee always just told details.
The expert was assuming the newbee knows that his basic approach is well suited.
which turns out to be very bad suited
that's the reason why it is always a good idea to give an overview and to explain what shall happen in the end. What is the final purpose of the details.
best regards Stefan
Do not ever confuse multi-threading with doing things at the same time. That is NOT what multi-threading does and cannot ever do. A single processor does only one thread for a short time and then the other thread and back and forth.
Okay, I get you are trying to help me so let me be very precise now.
After Pieter pointed me in the right direction, I have been reading up on the Arduino_Threads library.
It makes use of the multithreading capability of Arduino Boards that use an Mbed OS-based core library, just like my Nano 33 BLE.
You are supposed to devide the different threads in files with the file extension '.inot'. Therefore you're able to define multiple setup and loop methods.
// Thread1.inot
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println("Test Thread 1");
delay(500);
}
// Thread2.inot
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println("Test Thread 2");
delay(700);
}
In the main file you can start the threads.
// MySketch.ino
#include <Arduino_Threads.h>
void setup() {
Thread1.start();
Thread2.start();
}
void loop() {
}
When I compile (you have to compile with a special version of arduino_cli available from the repo), I get an 'Thread1 is not declared in this scope'-error, which makes sense. But how does one declare this thread at all? In the docs they didn't declare any thread either.
Best regards
still if you have only one bluetooth-hardware
thread1: accessing bluetooth-hardware
step 1
step 2
step 3
thread2: accessing the same bluetooth-hardware
step 1 
Again, I'm very aware the Nano 33 is a single core processor and has no 'real' multithreading. Therefore any code in the threads does not actually get executed at the same time and no hardware is used at the same time.
I'm also aware there are plenty of other solutions to handle multithreading, however I was trying to focus on Arduino_Threads, because it seemed the most straight forward. I'll look into the ones you suggested as well though.
At this point, I've come to the conclusion that you don't know what you're doing or talking about.
I wouldn't recommend that library, I'm not convinced that having multiple .inot files is the right approach. Pretending that you can just take multiple sketches and run them concurrently as different threads without things blowing up is just naive, IMHO.
I was talking about using the mbed::Thread class, without the use of any additional libraries.
Not only the same buffers ... Both threads will try to control the same control / status registers and they may try to put the hardware into different, conflicting modes.