117 lines
2.8 KiB
C++
Executable File
117 lines
2.8 KiB
C++
Executable File
#include "secrets.h"
|
|
#include <WiFi.h>
|
|
#include <PubSubClient.h>
|
|
#include <HardwareSerial.h>
|
|
|
|
// WiFi Credentials
|
|
const char* ssid = SECRET_WIFI_SSID;
|
|
const char* password = SECRET_WIFI_PASS;
|
|
|
|
// MQTT Broker
|
|
const char* mqtt_server = "192.168.20.30";
|
|
const char* mqtt_user = SECRET_MQTT_USER;
|
|
const char* password = SECRET_WIFI_PASS;
|
|
|
|
// MQTT Topics
|
|
const char* mqtt_topic = "homeassistant/ESP32/control";
|
|
|
|
WiFiClient espClient;
|
|
PubSubClient client(espClient);
|
|
|
|
// Define different Serial port for LoRa communication
|
|
#define LORA_UART Serial2
|
|
#define LORA_TX_PIN 17 // GPIO17 for TX
|
|
#define LORA_RX_PIN 16 // GPIO16 for RX
|
|
|
|
void setup_wifi() {
|
|
delay(10);
|
|
Serial.println();
|
|
Serial.print("Connecting to ");
|
|
Serial.println(ssid);
|
|
|
|
WiFi.begin(ssid, password);
|
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
|
|
Serial.println("");
|
|
Serial.println("WiFi connected");
|
|
Serial.println("IP address: ");
|
|
Serial.println(WiFi.localIP());
|
|
}
|
|
|
|
void callback(char* topic, byte* message, unsigned int length) {
|
|
Serial.print("Message arrived on topic: ");
|
|
Serial.print(topic);
|
|
Serial.print(". Message: ");
|
|
String messageTemp;
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
Serial.print((char)message[i]);
|
|
messageTemp += (char)message[i];
|
|
}
|
|
Serial.println();
|
|
|
|
// Handle incoming MQTT messages and send via LoRa
|
|
if (String(topic) == mqtt_topic) {
|
|
LORA_UART.print(messageTemp);
|
|
Serial.println("Sent via LoRa: " + messageTemp);
|
|
}
|
|
}
|
|
|
|
void reconnect() {
|
|
while (!client.connected()) {
|
|
Serial.print("Attempting MQTT connection...");
|
|
if (client.connect("ESP32Client", mqtt_user, mqtt_password)) {
|
|
Serial.println("connected");
|
|
client.subscribe(mqtt_topic);
|
|
} else {
|
|
Serial.print("failed, rc=");
|
|
Serial.print(client.state());
|
|
Serial.println(" try again in 5 seconds");
|
|
delay(5000);
|
|
}
|
|
}
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(115200); // Main serial for debugging
|
|
LORA_UART.begin(9600, SERIAL_8N1, LORA_RX_PIN, LORA_TX_PIN); // Initialize UART for LoRa
|
|
|
|
if (LORA_UART) {
|
|
Serial.println("LoRa Transmitter Initialized");
|
|
} else {
|
|
Serial.println("LoRa Transmitter Initialization Failed");
|
|
}
|
|
|
|
setup_wifi();
|
|
client.setServer(mqtt_server, 1883);
|
|
client.setCallback(callback);
|
|
}
|
|
|
|
void loop() {
|
|
if (!client.connected()) {
|
|
reconnect();
|
|
}
|
|
client.loop();
|
|
|
|
// Check for Serial Monitor command
|
|
if (Serial.available()) {
|
|
String command = Serial.readStringUntil('\n');
|
|
command.trim(); // Remove any extra whitespace
|
|
|
|
if (command == "restart") {
|
|
Serial.println("Restarting Transmitter...");
|
|
delay(1000); // Give time for message to print
|
|
ESP.restart();
|
|
} else {
|
|
LORA_UART.print(command);
|
|
Serial.println("Sent via LoRa: " + command);
|
|
}
|
|
}
|
|
|
|
delay(1000); // Add a delay to avoid watchdog timer reset
|
|
}
|