Back to Parent

// ------ Code for Pen Holder ------

// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>

#define PIXEL_PIN D8
#define PIXEL_COUNT 1
#define PIXEL_TYPE WS2812B
 
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

// This value will store the last time we published an event
long lastPublishedAt = 0;
int level = 0;

int lastState = 0;
// this is the time delay before we should publish a new event
// from this device
int publishAfter = 10000;

String activity = "drink";
// This variable is used to initiate the webpage to make a "Shhhhhh" sound

int level1state = false;
int level2state = false;
int level3state = false;
// These svariables are used to ensure that the potentiometer publishes an event only once when turned
// DO NOT CHANGE THIS
String sharedEventName = "diot/2019/smartstudio/";

String napTech = sharedEventName + "napTech/napping";
//used to subscribe to naptech's event
// Define a pin we'll place an LED on
// int ledPin = D2;


// Define a pin that we'll place the pot on
int potPin = A2;

// Create a variable to hold the pot reading
int potReading = 0;

// Create a variable to store the LED brightness.
int ledBrightness = 0;

bool msgTriger = false;
// to control the IFTTT trigger

void setup()
{
    
//   strip.begin();
//   strip.show();
//   strip.clear();
 
  // For input, we define the
  // pushbutton as an input-pullup
  // this uses an internal pullup resistor
  // to manage consistent reads from the device
  // called 'light' mapped to photoCellReading
  
  Particle.variable("pot", &potReading, INT);
//   Particle.subscribe(  sharedEventName , handleSharedEvent );
  Particle.subscribe(  napTech , handleSharedEvent );
}

void loop()
{
   // Use analogRead to read the potentiometer reading
  // This gives us a value from 0 to 4095
  potReading = analogRead(potPin);

  if(potReading <= 500){
      level = 1;
      //level 1 is when the pot is turned to the off position indicating the room is empty
  }else if (potReading <= 1700){
      level = 2;
      //level 2 is when the pot is turned to occupied position  the room
  }else{
      level = 3;
      //level 3 is when the pot is turned to "important position to indicate that an important meeting is taking place in the room
  }



if (level == 1 && level1state == false){
    
    delay(100);
    msgTriger = false;
    
    pixelwhite();
    //turns the feedback neopixel in the penstand to white colour
    Particle.publish( "lightsOff", "data goes there" );
    // publishes an event to particle cloud

    level1state = true;
    level2state = false;
    level3state = false;
    // changes level states to ensure the loop isn't executed more than once for one change of state


      
}else if(level == 2 && level2state == false){
    // Particle.publish( "test222", "data of test2" );
    delay(100);
    pixelblue();
    //turns the feedback neopixel in the penstand to blue colour

    Particle.publish( "lightsUp", "data goes there" );
    // publishes an event to particle cloud
    level1state = false;
    level2state = true;
    level3state = false;
    // changes level states to ensure the loop isn't executed more than once for one change of state
    
}else if (level == 3 && level3state == false) {
    // Particle.publish( "test333", "data of test3" );
    if(!msgTriger){
        delay(100);
        pixelpink();
        //turns the feedback neopixel in the penstand to pink colour
        Particle.publish( "Important", "data goes there" );
        // publishes an event to particle cloud to trigger IFTTT and events on "Rings of Taskaccomplishment"
        Particle.publish( "diot2019noisemaker", activity );
        // publishes an event to particle cloud to trigger "Shhhhh" sound
        publishMyEvent();
        msgTriger = true;
        
    level1state = false;
    level2state = false;
    level3state = true;
    // changes level states to ensure the loop isn't executed more than once for one change of state
        
    }
   
    
}
 
   
}


void publishMyEvent()
{
  // Remember that a device can publish at rate of about 1 event/sec,
  // with bursts of up to 4 allowed in 1 second.
  // Back to back burst of 4 messages will take 4 seconds to recover.
  // So we want to limit the amount of publish events that happen.

  // check that it's been 10 secondds since our last publish
  if( lastPublishedAt + publishAfter < millis() )
  {
      // Remember our subscribe is matching  "db2018/paired/"
      // We'll append the device id to get more specific
      // about where the event came from

      // System.deviceID() provides an easy way to extract the device
      // ID of your device. It returns a String object of the device ID,
      // which is used to identify your device.
      // This will build 'diot/2019/smartstudio/myEvent/A13231245345A078'

      String eventName = sharedEventName + "Shhh/" + System.deviceID();

      // now we have something like "diot/2019/paired/0123456789abcdef"
      // and that corresponds to this devices info

      // then we share it out
      Particle.publish( eventName, "data goes here" );

      // And this will get shared out to all devices using this code

      // we just pubished so capture this.
      lastPublishedAt = millis();
  }

}

// Our event handlde requires two bits of information
// This gives us:
// A character array that consists of the event name
// A character array that contains the data published in the event we're responding to.
void handleSharedEvent(const char *event, const char *data)
{
    // Now we're getting ALL events published using "db2018/paired/"
    // This includes events from this device.
    // So we need to ignore any events that we sent.

    // Let's check the event name
    String eventName = String( event ); // convert to a string object
    // This gives us access to a bunch of built in methods
    // Like indexOf()
    // Locates a character or String within another String.
    // By default, searches from the beginning of the String,
    // but can also start from a given index,
    // allowing for the locating of all instances of the character or String.
    // It Returns: The index of val within the String, or -1 if not found.

    // We can use this to check if our event name contains the
    // id of this device

    String deviceID = System.deviceID();

    // device id = 0123456789abcdef
    // event = "diot/2019/paired/0123456789abcdef"

    if( eventName.indexOf( deviceID ) != -1 ){
      // if we get anything other than -1
      // the event came from this device.
      // so stop doing stuff
      return;
    }
    
    else{
        Particle.publish( "diot2019noisemaker", activity );
      // makes Shhhh sound for team naptech 
    }

    if( eventName.indexOf( "myEvent") > -1 ){
        // A my event triggered
        // do soemthing with this...
    }

    // otherwise do your stuff to respond to
    // the paired device here

    //motorOn = true;

}

