Snippets
Created by
Pete Matthews
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | #include <ESP8266WiFi.h>
/*
* This bespoke "hack" code is for the H801 (LIXADA) control module.
* The H801 module is based on an ESP8266 and can drive a RGB LED strip of lights.
* This code responds to HTTP commands, but it could easily be converted to respond to MQTT etc - up to you
*
* N.B. You have to solder 6 header pins on the H801 - this is so you can connect a cheap FDTI USB for programming.
* N.B. You have to store your network SSID and password in the code variables *ssid and *pass (see below in code)
*
* When all is done and the module connected and powered up, you will see a new IP (server) appear in your network.
* If you access this new IP (server) with a web browser, you can control the lights as follows :
* To turn all three RGB outputs ON, use http://192.168.1.xxx/rgb/ffffff
* To turn all three RGB outputs OFF, use http://192.168.1.xxx/rgb/000000
* To turn on GREEN a little bit, use http://192.168.1.xxx/rgb/003300
* To turn on RED and BLUE a bit, with max GREEN, use http://192.168.1.xxx/rgb/33ff33
*/
void LED_RED();
void LED_GREEN();
void LED_BLUE();
void change_LED();
int convertToInt(char upper,char lower);
#define PWM_VALUE 63
int gamma_table[PWM_VALUE+1] = {
0, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8, 9, 10,
11, 12, 13, 15, 17, 19, 21, 23, 26, 29, 32, 36, 40, 44, 49, 55,
61, 68, 76, 85, 94, 105, 117, 131, 146, 162, 181, 202, 225, 250,
279, 311, 346, 386, 430, 479, 534, 595, 663, 739, 824, 918, 1023
};
// RGB FET
#define redPIN 15 //12
#define greenPIN 13 //15
#define bluePIN 12 //13
// onboard green LED D1
#define LEDPIN 5
// onboard red LED D2
#define LED2PIN 1
// note
// TX GPIO2 @Serial1 (Serial ONE)
// RX GPIO3 @Serial
#define LEDoff digitalWrite(LEDPIN,HIGH)
#define LEDon digitalWrite(LEDPIN,LOW)
#define LED2off digitalWrite(LED2PIN,HIGH)
#define LED2on digitalWrite(LED2PIN,LOW)
int led_delay_red = 0;
int led_delay_green = 0;
int led_delay_blue = 0;
#define time_at_colour 1300
unsigned long TIME_LED_RED = 0;
unsigned long TIME_LED_GREEN = 0;
unsigned long TIME_LED_BLUE = 0;
int RED, GREEN, BLUE;
int RED_A = 0;
int GREEN_A = 0;
int BLUE_A = 0;
const char *ssid = "Your network SSID here";
const char *pass = "Your network password here";
// Start WiFi Server
WiFiServer server(80);
void setup()
{
pinMode(LEDPIN, OUTPUT);
pinMode(LED2PIN, OUTPUT);
pinMode(redPIN, OUTPUT);
pinMode(greenPIN, OUTPUT);
pinMode(bluePIN, OUTPUT);
// Setup console
Serial1.begin(115200);
delay(10);
Serial1.println();
Serial1.println();
//client.set_callback(callback);
WiFi.begin(ssid, pass);
LEDon;
while (WiFi.status() != WL_CONNECTED) {
LED2off;
delay(500);
Serial1.print(".");
LED2on;
}
LEDoff;
Serial1.println("");
Serial1.println("WiFi connected");
Serial1.println("IP address: ");
Serial1.println(WiFi.localIP());
Serial1.println("");
server.begin();
Serial1.println("Webserver started");
}
void loop()
{
// Check if a client has connected
WiFiClient client = server.available();
if (client) {
LEDon;
// Read the first line of the request
String req = client.readStringUntil('\r');
Serial1.println(req);
client.flush();
// Prepare the response. Start with the common header:
String s = "HTTP/1.1 200 OK\r\n";
s += "Content-Type: text/html\r\n\r\n";
s += "<!DOCTYPE HTML>\r\n<html>\r\n";
if((req.indexOf("/rgb/") != -1)) {
// get RGB value
int pos = req.length();
int ind1 = req.indexOf("/rgb/") + 5;
String teststring = req.substring(ind1, pos);
int ind2 = teststring.indexOf("/");
String hexRGB = req.substring(ind1, ind2+ind1);
// convert HEX to RGB
hexRGB.toUpperCase();
char c[6];
hexRGB.toCharArray(c,7);
long r = convertToInt(c[0],c[1]); //red
long g = convertToInt(c[2],c[3]); //green
long b = convertToInt(c[4],c[5]); //blue
// set value of RGB controller
int red = map(r,0,255,0,PWM_VALUE);
red = constrain(red,0,PWM_VALUE);
int green = map(g,0,255,0,PWM_VALUE);
green = constrain(green, 0, PWM_VALUE);
int blue = map(b,0,255,0,PWM_VALUE);
blue = constrain(blue,0,PWM_VALUE);
RED = gamma_table[red];
GREEN = gamma_table[green];
BLUE = gamma_table[blue];
change_LED();
// confirm to client
s += "RGB value changed to ";
s += hexRGB;
}
s += "<br>"; // Go to the next line.
s += "Request OK!";
s += "</html>\n";
client.flush();
// Send the response to the client
client.print(s);
//delay(1);
Serial1.println("Client disconnected");
// The client will actually be disconnected
// when the function returns and 'client' object is destroyed
}
if ((RED != RED_A) or (GREEN != GREEN_A) or (BLUE != BLUE_A)) {
if(millis() - TIME_LED_RED >= led_delay_red){
TIME_LED_RED = millis();
LED_RED();
// };
}else{
if(millis() - TIME_LED_GREEN >= led_delay_green){
TIME_LED_GREEN = millis();
LED_GREEN();
// };
}else{
if(millis() - TIME_LED_BLUE >= led_delay_blue){
TIME_LED_BLUE = millis();
LED_BLUE();
// };
}}};
delayMicroseconds(200);
}else{
LEDoff;
// Put normal code here!!
}
}
void change_LED()
{
int diff_red = abs(RED-RED_A);
if(diff_red > 0){
led_delay_red = time_at_colour / abs(RED-RED_A);
}else{
led_delay_red = time_at_colour / 1023;
}
int diff_green = abs(GREEN-GREEN_A);
if(diff_green > 0){
led_delay_green = time_at_colour / abs(GREEN-GREEN_A);
}else{
led_delay_green = time_at_colour / 1023;
}
int diff_blue = abs(BLUE-BLUE_A);
if(diff_blue > 0){
led_delay_blue = time_at_colour / abs(BLUE-BLUE_A);
}else{
led_delay_blue = time_at_colour / 1023;
}
}
void LED_RED()
{
if(RED != RED_A){
if(RED_A > RED) RED_A = RED_A - 1;
if(RED_A < RED) RED_A++;
analogWrite(redPIN, RED_A);
}
}
void LED_GREEN()
{
if(GREEN != GREEN_A){
if(GREEN_A > GREEN) GREEN_A = GREEN_A - 1;
if(GREEN_A < GREEN) GREEN_A++;
analogWrite(greenPIN, GREEN_A);
}
}
void LED_BLUE()
{
if(BLUE != BLUE_A){
if(BLUE_A > BLUE) BLUE_A = BLUE_A - 1;
if(BLUE_A < BLUE) BLUE_A++;
analogWrite(bluePIN, BLUE_A);
}
}
int convertToInt(char upper,char lower)
{
int uVal = (int)upper;
int lVal = (int)lower;
uVal = uVal >64 ? uVal - 55 : uVal - 48;
uVal = uVal << 4;
lVal = lVal >64 ? lVal - 55 : lVal - 48;
return uVal + lVal;
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.