mosquitto_connect();

This commit is contained in:
Alexandre B 2024-03-07 20:22:30 -05:00
parent 136c606653
commit c70d2ad1ff
3 changed files with 19 additions and 18 deletions

View File

@ -1,8 +1,8 @@
// Uncomment modules as required // Uncomment modules as required
//#define RTC //#define RTC
//#define MICRO_SD //#define MICRO_SD
//#define BMP280 // Adafruit BMP280; temp & pressure //#define BMP280 // Adafruit BMP280; temp & pressure
#define BME280 // Generic BME280; temp, pressure & humidity #define BME280 // Generic I2C BME280; temp, pressure & humidity
/* /*
BME-280 BME-280
SCL -> GPIO 22 SCL -> GPIO 22
@ -29,14 +29,14 @@
// MQTT Channel specific for each node // MQTT Channel specific for each node
// [NODE]/sensors/sensor // [NODE]/sensors/sensor
// IoT ID // IoT ID
#define IoT_ID "node1" #define IoT_ID "node1"
#define MQTT_NODE_TEMPERATURE "/sensors/temperature" #define MQTT_NODE_TEMPERATURE "/sensors/temperature"
#define MQTT_NODE_PRESSURE "/sensors/pressure" #define MQTT_NODE_PRESSURE "/sensors/pressure"
#define MQTT_NODE_HUMIDITY "/sensors/humidity" #define MQTT_NODE_HUMIDITY "/sensors/humidity"
#define MQTT_NODE_PULSE "/pulse" #define MQTT_NODE_PULSE "/pulse"
#define MQTT_NODE_SWITCH_1 "/sw1" #define MQTT_NODE_SWITCH_1 "/sw1"
#define MQTT_NODE_SWITCH_2 "/sw2" #define MQTT_NODE_SWITCH_2 "/sw2"
#define MQTT_NODE_OUTPUT_PWM_1 "/pwm1" #define MQTT_NODE_OUTPUT_PWM_1 "/pwm1"
// Standardize switches channels // Standardize switches channels
#define MQTT_IOT_CHANNEL_OUTPUT_PULSE "node1/output/pulse" #define MQTT_IOT_CHANNEL_OUTPUT_PULSE "node1/output/pulse"
#define MQTT_IOT_CHANNEL_OUTPUT_SWITCH_1 "node1/output/sw1" #define MQTT_IOT_CHANNEL_OUTPUT_SWITCH_1 "node1/output/sw1"

View File

@ -1,5 +1,6 @@
/* /*
Written for ESP32 development board DEVKIT v1 (Espressive ESP32-WROOM-32) Written for ESP32 development board DEVKIT v1 (Espressive ESP32-WROOM-32).
Modified for ESP32 WROOM U (external anthena).
Node sending temperature and atmosphere pressure readings via MQTT. Node sending temperature and atmosphere pressure readings via MQTT.
@ -19,11 +20,10 @@
#include "secrets.h" #include "secrets.h"
#include "config.h" #include "config.h"
#ifdef BME280 #ifdef BME280 // I2C temperature, pressure and humidity sensor.
#include <Adafruit_BME280.h> #include <Adafruit_BME280.h>
#endif #endif
#ifdef BMP280 #ifdef BMP280 // ISP temperature, pressure and humidity sensor.
#include <Adafruit_BMP280.h> #include <Adafruit_BMP280.h>
#endif #endif
@ -67,13 +67,11 @@ PubSubClient connection(espClientSSL); //mosquitto_ssl
void mosquito_callback (char* topic, byte* message, unsigned int length) void mosquito_callback (char* topic, byte* message, unsigned int length)
{ {
//mosquitto.mosquito_callback(topic, message, length);
// Display topic and message received. // Display topic and message received.
Serial.print("\nMessage arrived on topic: "); Serial.print("\nMessage arrived on topic: ");
Serial.println(topic); Serial.println(topic);
Serial.print("Message: "); Serial.print("Message: ");
String messageTemp; // variable to temporary store message received String messageTemp; // variable to temporary store message received
// Convert message received in bytes into String // Convert message received in bytes into String
for (int i=0; i < length; i++) { for (int i=0; i < length; i++) {
@ -185,6 +183,7 @@ void mosquitto_connect ()
Serial.println(connection.state()); Serial.println(connection.state());
#endif #endif
#ifdef MQTT_SSL // MOSQUITTO MQTT port 8883 #ifdef MQTT_SSL // MOSQUITTO MQTT port 8883
// MQTX MQTT Cloud
Serial.println(":8883"); Serial.println(":8883");
espClientSSL.setCACert(NODE_CERT_CA); espClientSSL.setCACert(NODE_CERT_CA);
connection.setServer(mqtt_server, 8883); connection.setServer(mqtt_server, 8883);
@ -202,7 +201,7 @@ void mosquitto_connect ()
delay(5000); delay(5000);
} }
} }
/* on-premises MQTT /* on-premises Mosquitto MQTT
Serial.println(":8883"); Serial.println(":8883");
connection.setServer(mqtt_server, 8883); connection.setServer(mqtt_server, 8883);
espClientSSL.setCACert(NODE_CERT_CA); espClientSSL.setCACert(NODE_CERT_CA);
@ -348,6 +347,8 @@ void setup() {
Serial.print("Connecting to Mosquitto at IP: "); Serial.print("Connecting to Mosquitto at IP: ");
Serial.print(mqtt_server); Serial.print(mqtt_server);
mosquitto_connect();
/**
#ifdef MQTT // MOSQUITTO MQTT port 1883 #ifdef MQTT // MOSQUITTO MQTT port 1883
Serial.println(":1883"); Serial.println(":1883");
connection.setServer(mqtt_server, 1883); connection.setServer(mqtt_server, 1883);

Binary file not shown.