Creating multiple things in a loop

I’m implementing 8x ds18b20 temperature sensors as webthing. The protocol that they use is 1wire. Now i have a running loop collecting id’s and temperature of those eight sensors and i wold like to create a webthing for each. I looked in to the multiple thing example but don’t see how to create multiple things in a loop.
Is that actually possible?

The webthing libraries do not handle adding/removing things at runtime well. They expect the complete list of things to be present when the server is started.

Does that mean that I can loop through my sensors and create as many as are found on the 1wire line and then start the server with a list of things like:

list_of_things = [sensor1, sensor 2, sensor 3]
server = WebThingServer(MultipleThings(list_of_things, 'some devices'), port=8888)

or do you mean exactly that, is not handled properly?

Yes, that would be the right thing to do. My point was just that you’ll have to stop looping after you start the server, because it’s not straightforward to add new devices after that point.

Do people here do code reviews?
I made the w1 bus sensor to be read in loop and added to a list that is initialized by Multiplethings.
Did I do everything right? https://pastebin.com/7Tc1FvFm

At first glance, this looks reasonable. Are you having issues, or just wanting another set of eyes?

just another set of eyes.
It seams to work perfect, but im not in any way a experienced Programmer. Like to know what can be done better all the times…

I discovered issues over time.
I Don‘t yet understand when it ocours.
Is it a good practice to read from the sensor every 30s the temperature and updateValue() ?
Or should the gateway request a read from the sensor?
And if, how does a Log view (chart) request values?

Do i maybe have a race condition here with the gateway trying to get values while the webthing is reading in a loop and pushing to to the gateway every few seconds?

The gateway opens a WebSocket connection to your thing and just listens for property updates. It only polls when a WebSocket is not available, like with webthing-arduino.

Generally speaking, it should be fine for your code to read from the sensor whenever it wants. What’s the actual problem you’re having?

Here’s an example of reading from a (fake) sensor every 3 seconds: https://github.com/mozilla-iot/webthing-python/blob/master/example/multiple-things.py#L132-L144

my setup has a separate webserver, maybe that makes my sensors disapear after some time…

Is it possible to have a openwrt Luci webconfig on port 80 as well as the webthing on port 8888?

started like this:

server = WebThingServer(MultipleThings(things_list, TemperatureDevice'), 
                        port=8888)

I had the call to the sensors aside of run_server() that made my sensor work for (i asume) only one read_one(). Then they turn not responding.
patched that and uploaded it to git: https://github.com/novski/w1_webthing

I also moved the luci panel to Port 8080 but still i do not get RESTfull api when opening my http://myurl.local or http://myurl.local:8888

and should I not see the json in the console?

I managed to get the json view if i visit http://myurl.local:8888 but my things do not resolve to:
http://myurl.local:8888/things/first-thing
instead its:
http://myurl.local:8888/first-thing
why does that happen?

The URL paths are not required to match any specific pattern, just like websites are not forced to conform to some predefined set of URLs.

Ah, strange. The definition let me assume it has to be /things as all the examples are with it. https://iot.mozilla.org/wot/#thing-resource
Thanks for the clarification.