more G-Labs products

Author Topic: DS18B20 Sensors  (Read 12517 times)

September 13, 2014, 12:29:07 PM
Read 12517 times

codedmind

  • ***
  • Information
  • Full Member
  • Posts: 92
Hy there,

Before using HG i have my rpi with 3 DS18B20 sensors to check temperatures from my solar tank.

I use a python script to check the values, like adafruit examples for this sensor, i have three from this kind because this kind of sensors  can be placed in parallel

https://learn.adafruit.com/adafruits-raspberry-pi-lesson-11-ds18b20-temperature-sensing/overview

My question is how can i use the same script or should i do another one, and how, so i can have it working like the dh-11 sensor widget?

Thanks
« Last Edit: September 13, 2014, 12:43:27 PM by codedmind »

September 20, 2014, 06:28:33 AM
Reply #1

codedmind

  • ***
  • Information
  • Full Member
  • Posts: 92
No one can help?

I would love some directions how to do a hello world widget/program in HG.

Thanks

September 20, 2014, 11:34:55 AM
Reply #2

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
There should be two ways of reading 1-Wire sensors data:

1) use /sys/bus/w1/devices to access 1-wire sensors data. This way you could just create a Python app in HomeGenie and use thise code: https://www.modmypi.com/blog/ds18b20-one-wire-digital-temperature-sensor-and-the-raspberry-pi

2) directly communicating with 1-wire devices through gpio by using low level communication as it happen with the DHT-11 program. This is a lot of code to write since it has to implement all 1-wire comm specifications. In the future 1-wire protocol might be implemented as part of the RasbperrySharp-IO library (that HG is using for low level gpio communication).

I thinks the quickest way would be the 1 one. Let me know progress about this.

Cheers,
g.

September 20, 2014, 12:03:10 PM
Reply #3

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
I'm attacching the Python app so you can import it in HG.
The script just popup the current temp value. It doesn't currently implement a virtual module.

g.

September 20, 2014, 12:14:52 PM
Reply #4

codedmind

  • ***
  • Information
  • Full Member
  • Posts: 92
Thank you gene,

Later i will post my progress

Thanks

September 20, 2014, 12:20:34 PM
Reply #5

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
added modprobe commands to the script, the program will try to load required 1-wire modules first.

g.
« Last Edit: September 20, 2014, 12:22:48 PM by Gene »

September 20, 2014, 04:00:38 PM
Reply #6

codedmind

  • ***
  • Information
  • Full Member
  • Posts: 92
Hy gene,

After import i change the sensor id but i get the following error
Runtime.Error = "CR: expected str, got tuple"

I try remove the first three lines, the ones inside """ but the error keep pop

September 20, 2014, 04:13:36 PM
Reply #7

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
try changing last lines of the source code like this:

Code: [Select]


while True:
        values = read_temp()
        hg.Program.Notify("DS18B20", str(values[0]))
        time.sleep(5)
 


the read_temp() function is returning two values (array) with temperature celsius and fahrenheit, that was the problem.
Btw I'm attacching the updated program.
If this is working, then I'll add to the app a virtual sensor module, so it can be used like other hg modules.

g.
« Last Edit: September 20, 2014, 04:18:56 PM by Gene »

September 20, 2014, 04:27:20 PM
Reply #8

codedmind

  • ***
  • Information
  • Full Member
  • Posts: 92
Working like a charm :D

If you can do the virtual module would be great :D

One more question, what you sugest, in my case i have three sensors, should i replicate this programs one, for each sensor or the virtual module can receive data for the three sensors, and change the program so it have parameters to set sensors id and sensor numbers?

September 20, 2014, 05:47:32 PM
Reply #9

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
Added user-definable virtual modules (default 3).
User must specify sensor ID in the module options popup (see screenshot).
Let me know it it's working.

g.

September 20, 2014, 05:58:05 PM
Reply #10

codedmind

  • ***
  • Information
  • Full Member
  • Posts: 92
This is awsome!! Great work

Make the following changes.

line 33 changed to 
    lines = temp_raw(sensorid)  #sensorid is missing in the function call

line 46 changed to
for n in range(1, sensorcount+1): #because range(1,3) returns only 2 numbers [1,2] and if we do sensorount+1 i get range (1,4) and the return is [1,2,3]
« Last Edit: September 20, 2014, 09:16:02 PM by codedmind »

September 20, 2014, 06:29:27 PM
Reply #11

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
did you import the program? did you also disable previously imported scripts?
(sorry for silly questions =D)

Unfortunately cannot test much because I haven't got that temp sensor.
But the problem should be at line 52. It shouldn't get there if you didn't specify the sensorid in the module options, so it shouldn't give that error.
We can try surrounding the read_temp with a try..exept.
Let me know if this work.

g.

September 20, 2014, 09:14:05 PM
Reply #12

codedmind

  • ***
  • Information
  • Full Member
  • Posts: 92
Gene now is working... i edit my post, with the fix i made to make it running without errors!

It's perfect now

September 20, 2014, 09:36:10 PM
Reply #13

codedmind

  • ***
  • Information
  • Full Member
  • Posts: 92
I only want to know how can i modify somethings, for instance keep the time in the module the last update occur?

September 20, 2014, 09:48:37 PM
Reply #14

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
I am not sure to understand what you're asking.
Some more example please :)

g.