Added beating/relaxing mode

master
J. Fernando Sánchez 6 years ago
parent aa3ce00ef6
commit dd7f2b7750

@ -26,6 +26,21 @@ To get the IP of the board you have several options:
* The board prints the IP address it was given to the serial monitor
* Use nmap/zenmap/similar
# WEB UI
There is a simple HTML website that can be used to test the API and set the colors of the LED.
From the root of the repository, open it with your favourite browser, or with python:
```
firefox $PWD/index.html
# OR
python3 -m http.server
```
# API
* `/clear` clear WIFI credentials
@ -39,6 +54,17 @@ To get the IP of the board you have several options:
* `/brightness/up` turn up the brightness (up to 255)
* `/brightness/down` turn down the brightness (down to 0)
# Developing
* Install arduino
* Install support for ESP8266 boards in arduino: https://github.com/esp8266/Arduino
* Install the fastled library in arduino: https://github.com/FastLED/FastLED/
* Profit
# Debugging
You can use a serial monitor (e.g. in the Arduino IDE) to connect to the board.

@ -16,6 +16,11 @@
<input id="ip-address" type="text" value="http://192.168.1.104" />
<input id="chosen-value" class="jscolor {valueElement:'chosen-value', onFineChange:'setColor(this.rgb[0], this.rgb[1], this.rgb[2])'}" value="000000">
<button onclick="turnOff()">Turn OFF</button>
<button onclick="beatOn()">Beating ON</button>
<button onclick="beatOff()">Beating OFF</button>
<label>Beating period (ms)</label>
<input id="beating-period" type="int" value="2000" />
<button onclick="turnOff()">Turn OFF</button>
<button onclick="setColor(255, 255, 255)">White</button>
<script>
@ -51,6 +56,45 @@
xhttp.open("GET", ip + "/off", true);
xhttp.send();
}
function beatOn(){
period = document.getElementById("beating-period").value ;
BeatPeriod(period);
return Beat(true);
}
function beatOff(){
return Beat(false);
}
function Beat(b){
var state = "off";
if (b){
state = "on";
}
console.log("Turning beating " + state);
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log("Sent beating " + state);
ready = true;
};
};
ip = document.getElementById("ip-address").value ;
xhttp.open("GET", ip + "/beat/" + state, true);
xhttp.send();
}
function BeatPeriod(p){
console.log("Setting beating period to: " + p);
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log("Sent beating period to:" + p);
ready = true;
};
};
ip = document.getElementById("ip-address").value ;
xhttp.open("GET", ip + "/beat/?period=" + p, true);
xhttp.send();
}
</script>

@ -2,7 +2,9 @@
#define FASTLED_ESP8266_RAW_PIN_ORDER
#include "FastLED.h"
#define NUM_LEDS 300
#include "math.h"
#define NUM_LEDS 150
#define DATA_PIN 0
#define led 13
@ -17,6 +19,9 @@ CRGB leds[NUM_LEDS];
const char* customssid = "led";
const int binterval = 10;
bool isBeating = false;
String beatingFun = "sin";
int beatingPeriod = 5000;
bool isOn = false;
CRGB lastColor = CRGB::White;
@ -212,6 +217,52 @@ void handleBrightnessDown() {
httpServer.send(200, "text/plain", "Brightness set to " + String(brightness));
}
void handleBeat() {
beatingPeriod = httpServer.arg("period").toInt();
beatingFun = httpServer.arg("fun");
// min/max don't work
if (beatingPeriod < 0) {
beatingPeriod = 5000;
}
httpServer.send(200, "text/plain", "Beating period set to " + String(beatingPeriod) + " and function set to " + beatingFun);
}
void handleBeatOn() {
isBeating = true;
httpServer.send(200, "text/plain", "Now beating!");
}
void handleBeatOff() {
isBeating = false;
FastLED.setBrightness(brightness);
httpServer.send(200, "text/plain", "Not beating now!");
}
int getBeatingBrightness(){
double val = 255.0;
if (beatingFun.equals("lin")){
// statements
val = (millis() % (beatingPeriod)) - (beatingPeriod/2.0);
if (val < 0.0) {
val = -val;
}
val = 255.0 * 2.0 * (val/beatingPeriod);
}
else {
// statements
val = (cos(millis()*(2.0*PI)/((double) beatingPeriod))*127+127);
}
return (int) (val*(brightness/255.0));
}
void beat(){
FastLED.setBrightness(getBeatingBrightness());
}
void handleBrightness() {
int value = httpServer.arg("value").toInt();
if (value > 0) {
@ -358,6 +409,9 @@ void setup() {
httpServer.on ( "/on", handleOn );
httpServer.on ( "/toggle", handleToggle );
httpServer.on ( "/white", handleWhite );
httpServer.on ( "/beat/on", handleBeatOn );
httpServer.on ( "/beat/off", handleBeatOff );
httpServer.on ( "/beat/", handleBeat );
httpServer.on ( "/brightness", handleBrightness );
httpServer.on ( "/brightness/up", handleBrightnessUp );
httpServer.on ( "/brightness/down", handleBrightnessDown );
@ -376,5 +430,8 @@ void setup() {
void loop() {
ArduinoOTA.handle();
httpServer.handleClient();
if (isBeating){
beat();
}
FastLED.show();
}

Loading…
Cancel
Save