CiscoTheProot/ESP/mqtt_test/mqtt_test.ino

160 lines
4.2 KiB
C++

#include <WiFi.h>
#include <PubSubClient.h>
#include <Bounce2.h>
const char* ssid = "ProotAP";
const char* password = "prootproot!";
const char* mqttServer = "10.1.1.15";
const int mqttPort = 1883;
const char* mqttTopic = "test";
const int gpioPins[] = {25, 26, 27, 14}; // GPIO pins to check for shorting and wake-up
const unsigned int numPins = sizeof(gpioPins) / sizeof(gpioPins[0]);
const unsigned long debounceDelay = 40; // Debounce delay in milliseconds
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);
TaskHandle_t mqttTaskHandle = NULL;
Bounce debouncers[numPins];
// Variable to track the time since the last button press
unsigned long lastButtonPressTime = 0;
void setupWiFi() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void callback(char* topic, byte* payload, unsigned int length) {
// This function is called when a message is received from the MQTT server
// Add your desired code here to handle the received message
}
void reconnectMQTT() {
while (!mqttClient.connected()) {
Serial.println("Connecting to MQTT server...");
if (mqttClient.connect("ESP32Client")) {
Serial.println("Connected to MQTT server");
mqttClient.subscribe(mqttTopic);
} else {
Serial.print("MQTT connection failed, rc=");
Serial.print(mqttClient.state());
Serial.println(" Retrying in 5 seconds...");
delay(5000);
}
}
}
void mqttTask(void* parameter) {
for (;;) {
ulTaskNotifyTake(pdTRUE, portMAX_DELAY); // Wait for notification
if (mqttClient.connected()) {
for (unsigned int i = 0; i < numPins; i++) {
if (debouncers[i].fell()) {
String message = "Beanfell: " + String(i);
mqttClient.publish(mqttTopic, message.c_str());
Serial.println("Message sent to MQTT server");
}
}
for (unsigned int i = 0; i < numPins; i++) {
if (debouncers[i].rose()) {
String message = "Beanrose: " + String(i);
mqttClient.publish(mqttTopic, message.c_str());
Serial.println("Message sent to MQTT server");
}
}
}
}
}
void enterDeepSleep() {
// Disconnect from MQTT server
mqttClient.disconnect();
// Clear any pending button press notifications
ulTaskNotifyTake(pdTRUE, 0);
// Enter deep sleep
esp_deep_sleep_start();
}
void setup() {
Serial.begin(115200);
setupWiFi();
mqttClient.setServer(mqttServer, mqttPort);
mqttClient.setCallback(callback);
for (unsigned int i = 0; i < numPins; i++) {
pinMode(gpioPins[i], INPUT_PULLUP);
debouncers[i].attach(gpioPins[i]);
debouncers[i].interval(debounceDelay);
}
// Enable wake-up from deep sleep on all pins
for (unsigned int i = 0; i < numPins; i++) {
esp_sleep_enable_ext0_wakeup(static_cast<gpio_num_t>(gpioPins[i]), LOW);
}
xTaskCreatePinnedToCore(
mqttTask, // Task function
"mqttTask", // Task name
4096, // Stack size (bytes)
NULL, // Task parameter
1, // Task priority
&mqttTaskHandle, // Task handle
1 // Task core (0 or 1)
);
}
void loop() {
if (!mqttClient.connected()) {
reconnectMQTT();
}
mqttClient.loop();
for (unsigned int i = 0; i < numPins; i++) {
debouncers[i].update();
}
bool anyPinFell = false;
bool anyPinRose = false;
for (unsigned int i = 0; i < numPins; i++) {
if (debouncers[i].fell()) {
anyPinFell = true;
break;
}
}
for (unsigned int i = 0; i < numPins; i++) {
if (debouncers[i].rose()) {
anyPinRose = true;
break;
}
}
if (anyPinFell || anyPinRose) {
// Update the last button press time
lastButtonPressTime = millis();
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
vTaskNotifyGiveFromISR(mqttTaskHandle, &xHigherPriorityTaskWoken);
if (xHigherPriorityTaskWoken == pdTRUE) {
portYIELD_FROM_ISR();
}
}
// Check if the time since the last button press has exceeded 10 minutes (600000 milliseconds)
if (millis() - lastButtonPressTime > 600000) {
enterDeepSleep();
}
delay(10);
}