[split] Check presence of a file - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: [split] Check presence of a file (/thread-11116.html) |
[split] Check presence of a file - pascale - Jun-22-2018 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 RE: [split] Check presence of a file - gontajones - Jun-23-2018 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) RE: [split] Check presence of a file - pascale - Jun-23-2018 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! RE: [split] Check presence of a file - gontajones - Jun-23-2018 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! RE: [split] Check presence of a file - pascale - Jun-23-2018 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 !! RE: [split] Check presence of a file - gontajones - Jun-23-2018 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 RE: [split] Check presence of a file - pascale - Jun-23-2018 hi Gontajones, I think I have understood what you are saying, Thank you again, Kind regards Paul RE: [split] Check presence of a file - pascale - Jun-24-2018 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):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 RE: [split] Check presence of a file - pascale - Jun-24-2018 gontajones I should have read all your answers not just the last 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 RE: [split] Check presence of a file - snippsat - Jun-24-2018 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.0When read_sensor function has no return value only print(),it will return None .
|