RGB Controller Codes and Circuits
This commit is contained in:
BIN
Smart Home/Smart RGB Led Controller/ESP-12E & 12V.png
Normal file
BIN
Smart Home/Smart RGB Led Controller/ESP-12E & 12V.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 586 KiB |
BIN
Smart Home/Smart RGB Led Controller/ESP-12E & 5V.png
Normal file
BIN
Smart Home/Smart RGB Led Controller/ESP-12E & 5V.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 566 KiB |
BIN
Smart Home/Smart RGB Led Controller/Nodemcu & 12V.png
Normal file
BIN
Smart Home/Smart RGB Led Controller/Nodemcu & 12V.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 642 KiB |
BIN
Smart Home/Smart RGB Led Controller/Nodemcu & 5V.png
Normal file
BIN
Smart Home/Smart RGB Led Controller/Nodemcu & 5V.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 556 KiB |
124
Smart Home/Smart RGB Led Controller/Smart_RGB/Smart_RGB.ino
Normal file
124
Smart Home/Smart RGB Led Controller/Smart_RGB/Smart_RGB.ino
Normal file
@@ -0,0 +1,124 @@
|
|||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <WiFiUdp.h>
|
||||||
|
#include <WiFiClient.h>
|
||||||
|
#include <ESP8266WebServer.h>
|
||||||
|
#include <ESP8266HTTPUpdateServer.h>
|
||||||
|
#include <ESP8266HTTPClient.h>
|
||||||
|
|
||||||
|
#include "FS.h"
|
||||||
|
|
||||||
|
#define LED_R 15
|
||||||
|
#define LED_G 13
|
||||||
|
#define LED_B 12
|
||||||
|
|
||||||
|
ESP8266WebServer httpServer(80);
|
||||||
|
ESP8266HTTPUpdateServer httpUpdater;
|
||||||
|
|
||||||
|
String RGB = "0,0,0,255,0";
|
||||||
|
|
||||||
|
unsigned long lastSensorRead = 0;
|
||||||
|
unsigned long sensorReadDelay = 1000;
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
Serial.begin(115200);
|
||||||
|
|
||||||
|
analogWriteRange(255);
|
||||||
|
analogWrite(LED_R, 0);
|
||||||
|
analogWrite(LED_G, 0);
|
||||||
|
analogWrite(LED_B, 0);
|
||||||
|
|
||||||
|
bool success = SPIFFS.begin();
|
||||||
|
if (!success) Serial.println("Error mounting the file system");
|
||||||
|
|
||||||
|
connectToWifi();
|
||||||
|
|
||||||
|
readTheConfigFiles();
|
||||||
|
updateRGBColors();
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
if ((millis() - lastSensorRead) > sensorReadDelay) {
|
||||||
|
lastSensorRead = millis();
|
||||||
|
|
||||||
|
File f = SPIFFS.open("/mis_rgb.cfg", "w");
|
||||||
|
if (f) {
|
||||||
|
f.println(RGB);
|
||||||
|
f.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
httpServer.handleClient();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void connectToWifi() {
|
||||||
|
Serial.print("Connecting");
|
||||||
|
|
||||||
|
IPAddress staticIP(192, 168, 1, 230);
|
||||||
|
IPAddress gateway(192, 168, 1, 1);
|
||||||
|
IPAddress subnet(255, 255, 255, 0);
|
||||||
|
IPAddress dns(192, 168, 1, 1);
|
||||||
|
IPAddress dns2(8, 8, 8, 8);
|
||||||
|
|
||||||
|
const char* deviceName = "MakeItSmart_Smart_RGB";
|
||||||
|
const char *ssid = "**********Your_SSID************";
|
||||||
|
const char *password = "********Your Password**********";
|
||||||
|
|
||||||
|
WiFi.hostname(deviceName);
|
||||||
|
WiFi.config(staticIP, gateway, subnet, dns, dns2);
|
||||||
|
|
||||||
|
WiFi.mode(WIFI_AP_STA);
|
||||||
|
WiFi.begin(ssid, password);
|
||||||
|
|
||||||
|
int counter = 0;
|
||||||
|
while (WiFi.status() != WL_CONNECTED && counter++ < 20) {
|
||||||
|
delay(500);
|
||||||
|
Serial.print(".");
|
||||||
|
}
|
||||||
|
if (WiFi.status() != WL_CONNECTED) ESP.restart();
|
||||||
|
|
||||||
|
Serial.print("IP:");
|
||||||
|
Serial.println(WiFi.localIP());
|
||||||
|
|
||||||
|
httpUpdater.setup(&httpServer);
|
||||||
|
httpServer.on("/updateRGBColors", updateRGBColors);
|
||||||
|
httpServer.on("/getRGBColors", getRGBColors);
|
||||||
|
httpServer.on("/get_device_type", get_device_type);
|
||||||
|
httpServer.begin();
|
||||||
|
|
||||||
|
Serial.print("IP:");
|
||||||
|
Serial.println(WiFi.localIP());
|
||||||
|
|
||||||
|
delay(3000);
|
||||||
|
}
|
||||||
|
|
||||||
|
void readTheConfigFiles() {
|
||||||
|
File f = SPIFFS.open("/mis_rgb.cfg", "r");
|
||||||
|
if (f) {
|
||||||
|
RGB = f.readStringUntil('\n');
|
||||||
|
f.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//---------------------------------Http Functions---------------------------------------------------------------
|
||||||
|
void updateRGBColors() {
|
||||||
|
RGB = httpServer.arg("rgb_color");
|
||||||
|
|
||||||
|
int R, G, B, A, toggle ;
|
||||||
|
int n = sscanf(RGB.c_str(), "%d,%d,%d,%d,%d", &R, &G, &B, &A, &toggle);
|
||||||
|
|
||||||
|
A *= toggle;
|
||||||
|
analogWrite(LED_R, max(0, min(R * A / 255, 255)));
|
||||||
|
analogWrite(LED_G, max(0, min(G * A / 255, 255)));
|
||||||
|
analogWrite(LED_B, max(0, min(B * A / 255, 255)));
|
||||||
|
|
||||||
|
httpServer.send(200, "text/plain", RGB);
|
||||||
|
}
|
||||||
|
|
||||||
|
void getRGBColors(){
|
||||||
|
httpServer.send(200, "text/plain", RGB);
|
||||||
|
}
|
||||||
|
|
||||||
|
void get_device_type(){
|
||||||
|
httpServer.send(200, "text/plain", "RGB Controller");
|
||||||
|
}
|
||||||
@@ -1 +0,0 @@
|
|||||||
https://youtu.be/yxsz6cjm-5I
|
|
||||||
@@ -52,16 +52,12 @@ long last_position_saved = 0;
|
|||||||
long last_Position_Checked = 0;
|
long last_Position_Checked = 0;
|
||||||
|
|
||||||
int motor_direction = 0;
|
int motor_direction = 0;
|
||||||
int last_motor_move_direction = 0;
|
|
||||||
long motor_position_counter = 0;
|
long motor_position_counter = 0;
|
||||||
|
|
||||||
int goingTo = int_max;
|
int goingTo = int_max;
|
||||||
|
|
||||||
|
|
||||||
int timeZone = 3;
|
int timeZone = 3;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool isReverse = false;
|
bool isReverse = false;
|
||||||
|
|
||||||
struct Position {
|
struct Position {
|
||||||
@@ -74,8 +70,6 @@ struct Position {
|
|||||||
int numPositions = 0;
|
int numPositions = 0;
|
||||||
Position positionList[10];
|
Position positionList[10];
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
|
||||||
@@ -111,8 +105,6 @@ void loop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (motor_direction != 0) last_motor_move_direction = motor_direction;
|
|
||||||
|
|
||||||
digitalWrite(left_Motor_forward_pin, motor_direction > 0 ? HIGH : LOW);
|
digitalWrite(left_Motor_forward_pin, motor_direction > 0 ? HIGH : LOW);
|
||||||
digitalWrite(right_Motor_forward_pin, motor_direction > 0 ? HIGH : LOW);
|
digitalWrite(right_Motor_forward_pin, motor_direction > 0 ? HIGH : LOW);
|
||||||
|
|
||||||
@@ -191,12 +183,6 @@ void saveMotorPosition() {
|
|||||||
f.close();
|
f.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
f = SPIFFS.open("/last_motor_move_direction.log", "w");
|
|
||||||
if (f) {
|
|
||||||
f.println(last_motor_move_direction);
|
|
||||||
f.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
last_position_saved = millis();
|
last_position_saved = millis();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -207,12 +193,6 @@ void loadLastValues() {
|
|||||||
f.close();
|
f.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
f = SPIFFS.open("/last_motor_move_direction.log", "r");
|
|
||||||
if (f) {
|
|
||||||
last_motor_move_direction = f.readStringUntil('\n').toInt();
|
|
||||||
f.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
f = SPIFFS.open("/reverse.log", "r");
|
f = SPIFFS.open("/reverse.log", "r");
|
||||||
if (f) {
|
if (f) {
|
||||||
isReverse = f.readStringUntil('\n').toInt() == 1;
|
isReverse = f.readStringUntil('\n').toInt() == 1;
|
||||||
@@ -249,9 +229,6 @@ void loadLastValues() {
|
|||||||
|
|
||||||
void go(int p) {
|
void go(int p) {
|
||||||
goingTo = p;
|
goingTo = p;
|
||||||
|
|
||||||
//if (motor_position_counter < goingTo && last_motor_move_direction == -1) motor_position_counter -= 2;
|
|
||||||
//else if (motor_position_counter > goingTo && last_motor_move_direction == 1) motor_position_counter += 2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void read_encoder() {
|
void read_encoder() {
|
||||||
|
|||||||
Reference in New Issue
Block a user