Merge pull request #12 from Gallardo7761/feature/MQTT
Adding MQTT protocol to allow message interchange
This commit is contained in:
@@ -0,0 +1,2 @@
|
|||||||
|
#include <WiFi.h>
|
||||||
|
#include <PubSubClient.h>
|
||||||
@@ -1,3 +1,17 @@
|
|||||||
|
<<<<<<< HEAD
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
#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 <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
#include "JsonTools.hpp"
|
#include "JsonTools.hpp"
|
||||||
@@ -5,4 +19,5 @@
|
|||||||
#include "WifiConnection.hpp"
|
#include "WifiConnection.hpp"
|
||||||
#include "MqttClient.hpp"
|
#include "MqttClient.hpp"
|
||||||
|
|
||||||
uint32_t getChipID();
|
uint32_t getChipID();
|
||||||
|
>>>>>>> main
|
||||||
|
|||||||
7
hardware/include/wifi.hpp
Normal file
7
hardware/include/wifi.hpp
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
#include <WiFi.h>
|
||||||
|
#include <PubSubClient.h>
|
||||||
|
|
||||||
|
#define SSID "Redmi Note 14 Pro 5G"
|
||||||
|
#define PASSWORD "aitv5949"
|
||||||
|
|
||||||
|
int setup_wifi();
|
||||||
@@ -1 +1,61 @@
|
|||||||
#include "MqttClient.hpp"
|
#include "mqtt.hpp"
|
||||||
|
|
||||||
|
// MQTT configuration
|
||||||
|
WiFiClient espClient;
|
||||||
|
PubSubClient client(espClient);
|
||||||
|
|
||||||
|
void OnMqttReceived(char *topic, byte *payload, unsigned int length)
|
||||||
|
{
|
||||||
|
Serial.print("Received on ");
|
||||||
|
Serial.print(topic);
|
||||||
|
Serial.print(": ");
|
||||||
|
|
||||||
|
String content = "";
|
||||||
|
|
||||||
|
for (size_t i = 0; i < length; i++) {
|
||||||
|
content.concat((char)payload[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.print(content);
|
||||||
|
Serial.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
void InitMqtt(const char * MQTTServerAddress, uint16_t MQTTServerPort)
|
||||||
|
{
|
||||||
|
client.setServer(MQTTServerAddress, MQTTServerPort);
|
||||||
|
client.setCallback(OnMqttReceived);
|
||||||
|
}
|
||||||
|
|
||||||
|
// conecta o reconecta al MQTT
|
||||||
|
// consigue conectar -> suscribe a topic y publica un mensaje
|
||||||
|
// no -> espera 5 segundos
|
||||||
|
void ConnectMqtt(const char * MQTTClientName)
|
||||||
|
{
|
||||||
|
Serial.print("Starting MQTT connection...");
|
||||||
|
if (client.connect(MQTTClientName))
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// gestiona la comunicación MQTT
|
||||||
|
// comprueba que el cliente está conectado
|
||||||
|
// no -> intenta reconectar
|
||||||
|
// si -> llama al MQTT loop
|
||||||
|
void HandleMqtt(const char * MQTTClientName)
|
||||||
|
{
|
||||||
|
if (!client.connected())
|
||||||
|
{
|
||||||
|
ConnectMqtt(MQTTClientName);
|
||||||
|
}
|
||||||
|
client.loop();
|
||||||
|
}
|
||||||
|
|||||||
2
hardware/src/rest.cpp
Normal file
2
hardware/src/rest.cpp
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
#include "rest.hpp"
|
||||||
|
|
||||||
Reference in New Issue
Block a user