1
0

Add hardware support for GPS, MAX7219, BME280, and MQ7 sensors

- Implemented GPS functionality with initialization and reading latitude and longitude.
- Added MAX7219 display support, including initialization, text display, animation control, and brightness settings.
- Integrated BME280 sensor for reading temperature, pressure, and humidity.
- Developed MQ7 sensor interface for reading gas concentration values.
- Created HTTP client for GET and POST requests.
- Implemented MQTT client for message handling and connection management.
- Added JSON serialization and deserialization functions for sensor and actuator data.
- Established WiFi connection setup for network communication.
This commit is contained in:
Jose
2025-04-25 22:29:57 +02:00
parent 0c09fcd913
commit 51db158354
18 changed files with 441 additions and 303 deletions

View File

@@ -7,10 +7,17 @@
"functional": "cpp", "functional": "cpp",
"tuple": "cpp", "tuple": "cpp",
"utility": "cpp", "utility": "cpp",
"bmp280.h": "c" "bmp280.h": "c",
"deque": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"vector": "cpp",
"string_view": "cpp",
"initializer_list": "cpp"
}, },
"github.copilot.enable": { "github.copilot.enable": {
"*": false, "*": true,
"plaintext": false, "plaintext": false,
"markdown": false, "markdown": false,
"scminput": false, "scminput": false,

BIN
hardware/docs/MAX7219.PDF Normal file

Binary file not shown.

View File

@@ -1,7 +1,8 @@
#include <Wire.h> #include <Wire.h>
#include <BME280I2C.h> #include <BME280I2C.h>
#define I2C_BMP280_ADDRESS 0x76 #define I2C_BME280_ADDRESS 0x76
void BME280_Init(); void BME280_Init();
bool BME280_DataReady();
bool BME280_Read(float &pressure, float &temperature, float &humidity); bool BME280_Read(float &pressure, float &temperature, float &humidity);

7
hardware/include/GPS.hpp Normal file
View File

@@ -0,0 +1,7 @@
#include "TinyGPSPlus.h"
#define RX 4
#define TX 5
void GPS_Init();
void GPS_Read(float &lat, float &lon);

View File

@@ -0,0 +1,17 @@
#include <MD_Parola.h>
#include <MD_MAX72XX.h>
#include <SPI.h>
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4 // 4 modulos 8x8
#define DATA_PIN 19
#define CS_PIN 18
#define CLK_PIN 17
void MAX7219_Init();
void MAX7219_DisplayText(const char *text, textPosition_t align, int speed, int pause);
void MAX7219_StartAnimation();
void MAX7219_StopAnimation();
void MAX7219_ClearDisplay();
void MAX7219_SetBrightness(uint8_t brightness);

View File

@@ -4,4 +4,4 @@
#define DIGITAL_MQ7 32 #define DIGITAL_MQ7 32
void MQ7_Init(); void MQ7_Init();
void MQ7_Read(); void MQ7_Read(float &sensorVolt, float &RSAir, float &R0, float &sensorValue);

View File

@@ -1,8 +1,7 @@
#include <Arduino.h> #include <Arduino.h>
#define LED 2 #define SERVER_IP "https://contaminus.miarma.net/api/v1/"
#define SERVER_IP "192.168.1.178" #define REST_PORT 443
#define REST_PORT 80
#define MQTT_PORT 1883 #define MQTT_PORT 1883
#include "JsonTools.hpp" #include "JsonTools.hpp"
@@ -10,5 +9,12 @@
#include "WifiConnection.hpp" #include "WifiConnection.hpp"
#include "MqttClient.hpp" #include "MqttClient.hpp"
#include "BME280.hpp" #include "BME280.hpp"
#include "GPS.hpp"
#include "MAX7219.hpp"
#include "MQ7.hpp"
uint32_t getChipID(); uint32_t getChipID();
void prettyReadMQ7();
void prettyReadBME280();
void prettyReadGPS();
void testMatrix();

View File

@@ -12,8 +12,11 @@
platform = espressif32 platform = espressif32
board = esp32dev board = esp32dev
framework = arduino framework = arduino
upload_port = COM3
lib_deps = lib_deps =
knolleary/PubSubClient@^2.8 knolleary/PubSubClient@^2.8
mikalhart/TinyGPSPlus@^1.0.2 mikalhart/TinyGPSPlus@^1.0.2
bblanchon/ArduinoJson@^6.17.3 bblanchon/ArduinoJson@^6.17.3
finitespace/BME280@^3.0.0 finitespace/BME280@^3.0.0
majicdesigns/MD_MAX72XX@^3.5.1
majicdesigns/MD_Parola@^3.7.3

View File

@@ -1,24 +0,0 @@
#include "BME280.hpp"
BME280I2C bme;
void BME280_Init()
{
Wire.setPins(21, 22);
Wire.begin();
while(!bme.begin())
{
Serial.println("Could not find BME280 sensor!");
delay(1000);
}
}
bool BME280_Read(float &pressure, float &temperature, float &humidity)
{
BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);
BME280::PresUnit presUnit(BME280::PresUnit_Pa);
bme.read(pressure, temperature, humidity, tempUnit, presUnit);
return (temperature != 0.0f && pressure != 0.0f);
}

View File

@@ -0,0 +1,35 @@
#include "MAX7219.hpp"
MD_Parola display = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
void MAX7219_Init()
{
display.begin();
display.setIntensity(1); // 0-15
display.displayClear();
}
void MAX7219_DisplayText(const char *text, textPosition_t align, int speed, int pause)
{
display.displayText(text, align, speed, pause, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
}
void MAX7219_StartAnimation()
{
display.displayAnimate();
}
void MAX7219_StopAnimation()
{
display.displayReset();
}
void MAX7219_ClearDisplay()
{
display.displayClear();
}
void MAX7219_SetBrightness(uint8_t brightness)
{
display.setIntensity(brightness);
}

View File

@@ -1,7 +1,8 @@
#include "RestClient.hpp" #include "RestClient.hpp"
HTTPClient httpClient; // HTTP client object
void getRequest(HTTPClient &httpClient, const String url, String &response) void getRequest(const String url, String &response)
{ {
httpClient.begin(url); httpClient.begin(url);
int httpCode = httpClient.GET(); int httpCode = httpClient.GET();
@@ -13,7 +14,7 @@ void getRequest(HTTPClient &httpClient, const String url, String &response)
httpClient.end(); httpClient.end();
} }
void postRequest(HTTPClient &httpClient, const String url, String &payload, String &response) void postRequest(const String url, String &payload, String &response)
{ {
httpClient.begin(url); httpClient.begin(url);
httpClient.addHeader("Content-Type", "application/json"); httpClient.addHeader("Content-Type", "application/json");

View File

@@ -0,0 +1,34 @@
#include "BME280.hpp"
BME280I2C bme;
void BME280_Init()
{
Wire.setPins(21, 22);
Wire.begin();
BME280I2C::Settings settings(
BME280I2C::OSR::OSR_X1,
BME280I2C::OSR::OSR_X1,
BME280I2C::OSR::OSR_X1,
BME280I2C::Mode::Mode_Forced, // modo forzado
BME280I2C::StandbyTime::StandbyTime_1000ms,
BME280I2C::Filter::Filter_16,
BME280I2C::SpiEnable::SpiEnable_False,
BME280I2C::I2CAddr::I2CAddr_0x76 // dirección I2C del BME280
);
bme.setSettings(settings);
while (!bme.begin());
}
bool BME280_Read(float &pressure, float &temperature, float &humidity)
{
BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);
BME280::PresUnit presUnit(BME280::PresUnit_Pa);
bme.read(pressure, temperature, humidity, tempUnit, presUnit);
return (temperature != 0.0f && pressure != 0.0f);
}

View File

@@ -0,0 +1,22 @@
#include "GPS.hpp"
TinyGPSPlus gps;
void GPS_Init()
{
Serial.begin(9600);
}
void GPS_Read(float &lat, float &lon)
{
if (gps.location.isValid())
{
lat = gps.location.lat();
lon = gps.location.lng();
}
else
{
lat = 0.0f;
lon = 0.0f;
}
}

View File

@@ -2,17 +2,70 @@
const uint32_t deviceId = getChipID(); const uint32_t deviceId = getChipID();
// instances extern HTTPClient httpClient; // HTTP client object
HTTPClient httpClient; String response; // HTTP Response
float sensorVolt, sensorValue, RSAir, R0; // MQ7 vars
float temperature, pressure, humidity; // BME280 vars
float lon, lat; // GPS vars
extern MD_Parola display; // Display object
// HTTP Request void setup()
String response; {
Serial.begin(9600);
// MQ7 Serial.println("Iniciando...");
float sensorVolt, sensorValue, RSAir, R0; MQ7_Init();
Serial.println("Sensor MQ7 inicializado");
BME280_Init();
Serial.println("Sensor BME280 inicializado");
GPS_Init();
Serial.println("GPS inicializado");
MAX7219_Init();
Serial.println("Display inicializado");
// BMP280 prettyReadBME280();
float temperature, pressure, altitude; prettyReadMQ7();
testMatrix();
}
void loop()
{
}
void prettyReadMQ7()
{
Serial.println("Leyendo sensor MQ7...");
MQ7_Read(sensorVolt, RSAir, R0, sensorValue);
Serial.print("\t - Voltaje: "); Serial.print(sensorVolt); Serial.print("V\r\n");
Serial.print("\t - Valor sensor: "); Serial.print(sensorValue); Serial.print("\r\n");
Serial.print("\t - Resistencia aire: "); Serial.print(RSAir); Serial.print("kOhm\r\n");
Serial.print("\t - Resistencia aire: "); Serial.print(R0); Serial.print("kOhm\r\n");
Serial.print("\t - Concentración CO: "); Serial.print(sensorValue); Serial.print("ppm\r\n");
}
void prettyReadBME280()
{
Serial.println("Leyendo sensor BME280...");
BME280_Read(pressure, temperature, humidity);
Serial.print("\t - Presión: "); Serial.print(pressure/100); Serial.print("hPa\r\n");
Serial.print("\t - Temperatura: "); Serial.print(temperature); Serial.print("°C\r\n");
Serial.print("\t - Humedad: "); Serial.print(humidity); Serial.print("%\r\n");
}
void prettyReadGPS()
{
Serial.println("Leyendo GPS...");
GPS_Read(lat, lon);
Serial.print("\t - Latitud: "); Serial.print(lat); Serial.print("\r\n");
Serial.print("\t - Longitud: "); Serial.print(lon); Serial.print("\r\n");
}
void testMatrix()
{
Serial.println("Escribiendo en el display...");
MAX7219_DisplayText("Prueba de texto", PA_LEFT, 100, 500);
}
uint32_t getChipID() uint32_t getChipID()
{ {
@@ -23,27 +76,3 @@ uint32_t getChipID()
} }
return chipId; return chipId;
} }
void setup()
{
Serial.begin(9600);
/*// WiFi Connection
if(setupWifi() != 0)
{
Serial.print("Error connecting to WiFi");
}
// test get
getRequest(httpClient, "http://172.20.10.7:8082/api/v1/sensors/1/values", response);
deserializeSensorValue(httpClient, httpClient.GET()); */
BME280_Init();
}
void loop()
{
Serial.println(temperature);
Serial.println(pressure);
}