raspberry_pi:projects:temperature_sensor
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| raspberry_pi:projects:temperature_sensor [2025/02/05 15:23] – jmbargallo | raspberry_pi:projects:temperature_sensor [2025/02/19 08:59] (current) – 195.53.121.100 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== | + | ====== |
| + | ====== Initial Raspberry Pi Setup ====== | ||
| - | ====== Configuracio inicial raspberry pi ====== | + | See chapter on Initial Raspberry Pi Setup |
| - | Veure capitol configuració inicial Raspberry | + | ==== OS for RPI ==== |
| - | ==== OS per RPI ==== | + | Download the Raspbian/ |
| + | Download the Win32 Disk Imager program from http:// | ||
| - | Obtindre imatge | + | Start Win32 Disk Imager and select the downloaded |
| - | Obtindre programa Win32 Disk Imager | + | Imager will ask for confirmation to overwrite. Select " |
| - | Iniciar Win32 Disk Imager y seleccionar la imatge Rapsbian descarregada (.img file) amb el selector "Image File". Seleccionar la lletra on es la SD y " | + | ==== Basic Raspberry Pi Setup ==== |
| - | Imager demana confirmar sobreescritura. Seleccionar " | + | Insert the SD card into the Raspberry Pi (connect the keyboard and display before connecting the power). |
| - | ==== ajust basic Raspberry Pi ==== | + | The Raspberry Pi will boot and ask for a username and password. |
| - | + | By default, they are: | |
| - | Insertar la SD card a la Raspberry Pi (Conectar teclat y display abans de conectar la alimentacio). | + | |
| - | + | ||
| - | Rasperry Pi arrenca i demana username y password. | + | |
| - | + | ||
| - | Per defecte son : | + | |
| username: **pi** | username: **pi** | ||
| Line 30: | Line 27: | ||
| password: **raspberry** | password: **raspberry** | ||
| - | Despres de arrencar es pot entrar a configuracio amb: | + | After booting, you can enter the configuration with: |
| <code bash> | <code bash> | ||
| - | A la configuracio: | + | In the configuration: |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | - Open advanced options -> Expand filesystem |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| | | ||
| - | Finalment anar a " | + | Finally, go to " |
| <code bash> | <code bash> | ||
| - | ==== connexio | + | ==== PC Connection |
| + | From Windows, you can use PuTTY: http:// | ||
| - | + | To find the Raspberry | |
| - | Desde Windows es pot fer servir PuTTY http:// | + | |
| - | + | ||
| - | Per saber la ip de la Raspberry | + | |
| < | < | ||
| - | ==== cablejat | + | ==== Sensor |
| - | + | ||
| - | {{ : | + | |
| - | ==== instal.lacio llibreries DHT22 ==== | + | {{ : |
| - | + | ||
| + | ==== Install DHT22 Libraries ==== | ||
| - | Actualitzar la Raspberry | + | Update the Raspberry |
| <code bash> | <code bash> | ||
| Line 74: | Line 63: | ||
| sudo apt-get upgrade</ | sudo apt-get upgrade</ | ||
| - | Install Adafruit Python code. Necezsari per llegir el sensor | + | Install Adafruit Python code, necessary for reading the DHT22 sensor. |
| - | Primer, instalar compilador y llibreria python | + | First, install the compiler and Python library: |
| <code bash> | <code bash> | ||
| - | Situarse al directori on es vol instal.lar | + | Navigate to the directory where you want to install the Adafruit |
| <code bash>cd / | <code bash>cd / | ||
| - | Pot fer falta instl.lar | + | You may need to install |
| <code bash> | <code bash> | ||
| - | Clonar el repositori | + | Clone the git repository: |
| <code bash>git clone https:// | <code bash>git clone https:// | ||
| - | Anar al directori corresponent: | + | Go to the corresponding directory: |
| <code bash>cd Adafruit_Python_DHT</ | <code bash>cd Adafruit_Python_DHT</ | ||
| - | + | And finally, install the Adafruit library. Type the following and press enter: | |
| - | And finally install the Adafruit library. Type following and press enter. | + | |
| <code bash> | <code bash> | ||
| - | ==== prova del DHT22 ==== | + | ==== DHT22 Test ==== |
| - | + | Navigate to the Adafruit_Python_DHT | |
| - | Anar al directori | + | |
| <code bash>cd / | <code bash>cd / | ||
| - | provar el funcionament amb: | + | Test the operation with: |
| - | <code bash> | + | <code bash> |
| - | En el cas del cablejat del texte: | + | In the case of the wiring from the text: |
| <code bash> | <code bash> | ||
| - | Si tot esta ok, es veura la lectura del sensor. | + | If everything is correct, the sensor |
| - | + | ||
| - | ==== configuracio MySql per guardar dades Temperatura ==== | + | |
| + | ==== MySQL Configuration for Storing Temperature Data ==== | ||
| - | Instalar Mysql/MariaDb y els complements necesaris: | + | Install MySQL/MariaDB and the necessary plugins: |
| < | < | ||
| - | Entrar al mode consola de sql. | + | Enter the SQL console: |
| < | < | ||
| - | Pulsar | + | Press Enter for the password (empty), and we will enter the MariaDB console. |
| - | Sobre consola | + | On the console: |
| - | Primer, crear base de dades temperatures: | + | First, create a database called |
| <code sql> | <code sql> | ||
| - | Seleccionar la base de dades creada: | + | Select the created database: |
| <code sql>USE temperatures;</ | <code sql>USE temperatures;</ | ||
| - | Crear usuari i garantir acces a la base de dades a aquest usuari: | + | Create |
| <code sql> | <code sql> | ||
| Line 149: | Line 135: | ||
| FLUSH PRIVILEGES;</ | FLUSH PRIVILEGES;</ | ||
| - | El usuari/privilegis estan creats. Ara es possible cambiar de l´usuari | + | The user/privileges are created. Now, you can switch from the root user to this new user: |
| <code sql> | <code sql> | ||
| - | Tornar a conectar amb el nou usuari | + | Reconnect with the new user: |
| < | < | ||
| - | Donar la contrasenya implementada al pas anterior. | + | Enter the password created earlier. |
| - | Crear dues taules. Temperaturedata per guardar les dades del sensor | + | Create two tables: `temperaturedata` to store the sensor data (date/time, sensor, temperature, |
| - | Per crear les taules: | + | To create the tables: |
| <code sql>USE temperatures;</ | <code sql>USE temperatures;</ | ||
| - | Crear la primera taula amb les columnes dateandtime, sensor, temperature | + | Create the first table with columns for date/time, sensor, temperature, and humidity: |
| <code sql> | <code sql> | ||
| - | Crear segona taula amb columnes dateandtime, triggedsensor, triggedlimit y lasttemperature: | + | Create the second table with columns for date/time, triggered sensor, triggered limit, and last temperature: |
| <code sql> | <code sql> | ||
| - | Es pot confirmar, que les taules buides estan creades amb: | + | You can confirm that the empty tables were created with: |
| - | <code sql >SELECT * FROM mailsendlog; | + | <code sql> |
| SELECT * FROM temperaturedata;</ | SELECT * FROM temperaturedata;</ | ||
| - | Si les taules exiteixen, es veura: "Empty Set (0.00 sec)" | + | If the tables exist, you will see: "Empty Set (0.00 sec)" |
| - | La base de dades esta configurada es pot sortir de mysql: | + | The database is configured, and you can exit MySQL: |
| < | < | ||
| - | Reiniciar mysql perque els canvis tinguin efecte: | + | Restart MySQL to apply the changes: |
| < | < | ||
| + | ==== Temperature Logger Code ==== | ||
| - | ==== temperature Logger Code ==== | + | Make sure you are in the directory where you want to install the DHT22-TemperatureLogger, |
| + | <code bash>cd / | ||
| - | Asegurarse de estar en el directori on es vol instal.lar el DHT22-TemperatureLogger, | + | Clone the git repository: |
| - | + | ||
| - | <code bash >cd / | + | |
| - | + | ||
| - | Clonar el respositori | + | |
| <code bash>git clone https:// | <code bash>git clone https:// | ||
| - | Anar al directori | + | Go to the DHT22-TemperatureLogger |
| <code bash>cd / | <code bash>cd / | ||
| - | Obrir config.json | + | Open `config.json` in an editor: |
| <code bash> | <code bash> | ||
| - | Llistat de configuracions: | + | Configuration list: |
| <code python> | <code python> | ||
| - | #mysql:Configuracio de la base de dades. | + | # |
| - | # | + | # |
| - | # | + | #name: sensor |
| - | # | + | # |
| - | # | + | # |
| - | #Humidity limits | + | #Humidity limits |
| - | #Threshold :llindar de activacio de advertencies. | + | # |
| - | # | + | # |
| - | # | + | # |
| - | # | + | # |
| - | # | + | # |
| - | # | + | #Password: email password. |
| - | # | + | # |
| - | # | + | # |
| - | # | + | # |
| - | # | + | # |
| - | # | + | #backupDay: day for the backup. 1-7 (Monday to Sunday) 0 daily. |
| - | # | + | #backupHour: hour of the dump - 0-24 |
| - | # | + | # |
| - | # | + | # |
| - | # | + | # |
| - | # | + | # |
| - | # | + | # |
| - | # | + | # |
| # | # | ||
| - | #Per reduir | + | #To reduce |
| - | # | + | # |
| </ | </ | ||
| - | Un cop fetes les configuracions, premer | + | Once the configurations are done, press Ctrl+x |
| - | <code bash >python DHT22logger.py</ | + | <code bash> |
| - | Si tot es correcte el programa escriura a la base de dades per comprovar que es aixi entrar a la base de dades: | + | If everything is correct, the program will write to the database. To verify, enter the database: |
| <code bash> | <code bash> | ||
| - | Posar la password | + | Enter the corresponding |
| <code sql>use temperatures; | <code sql>use temperatures; | ||
| Line 280: | Line 264: | ||
| select * from temperaturedata;</ | select * from temperaturedata;</ | ||
| - | Comprovar que les lectures s´escriuen a la base de dades, per sortir: | + | Verify that the readings are written to the database. To exit: |
| <code sql> | <code sql> | ||
| - | ==== lectura automatica | + | ==== Automatic |
| - | + | Add a cron task: | |
| - | Afegir una tasca crontab: | + | |
| < | < | ||
| - | Seleccionar opcio / | + | Select the / |
| - | Afegir al crontab | + | Add the task to the crontab (for example, every 15 minutes): |
| < | < | ||
| - | Sortir i guardar | + | Exit and save with Ctrl+x, selecting |
| - | ==== Configurar | + | ==== Configure Static |
| - | Per configurar una IP estática | + | To configure |
| < | < | ||
| - | Si mirem el contingut veurem linies comentades | + | If you look at the content, you will see commented lines (starting with '#' |
| < | < | ||
| Line 316: | Line 299: | ||
| </ | </ | ||
| - | ==== thinkspeak | + | ==== ThinkSpeak |
| - | + | ||
| - | + | ||
| - | ==== Configuració del compte de ThingSpeak.com ==== | + | |
| - | Registrar-se a https:// | + | ==== ThingSpeak |
| - | Si no tenim compte previ a MathWorks | + | |
| - | Crear un nou channel al nostre perfil i afegir dos field chart (Temperatura i Humitat). | + | Register at https:// |
| - | Obtindre el "Write API Key" del channel creat servira per configurar el script de conexio | + | If you don't have a previous account with MathWorks, ThingSpeak will redirect you, with the option to use the same email to register at: https:// |
| - | ====Script Python per el envíament de dades al compte de ThingSpeak | + | Create a new channel in your profile and add two field charts (Temperature and Humidity). |
| + | Get the "Write API Key" for the created channel, which will be used to configure the ThingSpeak | ||
| + | ==== Python Script for Sending Data to ThingSpeak ts.py ==== | ||
| <code python> | <code python> | ||
| Line 338: | Line 318: | ||
| # Write API Key ThingSpeak.com | # Write API Key ThingSpeak.com | ||
| miWriteAPIKey = " | miWriteAPIKey = " | ||
| - | # Número | + | # GPIO number for DHT22 sensor |
| raspiNumGPIO = " | raspiNumGPIO = " | ||
| def getSensorData(): | def getSensorData(): | ||
| Line 344: | Line 324: | ||
| | | ||
| def main(): | def main(): | ||
| - | print 'Iniciando...' | + | print 'Starting...' |
| | | ||
| while True: | while True: | ||
| Line 354: | Line 334: | ||
| | | ||
| | | ||
| - | print 'Terminado.' | + | print 'Done.' |
| break | break | ||
| if __name__ == ' | if __name__ == ' | ||
| main() | main() | ||
| </ | </ | ||
| - | ==== Canvis al codi basic ==== | ||
| + | ==== Changes to the Basic Code ==== | ||
| - | A miWriteAPIKey | + | In `miWriteAPIKey`, input the Write API Key for the corresponding account at thingspeak. |
| < | < | ||
| - | Canviat: | + | Change: |
| < | < | ||
| - | S´ha canviat l´ordre per que apareixi primer la temperatura que es la dada mes rellevant. | + | The order has been switched so the temperature appears first, as it is the most important data. |
| - | ==== automatitzar l´enviament de dades ==== | + | |
| + | ==== Automate Data Sending ==== | ||
| - | Afegir una tasca crontab: | + | Add a cron task: |
| <code bash> | <code bash> | ||
| - | Seleccionar opcio / | + | Select the / |
| - | Afegir al crontab | + | Add the task to the crontab (for example, every 15 minutes): |
| - | <code >*/15 * * * * python / | + | < |
| - | Sortir i guardar | + | Exit and save with Ctrl+x, selecting |
| - | + | ||
| - | + | ||
| - | ==== Canals Thingspeak ==== | + | |
| + | ==== ThingSpeak Channels ==== | ||
| * [[https:// | * [[https:// | ||
| * [[https:// | * [[https:// | ||
| - | ==== enviament de e-mails de alerta amb thingspeak | + | ==== Sending Alert Emails with ThingSpeak |
| + | ==== MATLAB Analysis to Send Emails ==== | ||
| - | ==== fer un analisi MATLAB per enviar el mail ==== | + | |
| - | + | * Create a new MATLAB | |
| - | + | ||
| - | | + | |
| - | * Crear un nou MathLab | + | |
| - | + | ||
| < | < | ||
| Line 413: | Line 388: | ||
| </ | </ | ||
| - | * Ajustar el alert body, subject | + | * Adjust the alert body, subject, and the alert API key. |
| - | * Click "save and Run" | + | * Click "save and Run", and an email will be received in a few moments. |
| - | * Conectar el MATLAB Analysis | + | * Connect the MATLAB Analysis |
| {{: | {{: | ||
| - | ==== React App ==== | + | ==== React App ==== |
| + | |||
| + | React works with MATLAB® Analysis to take actions when the channel data meets certain conditions. | ||
| + | |||
| + | ==== Define Reactions to Channel Data ==== | ||
| + | |||
| + | Select Apps> | ||
| + | |||
| + | - Click "New React" | ||
| + | - Select Options | ||
| + | {{: | ||
| + | |||
| + | * React name: Name the alert | ||
| + | * Condition Type: Choose if the condition is a string, numeric, etc., in our case numeric | ||
| + | * Test Frequency: The frequency when the React should check | ||
| + | * React to Data in this channel: choose channel | ||
| + | * Input condition: define the value that triggers the alert. | ||
| + | |||
| + | The data that meets the conditions triggers a callback, either an HTTP request or an email. | ||
| + | ==== History of Alerts ==== | ||
| - | React treballa amb MATLAB® Analysis | + | After running the app, go to the Dashboard> |
| ==== Definir Reaccions a les dades del canal==== | ==== Definir Reaccions a les dades del canal==== | ||
raspberry_pi/projects/temperature_sensor.1738769007.txt.gz · Last modified: 2025/02/05 15:23 by jmbargallo
