Python Forum
[split] Check presence of a file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] Check presence of a file
#1
Hi,
I have been trying to create something like this for a while to use with a flask app, I am trying to learn python3 so I am lost with the above, I want to create a module that I can import

 def read_sensor(sensorID)
   tempfile = open("/sys/bus/w1/devices/"+ sensorID +"/w1_slave")
    thetext = tempfile.read()
    tempfile.close()
    tempdata = thetext.split("\n") [1].split(" ")[9]
    temperature = float(tempdata[2:])
    temp = temperature / 1000
    print (temp"sensorname")


with a seperate file with a list of the sensors

# Execute DS18B20 function for sensor ID's below.
    solar_panel = float(read_ds18b20('28-0516a3ff90ff'))   
    top_cyl = float(read_ds18b20('28-0317003f1cff'))   
    flow_panel = float(read_ds18b20('28-0416c4a3a0ff'))   
(I have stolen the code from above) I want the name of the sensor to print out with its temperature. I know the code needs to be modified.
Can any one tell me if the first code will work as a module to be imported ie:
import readsensor as sensorID ??? and if then I would be able to call the function with:

read_sensor(solar_panel) ???
so that I can then use the returned value to compare with another to operate the solar pump ie:

while
solar_panel + 5 > flow_panel
GPIO.output(relay,GPIO.HIGH)
As you can see I have a lot to learn!!!
I am using a pi zero 4gb card stretch 9 lite via ssh
Any help greatly wanted
Paul
Reply
#2
If your first code is in a file, for example, called mySensorModule.py.
You can import all of its content with:
import mySensorModule as mS
# To call read_sensor()
sensorID = 1
mS.read_sensor(sensorID)
Or...
from mySensorModule import read_sensor
# Call read_sensor()
sensorID = 1
read_sensor(sensorID)
Reply
#3
Hi gontajones
Thank you for your reply, I will be using two files as modules that will be imported into a flask app and other other apps, the first module is called mySensorModule.py
def read_sensor(sensorID)
  tempfile = open("/sys/bus/w1/devices/"+ sensorID +"/w1_slave")
   thetext = tempfile.read()
   tempfile.close()
   tempdata = thetext.split("\n") [1].split(" ")[9]
   temperature = float(tempdata[2:])
   temp_sensor = temperature / 1000
   print (temp_sensor)
the second file called mySensors.py will have a list of sensors
# Execute DS18B20 function for sensor ID's below.
sensorsID {
    solar_panel = float(read_ds18b20('28-0516a3ff90ff')),   
    top_cyl = float(read_ds18b20('28-0317003f1cff')),
    flow_panel = float(read_ds18b20('28-0416c4a3a0ff')), }
