BJYW

a portfolio by bernice wong


BIBBLE SMART OBJECTS

The Bibble is a desk light designed to let user’s loved ones know where they are (or at least where they are scheduled to be).

The connected object can be gifted to sit on the recipient’s desk, softly glowing a particular colour, depending on whether the user is at school/work, in a meeting, or out. It uses IFTTT integration to search for keywords in the user’s Google Calendar, triggering NeoPixels to change colour.

PROCESS

The project process and how-to is available on Instructables, where you can learn to make your own out of easily found materials! As well, you can download the code here!

#include < ESP8266WiFi.h>
#include < Adafruit_NeoPixel.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#ifdef __AVR__
  #include < avr/power.h>
#endif

// WiFi Configuration

#define WLAN_SSID       "WIFI_NAME"    // Your WiFi AP name.
#define WLAN_PASS       "WIFI_PASSWORD"     // Your WiFi AP password.

// Adafruit.IO Configuration

#define AIO_SERVER      "io.adafruit.com"      // Adafruit IO server address.
#define AIO_SERVERPORT  1883                   // AIO server port.
#define AIO_USERNAME    "YOUR_USERNAME" // Adafruit IO username (see http://accounts.adafruit.com).
#define AIO_KEY         "YOUR_PASSWORD"      // Adafruit IO key

// NeoPixel Setup
#define PIN       14                      // Pin connected to the NeoPixel data input.
#define NUM_LEDS     4                    // Number of NeoPixels.
#define BRIGHTNESS 100

// Global state: create ESP8266 wifi and connect to MQTT server
WiFiClient client;
// or use this for SSL: WiFiClientSecure client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

// Set up Subscribing Feed (MQTT paths for AIO follow the form: < username>/feeds/< feedname>)
Adafruit_MQTT_Subscribe localCalendar = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/calendar");

// Activate NeoPixel.
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRBW + NEO_KHZ800);

// Sketch Code

void MQTT_connect();
void lightPix(char*);

void setup() {
  // Initialize serial output.
  Serial.begin(115200);
  delay(10);
  Serial.println("Bibble is setting up");
    #if defined (__AVR_ATtiny85__)
    if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
  #endif

  // Connect to WiFi access point.
  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());
  mqtt.subscribe(&localCalendar);
  MQTT_connect();
  // Setup MQTT subscription.
  
  // Initialize NeoPixels
  strip.setBrightness(BRIGHTNESS);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  pulseOnce(5);

}

uint32_t x=0;
int event = 0;

void loop() {  

//Serial.println("Hello");

// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected).  See the MQTT_connect
// function definition further below.

  Adafruit_MQTT_Subscribe *subscription;

  // Check if any new data has been received from the feed
  while ((subscription = mqtt.readSubscription(500))) {
    Serial.println("Entered loop");
    if (subscription == &localCalendar) {
      Serial.print("Got: ");
      Serial.println((char*)localCalendar.lastread);
      lightPix((char*)localCalendar.lastread);
      
    }
  }
    
// OFF or No Data
if (event == 0){
  pulseOnce(5);
}

}


// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care of connecting.

//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(500);  // wait .5 seconds
//      retries--;
//      if (retries == 0) {
//        // basically die and wait for WDT to reset me
//        while (1);
//      }
// }
// Serial.println("MQTT Connected!");
//}

void MQTT_connect() {
 Serial.print(F("Connecting to Adafruit IO... "));

  int8_t ret;

  while ((ret = mqtt.connect()) != 0) {

    switch (ret) {
      case 1: Serial.println(F("Wrong protocol")); break;
      case 2: Serial.println(F("ID rejected")); break;
      case 3: Serial.println(F("Server unavail")); break;
      case 4: Serial.println(F("Bad user/pass")); break;
      case 5: Serial.println(F("Not authed")); break;
      case 6: Serial.println(F("Failed to subscribe")); break;
      default: Serial.println(F("Connection failed")); break;
    }

    if(ret >= 0){
      mqtt.disconnect();

    Serial.println(F("Retrying connection..."));
    delay(5000);

  }

  Serial.println(F("Adafruit IO Connected!"));
}}

//NEOPIXELS

void lightPix(char* what){
 if (String(what) == /*KEYWORD*/"CLASS"){
  event = 1;
  Serial.println("Class");
  colorWipe(strip.Color(0, 0, 255), 200); // Blue
 }

else if (String(what) == /*KEYWORD*/"MEETING"){
  event = 1;
  Serial.println("Meeting");
  colorWipe(strip.Color(255, 0, 0), 200); // Red
 }

else if (String(what) == /*KEYWORD*/"OUT"){
  event = 1;
  Serial.println("Out");
  colorWipe(strip.Color(246, 197, 37), 200); // Yellow
 }
 
else if (String(what) ==/*KEYWORD*/ "OFF"){
  event = 0;
  Serial.println("Off");
  pulseOnce(5); 
 }
 
// else{
//  Serial.println("No Input");
//  pulseOnce(5); 
// };
}

// Color Wipe
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i< strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}

// Pulse White
//void pulseWhite(uint8_t wait) {
//  int go = 1;
//  while(go == 1){
//  for(int j = 0; j < 256 ; j++){
//      for(uint16_t i=0; i< strip.numPixels(); i++) {
//          strip.setPixelColor(i, strip.Color(0,0,0, j ) );
//        }
//        delay(wait);
//        strip.show();
//      }
//
//  for(int j = 255; j >= 0 ; j--){
//      for(uint16_t i=0; i< strip.numPixels(); i++) {
//          strip.setPixelColor(i, strip.Color(0,0,0, j ) );
//        }
//        delay(wait);
//        strip.show();
//      }
//  }
//}

void pulseOnce(uint8_t wait){
  
  for(int j = 0; j < 256 ; j++){
      for(uint16_t i=0; i< strip.numPixels(); i++) {
          strip.setPixelColor(i, strip.Color(0,0,0, j ) );
        }
        delay(wait);
        strip.show();
      }

  for(int j = 255; j >= 0 ; j--){
      for(uint16_t i=0; i< strip.numPixels(); i++) {
          strip.setPixelColor(i, strip.Color(0,0,0, j ) );
        }
        delay(wait);
        strip.show();
      }
}