Rachel Chen
Published

IoT Lights: Project Llumin

IoT is all about connecting people to things and connecting things to other things. Let's connect people with people.

BeginnerFull instructions provided1,450
IoT Lights: Project Llumin

Things used in this project

Hardware components

MSP432 Microcontroller
Texas Instruments MSP432 Microcontroller
×2
CC3100BOOST SimpleLink CC3100 Wi-Fi BoosterPack
Texas Instruments CC3100BOOST SimpleLink CC3100 Wi-Fi BoosterPack
×2
Breadboard (generic)
Breadboard (generic)
×2
SPOKA LED Night Light
×2
Resistor 10k ohm
Resistor 10k ohm
×2
USB Wall Adapter
×2

Software apps and online services

Energia
Texas Instruments Energia
BlueMix
IBM BlueMix
Boxcar.io

Hand tools and fabrication machines

Wire Stripper
Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Code

Peer1ReactingtoPeer2.ino

C/C++
This code was written using Energia, an open source API framework based on Wiring/Arduino.
/* Peer 1 LAMP
  
  Peer 1 Button ON = Peer 1 Light ON = Publish to Peer 2 = Peer 2 ON
  Peer 1 Button OFF = Peer 1 Light OFF = Publish to Peer 2 = Peer 2 OFF
*/

#include <SPI.h>
#include <WiFi.h>
#include <PubSubClient.h>

// your network name also called SSID
char ssid[] = "YOUR NETWORK NAME HERE";
// your network password
char password[] = "YOUR PASSWORD HERE";
// MQTTServer to use
char server[] = "iot.eclipse.org";

const int buttonPin = 40;     // MCU pin connection to button
const int ledPin =  6;      // MCU pin connection to LED
int prevButtState = HIGH;
bool currLampState = 0;

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Received message for topic ");
  Serial.print(topic);
  Serial.print(" with length ");
  Serial.println(length);
  Serial.println("Message:");
  Serial.write(payload, length);
  Serial.println();
  
  /* disclaimer: I didn't know how to read the payload as a string, but I did
     figure out that when a 1 or 0 was received, *payload read 49 or 48,
     respectively. So I coded it as such. If this doesn't work for you, test to
     see what *payload reads when your unit receives a 1/0 from the publisher.
  */

  //  *payload == 49: Message received == 1
  //  *payload == 48: Message received == 0
  //  currLampState == 1 : Lamp ON
  //  currLampState == 0: Lamp OFF
  
  //  receive off, but your lamp is on
  //  note: code emulates handleButtPress
  if (*payload == 48 && currLampState == 1){
    simButtPress();
    delay(100);
    simButtPress();
    delay(100);
    currLampState = 0;
    
    Serial.println("receiving the off");
  }
  // receive on, but your lamp is off
  else if (*payload == 49 && currLampState == 0){
    simButtPress();
    currLampState = 1;
    
    Serial.println("receiving the on");
  }
}
WiFiClient wifiClient;
PubSubClient client(server, 1883, callback, wifiClient);

void setup(){
  //start serial connection
  Serial.begin(115200);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT); 
  prevButtState = digitalRead(buttonPin);
  
  // Start Ethernet with the build in MAC Address
  // attempt to connect to Wifi network:
  Serial.print("Attempting to connect to Network named: ");
  // print the network name (SSID);
  Serial.println(ssid); 
  // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
  WiFi.begin(ssid, password);
  while ( WiFi.status() != WL_CONNECTED) {
    // print dots while we wait to connect
    Serial.print(".");
    delay(300);
  }
  
  Serial.println("\nYou're connected to the network");
  Serial.println("Waiting for an ip address");
  
  while (WiFi.localIP() == INADDR_NONE) {
    // print dots while we wait for an ip addresss
    Serial.print(".");
    delay(300);
  }

  Serial.println("\nIP Address obtained");
  // We are connected and have an IP address.
  // Print the WiFi status.
  printWifiStatus();
}

/* The IKEA light has three states: off, cycle through colors, freeze on current color. 
To implement two states: off, cycle through colors, I had to simulate
button presses so that the output would be what I would want it to be.
*/
void simButtPress(){
    digitalWrite(ledPin, HIGH);
    delay(50);
    digitalWrite(ledPin, LOW);
}

