Planning to measure temperature using ds18b20 probe and raspberry pi 2 model B. 2 different gpio pins to connect. Whenever power on the raspberry, it need to auto start the program to measure the temperature every an hour and send to Excel file thru lan connection
Assistant
To achieve your goal of measuring temperature using two DS18B20 probes connected to different GPIO pins on a Raspberry Pi 2 Model B, auto-starting the measurement program on boot, recording the temperature every hour, and sending the data to an Excel file via LAN connection, you'll need to follow several steps involving hardware setup, software configuration, and network file sharing. Here's a comprehensive guide to help you through the process.
1. Hardware Setup
Materials Needed
Raspberry Pi 2 Model B
Two DS18B20 Temperature Sensors
Two 4.7kΩ Resistors
Breadboard and Jumper Wires
LAN Connection (Ethernet cable or Wi-Fi)
Connecting the DS18B20 Sensors
The DS18B20 is a digital temperature sensor that communicates using the 1-Wire protocol. Each sensor has a unique 64-bit serial code, allowing multiple devices to be connected to the same data line. However, since you want to connect them to two different GPIO pins, we'll set up two separate 1-Wire buses.
Wiring Diagram
For each DS18B20 sensor:
Connect the VDD Pin (Pin 3) to the 3.3V pin on the Raspberry Pi.
Connect the GND Pin (Pin 1) to a Ground pin on the Raspberry Pi.
Connect the Data Pin (Pin 2) to a GPIO pin on the Raspberry Pi (e.g., GPIO4 for the first sensor and GPIO17 for the second sensor).
Connect a 4.7kΩ pull-up resistor between the Data line and 3.3V for each sensor.
GPIO Pins: Use physical pin numbers for clarity (GPIO4 is Pin 7, GPIO17 is Pin 11)
Wiring Steps
First Sensor (Sensor A):
Data Pin to GPIO4 (Pin 7)
4.7kΩ resistor between Data Pin and 3.3V (Pin 1 or 17)
Second Sensor (Sensor B):
Data Pin to GPIO17 (Pin 11)
4.7kΩ resistor between Data Pin and 3.3V (Pin 1 or 17)
Note: Ensure that each sensor's Data line has its own pull-up resistor to 3.3V.
2. Software Configuration
Enable 1-Wire Interface on Multiple GPIO Pins
By default, the Raspberry Pi supports the 1-Wire interface on GPIO4. To enable 1-Wire on additional GPIO pins, you need to modify the boot configuration.
Edit /boot/config.txt
Open the config file:
bash
sudo nano /boot/config.txt
Add the following lines at the end of the file to enable 1-Wire on GPIO4 and GPIO17:
You should see directories starting with 28-, representing each sensor's unique ID.
3. Writing the Temperature Measurement Program
You'll need to write a Python script that reads the temperature from both sensors, logs the data, and sends it to an Excel file via LAN.
Create the Python Script
bash
cd ~
nano temperature_logger.py
Python Script Content
python
from w1thermsensor import W1ThermSensor
import datetime
import csv
import os
# Function to get sensor IDs
def get_sensor_ids():
base_dir = '/sys/bus/w1/devices/'
device_folders = os.listdir(base_dir)
sensor_ids = [folder for folder in device_folders if folder.startswith('28-')]
return sensor_ids
# Get sensor IDs
sensor_ids = get_sensor_ids()
# Initialize sensors
sensors = [W1ThermSensor(sensor_id=sensor_id) for sensor_id in sensor_ids]
# Get current timestamp
timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
# Read temperatures
temperatures = [sensor.get_temperature() for sensor in sensors]
# Prepare data row
data_row = [timestamp] + temperatures
# Define CSV file path
file_path = '/home/pi/temperature_data.csv'
# Check if file exists
file_exists = os.path.isfile(file_path)
# Write data to CSV
with open(file_path, 'a', newline='') as csvfile:
csvwriter = csv.writer(csvfile)
if not file_exists:
# Write header
header = ['Timestamp'] + [f'Temperature Sensor {i+1}' for i in range(len(sensors))]
csvwriter.writerow(header)
# Write data row
csvwriter.writerow(data_row)
# Here you can add code to send the CSV file over LAN
Note:
This script automatically detects the connected DS18B20 sensors.
It logs the timestamp and temperatures from all detected sensors.
Replace /home/pi/temperature_data.csv with the path where you want to save the CSV file.
Make the Script Executable
bash
chmod +x temperature_logger.py
4. Setting Up Auto-Start and Scheduled Execution
Auto-Starting the Program on Boot
We will use crontab to schedule the script to run every hour and also run it at boot.
0 * * * * schedules the script to run at minute 0 of every hour.
5. Sending Data to an Excel File via LAN
Since Excel files (.xlsx) are proprietary formats, it's easiest to save your data as a CSV file, which Excel can open. To make this file accessible over your LAN, you can share it using one of several methods.
Option A: Using Samba to Share the File
Install Samba
bash
sudo apt-get install samba samba-common-bin
Configure Samba
Open the Samba configuration file:
bash
sudo nano /etc/samba/smb.conf
Add the following at the end of the file:
ini
[TemperatureData]
path = /home/pi
browsable = yes
writeable = yes
only guest = no
create mask = 0777
directory mask = 0777
public = yes
This shares the /home/pi directory over the network.
Set a Samba password for the pi user:
bash
sudo smbpasswd -a pi
Enter a password when prompted.
Restart the Samba service:
bash
sudo systemctl restart smbd
Access the Shared Folder from Another Computer
On a Windows PC:
Open File Explorer.
In the address bar, type \\raspberrypi\TemperatureData or \\<Raspberry_Pi_IP_Address>\TemperatureData.
Enter the username pi and the Samba password you set earlier.
You should see temperature_data.csv in the shared folder, which you can open with Excel.
Option B: Synchronizing Files via SFTP or SCP
You can set up an automated script on another computer to pull the CSV file from the Raspberry Pi using scp (secure copy) or sftp.
Using SCP to Copy File to Remote Machine
Set Up SSH Access
Ensure SSH is enabled on your Raspberry Pi:
bash
sudo raspi-config
Go to Interface Options -> SSH -> Enable
Copy File from Raspberry Pi to Remote Machine
On the remote machine (e.g., another Linux computer or Windows machine with SCP client), set up a scheduled task to copy the file.