How to use raspberry pi 2 model B with DS18B20 probe to read temperature of 2 pumps temperature? Prefer individual gpio for each probe. The program need to auto start whenever power on the raspberry and show temperature for every 2 minutes. for visual, include 3 LED. first, green light on when the program start running.2nd and 3rd LED for each probe. light up when temp less than 35. blinking if no signal or error.
Assistant
User
how to see the temperatu in terminal
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To achieve your goal of using a Raspberry Pi 2 Model B with DS18B20 probes to monitor the temperature of two pumps and control LED indicators, you can follow these steps:
Hardware Setup
Raspberry Pi 2 Model B: Ensure you have Raspbian installed and the device is connected to a network.
DS18B20 Probes:
Connect each DS18B20 sensor to its individual GPIO pin on the Raspberry Pi.
Use a 4.7kΩ resistor between the VDD and Data line of each sensor.
LEDs:
Connect each LED (green for the program start, then one each for the pump temperatures) with appropriate resistors to individual GPIO pins.
Wiring
Connect each DS18B20 data line to separate GPIO pins for individual reading.
Use VCC (3.3V) and GND for both sensors.
Connect GPIO pins for LEDs, and don't forget the current-limiting resistors.
Software Setup
Enable 1-Wire Interface:
Edit the boot configuration file to enable the 1-wire interface.
bash
sudo nano /boot/config.txt
Add the following line:
code
dtoverlay=w1-gpio
Save and exit, then reboot your Raspberry Pi.
Python Script:
Below is a Python script to read temperatures from two DS18B20 sensors and control LEDs.
python
import os
import glob
import time
import RPi.GPIO as GPIO
# Set up GPIO mode
GPIO.setmode(GPIO.BCM)
# Define GPIO pins
GREEN_LED = 18 # Program start indicator
TEMP1_LED = 23 # Temperature probe 1 indicator
TEMP2_LED = 24 # Temperature probe 2 indicator
SENSOR1_PIN = 4 # GPIO pin for sensor 1
SENSOR2_PIN = 17 # GPIO pin for sensor 2
# Setup for LEDs
GPIO.setup(GREEN_LED, GPIO.OUT)
GPIO.setup(TEMP1_LED, GPIO.OUT)
GPIO.setup(TEMP2_LED, GPIO.OUT)
# Function to read temperature
def read_temp(sensor_file):
try:
with open(sensor_file, 'r') as f:
lines = f.readlines()
if lines[0].strip()[-3:] == 'YES':
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
return temp_c
except Exception as e:
return None
# Main program loop
def main():
# Turn on the green LED to show the program is running
GPIO.output(GREEN_LED, GPIO.HIGH)
# Locate the DS18B20 devices
base_dir = '/sys/bus/w1/devices/'
device_folders = glob.glob(base_dir + '28*')
device_files = [os.path.join(folder, 'w1_slave') for folder in device_folders]
try:
while True:
temps = [read_temp(file) for file in device_files]
# Control LEDs based on temperature readings
for i, temp in enumerate(temps):
if i == 0: # Temperature sensor 1
led_pin = TEMP1_LED
else: # Temperature sensor 2
led_pin = TEMP2_LED
if temp is not None:
if temp < 35.0:
GPIO.output(led_pin, GPIO.HIGH)
else:
GPIO.output(led_pin, GPIO.LOW)
else: # No signal or error
# Blink LED to indicate error
for _ in range(5):
GPIO.output(led_pin, GPIO.HIGH)
time.sleep(0.2)
GPIO.output(led_pin, GPIO.LOW)
time.sleep(0.2)
# Wait for 2 minutes
time.sleep(120)
finally:
GPIO.cleanup()
if __name__ == '__main__':
main()
Auto Start on Boot
Create a systemd Service:
Create a new service file for the script to start automatically on boot.
This setup ensures your Raspberry Pi reads temperatures from two DS18B20 sensors and controls three LEDs based on the conditions described. The program will auto-start upon power-up. Adjust the GPIO pin numbers and script path as necessary for your setup.
To display the temperature readings in the terminal, modify the Python script to include print statements or logging for each reading cycle. This way, every time the script reads the temperatures, it will output the values to the terminal.
Here's how you can modify the script to print temperature information:
python
import os
import glob
import time
import RPi.GPIO as GPIO
# Set up GPIO mode
GPIO.setmode(GPIO.BCM)
# Define GPIO pins
GREEN_LED = 18 # Program start indicator
TEMP1_LED = 23 # Temperature probe 1 indicator
TEMP2_LED = 24 # Temperature probe 2 indicator
# Setup for LEDs
GPIO.setup(GREEN_LED, GPIO.OUT)
GPIO.setup(TEMP1_LED, GPIO.OUT)
GPIO.setup(TEMP2_LED, GPIO.OUT)
# Read temperature from a sensor
def read_temp(device_file):
try:
with open(device_file, 'r') as f:
lines = f.readlines()
if lines[0].strip()[-3:] == 'YES':
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
return temp_c
except Exception as e:
return None
# Main program loop
def main():
# Turn on the green LED to show the program is running
GPIO.output(GREEN_LED, GPIO.HIGH)
# Locate the DS18B20 devices
base_dir = '/sys/bus/w1/devices/'
device_folders = glob.glob(base_dir + '28*')
device_files = [os.path.join(folder, 'w1_slave') for folder in device_folders]
try:
while True:
temps = [read_temp(file) for file in device_files]
for i, temp in enumerate(temps):
led_pin = TEMP1_LED if i == 0 else TEMP2_LED
if temp is not None:
print(f"Sensor {i+1}: {temp:.2f}°C")
if temp < 35.0:
GPIO.output(led_pin, GPIO.HIGH)
else:
GPIO.output(led_pin, GPIO.LOW)
else:
print(f"Sensor {i+1}: Read error or no signal.")
for _ in range(5):
GPIO.output(led_pin, GPIO.HIGH)
time.sleep(0.2)
GPIO.output(led_pin, GPIO.LOW)
time.sleep(0.2)
print("Waiting for the next reading...\n")
time.sleep(120) # Wait for 2 minutes
finally:
GPIO.cleanup()
if __name__ == '__main__':
main()
Explanation:
Print Statements: The code now includes print statements that log the temperature readings from each sensor.
Error Handling: If a sensor does not provide a correct reading, an error message will be printed.
Loop Indication: A message is printed before the 2-minute wait, indicating the program is waiting before the next cycle.
Viewing the Output:
Running Manually: If you run the script in a terminal window (e.g., with python3 /home/pi/temp_monitor.py), you'll see the output directly in the terminal.
Using the Systemd Service: If you are running the script automatically as a service, you can check the service logs to see the terminal output.
bash
journalctl -u temperature_monitor.service -f
This command will show the live logs for the service, similar to seeing direct terminal output.