0% found this document useful (0 votes)
12 views5 pages

ESP8266 MQTT LED and Vibrator Control

Uploaded by

rakeshshetti31
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

ESP8266 MQTT LED and Vibrator Control

Uploaded by

rakeshshetti31
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#include <ESP8266WiFi.

h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

#define WIFI_SSID "your_wifi_ssid"


#define WIFI_PASS "your_wifi_password"
#define AIO_USERNAME "your_adafruit_io_username"
#define AIO_KEY "your_adafruit_io_key"
#define AIO_SERVER "[Link]"
#define AIO_PORT 1883 // Replace 1883 with the appropriate port
number

#define LED_PIN 2
#define VIBRATOR_PIN 3
#define BUTTON_PIN 4

WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_PORT,
AIO_USERNAME, AIO_KEY);

Adafruit_MQTT_Publish led_control =
Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME
"/feeds/led-control");
Adafruit_MQTT_Publish vibrator_control =
Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME
"/feeds/vibrator-control");
Adafruit_MQTT_Subscribe led_status =
Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME
"/feeds/led-status");
Adafruit_MQTT_Subscribe vibrator_status =
Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME
"/feeds/vibrator-status");

bool isdevice1Pressing = false;


bool isdevice2Pressing = false;
bool timeoutOccurred = false;

void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(VIBRATOR_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);

[Link](115200);
[Link](WIFI_SSID, WIFI_PASS);
while ([Link]() != WL_CONNECTED) {
delay(500);
}

[Link](&led_status);
[Link](&vibrator_status);
}

void loop() {
[Link](10000);
if (![Link]()) {
[Link]();
[Link](WIFI_SSID, WIFI_PASS);
while ([Link]() != WL_CONNECTED) {
delay(500);
}
[Link]();
[Link](&led_status);
[Link](&vibrator_status);
}

if (digitalRead(BUTTON_PIN) == LOW) {
if (!isdevice2Pressing) {
// Device2 Presses the button
isdevic1Pressing = true;

digitalWrite(LED_PIN, HIGH);
led_control.publish("ON");

// Wait for device2 button press or 30 seconds


unsigned long start_time = millis();
while (millis() - start_time < 30000) {
[Link](10000);
if (isDevice2Pressing) {
break;
}
}

if (!isDevice2Pressing) {
// Timeout, Device2 didn't press within 30 seconds
timeoutOccurred = true;
}
} else {
// Device2 Presses the button after device1
isDevice2Pressing = false;
digitalWrite(LED_PIN, HIGH);
led_control.publish("ON");
vibrator_control.publish("ON");
digitalWrite(VIBRATOR_PIN, HIGH);
delay(10000);
digitalWrite(LED_PIN, LOW);
led_control.publish("OFF");
vibrator_control.publish("OFF");
digitalWrite(VIBRATOR_PIN, LOW);
}

// Check if there was a timeout and handle the behavior


accordingly
if (timeoutOccurred) {
digitalWrite(LED_PIN, LOW);
led_control.publish("OFF");
digitalWrite(VIBRATOR_PIN, LOW);
vibrator_control.publish("OFF");
isdevice1Pressing = false;
timeoutOccurred = false;
}
}
}

You might also like