Oulu
Hello everyone,
 
I am working on a project to manage my swimmingpool pump using ignition (webpage interface) over modbus protocol.
I will write a topic to describe it so it may be usefull for others !
 
My issue today is about the use of a DS18B20 sensor on a raspberry zero and manage it on openplc.
I followed Thiaggo tutorial and had an issue on the python part. https://www.youtube.com/watch?v=WYJM7Ss6SkA
When I do the command "from w1thermosensor import w1ThermSensor" i got an error, see below :
 
alex@raspberrypi:~ $ python3
Python 3.9.2 (default, Mar 12 2021, 04:06:34)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from w1thermsensor import w1ThermSensor
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'w1ThermSensor' from 'w1thermsensor' (/home/alex/.local/lib/python3.9/site-packages/w1thermsensor/__init__.py)
 
I'm not use to python in general, so i'm a bit lost on what to do !

Someone knows the solution?
 
By the way I wired correctly my sensor and could get temperature values on prompt.
 
 
Thanks.
Reply 0 0
thiagoralves
This means that the w1ThemSensor library is not installed in your Python environment. Run from terminal
pip3 install w1thermsensor
to install it
Reply 1 0
Oulu
Hi Thiago,
 
Here what I did : 
alex@raspberrypi:~ $ pip3 install w1thermsensor
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: w1thermsensor in ./.local/lib/python3.9/site-packages (2.3.0)
Requirement already satisfied: click in /usr/lib/python3/dist-packages (from w1thermsensor) (7.1.2)
alex@raspberrypi:~ $ python3
Python 3.9.2 (default, Mar 12 2021, 04:06:34)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from w1thermsensor import w1ThermSensor
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'w1ThermSensor' from 'w1thermsensor' (/home/alex/.local/lib/python3.9/site-packages/w1thermsensor/__init__.py)
>>>
 
It seems that w1thermsensor is in python3.9 rather than python3.
Do i need to do it over python3.9? I'm waiting your answer before trying !
Thanks
Reply 0 0
thiagoralves
Try this:
python3 -m pip install w1thermsensor
sudo python3 -m pip install w1thermsensor
Make sure to install both for your user and for sudo.
Reply 1 0
Oulu
Same result :
alex@raspberrypi:~ $ python3 -m pip install w1thermsensor
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: w1thermsensor in ./.local/lib/python3.9/site-packages (2.3.0)
Requirement already satisfied: click in /usr/lib/python3/dist-packages (from w1thermsensor) (7.1.2)
alex@raspberrypi:~ $ sudo python3 -m pip install w1thermsensor
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: w1thermsensor in /usr/local/lib/python3.9/dist-packages (2.3.0)
Requirement already satisfied: click in /usr/lib/python3/dist-packages (from w1thermsensor) (7.1.2)
alex@raspberrypi:~ $ python3
Python 3.9.2 (default, Mar 12 2021, 04:06:34)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from w1thermsensor import w1ThermSensor
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'w1ThermSensor' from 'w1thermsensor' (/home/alex/.local/lib/python3.9/site-packages/w1thermsensor/__init__.py)
>>>
Reply 0 0
thiagoralves
Hmm..that's weird. It looks like there is some problem importing the module. Indeed it does not say that the module is missing, just that it can't import it. Might need to google around what that specific error means
Reply 0 0
thiagoralves
Try removing the module and installing it again
Reply 0 0
thiagoralves
Hah! Got it. You misspelled the module name. It should be:
from w1thermsensor import W1ThermSensor
 
You're using lowercase "w" on W1ThermSensor
Reply 1 0
Oulu
HAAAAAAAA, it worked.....
That one was tricky ! thanks a lot for taking time to answer me.
Reply 0 0
Oulu
Ok now I am on the PSM code part. Actually the driver selected is "raspberry pi".
As it is said in the video, when choosing "Python on linux (PSM)" we lost all GPIO links. And we have to code them again.

I have no idea how to code them, do you have any example or a full code that is close to "raspberry pi" driver?
I will add the code for temp sensor on it.
 
I keep looking for answers.
Thanks
Reply 0 0
thiagoralves
I don't have any code example, but you can search the web on how to toggle and read Raspberry Pi GPIO pins in Python. I bet it is quite simple.
Reply 1 0
Oulu
Hi everyone,
 
I finally had some time to get forward in my project.
Based on the forum I wrote a PSM code to include temp sensor and IO i am using.
Unfortunately it is not working and I don't know why.
I have the following PSM error :

  File "/home/alex/OpenPLC_v3/webserver/./core/psm/main.py", line 79, in <module>
 update_inputs()
 File "/home/alex/OpenPLC_v3/webserver/./core/psm/main.py", line 55, in update_inputs
 temperature = sensor.get_temperature()
 TypeError: get_temperature() missing 1 required positional argument: 'self'
 
#                  - OpenPLC Python SubModule (PSM) -
# 
# PSM is the bridge connecting OpenPLC core to Python programs. PSM allows
# you to directly interface OpenPLC IO using Python and even write drivers 
# for expansion boards using just regular Python.
#
# PSM API is quite simple and just has a few functions. When writing your
# own programs, avoid touching on the "__main__" function as this regulates
# how PSM works on the PLC cycle. You can write your own hardware initialization
# code on hardware_init(), and your IO handling code on update_inputs() and
# update_outputs()
#
# To manipulate IOs, just use PSM calls psm.get_var([location name]) to read
# an OpenPLC location and psm.set_var([location name], [value]) to write to
# an OpenPLC location. For example:
#     psm.get_var("QX0.0")
# will read the value of %QX0.0. Also:
#     psm.set_var("IX0.0", True)
# will set %IX0.0 to true.
#
# Below you will find a simple example that uses PSM to switch OpenPLC's
# first digital input (%IX0.0) every second. Also, if the first digital
# output (%QX0.0) is true, PSM will display "QX0.0 is true" on OpenPLC's
# dashboard. Feel free to reuse this skeleton to write whatever you want.

#import all your libraries here
import psm
import time
from w1thermsensor import W1ThermSensor
import RPi.GPIO as GPIO


#global variables
# 8 Inputs Pins GPIO
i_Pin=[2,3,4]
# 8 Ouputs Pins GPIO
q_Pin=[14,15,23]

def hardware_init():
    #Insert your hardware initialization code in here
    global sensor
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)
    # Pin Configuration 
    for i in range(0,2):
        GPIO.setup(i_Pin[i], GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
        GPIO.setup(q_Pin[i], GPIO.OUT)
    psm.start()
    sensor = W1ThermSensor

def update_inputs():
    #place here your code to update inputs
    global sensor
    temperature = sensor.get_temperature()
    temp_int = int(temperature)
    psm.set_var("IW10", temp_int)
    for i in range(0,2):
        i_Str = 'IX0.'+str(i)
        i_State = int(GPIO.input(i_Pin[i]))
        print (i_Str,i_State)
        psm.set_var(i_Str, i_State)
    
def update_outputs():
    #place here your code to work on outputs
    for i in range(0,2):
        q_Str = 'QX0.'+str(i)
        a = psm.get_var(q_Str)
        print(q_Str,a)
        if a == True:
            GPIO.output(q_Pin[i], GPIO.HIGH)
        else:
            GPIO.output(q_Pin[i], GPIO.LOW)  


if __name__ == "__main__":
    hardware_init()
    while (not psm.should_quit()):
        update_inputs()
        update_outputs()
        time.sleep(0.1) #You can adjust the psm cycle time here
    psm.stop()
 
Any idea?
Thanks
Reply 0 0
Reply