resuther
Published © Apache-2.0

Arduino/Raspberry Pi website controllable Christmas lights

Use a Raspberry Pi and Arduino UNO to control Christmas lights with a website/app and sync music with it!

IntermediateShowcase (no instructions)816
Arduino/Raspberry Pi website controllable Christmas lights

Things used in this project

Story

Read more

Schematics

Wiring diagram

Code

app.py

Python
Replace the default Flask app.py on your Raspberry Pi with this script.
import serial
import time
import RPi.GPIO as GPIO
from flask import Flask, render_template, request
app = Flask(__name__)

GPIO.setmode(GPIO.BCM)

if __name__ == 'main':
    ser = serial.Serial('/dev/ttyACM0',9600, timeout=1)
    ser.flush()

pins = {
   15 : {'name' : 'LIGHTS', 'state' : GPIO.LOW},
   14 : {'name' : 'TREE','state' : GPIO.LOW},
   9 : {'name' : 'JINGLE BELLS LIGHTS', 'state' : GPIO.LOW}
   }

for pin in pins:
   GPIO.setup(pin, GPIO.OUT)
   GPIO.output(pin, GPIO.LOW)

@app.route("/")
def main():
   for pin in pins:
      pins[pin]['state'] = GPIO.input(pin)
   templateData = {
      'pins' : pins
      }
   return render_template('main.html', **templateData)

@app.route("/<changePin>/<action>")
def action(changePin, action):
   changePin = int(changePin)
   deviceName = pins[changePin]['name']
   if action == "on":
      if changePin == 9:
        GPIO.output(changePin, GPIO.LOW)
        ser = serial.Serial('/dev/ttyACM0',9600, timeout=1)
        ser.write(b"RESET NOW")
      if changePin == 14:
        GPIO.output(changePin, GPIO.HIGH)
      if changePin == 15:
        GPIO.output(changePin, GPIO.HIGH)
      message = "Turned " + deviceName + " on."
   if action == "off":
      GPIO.output(changePin, GPIO.LOW)
      message = "Turned " + deviceName + " off."

   for pin in pins:
      pins[pin]['state'] = GPIO.input(pin)

   templateData = {
      'pins' : pins
   }

   return render_template('main.html', **templateData)

if __name__ == "__main__":
   app.run(host='raspberry pi IP here', port=80, debug=True)

main.html

HTML
Replace the default Flask main.html with this script.
<!DOCTYPE html>
<head>
   <title>LED X-Mas Light</title>
   <meta name="viewport" content="width=device-width">
<style>
.test{
    height:200px;
    width:200px;
}

.block {
  display: block;
  width: 326px;
  height: 100px;
  border: none;
  background-color: #008ae6;
  padding: 14px 28px;
  font-size: 16px;
  cursor: pointer;
  text-align: center;
}
</style>
</head>

<body>
   <h1>LED X-Mas Lights</h1>
   {% for pin in pins %}
   <h2>{{ pins[pin].name }}
   {% if pins[pin].state == true %}
      is currently <strong>on</strong></h2><div class="row"><div class="col-md-2">
      <a href="/{{pin}}/off" class="block" role="button">Turn off</a></div></div>
   {% else %}
      is currently <strong>off</strong></h2><div class="row"><div class="col-md-2">
      <a href="/{{pin}}/on" class="block" role="button">Turn on</a></div></div>
   {% endif %}
   {% endfor %}
</body>
</html>

Arduino Code

Arduino
// Jingle Bells + Raspberry Pi BUS Restart
#include "pitches.h"

int melody[] = {
  NOTE_D4, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_D4, NOTE_D4, NOTE_D4,
  NOTE_D4, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_E4,
  NOTE_E4, NOTE_C5, NOTE_B4, NOTE_A4, NOTE_FS4,
  NOTE_D5, NOTE_D5, NOTE_C5, NOTE_A4, NOTE_B4, NOTE_D4,
  NOTE_D4, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_D4, NOTE_D4, NOTE_D4,
  NOTE_D4, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_E4, NOTE_E4,
  NOTE_E4, NOTE_C5, NOTE_B4, NOTE_A4, NOTE_D5, NOTE_D5, NOTE_D5, NOTE_D5,
  NOTE_E5, NOTE_D5, NOTE_C5, NOTE_A4, NOTE_G4,
  NOTE_B4, NOTE_B4, NOTE_B4, NOTE_B4, NOTE_B4, NOTE_B4,
  NOTE_B4, NOTE_D5, NOTE_G4, NOTE_A4, NOTE_B4,
  NOTE_C5, NOTE_C5, NOTE_C5, NOTE_C5, NOTE_C5, NOTE_B4, NOTE_B4, NOTE_B4, NOTE_B4,
  NOTE_B4, NOTE_A4, NOTE_A4, NOTE_B4, NOTE_A4, NOTE_D5,
  NOTE_B4, NOTE_B4, NOTE_B4, NOTE_B4, NOTE_B4, NOTE_B4,
  NOTE_B4, NOTE_D5, NOTE_G4, NOTE_A4, NOTE_B4,
  NOTE_C5, NOTE_C5, NOTE_C5, NOTE_C5, NOTE_C5, NOTE_B4, NOTE_B4, NOTE_B4, NOTE_B4,
  NOTE_D5, NOTE_D5, NOTE_C5, NOTE_A4, NOTE_G4 };

int noteDurations[] = {

  8, 8, 8, 8, 3, 16, 16,
  8, 8, 8, 8, 2,
  8, 8, 8, 8, 2, 
  8, 8, 8, 8, 3, 8,
  8, 8, 8, 8, 3, 16, 16,
  8, 8, 8, 8, 3, 8,
  8, 8, 8, 8, 8, 8, 8, 8,
  8, 8, 8, 8, 2,
  8, 8, 4, 8, 8, 4,
  8, 8, 6, 16, 2,
  8, 8, 6, 16, 8, 8, 8, 16, 16,
  8, 8, 8, 8, 4, 4,
  8, 8, 4, 8, 8, 4, 8, 8, 6, 16, 2,
  8, 8, 6, 16, 8, 8, 8, 16, 16,
  8, 8, 8, 8, 2 };

void setup() {
    pinMode(3,OUTPUT);
    pinMode(6,OUTPUT);
    pinMode(9,OUTPUT);
    pinMode(11,OUTPUT);
    digitalWrite(3, LOW);
    digitalWrite(6, LOW);
    digitalWrite(9, LOW);
    digitalWrite(11, LOW);
  for (int thisNote = 0; thisNote < 100; thisNote++) {

    int noteDuration = 1800/noteDurations[thisNote];
    // I found that by lengthening the noteDuration,
    // I was able to "slow" the song down.
    tone(7, melody[thisNote],noteDuration);
    LEDon();

    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(7);
    LEDoff();
  }
}

void loop() {

}

int currentLEDpin=3;
void LEDon()
{
    digitalWrite(currentLEDpin, HIGH);
}

void LEDoff()
{
    digitalWrite(currentLEDpin, LOW);
    if (currentLEDpin==3) currentLEDpin=9;
    else if (currentLEDpin==3) currentLEDpin=9;
    else if (currentLEDpin==9) currentLEDpin=3;
    else currentLEDpin=9;
}

void software_Reset()
{
asm volatile ("  jmp 0");  
}

Credits

resuther
0 projects • 0 followers
Contact

Comments

Please log in or sign up to comment.