Good examples of threading + simple web server on 4.1?

jdredd

Active member
Using Teensy 4.1
8mb ram added
16mb flashmem added
32gb SD card
Arduino IDE 1.8.16
Teensyduino 1.56 beta 2

Using TeensyThreads.h and NativeEthernet.h

Is there any good examples of threading and creating A simple-ish webserver?

The project I have currently, connects to my vehicle to scan it and logs to A SD card.

I have A simple web server deal going to where I can get my pages... view my SD card data, delete log files, even use jquery-csv plugin to generate A simple view of CSV file selected.
I even got AJAX working and A simple page to show "live" data via some gauges.

mainpage.jpg

gridview.png

gauges.png

It "works" with something like

Code:
void thread_server() {
  while (1) {
    threads.delay(1);
    serverloop();
  }
}


void setup() {
..
..
..
  webThreadID = threads.addThread(thread_server);
}

If the main feature of my project isn't running, the web interface works very well.

But once the main feature of the program starts to run where it is in a loop of Serial talking to the OBD2 of the vehicle that the web interface starts to no longer work.

It is as if the "serverloop" code in the thread that is all buried in there no longer really wants to run now.

Tried looking at the example and documents but not seeing anything standing out.

Like in my main thread/loop of the sketch, that I need to periodically give up some time slices back to a background thread? Since this is single core, need A way to allow back ground threads time to do their work
while A tight working main thread loop is running?
 
NativeEthernet is not setup to be thread-safe, the underlying FNET stack can be though and I have previously tested that though not for several years at this point so I don't have any examples for it. I don't believe QNEthernet is thread-safe either but the again the underlying LWIP stack can be setup to be as well.
 
I figured not exactly thread safe, but for just simple testing, the NatvieEthernet seemed to die.

Now.. I can't answer exactly why, but just playing around, I did do this

Code:
webThreadID = threads.addThread(thread_server, 0, 1024 * 16, 0);

so 16k stack size according to docs, and walla... things work very well. Extremely well it seems.

On this project, it is only going to be 0... to 1 person in theory to connect and use via ethernet.

If more than one, shame on them. As idea is to hook up to your phone via usb>ethernet cable to grab data if don't want to pop out SD card.

Or plug into network at home, and do same thing.

But so far, it has worked out well since doing the above.

I can use the web interface while moving around my menu system, and then while I am scanning the vehicle.

I haven't switched to QNEthernet as it seems still in A mode of development and progress, so holding off for now.

Even the AJAX update while scanning and everything via thread is working well too!

1e04b10200d1c3336ab8784b4822ac89.png

4df19b5c5291ff40530a42deaa85534b.png

46f1f4f76fb06960eaad3b4c7726068a.jpg

ff81568b4f9ca601f10bfdd368312f2c.png

I assume thread code locks a chuck of memory to dedicate to the process? So more indepth the process, more memory you need to lock up for it?

I do Windows dev so threads today, I don't have to worry anymore about such things and haven't had to worry about manually thinking about memory in a long time :)
 
@vjmuzik is right. QNEthernet isn’t thread safe. But I’ll add that you could access it from a single thread and it should work fine, even if that single thread shares time with other threads. (I’m stating this here because I’ve seen misunderstandings where “not thread safe” to some people means “don’t run where there’s threads,” so I’ll state it for posterity. :))

The changes I’m still making to the library are along the lines of default hostnames and PTP timestamping. I’d consider the core of the library pretty stable (and I should probably make it available in the Library Manager already). If you feel like it, give it a shot. I’d love your feedback, for example if you have to make similar thread stack changes to get it to work, like you did for NativeEthernet.
 
@vjmuzik is right. QNEthernet isn’t thread safe. But I’ll add that you could access it from a single thread and it should work fine, even if that single thread shares time with other threads. (I’m stating this here because I’ve seen misunderstandings where “not thread safe” to some people means “don’t run where there’s threads,” so I’ll state it for posterity. :))

The changes I’m still making to the library are along the lines of default hostnames and PTP timestamping. I’d consider the core of the library pretty stable (and I should probably make it available in the Library Manager already). If you feel like it, give it a shot. I’d love your feedback, for example if you have to make similar thread stack changes to get it to work, like you did for NativeEthernet.

If I have time this weekend, I may try and get the QNEthernet working in here. I didn't do so yet as everything for most part "worked" ... thread was a stumble, but it is good now. So far since the stack code change, haven't had a problem.

I'd be interested if QNEthernet adds any speed increase. As the NativeEthernet while it works pretty well for me, it seems "slow" maybe... even being a 100mbit connection, I am not dealing with BIG data here.

Dealing with "not thread safe" isn't a big issue. Since most things I deal in Windows dev in Delphi, isn't thread safe. The VCL / GUI isn't thread safe. Just gotta know when to use critical sections to protect read/write data and
when and how to synchronize from background thread to main thread.
 
Back
Top