Philippe Gackstatter
Published © GPL3+

Smart Lighting and Notifications

Using a MediaTek LinkIt Smart 7688 Duo to wirelessly connect smart LEDs to your home automation system.

IntermediateFull instructions provided4 hours4,547

Things used in this project

Hardware components

NeoPixel strip
NeoPixel strip
×2
Capacitor 1000 µF
Capacitor 1000 µF
×1
Resistor 475 ohm
Resistor 475 ohm
×2
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1
Power Supply 5V/40A
×1

Software apps and online services

Arduino IDE
Arduino IDE
Visual Studio 2015
Microsoft Visual Studio 2015
PuTTY
WinSCP

Story

Read more

Schematics

Project Overview

Highlevel Overview of the used components

Wiring Setup

How the hardware components are connected

Code

Arduino Sketch

Arduino
#include <Adafruit_NeoPixel.h>

#define PIN1 2
#define PIN2 3
const int numPixels1 = 240;
const int numPixels2 = 240;
Adafruit_NeoPixel strip1;
Adafruit_NeoPixel strip2;

int countPixel = 0;
int countVal = 0;
byte r = 0;
byte g = 0;
byte b = 0;
byte w = 0;
int p = 0;
bool isOn = false;
byte command = 0;
bool isCommand = false;

void setup()
{
  strip1 = Adafruit_NeoPixel(numPixels1, PIN1, NEO_GRBW + NEO_KHZ800);
  delay(100);
  strip2 = Adafruit_NeoPixel(numPixels2, PIN2, NEO_GRBW + NEO_KHZ800);
  delay(100);
  // start serial port and wait for port to open:
  Serial.begin(115200);
  Serial1.begin(115200);
  strip1.setBrightness(255);
  strip1.begin();
  strip1.show(); // Initialize all pixels to 'off'
  strip2.setBrightness(255);
  strip2.begin();
  strip2.show(); // Initialize all pixels to 'off'
}

void fadeIn()
{
  isOn = true;
  for(int j = 0; j<=51; j++)
  {
    for(int i = 0; i<numPixels1; i++)
    {
      strip1.setPixelColor(i, 0, 0, 0, 5*j);
      strip2.setPixelColor(i, 0, 0, 0, 5*j);
    }
    strip1.show();
    strip2.show();
    delay(15);
  }
}

void fadeOut()
{
  isOn = false;
  for(int j = 51; j>=0; j--)
  {
    for(int i = 0; i<numPixels1; i++)cxdswq1
    {
      strip1.setPixelColor(i, 0, 0, 0, 5*j);
      strip2.setPixelColor(i, 0, 0, 0, 5*j);
    }
    strip1.show();
    strip2.show();
    delay(15);
  }
}

void fire()
{
    int r = 255;
    int g = r-60;
    int b = 20;
    for(int x = 0; x <numPixels1; x++)
    {
    int flicker = random(0,150);
    int r1 = r-flicker;
    int g1 = g-flicker;
    int b1 = b-flicker;
    if(g1<0) g1=0;
    if(r1<0) r1=0;
    if(b1<0) b1=0;
    strip1.setPixelColor(x,r1,g1, b1,0);
    strip2.setPixelColor(x,r1,g1, b1,0);
    }
    strip1.show();
    strip2.show();
    delay(random(10,20));
}

void rainbowCycle(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< strip1.numPixels(); i++) {
      strip1.setPixelColor(i, Wheel(((i * 256 / strip1.numPixels()) + j) & 255));
      strip2.setPixelColor(i, Wheel(((i * 256 / strip2.numPixels()) + j) & 255));
    }
    strip1.show();
    strip2.show();
    delay(wait);
  }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip1.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip1.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip1.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
void handleCommand(byte Command, byte Value)
{
  switch(Command)
  {
    //Toggle
    case 1:
      isCommand = false;
      command = 0;
      if(isOn)
      {
        fadeOut();
      }
      else
      {
        fadeIn();
      }
      break;
    //Turn on  
    case 2:
      isCommand = false;
      command = 0;
      fadeIn();
      break;
    //Turn off
    case 3:
      isCommand = false;
      command = 0;
      fadeOut();
      break;
  }
}
void loop()
{ 
  if (Serial1.available()>0)
  {
    int value = Serial1.read();
    if(value == 255 && command == 0)
    {
      isCommand = true;
    }
    else if(isCommand && command == 0)
    {
      command = value;
    }
    else
    {
      handleCommand(command, value);
    }
  }
}

Python Script

Python
#!/usr/bin/python
import serial 
import time 
import socket
import sys

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# Bind the socket to the port
server_address = ('10.0.3.13', 8077)
print >>sys.stderr, 'starting up on %s port %s' % server_address
sock.bind(server_address)

s = None 
def setup(): 
	global s 
# open serial COM port to /dev/ttyS0, which maps to UART0(D0/D1)
# the baudrate is set to 921600 and should be the same as the one
# specified in the Arduino sketch uploaded to ATMega32U4.
	s = serial.Serial("/dev/ttyS0", 115200) 
if __name__ == '__main__': 
	setup() 
while True:
    print >>sys.stderr, '\nwaiting to receive message'
    data, address = sock.recvfrom(4096)
    
    print >>sys.stderr, 'received %s bytes from %s' % (len(data), address)
    print >>sys.stderr, data
    for x in data:
        s.write(x);
    
#    if data:
#        sent = sock.sendto(data, address)
#        print >>sys.stderr, 'sent %s bytes back to %s' % (sent, address)

C# Client

C#
using System.Net.Sockets;

namespace phigax.Connector.NeoPixel
{
    public class NeoPixelConnector
    {
        UdpClient _client;
        public NeoPixelConnector()
        {

            _client = new UdpClient();
            _client.DontFragment = true;
            _client.Connect("10.0.3.13", 8077);
        }

        public void Toggle()
        {
            byte[] data = new byte[] { 255, 1, 0 };
            _client.Send(data, 3);
        }

        public void TurnOn()
        {
            byte[] data = new byte[] { 255, 2, 0 };
            _client.Send(data, 3);
        }

        public void TurnOff()
        {
            byte[] data = new byte[] { 255, 3, 0 };
            _client.Send(data, 3);
        }
    }
}

Startup script

BatchFile
#!/bin/sh /etc/rc.common

SCRIPT_NAME="Neopixel Script"
SCRIPT_PATH="/root/Jarvis.Linkit.Server.py"
LOG_FILE="/root/neopixel.log"
 
START=99
STOP=99
 
start() {        
        echo "Starting $SCRIPT_NAME"
        $SCRIPT_PATH >> $LOG_FILE 2>&1 &
}                 
 
stop() {          
        echo "Stopping $SCRIPT_NAME"
	killall `basename $SCRIPT_PATH`
}

Credits

Philippe Gackstatter

Philippe Gackstatter

1 project • 5 followers
maker noob and home automation enthusiast
Thanks to Tweaking4all.

Comments