0% found this document useful (0 votes)
204 views10 pages

Cod Fain LED Server WS2812FX

This document contains code for an ESP8266-based IoT device that monitors temperature, humidity, gas levels, and motion using a DHT sensor, gas sensor, and PIR sensor. It displays the sensor readings on an LCD and controls WS2812B LED strips using the WS2812FX library. It includes WiFi setup code and a web server to control the LED effects, brightness, speed, and auto cycling via a web UI.

Uploaded by

Iulian Lungu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
204 views10 pages

Cod Fain LED Server WS2812FX

This document contains code for an ESP8266-based IoT device that monitors temperature, humidity, gas levels, and motion using a DHT sensor, gas sensor, and PIR sensor. It displays the sensor readings on an LCD and controls WS2812B LED strips using the WS2812FX library. It includes WiFi setup code and a web server to control the LED effects, brightness, speed, and auto cycling via a web UI.

Uploaded by

Iulian Lungu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

#include <ESP8266WiFi.

h>

#include <ESP8266WebServer.h>

#include <WS2812FX.h>

#include "DHT.h"

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321

uint8_t GAS = D8;

uint8_t DHTPin = D6;

uint8_t BUZZERPin = D5;

uint8_t PIRpin = D7;

DHT dht(DHTPin, DHTTYPE);

float Temperature=0;

float Humidity=0;

int GASlevel;

int PIRvalue;

extern const char index_html[];

extern const char main_js[];

#define WIFI_SSID "Olga"

#define WIFI_PASSWORD "069111475"

//#define STATIC_IP // uncomment for static IP, set IP below

#ifdef STATIC_IP

IPAddress ip(192,168,0,123);

IPAddress gateway(192,168,0,1);

IPAddress subnet(255,255,255,0);

#endif
// QUICKFIX...See https://github.com/esp8266/Arduino/issues/263

#define min(a,b) ((a)<(b)?(a):(b))

#define max(a,b) ((a)>(b)?(a):(b))

#define LED_PIN 2 // 0 = GPIO0, 2=GPIO2

#define LED_COUNT 60

#define WIFI_TIMEOUT 30000 // checks WiFi every ...ms. Reset after this time, if WiFi cannot
reconnect.

#define HTTP_PORT 80

#define DEFAULT_COLOR 0xFF5900

#define DEFAULT_BRIGHTNESS 0

#define DEFAULT_SPEED 1000

#define DEFAULT_MODE FX_MODE_STATIC

unsigned long auto_last_change = 0;

unsigned long last_wifi_check_time = 0;

String modes = "";

uint8_t myModes[] = {}; // *** optionally create a custom list of effect/mode numbers

boolean auto_cycle = false;

WS2812FX ws2812fx = WS2812FX(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);

