From 25382c3396d5a5f27bc1e70e6b6bf95757f9a9ec Mon Sep 17 00:00:00 2001 From: Jose Date: Tue, 22 Apr 2025 10:36:02 +0200 Subject: [PATCH] Co-authored-by: clajimbus --- hardware/include/BMP280.hpp | 3 +-- hardware/include/main.hpp | 25 ++++++++--------------- hardware/lib/BMP280.cpp | 17 ---------------- hardware/src/BMP280.cpp | 25 +++++++++++++++++++++++ hardware/{lib => src}/MQ7.cpp | 12 +++++------ hardware/src/main.cpp | 38 ++++++++++++++++++++++++++--------- 6 files changed, 68 insertions(+), 52 deletions(-) delete mode 100644 hardware/lib/BMP280.cpp create mode 100644 hardware/src/BMP280.cpp rename hardware/{lib => src}/MQ7.cpp (64%) diff --git a/hardware/include/BMP280.hpp b/hardware/include/BMP280.hpp index 858bbf3..69414cf 100644 --- a/hardware/include/BMP280.hpp +++ b/hardware/include/BMP280.hpp @@ -4,5 +4,4 @@ #define I2C_BMP280_ADDRESS 0x76 void BMP280_Init(); -uint8_t BMP280_DataReady(); -void BMP280_Read(); \ No newline at end of file +bool BMP280_Read(float &temperature, float &pressure, float &altitude); \ No newline at end of file diff --git a/hardware/include/main.hpp b/hardware/include/main.hpp index f556608..44105ba 100644 --- a/hardware/include/main.hpp +++ b/hardware/include/main.hpp @@ -1,23 +1,14 @@ -<<<<<<< HEAD #include -#include "json.hpp" -#include "rest.hpp" -#include "wifi.hpp" -#include "test.hpp" -#include "mqtt.hpp" - #define LED 2 #define SERVER_IP "192.168.1.178" #define REST_PORT 80 #define MQTT_PORT 1883 -======= -#include - -#include "JsonTools.hpp" -#include "RestClient.hpp" -#include "WifiConnection.hpp" -#include "MqttClient.hpp" - -uint32_t getChipID(); ->>>>>>> main + +#include "JsonTools.hpp" +#include "RestClient.hpp" +#include "WifiConnection.hpp" +#include "MqttClient.hpp" +#include "BMP280.hpp" + +uint32_t getChipID(); \ No newline at end of file diff --git a/hardware/lib/BMP280.cpp b/hardware/lib/BMP280.cpp deleted file mode 100644 index 4c455cc..0000000 --- a/hardware/lib/BMP280.cpp +++ /dev/null @@ -1,17 +0,0 @@ -#include "BMP280.h" - - -void BMP280_Init() -{ - -} - -uint8_t BMP280_DataReady() -{ - -} - -void BMP280_Read(float &temperature, float &humidity, float &pressure, float &altitude) -{ - -} \ No newline at end of file diff --git a/hardware/src/BMP280.cpp b/hardware/src/BMP280.cpp new file mode 100644 index 0000000..ffbe24d --- /dev/null +++ b/hardware/src/BMP280.cpp @@ -0,0 +1,25 @@ +#include "BMP280.hpp" + +BMP280_DEV bme; + +void BMP280_Init() +{ + Wire.setPins(21, 22); + if (!bme.begin(NORMAL_MODE, I2C_BMP280_ADDRESS)) + { + Serial.println("BMP280 no detectado o error en la inicialización"); + } + else + { + Serial.println("BMP280 inicializado correctamente"); + } + bme.setTimeStandby(TIME_STANDBY_2000MS); + bme.startNormalConversion(); +} + +bool BMP280_Read(float &temperature, float &pressure, float &altitude) +{ + bme.getCurrentMeasurements(temperature, pressure, altitude); + return (temperature != 0.0f && pressure != 0.0f); +} + diff --git a/hardware/lib/MQ7.cpp b/hardware/src/MQ7.cpp similarity index 64% rename from hardware/lib/MQ7.cpp rename to hardware/src/MQ7.cpp index e5d7ab8..f7fbbe0 100644 --- a/hardware/lib/MQ7.cpp +++ b/hardware/src/MQ7.cpp @@ -1,20 +1,20 @@ -#include "MQ7.h" +#include "MQ7.hpp" void MQ7_Init() { - pinMode(digitalMQ7, INPUT); - pinMode(analogMQ7, INPUT); + pinMode(DIGITAL_MQ7, INPUT); + pinMode(ANALOG_MQ7, INPUT); } void MQ7_Read(float &sensorVolt, float &RSAir, float &R0, float &sensorValue) { - analogWrite(analogMQ7, 1023); + analogWrite(ANALOG_MQ7, 1023); delay(60000); - analogWrite(analogMQ7, (1023/5)*1.4 ); + analogWrite(ANALOG_MQ7, (1023/5)*1.4 ); for(int i = 0; i<100; i++) { - sensorValue = sensorValue + analogRead(analogMQ7); + sensorValue = sensorValue + analogRead(ANALOG_MQ7); delay(90000); } diff --git a/hardware/src/main.cpp b/hardware/src/main.cpp index faa587a..3fc5b66 100644 --- a/hardware/src/main.cpp +++ b/hardware/src/main.cpp @@ -4,30 +4,31 @@ const uint32_t deviceId = getChipID(); // instances HTTPClient httpClient; -BMP280_DEV bme; // HTTP Request String response; // MQ7 -float sensorVolt, sensorValue, RSAir, R0; +float sensorVolt, sensorValue, RSAir, R0; // BMP280 -float temperature, humidity, pressure, altitude; +float temperature, pressure, altitude; uint32_t getChipID() { uint32_t chipId; - for (int i = 0; i < 17; i = i + 8) { + for (int i = 0; i < 17; i = i + 8) + { chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i; } return chipId; } -void setup() { +void setup() +{ Serial.begin(9600); - // WiFi Connection + /*// WiFi Connection if(setupWifi() != 0) { Serial.print("Error connecting to WiFi"); @@ -35,9 +36,26 @@ void setup() { // test get getRequest(httpClient, "http://172.20.10.7:8082/api/v1/sensors/1/values", response); - deserializeSensorValue(httpClient, httpClient.GET()); + deserializeSensorValue(httpClient, httpClient.GET()); */ + + BMP280_Init(); } - -void loop() { - + +void loop() +{ + if (BMP280_Read(temperature, pressure, altitude)) + { + Serial.print("Temperature: "); + Serial.println(temperature); + Serial.print("Pressure: "); + Serial.println(pressure); + Serial.print("Altitude: "); + Serial.println(altitude); + } + else + { + Serial.println("❌ Lectura fallida del BMP280"); + } + + delay(2000); } \ No newline at end of file