Update wifi.ino
This commit is contained in:
parent
9666c3ef44
commit
a2e8efc215
166
wifi.ino
166
wifi.ino
@ -1,41 +1,54 @@
|
||||
#include <Wire.h>
|
||||
#include <Adafruit_GFX.h>
|
||||
#include <Adafruit_SSD1306.h>
|
||||
#include <FS.h>
|
||||
#include <SPIFFS.h>
|
||||
#include <WiFiManager.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <Wire.h>
|
||||
#include <Adafruit_GFX.h>
|
||||
#include <Adafruit_SSD1306.h>
|
||||
#include <FS.h>
|
||||
#include <SPIFFS.h>
|
||||
#include <WiFiManager.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <PubSubClient.h> // <-- Add this
|
||||
|
||||
#define PRODUCT_NAME "TAKEONE"
|
||||
#define PRODUCT_NAME "TAKEONE"
|
||||
|
||||
// OLED display setup (on I2C bus 0)
|
||||
#define OLED_SDA 18
|
||||
#define OLED_SCL 19
|
||||
#define SCREEN_WIDTH 128
|
||||
#define SCREEN_HEIGHT 32
|
||||
#define OLED_RESET -1
|
||||
// OLED display setup (on I2C bus 0)
|
||||
#define OLED_SDA 18
|
||||
#define OLED_SCL 19
|
||||
#define SCREEN_WIDTH 128
|
||||
#define SCREEN_HEIGHT 32
|
||||
#define OLED_RESET -1
|
||||
|
||||
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
|
||||
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
|
||||
|
||||
// Second I2C bus for other sensors/devices
|
||||
#define I2C2_SDA 1
|
||||
#define I2C2_SCL 2
|
||||
TwoWire I2C_Two = TwoWire(1);
|
||||
// Second I2C bus for other sensors/devices
|
||||
#define I2C2_SDA 1
|
||||
#define I2C2_SCL 2
|
||||
TwoWire I2C_Two = TwoWire(1);
|
||||
|
||||
// MQTT/WiFi config
|
||||
char mqtt_server[40] = "";
|
||||
char mqtt_port[6] = "8080";
|
||||
char api_token[34] = "YOUR_API_TOKEN";
|
||||
bool shouldSaveConfig = false;
|
||||
// MQTT/WiFi config
|
||||
char mqtt_server[40] = "";
|
||||
char mqtt_port[6] = "8080";
|
||||
char api_token[34] = "YOUR_API_TOKEN";
|
||||
bool shouldSaveConfig = false;
|
||||
|
||||
// WiFi loss detection
|
||||
unsigned long wifiLostTime = 0;
|
||||
bool portalOpened = false;
|
||||
// WiFi loss detection
|
||||
unsigned long wifiLostTime = 0;
|
||||
bool portalOpened = false;
|
||||
|
||||
// Button reset
|
||||
#define BOOT_BUTTON 0 // Usually GPIO 0 on ESP32
|
||||
// Button reset
|
||||
#define BOOT_BUTTON 0 // Usually GPIO 0 on ESP32
|
||||
|
||||
void saveConfigCallback() {
|
||||
// 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;
|
||||
display.clearDisplay();
|
||||
@ -43,9 +56,13 @@ void saveConfigCallback() {
|
||||
display.println("Saving config...");
|
||||
display.display();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void showStatus(const String& line1, const String& line2 = "", const String& line3 = "") {
|
||||
//================================================================
|
||||
// SETUP
|
||||
//================================================================
|
||||
|
||||
void showStatus(const String& line1, const String& line2 = "", const String& line3 = "") {
|
||||
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
@ -54,9 +71,54 @@ void showStatus(const String& line1, const String& line2 = "", const String& lin
|
||||
if (!line3.isEmpty()) display.println(line3);
|
||||
display.display();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
//================================================================
|
||||
// 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,9 +196,18 @@ void setup() {
|
||||
|
||||
// Button setup
|
||||
pinMode(BOOT_BUTTON, INPUT_PULLUP);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// MQTT setup
|
||||
mqttClient.setServer(mqtt_server, atoi(mqtt_port));
|
||||
mqttClient.setCallback(mqttCallback);
|
||||
|
||||
}
|
||||
|
||||
//================================================================
|
||||
// SETUP
|
||||
//================================================================
|
||||
|
||||
void loop() {
|
||||
|
||||
// WiFi loss detection
|
||||
if (WiFi.status() != WL_CONNECTED) {
|
||||
@ -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