void handleButtPress(){
  // light is on, turn off
  if (currLampState == 1){
    simButtPress();
    delay(100);
    simButtPress();
    delay(100);
    currLampState = 0;
    // send to subscriber
    if(client.publish("peer1", "0")) {
      Serial.println("Publish success OFF");
    } else {
      Serial.println("Publish failed");
    }     
  // light is off, turn on  
  } else{
    simButtPress();
    currLampState = 1;
    //send to subscriberM
    if(client.publish("peer1", "1")) {
      Serial.println("Publish success ON");
    } else {
      Serial.println("Publish failed");
    }
  }
}
  
void loop(){ 
  // read the input of the button
  int currButtState = digitalRead(buttonPin);
  
  if (!client.connected()) {
    Serial.println("Disconnected. Reconnecting....");

    if(!client.connect("peertopeer1")) {
      Serial.println("Connection failed");
    } else {
      Serial.println("Connection success");
      if(client.subscribe("peer2")) {
        Serial.println("Subscription successfull");
      }
    }
  }
  
   // button is pressed
  if (currButtState == LOW && prevButtState == HIGH){
   //turn off or turn on
    handleButtPress();
    prevButtState = LOW;
  
  // button is released
  } else if (currButtState == HIGH && prevButtState == LOW) {
    prevButtState = HIGH;
    delay(200);
  }
  
  client.poll();
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

Peer2ReactingtoPeer1.ino

C/C++
This code was written using Energia, an open source API framework based on Wiring/Arduino.
/* Peer 2 LAMP
  
  Peer 2 Button ON = Peer 2 Light ON = Publish to Peer 1 = Peer 1 ON
  Peer 2 Button OFF = Peer 2 Light OFF = Publish to Peer 1 = Peer 1 OFF
*/

#include <SPI.h>
#include <WiFi.h>
#include <PubSubClient.h>

// your network name also called SSID
char ssid[] = "YOUR NETWORK NAME HERE";
// your network password
char password[] = "YOUR PASSWORD HERE";
// MQTTServer to use
char server[] = "iot.eclipse.org";

const int buttonPin = 40;     // MCU pin connection to button
const int ledPin =  6;      // MCU pin connection to LED
int prevButtState = HIGH;
bool currLampState = 0;

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Received message for topic ");
  Serial.print(topic);
  Serial.print(" with length ");
  Serial.println(length);
  Serial.println("Message:");
  Serial.write(payload, length);
  Serial.println();
  
  /* disclaimer: I didn't know how to read the payload as a string, but I did
     figure out that when a 1 or 0 was received, *payload read 49 or 48,
     respectively. So I coded it as such. If this doesn't work for you, test to
     see what *payload reads when your unit receives a 1/0 from the publisher.
  */

  //  *payload == 49: Message received == 1
  //  *payload == 48: Message received == 0
  //  currLampState == 1 : Lamp ON
  //  currLampState == 0: Lamp OFF
  
  //  receive off, but your lamp is on
  //  note: code emulates handleButtPress
  if (*payload == 48 && currLampState == 1){
    simButtPress();
    delay(100);
    simButtPress();
    delay(100);
    currLampState = 0;
    
    Serial.println("receiving the off");
  }
  // receive on, but your lamp is off
  else if (*payload == 49 && currLampState == 0){
    simButtPress();
    currLampState = 1;
    
    Serial.println("receiving the on");
  }
}

WiFiClient wifiClient;
PubSubClient client(server, 1883, callback, wifiClient);

void setup(){
  //start serial connection
  Serial.begin(115200);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT); 
  prevButtState = digitalRead(buttonPin);
  
  // Start Ethernet with the build in MAC Address
  // attempt to connect to Wifi network:
  Serial.print("Attempting to connect to Network named: ");
  // print the network name (SSID);
  Serial.println(ssid); 
  // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
  WiFi.begin(ssid, password);
  while ( WiFi.status() != WL_CONNECTED) {
    // print dots while we wait to connect
    Serial.print(".");
    delay(300);
  }
  
  Serial.println("\nYou're connected to the network");
  Serial.println("Waiting for an ip address");
  
  while (WiFi.localIP() == INADDR_NONE) {
    // print dots while we wait for an ip addresss
    Serial.print(".");
    delay(300);
  }

  Serial.println("\nIP Address obtained");
  // We are connected and have an IP address.
  // Print the WiFi status.
  printWifiStatus();
}

/* The IKEA light has three states: off, cycle through colors, freeze on current color. 
To implement two states: off, cycle through colors, I had to simulate
button presses so that the output would be what I would want it to be.
*/
void simButtPress(){
    digitalWrite(ledPin, HIGH);
    delay(50);
    digitalWrite(ledPin, LOW);
}