ESP8266WebServer server(HTTP_PORT);

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup(){

Serial.begin(115200);

delay(500);

Serial.println("\n\nStarting...");

modes.reserve(5000);
modes_setup();

Serial.println("WS2812FX setup");

ws2812fx.init();

ws2812fx.setMode(DEFAULT_MODE);

ws2812fx.setColor(DEFAULT_COLOR);

ws2812fx.setSpeed(DEFAULT_SPEED);

ws2812fx.setBrightness(DEFAULT_BRIGHTNESS);

ws2812fx.start();

Serial.println("Wifi setup");

wifi_setup();

Serial.println("HTTP server setup");

server.on("/", srv_handle_index_html);

server.on("/main.js", srv_handle_main_js);

server.on("/modes", srv_handle_modes);

server.on("/set", srv_handle_set);

server.onNotFound(srv_handle_not_found);

server.begin();

Serial.println("HTTP server started.");

Serial.println("ready!");

pinMode(4,INPUT_PULLUP);

pinMode(5,INPUT_PULLUP);

pinMode(DHTPin, INPUT);

dht.begin();

lcd.init(); // initialize LCD

lcd.backlight(); // turn on LCD backlight

pinMode(GAS, INPUT);

pinMode(BUZZERPin, OUTPUT);
pinMode(PIRpin, INPUT);

void loop() {

unsigned long now = millis();

server.handleClient();

ws2812fx.service();

if(now - last_wifi_check_time > WIFI_TIMEOUT) {

Serial.print("Checking WiFi... ");

if(WiFi.status() != WL_CONNECTED) {

Serial.println("WiFi connection lost. Reconnecting...");

wifi_setup();

} else {

Serial.println("OK");

last_wifi_check_time = now;

if(auto_cycle && (now - auto_last_change > 10000)) { // cycle effect mode every 10 seconds

uint8_t next_mode = (ws2812fx.getMode() + 1) % ws2812fx.getModeCount();

if(sizeof(myModes) > 0) { // if custom list of modes exists

for(uint8_t i=0; i < sizeof(myModes); i++) {

if(myModes[i] == ws2812fx.getMode()) {

next_mode = ((i + 1) < sizeof(myModes)) ? myModes[i + 1] : myModes[0];

break;

}
}

ws2812fx.setMode(next_mode);

Serial.print("mode is "); Serial.println(ws2812fx.getModeName(ws2812fx.getMode()));

auto_last_change = now;

Temperature = dht.readTemperature(); // Gets the values of the temperature

Humidity = dht.readHumidity(); // Gets the values of the humidity

lcd.print("Temp.: "); // Prints string "Temp." on the LCD

lcd.print(Temperature); // Prints the temperature value from the sensor

lcd.print(" C");

lcd.setCursor(0,1);

lcd.print("Humi.: ");

lcd.print(Humidity);

lcd.print(" % ");

GASlevel=analogRead(GAS);

if(GASlevel==1023){

int a=ws2812fx.getMode();

int b=ws2812fx.getBrightness();

int c=ws2812fx.getColor();

Serial.println("GAZ");

tone(BUZZERPin, 50);

delay(50);

ws2812fx.setMode(0);

ws2812fx.setBrightness(255);
ws2812fx.setColor(0xFF0000);

delay(500);

noTone(BUZZERPin);

delay(100);

ws2812fx.setMode(0);

ws2812fx.setBrightness(0);

ws2812fx.setColor(0xFF0000);

delay(500);

ws2812fx.setMode(a);

ws2812fx.setBrightness(b);

ws2812fx.setColor(c);

PIRvalue = digitalRead(PIRpin);

if(PIRvalue==1){

Serial.println("PIR DETECTED");

ws2812fx.setBrightness(255);

/*

* Connect to WiFi. If no connection is made within WIFI_TIMEOUT, ESP gets resettet.

*/

void wifi_setup() {

Serial.println();
Serial.print("Connecting to ");

Serial.println(WIFI_SSID);

WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

WiFi.mode(WIFI_STA);

#ifdef STATIC_IP

WiFi.config(ip, gateway, subnet);

#endif

unsigned long connect_start = millis();

while(WiFi.status() != WL_CONNECTED) {

delay(500);

Serial.print(".");

if(millis() - connect_start > WIFI_TIMEOUT) {

Serial.println();

Serial.print("Tried ");

Serial.print(WIFI_TIMEOUT);

Serial.print("ms. Resetting ESP now.");

ESP.reset();

Serial.println("");

Serial.println("WiFi connected");

Serial.print("IP address: ");

Serial.println(WiFi.localIP());

Serial.println();

}
/*

* Build <li> string for all modes.

*/

void modes_setup() {

modes = "";

uint8_t num_modes = sizeof(myModes) > 0 ? sizeof(myModes) : ws2812fx.getModeCount();

for(uint8_t i=0; i < num_modes; i++) {

uint8_t m = sizeof(myModes) > 0 ? myModes[i] : i;

modes += "<li><a href='#'>";

modes += ws2812fx.getModeName(m);

modes += "</a></li>";

/* #####################################################

# Webserver Functions

##################################################### */

void srv_handle_not_found() {

server.send(404, "text/plain", "File Not Found");

void srv_handle_index_html() {

server.send_P(200,"text/html", index_html);

void srv_handle_main_js() {

server.send_P(200,"application/javascript", main_js);

void srv_handle_modes() {
server.send(200,"text/plain", modes);

void srv_handle_set() {

for (uint8_t i=0; i < server.args(); i++){

if(server.argName(i) == "c") {

uint32_t tmp = (uint32_t) strtol(server.arg(i).c_str(), NULL, 10);

if(tmp >= 0x000000 && tmp <= 0xFFFFFF) {

ws2812fx.setColor(tmp);

if(server.argName(i) == "m") {

uint8_t tmp = (uint8_t) strtol(server.arg(i).c_str(), NULL, 10);

ws2812fx.setMode(tmp % ws2812fx.getModeCount());

Serial.print("mode is "); Serial.println(ws2812fx.getModeName(ws2812fx.getMode()));

if(server.argName(i) == "b") {

if(server.arg(i)[0] == '-') {

ws2812fx.setBrightness(ws2812fx.getBrightness() * 0.8);

} else if(server.arg(i)[0] == ' ') {

ws2812fx.setBrightness(min(max(ws2812fx.getBrightness(), 5) * 1.2, 255));

} else { // set brightness directly

uint8_t tmp = (uint8_t) strtol(server.arg(i).c_str(), NULL, 10);

ws2812fx.setBrightness(tmp);

Serial.print("brightness is "); Serial.println(ws2812fx.getBrightness());

if(server.argName(i) == "s") {
if(server.arg(i)[0] == '-') {

ws2812fx.setSpeed(max(ws2812fx.getSpeed(), 5) * 1.2);

} else if(server.arg(i)[0] == ' ') {

ws2812fx.setSpeed(ws2812fx.getSpeed() * 0.8);

} else {

uint16_t tmp = (uint16_t) strtol(server.arg(i).c_str(), NULL, 10);

ws2812fx.setSpeed(tmp);

Serial.print("speed is "); Serial.println(ws2812fx.getSpeed());

if(server.argName(i) == "a") {

if(server.arg(i)[0] == '-') {

auto_cycle = false;

} else {

auto_cycle = true;

auto_last_change = 0;

server.send(200, "text/plain", "OK");

You might also like