So if i use your script like this
from mySensorModule import read_sensor
from mySensors import sensorsID
# Call read_sensor()
sensorID = solar_panel
read_sensor(sensorID)
Will this work ??????? what needs to be corrected in this script ????
I am trying to learn python so that I can Make a home heating app using a raspberry pi, and then after that many other things !!! my daughter introduced me to pi, (to my wifes discuss as I am now addicted to learning Python)
Tommorrow is Sunday My wife is out all morning so I really want to make good progress if I can, All help greatly needed,
regards Paul
(sorry I hijacked another persons thread, but I thought that as it was about ds18b20 sensors it made sense rather than start a new one! Blush
Reply
#4
The first file:
def read_sensor(sensorID)
   tempfile = open("/sys/bus/w1/devices/"+ sensorID +"/w1_slave")
   thetext = tempfile.read()
   tempfile.close()
   tempdata = thetext.split("\n")[1].split(" ")[9]
   temperature = float(tempdata[2:])
   temp_sensor = temperature / 1000
   print (temp_sensor)
Seconde file:
# Execute DS18B20 function for sensor ID's below.
# As sensorsID is a dictionary, you declare it like this:
sensorsID = {
    'solar_panel': float(read_ds18b20('28-0516a3ff90ff')),   
    'top_cyl': float(read_ds18b20('28-0317003f1cff')),
    'flow_panel': float(read_ds18b20('28-0416c4a3a0ff'))
}
Now, in the last file:
from mySensorModule import read_sensor
from mySensors import sensorsID
# Accessing a dictionary: dict_name[key] = value
sID = sensorsID['solar_panel']
read_sensor(sID)
# Or...
read_sensor(sensorsID['solar_panel'])
PS: Raspi is so cool!
Reply
#5
gontajones,
Thank you so much, I can now use this to display the temperatures with a flask app, to local network, and then I can create another file to operate a relay for the solar panel pump. I want to keep things a simple as possible, rather than having one large file with lots of script, i will have several files so that I can easily adapt this to other uses and update sensor names quickly, if needed. I am so excited, tomorrow I can really get on !!!
Thank you again for your help
Regards Paul
I am now reading about flask apps ready for tommorrow, and yes if only raspi was about 40 years ago !!
Reply
#6
Just be careful with naming...
from mySensorModule import read_sensor
from mySensors import sensorsID

print(sensorsID) # Will print the dict from mySensors

# But if you do something like:
sensorsID = 0
# Your import will be override...
print(sensorsID) # Will print 0
Reply
#7
hi Gontajones,
I think I have understood what you are saying, Thank you again,
Kind regards
Paul
Reply
#8
OK, Its sunday morning,
There was an error in my origional code for the sensor list as I stole this from another thread, check presence of file
# Execute DS18B20 function for sensor ID's below.
sensorsID {
    solar_panel = float(read_ds18b20('28-0516a3ff90ff')),   
    top_cyl = float(read_ds18b20('28-0317003f1cff')),
    flow_panel = float(read_ds18b20('28-0416c4a3a0ff')), }
When I tried this out in my test program I got lots of errors (heat.py)
from mySensorModule import read_sensor
from mySensors import sensorsID
# accessing a dictionary: dict_name[key] = value
sID1 = sensorsID['solar_panel']
sID2 = sensorsID['cyl_bottom']

while 1:
    sID1 + 5 > sID2
    print("it's hot")
else:
    print("got it wrong again dad")
so after many changes this new code worked
# Execute DS18B20 function for sensor ID's below.
# As sensorsID is a dictionary, you declare it like this:
sensorsID = {
    'solar_panel': '28-0516a3ff90ff',
    'top_cyl': '28-0317003f1cff',
    'flow_panel': '28-0416c4a3a0ff',
    'cyl_bottom': '28-03170017b5ff'
}
gonatajones I followed your script and this is the modified version, But heat.py only works if I remove the "+ 5" from the "while 1:" loop. If I leave that in I get;
Quote:Traceback (most recent call last):
File "temp.py", line 8, in <module>
sID1 + 5 > sID2
TypeError: Can't convert 'int' object to str implicitly
I want to import RPi.GPIO to operate a relay to work the solar pump automatically.
Some may say "Whytry to reinvent the wheel" search google an you will fin many tutorials tha give you the code, and I have done this, But you learn little just copying others you have got to try it youself to learn!
So keep watching, and I will post all changes to my Script as I go.
But any and all help is greatly needed!
Kind regards
Paul

Ok,
I tried :
if (true)
isnstance(sID1, int)
    print("true")
And I got "true" so I did the same for "x = 5" and got true so I changed my code to
from mySensorModule import read_sensor
from mySensors import sensorsID
# accessing a dictionary: dict_name[key] = value
sID1 = sensorsID['solar_panel']
sID2 = sensorsID['cyl_bottom']
x = 5

while 1:
    sID1 + x > sID2
    print("it's hot")
else:
    print("got it wrong again dad")
And I still get the same error message as above I will keep working!!
Regards Paul
ps I am now a "programmer named tim", I think I've a long way to go yet!!!

Big problem!
My code
from mySensorModule import read_sensor
from mySensors import sensorsID
# accessing a dictionary: dict_name[key] = value
sID1 = sensorsID['solar_panel']
sID2 = sensorsID['cyl_bottom']
x = 5
 
while 1:
    sID1 + x > sID2
    print("it's hot")
else:
    print("got it wrong again dad")
was comparing the id's of each sensor not the temperatures!
Back to the drawing board!
Regards
Paul Smile
Reply
#9
gontajones
I should have read all your answers not just the last Smile
new script new error code
from mySensorModule import read_sensor
from mySensors import sensorsID
import time

# accessing a dictionary: dict_name[key] = value
sID1 = sensorsID['solar_panel']
sID2 = sensorsID['cyl_bottom']

while 1:
    read_sensor(sID1) > read_sensor(sID2)
    print("it's Hot!")
    time.sleep(1)
else:
    print("Got it wrong again Dad")
57.0
35.875
Traceback (most recent call last):
File "temp.py", line 10, in <module>
read_sensor(sID1) > read_sensor(sID2)
TypeError: unorderable types: NoneType() > NoneType()

I have got the temperatures but they are not being compared
Regards Paul
I love learning
Reply
#10
The code you have posted of function read_sensor(sensorID) missing : SyntaxError.
The read read_sensor(sensorID) should also return a value as you need that later when comparing.
def read_sensor(sensorID):
    tempfile = open("/sys/bus/w1/devices/"+ sensorID +"/w1_slave")
    thetext = tempfile.read()
    tempfile.close()
    tempdata = thetext.split("\n")[1].split(" ")[9]
    temperature = float(tempdata[2:])
    temp_sensor = temperature / 1000
    return temp_sensor  # Not print()
Quote:57.0
35.875
Traceback (most recent call last):
File "temp.py", line 10, in <module>
read_sensor(sID1) > read_sensor(sID2)
TypeError: unorderable types: NoneType() > NoneType()
When read_sensor function has no return value only print(),it will return None.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to "tee" (=split) output to screen and into file? pstein 6 1,292 Jun-24-2023, 08:00 AM
Last Post: Gribouillis
  Split pdf in pypdf based upon file regex standenman 1 1,975 Feb-03-2023, 12:01 PM
Last Post: SpongeB0B
  please check this i wanna use a csv file as a graph xCj11 5 1,439 Aug-25-2022, 08:19 PM
Last Post: deanhystad
  check if a file exist on the internet and get the size kucingkembar 6 1,715 Apr-16-2022, 05:09 PM
Last Post: kucingkembar
  How to split file by same values from column from imported CSV file? Paqqno 5 2,705 Mar-24-2022, 05:25 PM
Last Post: Paqqno
  [split] Results of this program in an excel file eisamabodian 1 1,543 Feb-11-2022, 03:18 PM
Last Post: snippsat
  Code to check folder and sub folders for new file and alert fioranosnake 2 1,877 Jan-06-2022, 05:03 PM
Last Post: deanhystad
  split txt file data on the first column value shantanu97 2 2,380 Dec-29-2021, 05:03 PM
Last Post: DeaD_EyE
  [split] Help- converting file with pyton script eltomassito 6 3,193 Jul-02-2021, 05:29 PM
Last Post: snippsat
  Check last time file was accessed Pavel_47 4 2,761 Jun-01-2021, 05:47 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020