Habeeb AlmatarJohn Gasson
Published

Electricity Saving Device

Do you want to save energy? You are on the right place.

BeginnerFull instructions provided1 hour499
Electricity Saving Device

Things used in this project

Hardware components

Photon
Particle Photon
×2
Photo resistor
Photo resistor
×1
Resistor 10k ohm
Resistor 10k ohm
×1
Resistor 220 ohm
Resistor 220 ohm
×3
Jumper wires (generic)
Jumper wires (generic)
×1
1-Channel Signal Relay 1A SPDT I²C Mini Module
ControlEverything.com 1-Channel Signal Relay 1A SPDT I²C Mini Module
×1
5 mm LED: Red
5 mm LED: Red
×1
Temperature Sensor
Temperature Sensor
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×2
Lamp Holder, B22d Lamps
Lamp Holder, B22d Lamps
×1
LED Light Bulb, Frosted GLS
LED Light Bulb, Frosted GLS
×1
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×2

Software apps and online services

Maker service
IFTTT Maker service
Google Sheets
Google Sheets
Particle Build Web IDE
Particle Build Web IDE
Windows 10
Microsoft Windows 10

Hand tools and fabrication machines

Wire Stripper & Cutter, 18-10 AWG / 0.75-4mm² Capacity Wires
Wire Stripper & Cutter, 18-10 AWG / 0.75-4mm² Capacity Wires
Flathead screwdrivers

Story

Read more

Schematics

Photon#1

Photon#2

CONSOLE View

Code

Photon#1

C/C++
// -----------------------------------------
// MEGR 3171 IoT project - Home Automation System (Group 1)
//Habeeb Almatar && John Gasson
// -----------------------------------------
// Soures: http://diotlabs.daraghbyrne.me/7-communicating-events/pir/
//         https://gist.github.com/zsup/9496462
//         http://community.garadget.com/t/instructions-for-home-grown-garadget-clone/69
//         https://docs.particle.io/tutorials/hardware-projects/maker-kit/
//         https://docs.particle.io/reference/device-os/firmware/photon/
// -----------------------------------------

#include <OneWire.h>

OneWire ds = OneWire(D4);       // Temperature sensor pin
int relay = D1;                 // Relay pin
int boardLed = D7;              // Photon onBoard LED
float light_threshold = 2000;   // Brightness to decide whether to switch light on
int analogvalue;                // Integer variable 'analogvalue', used later to store the value of the photoresistor.
int photoresistor = A0;         // This is where your photoresistor is plugged in. The other side goes to the "power" pin (A5).
int photopower = A5;             
float temperature;
float lastTemp;
unsigned long lastmillis = 0;

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

  pinMode(photoresistor,INPUT); 
  pinMode(photopower,OUTPUT);
  digitalWrite(photopower,HIGH);      
  pinMode(boardLed,OUTPUT); 
  pinMode(relay, OUTPUT);   
    
    Particle.variable("light", &photoresistor, INT);
    Particle.subscribe("light_status", myRelay, "4e003f000751373238323937");
}

void myRelay (const char *event, const char *data){
    if (strcmp(data,"On")==0) {
    
        digitalWrite(relay,LOW);
        delay(200);
  }
  else if (strcmp(data,"Off")==0) {
    
        digitalWrite(relay,HIGH);
        delay(200);
  }
}

