Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
gusgonnet
Published © CC BY-NC-SA

A More Realistic Electric Fireplace

Add some sound FX to your electric fireplace!

IntermediateFull instructions provided10 hours6,318

Things used in this project

Story

Read more

Code

Pi firmware

Arduino
Flash it to your Particle Pi
Particle App link: https://go.particle.io/shared_apps/5a4d9d89b7a129684e000133
// Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)
// This is a human-readable summary of (and not a substitute for) the license.
// Disclaimer
//
// You are free to:
// Share β€” copy and redistribute the material in any medium or format
// Adapt β€” remix, transform, and build upon the material
// The licensor cannot revoke these freedoms as long as you follow the license terms.
//
// Under the following terms:
// Attribution β€” You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
// NonCommercial β€” You may not use the material for commercial purposes.
// ShareAlike β€” If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.
// No additional restrictions β€” You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits.
//
// Notices:
// You do not have to comply with the license for elements of the material in the public domain or where your use is permitted by an applicable exception or limitation.
// No warranties are given. The license may not give you all of the permissions necessary for your intended use. For example, other rights such as publicity, privacy, or moral rights may limit how you use the material.
//
// github: https://github.com/gusgonnet
// hackster: https://www.hackster.io/gusgonnet
//
// Free for personal use.
//
// https://creativecommons.org/licenses/by-nc-sa/4.0/

#include <blynk.h>

#define BLYNK_BUTTON_ON_OFF V27
#define BLYNK_SLIDER_VOLUME V28
#define BLYNK_BUTTON_VOLUME V29

// DO NOT SHARE YOUR BLYNK TOKEN
char auth[] = "123720194708912734981273408912";

bool soundOn = false;
bool volumeHigh = false;
String volumeLevel = "-1800";

// auto on in the morning
const String timeOn = "09:00AM";
// auto off at midnight
const String timeOff = "11:59PM";


void setup() {

  //set time zone and format
  Time.zone(-5);
  Time.setFormat(TIME_FORMAT_ISO8601_FULL);
    
  Particle.function("playSound", playSound);
  Particle.function("stopSound", stopSound);
  Blynk.begin(auth);
}

void loop() {
  Blynk.run();
  
  // get time. Example: 10:38PM
  String timeNow = Time.format(Time.now(), "%I:%M%p");

  if (timeNow == timeOn)
  {
    soundOn = true;
    playSound("");
  }
  if (timeNow == timeOff)
  {
    stopSound("");
    soundOn = false;
  }  
  
 // this delay will slow down this rasppi particle agent process so the pi keeps its cool ;)
 // without this delay, top (the command line util on the pi) shows 100% CPU use (of the firmware.bin process)
 // with this delay in place, top shows 2% CPU use, which is much better
 delay(1000);
}


int playSound( String dummyParameter ) {
    
  // set the volume to your liking as explained here: 
  // https://stackoverflow.com/questions/33162820/adjust-audio-volume-level-with-cli-omxplayer-raspberry-pi
  Process proc;
  
  proc = Process::run("omxplayer --vol " + volumeLevel + " --loop /home/pi/fire.mp3 &");      
  Particle.publish("Napoleon Sound Fx", "Sound on", PRIVATE);
  
  proc.wait();
 
  return 0;
}

int stopSound( String dummyParameter ) {
    
  Process proc = Process::run("sudo killall omxplayer.bin");
  proc.wait();
  Particle.publish("Napoleon Sound Fx","Sound off", PRIVATE);
  return 0;
}

BLYNK_WRITE(BLYNK_BUTTON_ON_OFF)
{
  // background: in a BLYNK push button, blynk sends 0 then 1 when user taps on it
  // source: http://docs.blynk.cc/#widgets-controllers-button
  if (param.asInt() == 1)
  {
    soundOn = true;
    playSound("");
  } else
  {
    stopSound("");
    soundOn = false;
  }
}

BLYNK_WRITE(BLYNK_BUTTON_VOLUME)
{
  // background: in a BLYNK push button, blynk sends 0 then 1 when user taps on it
  // source: http://docs.blynk.cc/#widgets-controllers-button
  if (param.asInt() == 1)
  {
    volumeHigh = true;
  } else
  {
    volumeHigh = false;
  }

  if ( soundOn )
  {
    stopSound("");
    playSound("");
  }

}

BLYNK_WRITE(BLYNK_SLIDER_VOLUME)
{
  // background: in a BLYNK push button, blynk sends 0 then 1 when user taps on it
  // source: http://docs.blynk.cc/#widgets-controllers-button
  if (param.asInt() == 1)
  {
    volumeLevel = "-1800";
  }
  if (param.asInt() == 2)
  {
    volumeLevel = "-1000";
  }
  if (param.asInt() == 3)
  {
    volumeLevel = "-1";
  }
  
  if ( soundOn )
  {
    stopSound("");
    playSound("");
  }

}

Credits

gusgonnet

gusgonnet

37 projects β€’ 306 followers
With a decade of Software Engineering experience in IoT, I focus on creating Particle IoT solutions coupled with mobile and web applications

Comments