void pixelpink()
// neo pixel to pink
{
    uint32_t c = strip.Color(244, 66, 220);
    
    for (int i = 0; i < strip.numPixels(); i++)
    {
        strip.setPixelColor(i, c);
    }
    strip.show();
    
    delay(500);
}
    
    
  void pixelwhite()
  // neo pixel to white
 {
     
    uint32_t off = strip.Color(200, 200, 200);
    
    for (int i = 0; i < strip.numPixels(); i++)
    {
        strip.setPixelColor(i, off);
    }
    strip.show();
		  		     
  delay(500);
   
 }
 
  void pixelblue()
  // neo pixel to blue
 {
     
    uint32_t off = strip.Color(0, 0, 200);
    
    for (int i = 0; i < strip.numPixels(); i++)
    {
        strip.setPixelColor(i, off);
    }
    strip.show();
	
	delay(500);	     
   
   
 }
 
 
    void pixel()
 {
     
    uint32_t off = strip.Color(0, 0, 0
    );
    
    for (int i = 0; i < strip.numPixels(); i++)
    {
        strip.setPixelColor(i, off);
    }
    strip.show();
	
	delay(500);	     
   
   
 }


------ Code for the "Shhh" Sound ------

<!DOCTYPE HTML>
<html>

<head>
  <meta charset="UTF-8">
  <!-- uncomment lines below to include extra p5 libraries -->
  <!--<script language="javascript" src="libraries/p5.dom.js"></script>-->
  <!--<script language="javascript" src="libraries/p5.sound.js"></script>-->
  <!-- this line removes any default padding and style. you might only need one of these values set. -->
  <style> body {padding: 0; margin: 0;} </style>
  <script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
   <!-- Latest compiled and minified CSS -->
</script>
  
</head>

<body>
	
	<div id="particle-login" />
	
    <P>Event name:<input type="text" name="eventNameBox" id="evText" value="diot2019noisemaker">
    <br><br>
    <button id="connectbutton" onclick="start()">Connect</button>
    <br><br>
    
    <audio controls id="Shhhh">
      <source src="Shhhh.mp3" type="audio/mpeg">
      Your browser does not support the audio element.
    </audio>
    
    <pre><code id="datalog"></code></pre>
    

    <script type="text/javascript">
    
    // Go to Particle Build
    // Access Settings
    // And add your access token here
	
    var accessToken = "d2aa553dee60d541c669cd5b1edb98c54b2d0d49";

;
    function start(objButton) {
        document.getElementById("connectbutton").innerHTML = "Running";
        var eventName = document.getElementById('evText').value;
		var requestURL = "https://api.particle.io/v1/events/diot2019noisemaker?access_token=" + accessToken;

        var eventSource = new EventSource(requestURL);

        eventSource.addEventListener('open', function(e) {
            console.log("Opened!"); },false);

        eventSource.addEventListener('error', function(e) {
            console.log("Errored!"); },false);

        eventSource.addEventListener(eventName, function(e) {
            var eventData = JSON.parse(e.data);

            var data = eventData['data'];
            
            datalog.prepend( data + "\n" );
            
            if( data == "silent" ){
              $("#Shhhh").trigger('play');
            }
        }, false);
    }
    </script>
</body>
</html>


// ------ Code for Door Light ------

// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>


#define PIXEL_PIN D4
#define PIXEL_COUNT 12
#define PIXEL_TYPE WS2812
 
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);



void setup(){
    
  strip.begin();
  
  strip.show();

  Particle.subscribe(  "lightsUp" , handleSharedEvent1 );
// subscribe the event published by device 1
  
  Particle.subscribe(  "lightsOff", handleSharedEvent2 );
// subscribe the event published by device 1 
}

void loop(){

  

}


void handleSharedEvent1(const char *event, const char *data)
// function of "lightsUp" event
{
 
    String eventName = String( event ); // convert to a string object

    String deviceID = System.deviceID();

    if( eventName.indexOf( deviceID ) != -1 ){
      // if we get anything other than -1
      // the event came from this device.
      // so stop doing stuff
      return;
    }
    
    else{
        
      pixelfadesup();

		}


}


void handleSharedEvent2(const char *event, const char *data)
// function of "lightsOff" event
{

 
    String eventName = String( event ); // convert to a string object

    String deviceID = System.deviceID();

    if( eventName.indexOf( deviceID ) != -1 ){
      // if we get anything other than -1
      // the event came from this device.
      // so stop doing stuff
      return;
    }
    
    else{
        
      pixelfadesoff();

		}

   
}



 void pixelfadesup()
 {
     
   // set colors
    uint32_t pink = strip.Color(244, 66, 220, 0);
    
    for (int i = 0; i < strip.numPixels(); i++)
    {
        strip.setPixelColor(i, pink);
    }

    strip.show();
   
    delay(500);
    
 }
 
  void pixelfadesoff()
 {
     

    // set colors

    uint32_t off = strip.Color(0, 0, 0, 0);
    
    for (int i = 0; i < strip.numPixels(); i++)
    {
        strip.setPixelColor(i, off);
    }
    strip.show();
		  		     
    delay(500);
   
   
 }
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0