===== Freezer Temperature Control System (-80) using Raspberry Pi =====
==== Hardware ====
To adapt the PT100 sensor, the MAX31855 can be used, which communicates with the Raspberry Pi via SPI.
Both software SPI (using any GPIO pins) and hardware SPI are options.
If unsure, it is preferable to use software SPI due to its flexibility, as speed is not critical when using the MAX31855.
==== Hardware Setup for Software SPI ====
To use software SPI, connect the MAX31855 to the Raspberry Pi as shown in the figure.
Any three GPIO pins can be used for CLK, CS, and DO.
{{:sistema_control_de_temperatures_ultracongelador_-80c:max31855.jpg?400|}}
- Connect Pi 3.3V to MAX31855 Vin.
- Connect Pi GND to MAX31855 GND.
- Connect Pi GPIO 18 to MAX31855 DO.
- Connect Pi GPIO 24 to MAX31855 CS.
- Connect Pi GPIO 25 to MAX31855 CLK.
{{ :raspberry_pi:projects:max31855.jpg?600 |}}
==== Software ====
=== Dependencies ===
First, install the required dependencies by running the following commands:
sudo apt-get update
sudo apt-get install build-essential python-dev python-pip python-smbus git
=== RPi.GPIO Library ===
Ensure that the GPIO libraries are installed by running:
sudo pip install RPi.GPIO
=== MAX31855 Library ===
Download the MAX31855 Python library to the home directory by running:
cd /home
git clone https://github.com/adafruit/Adafruit_Python_MAX31855
cd Adafruit_Python_MAX31855
sudo python setup.py install
=== Configuration and Testing ===
To configure and test the hardware/software setup, use the example programs included in the `examples` directory and open the `simpletest.py` script:
sudo nano simpletest.py
Scroll down to find the configuration options and uncomment the relevant block for your setup:
# Uncomment one of the blocks of code below to configure your Pi or BBB to use
# software or hardware SPI.
# Raspberry Pi software SPI configuration.
CLK = 25
CS = 24
DO = 18
sensor = MAX31855.MAX31855(CLK, CS, DO)
Make sure the uncommented configuration matches your physical wiring.
Save and exit the text editor, then test the setup by running:
sudo python simpletest.py
If everything is correct, you will see output like this:
Press Ctrl-C to quit.
Thermocouple Temperature: 22.000*C / 71.600*F
Internal Temperature: 23.312*C / 73.963*F
==== MySQL Configuration for Storing Temperature Data ====
Install MySQL/MariaDB and necessary modules:
sudo apt-get install mysql-server python-mysqldb
Log into the MySQL console:
sudo mysql -u root -p -h localhost
Create a database named "temperatures":
CREATE DATABASE temperatures;
USE temperatures;
CREATE USER 'logger'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON temperatures.* TO 'logger'@'localhost';
FLUSH PRIVILEGES;
Create a table to store temperature and timestamp data:
CREATE TABLE temperaturedata (dateandtime DATETIME, sensor VARCHAR(32), temperature DOUBLE, humidity DOUBLE);
Restart MySQL to apply changes:
sudo /etc/init.d/mysql restart
=== Reading Sensor Data and Writing to the Database ===
The `simpletest2.py` program reads sensor data and writes it to the database:
import time
import datetime
import Adafruit_GPIO.SPI as SPI
import Adafruit_MAX31855.MAX31855 as MAX31855
import mysql.connector
# Raspberry Pi software SPI configuration.
CLK = 25
CS = 24
DO = 18
sensor = MAX31855.MAX31855(CLK, CS, DO)
temp = sensor.readTempC()
now = datetime.datetime.now()
timestamp = now.strftime("%Y/%m/%d %H:%M")
print('Temperature: {0:0.2F} C degrees'.format(temp))
conexion1 = mysql.connector.connect(host="localhost", user="logger", passwd="password", database="temperatures")
cursor1 = conexion1.cursor()
sql = "insert into temperaturedata(dateandtime, sensor, temperature, humidity) values (%s,%s,%s,%s)"
data = (timestamp, "sensor_1", temp, 20.0)
cursor1.execute(sql, data)
conexion1.commit()
==== Sending Data to Thingspeak ====
The `IOT.py` program sends data to a Thingspeak account:
import urllib2
import Adafruit_MAX31855.MAX31855 as MAX31855
CLK = 25
CS = 24
DO = 18
sensor = MAX31855.MAX31855(CLK, CS, DO)
miWriteAPIKey = "YOUR_API_KEY"
def getSensorData():
T = sensor.readTempC()
return str(T)
def main():
baseURL = 'https://api.thingspeak.com/update?api_key=%s' % miWriteAPIKey
while True:
try:
T = getSensorData()
f = urllib2.urlopen(baseURL + "&field1=%s" % T)
print(f.read())
f.close()
time.sleep(5)
except:
print('Error sending data')
break
if __name__ == '__main__':
main()