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