Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ds18b20 module #2003

Merged
merged 4 commits into from
Aug 16, 2017
Merged

add ds18b20 module #2003

merged 4 commits into from
Aug 16, 2017

Conversation

fetchbot
Copy link
Contributor

  • This PR is for the dev branch rather than for master.
  • This PR is compliant with the other contributing guidelines as well (if not, please describe why).
  • I have thoroughly tested my contribution.
  • The code changes are reflected in the documentation at docs/en/*.

DS18B20 module in C rather than Lua #1996, #1883, #1820.

@marcelstoer
Copy link
Member

I guess you'll want to drop /lua_modules/ds18b20 with this as well?

@fetchbot
Copy link
Contributor Author

If it covers @vsky279 and @TerryE use cases?

@TerryE
Copy link
Collaborator

TerryE commented Jun 11, 2017

TBH, I don't understand the desire to use C and write C libraries where Lua ones work fine. Lua developers can always tweak a Lua module if their needs aren't 100% aligned to the current module implementation -- and can do so without having to get into C or rebuild the firmware. In this case the core One-wire C library encapsulates everything that needs to be done in C. So why reimplement DS18B20 in C?

@fetchbot
Copy link
Contributor Author

My main problem is just the lack of available memory if I'll run all my scripts in pure Lua, so I try to off-load the most to C.

@joysfera
Copy link
Contributor

joysfera commented Jun 11, 2017

I've been told that almost nothing needs to be running in memory - most if not all code can be loaded on demand, like using dofile("ds18b20.lc"). The available NodeMCU documentation covers require() use only, though. No detailed word about dofile(), IIRC. I think it would be great if NodeMCU beginners could see some large-ish example programs that would show how to keep only the necessary minimum of Lua code in memory. Then the need for offloading Lua code to C modules would disappear, perhaps.

The DS18B20 is actually a good example of a code that might not be simple to keep out of memory: it needs one timer to wait those 750+ milliseconds between decoding and reading the temperature, and usually another timer to measure temperatures say every minute. How to write the dofile() or require() in a way that ensures the memory is indeed freed immediately and completely?

@devyte
Copy link

devyte commented Jun 12, 2017 via email

@marcelstoer
Copy link
Member

@joysfera I think the dofile() vs. require() discussion could be fed into #1899 for @TerryE to be addressed in the FAQ update?

@TerryE
Copy link
Collaborator

TerryE commented Jun 14, 2017

I think the dofile() vs. require() discussion could be fed into #1899 for @TerryE to be addressed in the FAQ update?

@marcelstoer Marcel, thanks for the prompt. I am coming to a checkpoint in my Lua 5.3 work -- time to migrate into the Xtensa build environment -- so I will do this refresh first. 😄

}

// no change to th and tl setting
ds18b20_device_conf[0] = 0x4B;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you put the magic numbers into #defines?

- `RES` temperature resolution
- `TEMP` temperature
- `TEMP_DEC` temperature decimals for integer firmware
- `PAR` sensor parasitc flag
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo

end
end);

-- print if temperature is greater or less then a defined value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo

@marcelstoer
Copy link
Member

Can we have a quick voting among the maintainers as to whether to include this module? Terry already voted against it I understand.

@TerryE
Copy link
Collaborator

TerryE commented Jun 28, 2017

@marcelstoer, my vote is more of an abstain rather than a 👎 It's just that I personally don't see the point of doing this one in C and wouldn't personally use it, but I wouldn't wish to block its inclusion if other committers think that there is value in having it.

@devsaurus
Copy link
Member

My experience with this sensor is that people use it in many different ways. I guess that neither a C nor a Lua module would cover all. I regarded the old Lua module more like a template to start from and adapt to one's needs. The C module has a more "official" character and need more support effort on the long run.
We have a couple of modules in C that would also work as plain Lua code, so there's no point in blocking this one IMO.

@nickandrew
Copy link
Contributor

The argument against a C module is that it needs to be compiled-in. Arguments for are that it may use less memory, and be faster - which is good to ensure that any timing constraints on the 1wire bus are met. I believe the timing in this module is not critical (as it uses the onewire lib). I'd probably use this module in my devices.

All up I give it a weak thumbs-up; merge it and if problems are reported in future then reconsider.

@marcelstoer
Copy link
Member

How about we merge both #1996 and this one but cross-reference them in the documentation (I could do that after the merge)?

@nickandrew
Copy link
Contributor

I just had a look at #1996 but I don't have an opinion on its value, yet. I don't think it should hold up #2003.

@TerryE
Copy link
Collaborator

TerryE commented Jul 13, 2017

OK, I've had a look at this code.

I accept that this might be a good approach for @fetchbot 's specific requirements, but it is very functionally limited and I really don't see the point of doing this in C as it will run a lot slower than my or @vsky279 Lukas' Lua version. Why? and other points:

  • The read() function does a broadcast convert request and then uses an OW search to enumerate the OW address space (not even limiting it to DS18B20 device types). This is a slow binary-chop scan. Surely this only needs to be once or at least optional so you can do fast reads of specified thermometers?

  • The documentation is misleading as the read() returns nothing. Instead it executes the supplied cb once per DS18B20 found passing the return data as input parameters.

  • It makes a lot more sense to return the ROM1..ROM8 bytes as a single 8-byte string. Lua strings aren't the same as C strings and can be binary. You can always use the byte and substr functions to pick them apart. Ditto the input ROM array to setting().

  • The callback is called using a lua_call rather than a lua_pcall which means that any errors in the calback will panic the system (though to be fair, this is a common failing of most modules).

I still will register an abstain rather than a 👎 -- so long as we get resolution / consensus on the first 3 points.

@nickandrew Nick what are your thoughts? Am I being too pedantic?

@nickandrew
Copy link
Contributor

@TerryE I think those are fair comments. My temp code treats the device IDs as a string or array rather than individual bytes (I also munge the byte order in a way that suits me). How much slower is the discovery algorithm than reading only a specified sensor? My temp code always does a select-all; convert; then goes through the algorithm to process all responses.

I need to spend more time looking at this 😬

@TerryE
Copy link
Collaborator

TerryE commented Aug 3, 2017

BTW,

I guess you'll want to drop /lua_modules/ds18b20 with this as well?

I for one will stick a Lua version. It's faster and easier to tailor.

As to the OW enumeration algo, it's pretty slow.

@marcelstoer marcelstoer added this to the 2.1 follow-up II milestone Aug 16, 2017
@marcelstoer marcelstoer merged commit d079b84 into nodemcu:dev Aug 16, 2017
@marcelstoer
Copy link
Member

marcelstoer commented Aug 16, 2017

I believe that sometimes making the wrong decision is "less bad" than not making a decision at all. I cross-linked the C/Lua module docs: http://nodemcu.readthedocs.io/en/latest/en/modules/ds18b20/

@fetchbot fetchbot deleted the dev-ds18b20 branch August 27, 2017 09:25
eiselekd pushed a commit to eiselekd/nodemcu-firmware that referenced this pull request Jan 7, 2018
* add ds18b20 module
* add intitial eeprom value definition
* adjust read() function and address handling
@Fisherman48
Copy link

Missing leading zero of decimal part?
Using integer firmware and concatenate temp and tdec strings ex:
..romhex.."="..temp..","..tdec.." " I get 6,62 when I expect 6,0625
Other examples with 3 ds18b20 and decreasing temp:
18,250 18,250 18,437
18,62 18,62 18,250
18,62 18,62 18,250
18,62 18,125 18,250
18,0 18,62 18,250
Help

@devsaurus
Copy link
Member

..romhex.."="..temp..","..tdec.." " I get 6,62 when I expect 6,0625

Guess you need to format tdec with leading zeros:

print(string.format("%d,%04d", temp, tdec))

@Fisherman48
Copy link

Strange behaviour at 0,0 C?
Integer master modules , powered by Lua 5.1.4 on SDK 2.1.0(116b762)
adding temp + tdec in excel

Never show zero C temp- temp+tdec show -1 C when I expect zero.
Se discontinuity in graph graph
image

`
local ow_pin = 3
ds18b20.setup(ow_pin)

dstimer = tmr.create()
dstimer:register(60*1000, tmr.ALARM_AUTO, function()

-- read all sensors and print all measurement results
ds18b20.read(
    function(ind,rom,res,temp,tdec,par)
        print(ind,string.format("%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",string.match(rom,"(%d+):(%d+):(%d+):(%d+):(%d+):(%d+):(%d+):(%d+)")),res,temp,tdec,par)
    end,{});
end)

dstimer:start()`

/Fisherman

@Fisherman48
Copy link

After a cold night the same effect occurs at all negative x.0 temps from -2.0, -3.0 to -9.0
Is it something due to scaling and 2'nd complement for neg temps for integer version of module?
/Fisherman

@TerryE
Copy link
Collaborator

TerryE commented Mar 2, 2018

@Fisherman48, have tried extending the wait time Line 188 out to 800 or 900 mSec. 760 is only 10mSec over the datasheet minimum, and the code might just be jumping in too early before the reading is complete in cold weather. I use 1sec in my Lua version but then again I do all of my device converts in parallel then loop around the devices to do the read, so it still works out a lot faster.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants