Elmin Delibašić
Published © GPL3+

Smart Lights

Wemos D1 Mini + Adafruit IO + IFTTT + Google Assistant

AdvancedFull instructions providedOver 2 days455
Smart Lights

Things used in this project

Hardware components

Wemos D1 Mini
Espressif Wemos D1 Mini
×1
Arduino Nano R3
Arduino Nano R3
×1
SparkFun SHARP 2Y0A21
×1
Grove - Relay
Seeed Grove - Relay
×4
LED (generic)
LED (generic)
×4
Resistor 100 ohm
Resistor 100 ohm
×4

Software apps and online services

Adafruit IO
Maker service
IFTTT Maker service
Assistant SDK
Google Assistant SDK
Arduino IDE
Arduino IDE

Story

Read more

Schematics

Schematic 1

Schematic 2

Code

wemos_d1_mini_code.ino

Arduino
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

#define led1            D7
#define led2            D6
#define led3            D5

#define WLAN_SSID       "your_ssid"   // Your SSID
#define WLAN_PASS       "your_password"           // Your password


/************************* Adafruit.io Setup *********************************/

#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883
#define AIO_USERNAME    "your_aio_username"
#define AIO_KEY       "your_aio_key"

WiFiClient client;

Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

/****************************** Feeds ***************************************/

// Setup a feed called 'onoff' for subscribing to changes.
Adafruit_MQTT_Subscribe livingroomlight = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME"/feeds/Livingroom");
Adafruit_MQTT_Subscribe bedroomlight = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/Bedroom");
Adafruit_MQTT_Subscribe bathroomlight = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/Bathroom");




void MQTT_connect();

void setup() {
  Serial.begin(115200);

  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  
  // Connect to WiFi access point.
  Serial.println(); Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WLAN_SSID);

  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

  Serial.println("WiFi connected");
  Serial.println("IP address: "); 
  Serial.println(WiFi.localIP());

   // Setup MQTT subscription for onoff feed.
  mqtt.subscribe(&livingroomlight);
  mqtt.subscribe(&bedroomlight);
  mqtt.subscribe(&bathroomlight);

}

void loop() {
 
  MQTT_connect();
  

  Adafruit_MQTT_Subscribe *subscription;
  while ((subscription = mqtt.readSubscription(20000))) {
    if (subscription == &livingroomlight) {
      int led1_data = atoi((char *)livingroomlight.lastread);
      digitalWrite(led1, led1_data);
      
    }
    if (subscription == &bedroomlight) {
      int led2_data = atoi((char *)bedroomlight.lastread);
      digitalWrite(led2, led2_data);
    }
    if (subscription == &bathroomlight) {
      int led3_data = atoi((char *)bathroomlight.lastread);
      digitalWrite(led3, led3_data);
    }

  }

  
}

void MQTT_connect() {
  int8_t ret;

   // Stop if already connected.
  if (mqtt.connected()) {
    return;
  }

  Serial.print("Connecting to MQTT... ");

  uint8_t retries = 3;
  
  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
    Serial.println(mqtt.connectErrorString(ret));
    Serial.println("Retrying MQTT connection in 5 seconds...");
    mqtt.disconnect();
    delay(5000);
    retries--;
    if (retries == 0) {
      // basically die and wait for WDT to reset me
      while (1);
    }
  }
  Serial.println("MQTT Connected!");
  
}

arduino_nano_code.ino

Arduino
#include <SharpIR.h>

#define ir A7
#define model 1080

// model: 1080 for GP2Y0A21Y

#define led 4

SharpIR SharpIR(ir, model);

void setup() {

  Serial.begin(9600);
  pinMode(led, OUTPUT);
}

void loop() {
  int dis = SharpIR.distance(); 
  if (dis < 50) {
      digitalWrite(led, HIGH);
      delay(5000);
  }
  else {
      digitalWrite(led, LOW);
  }
  }

Credits

Elmin Delibašić

Elmin Delibašić

7 projects • 38 followers
Bachelor of Electrical Engineering (Department of Telecommunications)

Comments

Add projectSign up / Login