priyanka08
Posts: 7
Joined: Mon Sep 23, 2019 10:00 am

error in temperature with buzzer sensor

Wed Sep 25, 2019 6:17 am

hello, I am doing E-health monitoring system prototype. now I calculate body temperature using DS18B20(waterproof) sensor. i want, if body temperature is increase then buzzer will automatically On and if body temperature is decrease then buzzer will Off automatically.(in while loop)

Code: Select all

import os
import glob
import time
#import RPi.GPIO as GPIO
from gpiozero import Buzzer
from time import sleep

# Initialize the GPIO Pins
os.system('modprobe w1-gpio')  # Turns on the GPIO module
os.system('modprobe w1-therm') # Turns on the Temperature module
 
# Finds the correct device file that holds the temperature data
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
 
# A function that reads the sensors data
def read_temp_raw():
  f = open(device_file, 'r') # Opens the temperature device file
  lines = f.readlines() # Returns the text
  f.close()
  return lines
 
# Convert the value of the sensor into a temperature
def read_temp():
  lines = read_temp_raw() # Read the temperature 'device file'

  # While the first line does not contain 'YES', wait for 0.2s
  # and then read the device file again.
  while lines[0].strip()[-3:] != 'YES':
    time.sleep(0.1)
    lines = read_temp_raw()
 
  # Look for the position of the '=' in the second line of the
  # device file.
  equals_pos = lines[1].find('t=')
 
  # If the '=' is found, convert the rest of the line after the
  # '=' into degrees Celsius, then degrees Fahrenheit
  if equals_pos != -1:
    temp_string = lines[1][equals_pos+2:]
    global temp_c, temp_f
    temp_c = float(temp_string) / 1000.0
    temp_f = temp_c * 9.0 / 5.0 + 32.0
    return temp_c, temp_f

#Print out the temperature until the program is stopped.
while True:
    print(read_temp())
    time.sleep(2)
    buzzer = Buzzer(23)
    if temp_c >=28 and temp_f >=82:
        while True:
            buzzer.on()
            sleep(1)
        break
  
    buzzer.off()
    time.sleep(2)
   

User avatar
bensimmo
Posts: 4184
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: error in temperature with buzzer sensor

Wed Sep 25, 2019 6:52 am

Is Python3 new to you?

You seem to not quite understand functions, e.g. how to use read_temp()

I can see it's a cut and paste from the web.

You should remove your globals, there is no need for them (I'm guessing you added them?)
Then call the function, returning the values to a variable.
Then use that for you if then checks.

You are also checking temp_c and temp_f
They are two different values of the same temperature, pick one (hint C is the better of the two) but if you are used to F, say in the USA, then use that.

Once you get that, alter read_temp() to only return on of them, it makes returning and using it a bit simpler if you are just starting out.
Any more help just shout.

User avatar
bensimmo
Posts: 4184
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: error in temperature with buzzer sensor

Wed Sep 25, 2019 7:00 am

On to the main loop.
You don't need that many sleeps, it's no harm but slows it all down.
(Not a sensor read will automatically be just under a second delay by itself)

As before the if and, pick one C or F

Take the Buzzer(23) setting out of the the whole thing and place it before the main loops start, it never changes and one set stays set.

one the Buzzer is on,, it will stay in that loop forever.
The break is not neede, not sure why it is there?

I would advise having a go at the RaspberryPi Learn tutorial on physical computing (using gpiozero like you are :-)) to get a better idea of loops and things.

Unless you want it writing for you and I can give an example, but I don't see the gain there.

priyanka08
Posts: 7
Joined: Mon Sep 23, 2019 10:00 am

Re: error in temperature with buzzer sensor

Wed Sep 25, 2019 7:11 am

bensimmo wrote:
Wed Sep 25, 2019 6:52 am
Is Python3 new to you?

You seem to not quite understand functions, e.g. how to use read_temp()

I can see it's a cut and paste from the web.

You should remove your globals, there is no need for them (I'm guessing you added them?)
Then call the function, returning the values to a variable.
Then use that for you if then checks.

You are also checking temp_c and temp_f
They are two different values of the same temperature, pick one (hint C is the better of the two) but if you are used to F, say in the USA, then use that.

Once you get that, alter read_temp() to only return on of them, it makes returning and using it a bit simpler if you are just starting out.
Any more help just shout.
yes, I use Python3.

and yes, it is web code. and i want to know how can i put buzzer if temperature is increase .

pcmanbob
Posts: 7160
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: error in temperature with buzzer sensor

Wed Sep 25, 2019 8:34 am

So looking at your code you no longer use modprobe to enable the 1-wire interface you should just enable it one using
raspi-config under interfaces.

so I have tided up your while true loop to read the temperature and than turn on or off the buzzer depending on temperature.
but I think you might need to change you values as human body surface temperature is in 34C range so your buzzer will be on all the time.

Code: Select all

import os
import glob
import time
#import RPi.GPIO as GPIO
from gpiozero import Buzzer
from time import sleep

 
# Finds the correct device file that holds the temperature data
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
 
# A function that reads the sensors data
def read_temp_raw():
  f = open(device_file, 'r') # Opens the temperature device file
  lines = f.readlines() # Returns the text
  f.close()
  return lines
 
# Convert the value of the sensor into a temperature
def read_temp():
  lines = read_temp_raw() # Read the temperature 'device file'

  # While the first line does not contain 'YES', wait for 0.2s
  # and then read the device file again.
  while lines[0].strip()[-3:] != 'YES':
    time.sleep(0.1)
    lines = read_temp_raw()
 
  # Look for the position of the '=' in the second line of the
  # device file.
  equals_pos = lines[1].find('t=')
 
  # If the '=' is found, convert the rest of the line after the
  # '=' into degrees Celsius, then degrees Fahrenheit
  if equals_pos != -1:
    temp_string = lines[1][equals_pos+2:]
    temp_c = float(temp_string) / 1000.0
    temp_f = temp_c * 9.0 / 5.0 + 32.0
    return temp_c

#Print out the temperature until the program is stopped.
while True:
    temperature = read_temp()
    print (temperature)
    time.sleep(2)
    buzzer = Buzzer(23)
    if temperature >= 28.0:
         buzzer.on()
         
    if temperature <= 27.5:
        buzzer.off()
    
    time.sleep(2)
   
note this is un-tested code so expect errors.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Beginners”