Update wifi.ino
This commit is contained in:
parent
9666c3ef44
commit
a2e8efc215
92
wifi.ino
92
wifi.ino
@ -5,6 +5,7 @@
|
||||
#include <SPIFFS.h>
|
||||
#include <WiFiManager.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <PubSubClient.h> // <-- Add this
|
||||
|
||||
#define PRODUCT_NAME "TAKEONE"
|
||||
|
||||
@ -35,6 +36,18 @@ bool portalOpened = false;
|
||||
// Button reset
|
||||
#define BOOT_BUTTON 0 // Usually GPIO 0 on ESP32
|
||||
|
||||
// MQTT client setup
|
||||
WiFiClient espClient;
|
||||
PubSubClient mqttClient(espClient);
|
||||
|
||||
// MQTT topics
|
||||
#define MQTT_TOPIC_SUB "takeone/in"
|
||||
#define MQTT_TOPIC_PUB "takeone/out"
|
||||
|
||||
//================================================================
|
||||
// SETUP
|
||||
//================================================================
|
||||
|
||||
void saveConfigCallback() {
|
||||
|
||||
shouldSaveConfig = true;
|
||||
@ -45,6 +58,10 @@ void saveConfigCallback() {
|
||||
|
||||
}
|
||||
|
||||
//================================================================
|
||||
// SETUP
|
||||
//================================================================
|
||||
|
||||
void showStatus(const String& line1, const String& line2 = "", const String& line3 = "") {
|
||||
|
||||
display.clearDisplay();
|
||||
@ -56,6 +73,51 @@ void showStatus(const String& line1, const String& line2 = "", const String& lin
|
||||
|
||||
}
|
||||
|
||||
//================================================================
|
||||
// SETUP
|
||||
//================================================================
|
||||
|
||||
// MQTT message received callback
|
||||
void mqttCallback(char* topic, byte* payload, unsigned int length) {
|
||||
|
||||
char message[length + 1];
|
||||
memcpy(message, payload, length);
|
||||
message[length] = '\0';
|
||||
Serial.print("Message received: ");
|
||||
Serial.println(message);
|
||||
showStatus("MQTT:", "RX: " + String(message));
|
||||
|
||||
}
|
||||
|
||||
//================================================================
|
||||
// SETUP
|
||||
//================================================================
|
||||
|
||||
// Connect to MQTT broker
|
||||
bool mqttConnect() {
|
||||
|
||||
if (!mqttClient.connected()) {
|
||||
Serial.println("Connecting to MQTT broker...");
|
||||
showStatus("MQTT:", "Connecting...");
|
||||
if (mqttClient.connect("ESP32Client", "", "")) {
|
||||
Serial.println("Connected to MQTT broker");
|
||||
mqttClient.subscribe(MQTT_TOPIC_SUB);
|
||||
showStatus("MQTT:", "Connected!");
|
||||
return true;
|
||||
} else {
|
||||
Serial.println("Failed to connect to MQTT broker");
|
||||
showStatus("MQTT:", "Failed!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
//================================================================
|
||||
// SETUP
|
||||
//================================================================
|
||||
|
||||
void setup() {
|
||||
|
||||
Serial.begin(115200);
|
||||
@ -134,8 +196,17 @@ void setup() {
|
||||
|
||||
// Button setup
|
||||
pinMode(BOOT_BUTTON, INPUT_PULLUP);
|
||||
|
||||
// MQTT setup
|
||||
mqttClient.setServer(mqtt_server, atoi(mqtt_port));
|
||||
mqttClient.setCallback(mqttCallback);
|
||||
|
||||
}
|
||||
|
||||
//================================================================
|
||||
// SETUP
|
||||
//================================================================
|
||||
|
||||
void loop() {
|
||||
|
||||
// WiFi loss detection
|
||||
@ -152,6 +223,11 @@ void loop() {
|
||||
}
|
||||
} else {
|
||||
wifiLostTime = 0;
|
||||
// MQTT keepalive and reconnect
|
||||
if (!mqttClient.connected()) {
|
||||
mqttConnect();
|
||||
}
|
||||
mqttClient.loop();
|
||||
}
|
||||
|
||||
// Button hold check
|
||||
@ -166,7 +242,7 @@ void loop() {
|
||||
WiFi.disconnect(true);
|
||||
WiFiManager wifiManager;
|
||||
wifiManager.resetSettings();
|
||||
wifiManager.startConfigPortal("AutoConnectAP", "12345678");
|
||||
wifiManager.startConfigPortal(PRODUCT_NAME);
|
||||
buttonActionTaken = true;
|
||||
}
|
||||
} else {
|
||||
@ -174,4 +250,18 @@ void loop() {
|
||||
buttonActionTaken = false;
|
||||
}
|
||||
|
||||
// Example: Publish a message every 5 seconds
|
||||
static unsigned long lastPublish = 0;
|
||||
if (millis() - lastPublish > 5000) {
|
||||
lastPublish = millis();
|
||||
if (mqttClient.connected()) {
|
||||
String msg = "Hello from " + String(PRODUCT_NAME);
|
||||
mqttClient.publish(MQTT_TOPIC_PUB, msg.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//================================================================
|
||||
// SETUP
|
||||
//================================================================
|
Loading…
x
Reference in New Issue
Block a user