Andy Warburton
Published © GPL3+

Networked Nightlights with ESP8266 and Raspberry Pi

Want to create low-cost nightlights that get their colour from a simple web server running on your network? This guide will show you how!

IntermediateFull instructions provided2 hours3,269
Networked Nightlights with ESP8266 and Raspberry Pi

Things used in this project

Story

Read more

Schematics

Hooking up the LEDs

Fritzing fiagram

Code

Flask Webserver

Python
Simple webserver using Flask to tell the lights what colour to be!
from flask import Flask, request
import datetime

app = Flask(__name__)

@app.route('/')

def index():

	# get the time
	a = datetime.datetime.now().time()

	# convert time to a float
	mytime = a.hour+a.minute/60.0

	# default to off
	color = "0,0,0,0"

	if(mytime > 7):

		r = 0
		g = 255
		b = 0
		a = 255

	if(mytime > 9):

		r = 255
		g = 255
		b = 255
		a = 255

	color = str(r) + "," + str(g) + "," + str(b) + "," + str(a)

	return color

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=999)

Arduino code (for ESP8266)

Arduino
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <FastLED.h>

FASTLED_USING_NAMESPACE

#define DATA_PIN    3
#define LED_TYPE    WS2812
#define COLOR_ORDER GRB
#define NUM_LEDS    15
#define WIFI_SSID   "your wifi network name here!"
#define WIFI_PWD    "Your wifi password"
#define HOST_NAME   "NightLight01"
#define DATA_SRC    "URL for the location of your little API"


#define BRIGHTNESS          90
#define FRAMES_PER_SECOND  120

CRGB leds[NUM_LEDS];

// function to explode strings
String getValue(String data, char separator, int index) {
  int found = 0;
  int strIndex[] = {0, -1};
  int maxIndex = data.length()-1;
  for(int i=0; i<=maxIndex && found<=index; i++){
    if(data.charAt(i)==separator || i==maxIndex){
        found++;
        strIndex[0] = strIndex[1]+1;
        strIndex[1] = (i == maxIndex) ? i+1 : i;
    }
  }
  return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}

void setup() {

  Serial.begin(230400);

  // tell FastLED about the LED strip configuration
  FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);

  // set master brightness control
  FastLED.setBrightness(BRIGHTNESS);

  WiFi.begin(WIFI_SSID, WIFI_PWD);
  WiFi.hostname(HOST_NAME);

  while (WiFi.status() != WL_CONNECTED) {
    delay(100); 
    Serial.println("Connecting.");
  }

}

void loop() {

  delay(1000);

  if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status

    HTTPClient http;		// Declare an object of class HTTPClient
    http.begin(DATA_SRC);	// Specify request destination
    int httpCode = http.GET();  // Send the request

    if (httpCode > 0) { 

      String payload = http.getString();   
      Serial.println(payload);             

      int r = getValue(payload, ',', 0).toInt();
      int g = getValue(payload, ',', 1).toInt();
      int b = getValue(payload, ',', 2).toInt();
      int a = getValue(payload, ',', 3).toInt();

      FastLED.setBrightness(a);

      for (int i=0; i <= NUM_LEDS; i++) {
        leds[i].setRGB(r,g,b); 
      }
      FastLED.show();

    }

    http.end();   //Close connection

  }

  // pause for 60 seconds
  delay(1000*60); 

}

Sunrise

Python
Want to simulate a sunrise? This basic example can get you started!
if(mytime > 0):
# dull red
	r = 255
	g = 0
	b = 0
	a = 20

if(mytime > 5):
# orange
	r = 255
	g = 33
	b = 0
	a = 150

if(mytime > 6):
# brighter orange
	r = 255
	g = 100
	b = 0
	a = 200	

if(mytime > 6.5):
# more orangy
	r = 255
	g = 157
	b = 0
	a = 255

if(mytime > 6.75):
# yellow
	r = 255
	g = 255
	b = 0			
	a = 255

if(mytime > 7):
# white
	r = 255
	g = 255
	b = 255
	a = 255		

Credits

Andy Warburton

Andy Warburton

1 project • 5 followers
daddy, husband, maker, tinkerer, hacker and nerd. Builder of web and electronic doohickies #raspberrypi #arduino #maker

Comments