void loop()
{
    //////////////////////Start of Photoresistor Code /////////////////////////////////////////////
    analogvalue = analogRead(photoresistor); // Read the photoresistor value
    delay(1000);
    Particle.publish("light", String(analogvalue));
    delay(1000);
    
    if (analogvalue <= light_threshold){ // Relay is NO(Normally Open) and "LOW" switches the light on
        digitalWrite(relay,LOW);
        Particle.publish("lightSwitch", "On");
        delay(200);
    }else{
        digitalWrite(relay,HIGH);
        Particle.publish("lightSwitch", "Off");
        delay(200);
    }
    
             if ((millis() - lastmillis) > 20000) {
                lastmillis = millis();
            }
  ////////////////////// End of Photoresistor Code /////////////////////////////////////////////
  
  //////////////////////Start of Temperature Sensor Code /////////////////////////////////////////////
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;
  
  if ( !ds.search(addr)) {
    Serial.println("No more addresses.");
    Serial.println();
    ds.reset_search();
    delay(250);
    return;
  }

  // The order is changed a bit in this example
  // first the returned address is printed

  Serial.print("ROM =");
  for( i = 0; i < 8; i++) {
    Serial.write(' ');
    Serial.print(addr[i], HEX);
  }

  // second the CRC is checked, on fail,
  // print error and just return to try again

  if (OneWire::crc8(addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return;
  }
  Serial.println();

  // we have a good address at this point
  // we will set a type_s value for known types or just return

  // the first ROM byte indicates which chip
  switch (addr[0]) {
    case 0x10:
      Serial.println("  Chip = DS1820/DS18S20");
      type_s = 1;
      break;
    case 0x28:
      Serial.println("  Chip = DS18B20");
      type_s = 0;
      break;
    case 0x22:
      Serial.println("  Chip = DS1822");
      type_s = 0;
      break;
    case 0x26:
      Serial.println("  Chip = DS2438");
      type_s = 2;
      break;
    default:
      Serial.println("Unknown device type.");
      return;
  }

  // this device has temp so let's read it

  ds.reset();               // first clear the 1-wire bus
  ds.select(addr);          // select the device found
  // ds.write(0x44, 1);     // tell it to start a conversion, with parasite power on at the end
  ds.write(0x44, 0);        // start conversion in powered mode (bus finishes low)

  // just wait a second while the conversion takes place
  // different chips have different conversion times, check the specs, 1 sec is worse case + 250ms
  // you could also communicate with other devices if you like but you would need
  // to already know their address to select them.

  delay(1000);     // Check 750ms

  // we might do a ds.depower() (parasite) here, but the reset will take care of it.

  // first make sure current values are in the scratch pad

  present = ds.reset();
  ds.select(addr);
  ds.write(0xB8,0);         // Recall Memory 0
  ds.write(0x00,0);         // Recall Memory 0

  // now read the scratch pad

  present = ds.reset();
  ds.select(addr);
  ds.write(0xBE,0);         // Read Scratchpad
  if (type_s == 2) {
    ds.write(0x00,0);       // The DS2438 needs a page# to read
  }

  // transfer and print the values

  Serial.print("  Data = ");
  Serial.print(present, HEX);
  Serial.print(" ");
  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
    Serial.print(data[i], HEX);
    Serial.print(" ");
  }
  Serial.print(" CRC=");
  Serial.print(OneWire::crc8(data, 8), HEX);
  Serial.println();

  // Convert the data to actual temperature
  // because the result is a 16 bit signed integer, it should
  // be stored to an "int16_t" type, which is always 16 bits
  // even when compiled on a 32 bit processor.
  int16_t raw = (data[1] << 8) | data[0];
  if (type_s == 2) raw = (data[2] << 8) | data[1];
  byte cfg = (data[4] & 0x60);

  switch (type_s) {
    case 1:
      raw = raw << 3; // 9 bit resolution default
      if (data[7] == 0x10) {
        // "count remain" gives full 12 bit resolution
        raw = (raw & 0xFFF0) + 12 - data[6];
      }
      celsius = (float)raw * 0.0625;
      break;
    case 0:
      // at lower res, the low bits are undefined, so let's zero them
      if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
      if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
      if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
      // default is 12 bit resolution, 750 ms conversion time
      celsius = (float)raw * 0.0625;
      break;

    case 2:
      data[1] = (data[1] >> 3) & 0x1f;
      if (data[2] > 127) {
        celsius = (float)data[2] - ((float)data[1] * .03125);
      }else{
        celsius = (float)data[2] + ((float)data[1] * .03125);
      }
  }

  // remove random errors
  if((((celsius <= 0 && celsius > -1) && lastTemp > 5)) || celsius > 125) {
      celsius = lastTemp;
  }

  fahrenheit = celsius * 1.8 + 32.0;
  lastTemp = celsius;
  Serial.print("  Temperature = ");
  Serial.print(celsius);
  Serial.print(" Celsius, ");
  Serial.print(fahrenheit);
  Serial.println(" Fahrenheit");

  
  String temperature = String(fahrenheit); // store temp in "temperature" string
  Particle.publish("temperature", temperature); 
  
  delay(1000);
}
////////////////////// End of Temperature Sensor Code /////////////////////////////////////////////

Photon#2

C/C++
// -----------------------------------------
// MEGR 3171 IoT project - Home Automation System (Group 1)
//Habeeb Almatar && John Gasson
// -----------------------------------------
// Soures: http://diotlabs.daraghbyrne.me/7-communicating-events/pir/
//         https://gist.github.com/zsup/9496462
//         http://community.garadget.com/t/instructions-for-home-grown-garadget-clone/69
//         https://docs.particle.io/tutorials/hardware-projects/maker-kit/
//         https://docs.particle.io/reference/device-os/firmware/photon/
// -----------------------------------------


#include <stdio.h>
#include <stdlib.h>

int i;
int led = D7;
int slitch = D5; //slide switch pin
int slitch_status = 0; //slide switch status
int lasttime = 0;
char *message = "The light is on";
char *message2 = "The light is off";

void setup() {

 pinMode(led, OUTPUT);
 pinMode(slitch, INPUT);
 Particle.subscribe("lightSwitch", LightFlip, "4d004e001451353432393433"); //Subscribed to the relay status and the status published
 Particle.variable("light_notify", message);
 Particle.variable("light_status", slitch_status);
}

void LightFlip (const char *event, const char *data){
    if (strcmp(data,"On")==0) {
    
    Particle.publish("light_notify", message);
  }
  else if (strcmp(data,"Off")==0) {
    
    Particle.publish("light_notify", message2);
  }
  else {
    
  }
}


void loop() {
  digitalWrite(led,HIGH);
  delay(1000);
  digitalWrite(led,LOW);
  delay(1000);
  slitch_status = digitalRead(slitch);
  delay(500);
  
  if (slitch_status == HIGH){
      Particle.publish("light_status", "On");
      delay(200);
  }
  else if (slitch_status == LOW){
      Particle.publish("light_status", "Off");
      delay(200);
  }
}

Credits

Habeeb Almatar

Habeeb Almatar

1 project • 1 follower
John Gasson

John Gasson

1 project • 0 followers

Comments

Add projectSign up / Login