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
#21
Hi gontajones
here are the three files that you have help me write.
mySensorModule.py
#! bash/bin/python3  
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)
mySensors.py
# 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'
}
and finally temp.py
from mySensorModule import read_sensor
from mySensors import sensorsID
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(25,GPIO.OUT)
# accessing a dictionary: dict_name[key] = value
sID1 = sensorsID['solar_panel']
sID2 = sensorsID['cyl_bottom']
x = 5
try:
    while True:
        if read_sensor(sID1) + x > read_sensor(sID2):
            GPIO.output(25,GPIO.HIGH)
          # print("it's Hot!")
        else:
         #  print("Got it wrong again Dad")
            GPIO.output(25,GPIO.LOW)
      # time.sleep(10)
except KeyboardInterrupt:
    GPIO.cleanup()
I have commented out the lines for print and time. Before I run temp.py file in the command line, gpio readall shows this
Output:
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+ | | | 3.3v | | | 1 || 2 | | | 5v | | | | 2 | 8 | SDA.1 | IN | 1 | 3 || 4 | | | 5v | | | | 3 | 9 | SCL.1 | IN | 1 | 5 || 6 | | | 0v | | | | 4 | 7 | GPIO. 7 | IN | 1 | 7 || 8 | 1 | ALT0 | TxD | 15 | 14 | | | | 0v | | | 9 || 10 | 1 | ALT0 | RxD | 16 | 15 | | 17 | 0 | GPIO. 0 | IN | 0 | 11 || 12 | 0 | IN | GPIO. 1 | 1 | 18 | | 27 | 2 | GPIO. 2 | IN | 0 | 13 || 14 | | | 0v | | | | 22 | 3 | GPIO. 3 | IN | 0 | 15 || 16 | 0 | IN | GPIO. 4 | 4 | 23 | | | | 3.3v | | | 17 || 18 | 0 | IN | GPIO. 5 | 5 | 24 | | 10 | 12 | MOSI | IN | 0 | 19 || 20 | | | 0v | | | | 9 | 13 | MISO | IN | 0 | 21 || 22 | 0 | IN | GPIO. 6 | 6 | 25 | | 11 | 14 | SCLK | IN | 0 | 23 || 24 | 1 | IN | CE0 | 10 | 8 | | | | 0v | | | 25 || 26 | 1 | IN | CE1 | 11 | 7 | | 0 | 30 | SDA.0 | IN | 1 | 27 || 28 | 1 | IN | SCL.0 | 31 | 1 | | 5 | 21 | GPIO.21 | IN | 1 | 29 || 30 | | | 0v | | | | 6 | 22 | GPIO.22 | IN | 1 | 31 || 32 | 1 | IN | GPIO.26 | 26 | 12 | | 13 | 23 | GPIO.23 | IN | 1 | 33 || 34 | | | 0v | | | | 19 | 24 | GPIO.24 | IN | 1 | 35 || 36 | 1 | IN | GPIO.27 | 27 | 16 | | 26 | 25 | GPIO.25 | IN | 0 | 37 || 38 | 1 | IN | GPIO.28 | 28 | 20 | | | | 0v | | | 39 || 40 | 1 | IN | GPIO.29 | 29 | 21 | +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+ | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM | +-----+-----+---------+------+---+-Pi Zero--+---+------+---------+-----+-----+
BCM 25 shows 0 and IN, and onthe relay the little red light is off (on the relay the other lights are a dim red corrisponding to 1 and IN. Whe I run the file I get no errors and if I open another terminal I get from gpio readall
Output:
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+ | | | 3.3v | | | 1 || 2 | | | 5v | | | | 2 | 8 | SDA.1 | IN | 1 | 3 || 4 | | | 5v | | | | 3 | 9 | SCL.1 | IN | 1 | 5 || 6 | | | 0v | | | | 4 | 7 | GPIO. 7 | IN | 1 | 7 || 8 | 1 | ALT0 | TxD | 15 | 14 | | | | 0v | | | 9 || 10 | 1 | ALT0 | RxD | 16 | 15 | | 17 | 0 | GPIO. 0 | IN | 0 | 11 || 12 | 0 | IN | GPIO. 1 | 1 | 18 | | 27 | 2 | GPIO. 2 | IN | 0 | 13 || 14 | | | 0v | | | | 22 | 3 | GPIO. 3 | IN | 0 | 15 || 16 | 0 | IN | GPIO. 4 | 4 | 23 | | | | 3.3v | | | 17 || 18 | 0 | IN | GPIO. 5 | 5 | 24 | | 10 | 12 | MOSI | IN | 0 | 19 || 20 | | | 0v | | | | 9 | 13 | MISO | IN | 0 | 21 || 22 | 1 | OUT | GPIO. 6 | 6 | 25 | | 11 | 14 | SCLK | IN | 0 | 23 || 24 | 1 | IN | CE0 | 10 | 8 | | | | 0v | | | 25 || 26 | 1 | IN | CE1 | 11 | 7 | | 0 | 30 | SDA.0 | IN | 1 | 27 || 28 | 1 | IN | SCL.0 | 31 | 1 | | 5 | 21 | GPIO.21 | IN | 1 | 29 || 30 | | | 0v | | | | 6 | 22 | GPIO.22 | IN | 1 | 31 || 32 | 1 | IN | GPIO.26 | 26 | 12 | | 13 | 23 | GPIO.23 | IN | 1 | 33 || 34 | | | 0v | | | | 19 | 24 | GPIO.24 | IN | 1 | 35 || 36 | 1 | IN | GPIO.27 | 27 | 16 | | 26 | 25 | GPIO.25 | IN | 0 | 37 || 38 | 1 | IN | GPIO.28 | 28 | 20 | | | | 0v | | | 39 || 40 | 1 | IN | GPIO.29 | 29 | 21 | +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+ | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM | +-----+-----+---------+------+---+-Pi Zero--+---+------+---------+-----+-----+
as you see BCM 25 is now 1 and OUT on the relay the LED is now bright red but the 240volt test lamp has not lit up. so the problem must be my wiring 240volt side or as the module is twelve volts and I have connected it to the 5 volt gpio on pi I think this could be it.
I will find another power supply (12 volts) and have another go tommorrow.
gontajones hope all my code helps, thank you so much for all your help, so much is now clear to me and I have learnt so much as well, this will all go to help me in the future. Smile
Kind regards
Paul

Hi again, once I have sorted the electrics I am going to have to find out how to run the file permanently, in the background especially on a reboot. a CRONJOB ?
Paul
Reply
#22
If your relay module is 12V and you provide only 5V it will not behave like it should.
I recommend you to buy a 5V relay module and do not use the raspi's output pins to power it, because when you do that you may drain current from raspi and the kernel may reboot or freeze.
What I do is use the 5V relay module and power it with the same power supply for the raspi.
It works but the best system is using 2 independent power supplies.
Reply
#23
Hi gontajone,
Yuo are right it is far easier and cheaper to replace my 12v module far a 5v,
I have spent two days trying to get it to work no luck, so I have ordered a new one only 10€ free delivery, from france!
I will get back when all is working.
I have found plety of tutorials to automise the script with a cronjob
kind regards
Paul
Reply
#24
I always bought modules from China (aliexpress, dx, etc).
Just check if the seller has a good rating and pay attention to the delivery time.
Here is an example of a good seller for arduino/raspi modules:
Relay 2-channels

Quote:I have found plety of tutorials to automise the script with a cronjob
Cron is very good, but if you want something more out-of-the-box, I recommend the Supervisor package.
It comes with a lot of features for monitoring, logging and more for your application.
Reply
#25
Hi gontajones
Thank you for your link to aliexpress, I hav now ordered two 8 modules from this supplier Smile , I will investigate the supervisor for automating the script, and start a new thread if I have problems.
Kind regards
Paul
Reply
#26
Hi all
I hope you can help with this, as it is a relay problem not python. But I am struggling to get this to work below is a photo of the relay module, It is a Songle relay connected to a hl-58s v1.2 board (this means nothing to me)
[Image: %24_1.JPG?set_id=880000500F] I know my script that I have set up works as I have tested it witha simple LED, but It does not trigger the relay. on the module are A(gnd, in1, in2, in3, in4, in5, in6, in7, in8, vcc.) and also B(gnd, vcc, jd-vcc) I have connected from my pi zero 5v(2)-A(vcc), gnd(6)-A(gnd) and out(25)-A(in1) and also an external 5volt supply positive to B(jd-vcc) and negative to B(gnd), nothing has exploded or burnt out, nor has the relay operated.
I have carried out this test, I diconnected the out cble from pi leaving it connected to the module then then connect it to 5v pin on pi result = nothing

then connect it to Gnd pin on pi result = switch operates?
what does this mean and what do I need to do?
kind regards
paul
Solved it!
I changed the code in temp.py
if read_sensor(sID1) + x > read_sensor(sID2):
            GPIO.output(25,GPIO.HIGH)
          # print("it's Hot!")
to
if read_sensor(sID1) + x > read_sensor(sID2):
            GPIO.output(25,GPIO.LOW)
          # print("it's Hot!")
and it worked
Big Grin
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to "tee" (=split) output to screen and into file? pstein 6 1,288 Jun-24-2023, 08:00 AM
Last Post: Gribouillis
  Split pdf in pypdf based upon file regex standenman 1 1,971 Feb-03-2023, 12:01 PM
Last Post: SpongeB0B
  please check this i wanna use a csv file as a graph xCj11 5 1,437 Aug-25-2022, 08:19 PM
Last Post: deanhystad
  check if a file exist on the internet and get the size kucingkembar 6 1,714 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,704 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,870 Jan-06-2022, 05:03 PM
Last Post: deanhystad
  split txt file data on the first column value shantanu97 2 2,376 Dec-29-2021, 05:03 PM
Last Post: DeaD_EyE
  [split] Help- converting file with pyton script eltomassito 6 3,190 Jul-02-2021, 05:29 PM
Last Post: snippsat
  Check last time file was accessed Pavel_47 4 2,759 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