void handleButtPress(){
  // light is on, turn off
  if (currLampState == 1){
    simButtPress();
    delay(100);
    simButtPress();
    delay(100);
    currLampState = 0;
    // send to subscriber
    if(client.publish("peer2", "0")) {
      Serial.println("Publish success OFF");
    } else {
      Serial.println("Publish failed");
    }     
  // light is off, turn on  
  } else{
    simButtPress();
    currLampState = 1;
    
    if(client.publish("peer2", "1")) {
      Serial.println("Publish success ON");
    } else {
      Serial.println("Publish failed");
    }
  }
}
  
void loop(){ 
  // read the input of the button
  int currButtState = digitalRead(buttonPin);
  
  if (!client.connected()) {
    Serial.println("Disconnected. Reconnecting....");

    if(!client.connect("peertopeer2")) {
      Serial.println("Connection failed");
    } else {
      Serial.println("Connection success");
      if(client.subscribe("peer1")) {
        Serial.println("Subscription successfull");
      }
    }
  }
  
   // button is pressed
  if (currButtState == LOW && prevButtState == HIGH){
   //turn off or turn on
    handleButtPress();
    prevButtState = LOW;
  
  // button is released
  } else if (currButtState == HIGH && prevButtState == LOW) {
    prevButtState = HIGH;
    delay(200);
  }
  
  client.poll();
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

Child1.ino

C/C++
This code was written using Energia, an open source API framework based on Wiring/Arduino.
/* CHILD 1 LAMP
  
  Button ON = Light ON = Publish to Parent ON
  Button OFF= Light OFF = Publish to Parent OFF
*/

#include <SPI.h>
#include <WiFi.h>
#include <PubSubClient.h>

// your network name also called SSID
char ssid[] = "YOUR NETWORK NAME HERE";
// your network password
char password[] = "YOUR PASSWORD HERE";
// MQTTServer to use
char server[] = "iot.eclipse.org";

const int buttonPin = 40;     // MCU pin connection to button
const int ledPin =  6;      // MCU pin connection to LED
int prevButtState = HIGH;
bool currLampState = 0;

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Received message for topic ");
  Serial.print(topic);
  Serial.print("with length ");
  Serial.println(length);
  Serial.println("Message:");
  Serial.write(payload, length);
  Serial.println();
}

WiFiClient wifiClient;
PubSubClient client(server, 1883, callback, wifiClient);

void setup(){
  //start serial connection
  Serial.begin(115200);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT); 
  prevButtState = digitalRead(buttonPin);
  
  // Start Ethernet with the build in MAC Address
  // attempt to connect to Wifi network:
  Serial.print("Attempting to connect to Network named: ");
  // print the network name (SSID);
  Serial.println(ssid); 
  // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
  WiFi.begin(ssid, password);
  while ( WiFi.status() != WL_CONNECTED) {
    // print dots while we wait to connect
    Serial.print(".");
    delay(300);
  }
  
  Serial.println("\nYou're connected to the network");
  Serial.println("Waiting for an ip address");
  
  while (WiFi.localIP() == INADDR_NONE) {
    // print dots while we wait for an ip addresss
    Serial.print(".");
    delay(300);
  }

  Serial.println("\nIP Address obtained");
  // We are connected and have an IP address.
  // Print the WiFi status.
  printWifiStatus();
}

/* The IKEA light has three states: off, cycle through colors, freeze on current color. 
To implement two states: off, cycle through colors, I had to simulate
button presses so that the output would be what I would want it to be.
*/

void simButtPress(){
    digitalWrite(ledPin, HIGH);
    delay(50);
    digitalWrite(ledPin, LOW);
}

//  currLampState == 0 : Lamp OFF
//  currLampState == 1: Lamp ON
void handleButtPress(){
  // light is on, turn off
  if (currLampState == 1){
    simButtPress();
    delay(100);
    simButtPress();
    delay(100);
    currLampState = 0;
   
    if(client.publish("parent","Child 1 OFF!")) {
      Serial.println("Publish success OFF");
    } else {
      Serial.println("Publish failed");
    } 
  // light is off, turn on
  } else{
    simButtPress();
    currLampState = 1;
    
    if(client.publish("parent","Child 1 ON!")) {
      Serial.println("Publish success ON");
    } else {
      Serial.println("Publish failed");
    }
  }
}
  
