succesful mqtt connection to server
This commit is contained in:
@@ -1,4 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include "globals.hpp"
|
||||
#include <WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
|
||||
void MQTT_OnReceived(char *topic, byte *payload, unsigned int length);
|
||||
void MQTT_Init(const char *MQTTServerAddress, uint16_t MQTTServerPort);
|
||||
void MQTT_Connect(const char *MQTTClientName);
|
||||
void MQTT_Handle(const char *MQTTClientName);
|
||||
String buildTopic(int groupId, const String& deviceId, const String& topic);
|
||||
@@ -3,7 +3,4 @@
|
||||
#include <WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
|
||||
#define SSID "DIGIFIBRA-D2ys"
|
||||
#define PASSWORD "4EEATsyTcZ"
|
||||
|
||||
int setupWifi();
|
||||
22
hardware/include/globals.hpp
Normal file
22
hardware/include/globals.hpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
#define MQTT_URI "miarma.net"
|
||||
#define API_URI "https://contaminus.miarma.net/api/v1/"
|
||||
#define REST_PORT 443
|
||||
#define MQTT_PORT 1883
|
||||
|
||||
#define USER "contaminus"
|
||||
#define MQTT_PASSWORD "contaminus"
|
||||
|
||||
#define SSID "DIGIFIBRA-D2ys"
|
||||
#define WIFI_PASSWORD "4EEATsyTcZ"
|
||||
|
||||
#define MQ7_ID 3
|
||||
#define BME280_ID 2
|
||||
#define GPS_ID 1
|
||||
#define MAX7219_ID 1
|
||||
|
||||
#define DEBUG
|
||||
|
||||
extern const uint32_t DEVICE_ID;
|
||||
extern const int GROUP_ID;
|
||||
@@ -1,18 +1,6 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
#define SERVER_IP "https://contaminus.miarma.net/api/v1/"
|
||||
#define REST_PORT 443
|
||||
#define MQTT_PORT 1883
|
||||
|
||||
#define GROUP_ID 1
|
||||
|
||||
#define MQ7_ID 3
|
||||
#define BME280_ID 2
|
||||
#define GPS_ID 1
|
||||
#define MAX7219_ID 1
|
||||
|
||||
#define DEBUG
|
||||
#pragma once
|
||||
|
||||
#include "globals.hpp"
|
||||
#include "JsonTools.hpp"
|
||||
#include "RestClient.hpp"
|
||||
#include "WifiConnection.hpp"
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
#include "MqttClient.hpp"
|
||||
|
||||
// MQTT configuration
|
||||
WiFiClient espClient;
|
||||
PubSubClient client(espClient);
|
||||
extern WiFiClient wifiClient;
|
||||
|
||||
void OnMqttReceived(char *topic, byte *payload, unsigned int length)
|
||||
PubSubClient client(wifiClient);
|
||||
|
||||
void MQTT_OnReceived(char *topic, byte *payload, unsigned int length)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
Serial.print("Received on ");
|
||||
Serial.print(topic);
|
||||
Serial.print(": ");
|
||||
#endif
|
||||
|
||||
String content = "";
|
||||
|
||||
@@ -17,46 +19,46 @@ void OnMqttReceived(char *topic, byte *payload, unsigned int length)
|
||||
content.concat((char)payload[i]);
|
||||
}
|
||||
|
||||
Serial.print(content);
|
||||
Serial.println();
|
||||
#ifdef DEBUG
|
||||
Serial.println(content);
|
||||
#endif
|
||||
}
|
||||
|
||||
void InitMqtt(const char *MQTTServerAddress, uint16_t MQTTServerPort)
|
||||
void MQTT_Init(const char *MQTTServerAddress, uint16_t MQTTServerPort)
|
||||
{
|
||||
client.setServer(MQTTServerAddress, MQTTServerPort);
|
||||
client.setCallback(OnMqttReceived);
|
||||
client.setCallback(MQTT_OnReceived);
|
||||
}
|
||||
|
||||
// conecta o reconecta al MQTT
|
||||
// consigue conectar -> suscribe a topic y publica un mensaje
|
||||
// no -> espera 5 segundos
|
||||
void ConnectMqtt(const char *MQTTClientName)
|
||||
void MQTT_Connect(const char *MQTTClientName)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
Serial.print("Starting MQTT connection...");
|
||||
if (client.connect(MQTTClientName))
|
||||
#endif
|
||||
if (client.connect(MQTTClientName, USER, MQTT_PASSWORD))
|
||||
{
|
||||
client.subscribe("hello/world");
|
||||
client.publish("hello/world", "connected");
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print("Failed MQTT connection, rc=");
|
||||
Serial.print(client.state());
|
||||
Serial.println(" try again in 5 seconds");
|
||||
|
||||
delay(5000);
|
||||
String statusTopic = buildTopic(GROUP_ID, String(DEVICE_ID, HEX), "status");
|
||||
client.subscribe(statusTopic.c_str());
|
||||
client.publish(statusTopic.c_str(), "connected");
|
||||
}
|
||||
#ifdef DEBUG
|
||||
Serial.print("Failed MQTT connection, rc=");
|
||||
Serial.print(client.state());
|
||||
Serial.println(" try again in 5 seconds");
|
||||
#endif
|
||||
}
|
||||
|
||||
// gestiona la comunicación MQTT
|
||||
// comprueba que el cliente está conectado
|
||||
// no -> intenta reconectar
|
||||
// si -> llama al MQTT loop
|
||||
void HandleMqtt(const char *MQTTClientName)
|
||||
void MQTT_Handle(const char *MQTTClientName)
|
||||
{
|
||||
if (!client.connected())
|
||||
{
|
||||
ConnectMqtt(MQTTClientName);
|
||||
MQTT_Connect(MQTTClientName);
|
||||
}
|
||||
client.loop();
|
||||
}
|
||||
|
||||
String buildTopic(int groupId, const String& deviceId, const String& topic)
|
||||
{
|
||||
String topicString = "group/" + String(groupId) + "/device/" + deviceId + "/" + topic;
|
||||
return topicString;
|
||||
}
|
||||
@@ -39,7 +39,7 @@ int setupWifi()
|
||||
setupLED();
|
||||
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(SSID, PASSWORD);
|
||||
WiFi.begin(SSID, WIFI_PASSWORD);
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.print("Conectando a la red WiFi: ");
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
#include "main.hpp"
|
||||
|
||||
const uint32_t DEVICE_ID = getChipID();
|
||||
const int GROUP_ID = 1;
|
||||
const char ALL_VEHICLES[] = "Todo tipo de vehiculos";
|
||||
const char ELECTRIC_VEHICLES[] = "Solo vehiculos electricos/hibridos";
|
||||
const char *currentMessage = nullptr;
|
||||
const String id = "CUS-" + String(DEVICE_ID, HEX);
|
||||
|
||||
TaskTimer matrixTimer{0, 25};
|
||||
TaskTimer globalTimer{0, 60000};
|
||||
TaskTimer mqttTimer{0, 5000};
|
||||
|
||||
extern HTTPClient httpClient;
|
||||
String response;
|
||||
@@ -24,6 +27,7 @@ void setup()
|
||||
Serial.println("Iniciando...");
|
||||
|
||||
setupWifi();
|
||||
MQTT_Init(MQTT_URI, MQTT_PORT);
|
||||
|
||||
BME280_Init();
|
||||
Serial.println("Sensor BME280 inicializado");
|
||||
@@ -41,7 +45,7 @@ void loop()
|
||||
{
|
||||
uint32_t now = millis();
|
||||
|
||||
if (now - matrixTimer.lastRun >= matrixTimer.interval)
|
||||
/*if (now - matrixTimer.lastRun >= matrixTimer.interval)
|
||||
{
|
||||
if (MAX7219_Animate())
|
||||
{
|
||||
@@ -63,6 +67,12 @@ void loop()
|
||||
sendSensorData();
|
||||
|
||||
globalTimer.lastRun = now;
|
||||
}*/
|
||||
|
||||
if (now - mqttTimer.lastRun >= mqttTimer.interval)
|
||||
{
|
||||
MQTT_Handle(id.c_str());
|
||||
mqttTimer.lastRun = now;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,7 +176,7 @@ void sendSensorData()
|
||||
Serial.println("📤 Enviando datos al servidor...");
|
||||
#endif
|
||||
|
||||
postRequest(String(SERVER_IP) + "/batch", json, response);
|
||||
postRequest(String(API_URI) + "/batch", json, response);
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.println("📬 Respuesta del servidor:");
|
||||
|
||||
Reference in New Issue
Block a user