void loop(){ 
  // read the input of the button
  int currButtState = digitalRead(buttonPin);
  
  if (!client.connected()) {
    Serial.println("Disconnected. Reconnecting....");

    if(!client.connect("child1")) {
      Serial.println("Connection failed");
    } else {
      Serial.println("Connection success");
      if(client.subscribe("none")) {
        Serial.println("Subscription successfull");
      }
    }
  }
  
   // button is pressed
  if (currButtState == LOW && prevButtState == HIGH){
   //turn off or turn on
    handleButtPress();
    prevButtState = LOW;
  
  // button is released
  } else if (currButtState == HIGH && prevButtState == LOW) {
    prevButtState = HIGH;
    delay(200);
  }
  
  client.poll();
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

Child2.ino

C/C++
This code was written using Energia, an open source API framework based on Wiring/Arduino.
/* CHILD 2 LAMP
  
  Button ON = Light ON = Publish to Parent ON
  Button OFF= Light OFF = Publish to Parent OFF
*/

#include <SPI.h>
#include <WiFi.h>
#include <PubSubClient.h>

// your network name also called SSID
char ssid[] = "YOUR NETWORK NAME HERE";
// your network password
char password[] = "YOUR PASSWORD HERE";
// MQTTServer to use
char server[] = "iot.eclipse.org";

const int buttonPin = 40;     // MCU pin connection to button
const int ledPin =  6;      // MCU pin connection to LED
int prevButtState = HIGH;
bool currLampState = 0;

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Received message for topic ");
  Serial.print(topic);
  Serial.print("with length ");
  Serial.println(length);
  Serial.println("Message:");
  Serial.write(payload, length);
  Serial.println();
}

WiFiClient wifiClient;
PubSubClient client(server, 1883, callback, wifiClient);

void setup(){
  //start serial connection
  Serial.begin(115200);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT); 
  prevButtState = digitalRead(buttonPin);
  
  // Start Ethernet with the build in MAC Address
  // attempt to connect to Wifi network:
  Serial.print("Attempting to connect to Network named: ");
  // print the network name (SSID);
  Serial.println(ssid); 
  // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
  WiFi.begin(ssid, password);
  while ( WiFi.status() != WL_CONNECTED) {
    // print dots while we wait to connect
    Serial.print(".");
    delay(300);
  }
  
  Serial.println("\nYou're connected to the network");
  Serial.println("Waiting for an ip address");
  
  while (WiFi.localIP() == INADDR_NONE) {
    // print dots while we wait for an ip addresss
    Serial.print(".");
    delay(300);
  }

  Serial.println("\nIP Address obtained");
  // We are connected and have an IP address.
  // Print the WiFi status.
  printWifiStatus();
}

/* The IKEA light has three states: off, cycle through colors, freeze on current color. 
To implement two states: off, cycle through colors, I had to simulate
button presses so that the output would be what I would want it to be.
*/

void simButtPress(){
    digitalWrite(ledPin, HIGH);
    delay(50);
    digitalWrite(ledPin, LOW);
}

//  currLampState == 0 : Lamp OFF
//  currLampState == 1: Lamp ON
void handleButtPress(){
  // light is on, turn off
  if (currLampState == 1){
    simButtPress();
    delay(100);
    simButtPress();
    delay(100);
    currLampState = 0;
   
    if(client.publish("parent","Child 2 OFF!")) {
      Serial.println("Publish success OFF");
    } else {
      Serial.println("Publish failed");
    }     
  // light is off, turn on  
  } else{
    simButtPress();
    currLampState = 1;
    
    if(client.publish("parent","Child 2 ON!")) {
      Serial.println("Publish success ON");
    } else {
      Serial.println("Publish failed");
    }
  }
}
  
void loop(){ 
  // read the input of the button
  int currButtState = digitalRead(buttonPin);
  
  if (!client.connected()) {
    Serial.println("Disconnected. Reconnecting....");

    if(!client.connect("child2")) {
      Serial.println("Connection failed");
    } else {
      Serial.println("Connection success");
      if(client.subscribe("none")) {
        Serial.println("Subscription successfull");
      }
    }
  }
  
   // button is pressed
  if (currButtState == LOW && prevButtState == HIGH){
   //turn off or turn on
    handleButtPress();
    prevButtState = LOW;
  
  // button is released
  } else if (currButtState == HIGH && prevButtState == LOW) {
    prevButtState = HIGH;
    delay(200);
  }
  
  client.poll();
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

Credits

Rachel Chen

Rachel Chen

1 project • 3 followers
on track to be a full time cat lady

Comments

Add projectSign up / Login