1
0
mirror of https://github.com/balkian/shinesp.git synced 2024-12-22 13:38:12 +00:00

Fixed WiFi credentials

* Fixed bug when setting short WiFi ssid/passwords
* Added README
This commit is contained in:
J. Fernando Sánchez 2017-12-04 12:40:32 +01:00
parent b9b782a828
commit 8e075a40a4
2 changed files with 82 additions and 8 deletions

42
README.md Normal file
View File

@ -0,0 +1,42 @@
# RGB strip WiFi server
This server has been tested with:
* ws2812b LED strip
* WeMos ESP8266 board
# Instructions
The first time it boots, the board will create its own access point so you can configure the SSID and password.
To configure it, just connect to the `led` network and access the board either using its IP (it is the default gateway for the network) or its MDNS name (`esp8266.local`):
```
curl http://esp8266.local/credentials?ssid=mywifi&pass=mypassword
```
The board will then reboot and try to log in with the given credentials.
If the credentials were correct, the `led` network should have disappeared.
If it hasn't, try rebooting the board or debugging.
Once the board is connected to your router, you can start using the API of the board.
# API
* `/clear` clear WIFI credentials
* `/credentials?ssid=<SSID>&pass=<PASSWD>` Set the
* `/color?r=<R>&g=<G>&b=<B>` Set
* `/off` turn off the lights
* `/on` turn on the lights (using the last color and value)
* `/toggle` turn the lights if they are off, and vice versa
* `/white` set the color to white
* `/brightness?value=<VALUE>` set the brightness value (0-255)
* `/brightness/up` turn up the brightness (up to 255)
* `/brightness/down` turn down the brightness (down to 255)
# Debugging
You can use a serial monitor (e.g. in the Arduino IDE) to connect to the board.
# Over-the-air updates
You can connect to your board wirelessly and update its firmware using OTA updates.
To do so, follow this instructions: http://esp8266.github.io/Arduino/versions/2.0.0/doc/ota_updates/ota_updates.html

View File

@ -17,6 +17,9 @@ CRGB leds[NUM_LEDS];
const char* customssid = "led"; const char* customssid = "led";
const int binterval = 10; const int binterval = 10;
bool isOn = false;
CRGB lastColor = CRGB::White;
ESP8266WebServer httpServer(80); ESP8266WebServer httpServer(80);
int brightness = 255; int brightness = 255;
@ -98,6 +101,11 @@ void setCredentials(struct credential cred) {
Serial.print("Wrote: "); Serial.print("Wrote: ");
Serial.println(qsid[i]); Serial.println(qsid[i]);
} }
for (int i = qsid.length(); i< 32; ++i){
EEPROM.write(i, 0);
}
Serial.println("writing eeprom pass:"); Serial.println("writing eeprom pass:");
for (int i = 0; i < qpass.length(); ++i) for (int i = 0; i < qpass.length(); ++i)
{ {
@ -105,6 +113,10 @@ void setCredentials(struct credential cred) {
Serial.print("Wrote: "); Serial.print("Wrote: ");
Serial.println(qpass[i]); Serial.println(qpass[i]);
} }
for (int i = qpass.length(); i< 32; ++i){
EEPROM.write(i+32, 0);
}
EEPROM.commit(); EEPROM.commit();
} }
@ -140,9 +152,8 @@ void handleClearCredentials() {
} }
void handleWhite() { void handleWhite() {
fill_solid(leds, NUM_LEDS, CRGB(255, 255, 255)); lastColor = CRGB::White;
FastLED.show(); handleOn();
httpServer.send(200, "text/plain", "White!");
} }
void handleColor() { void handleColor() {
@ -150,17 +161,35 @@ void handleColor() {
int g = httpServer.arg("g").toInt(); int g = httpServer.arg("g").toInt();
int b = httpServer.arg("b").toInt(); int b = httpServer.arg("b").toInt();
char msg[400]; char msg[400];
fill_solid( leds, NUM_LEDS, CRGB(r,g,b)); lastColor = CRGB(r, g, b);
fill_solid( leds, NUM_LEDS, lastColor);
FastLED.show(); FastLED.show();
snprintf ( msg, 400, "RGB=(%d, %d, %d)", r, g, b); snprintf ( msg, 400, "RGB=(%d, %d, %d)", r, g, b);
httpServer.send(200, "text/plain", msg); httpServer.send(200, "text/plain", msg);
} }
void handleOff() { void handleOff() {
isOn = false;
fill_solid( leds, NUM_LEDS, CRGB::Black); fill_solid( leds, NUM_LEDS, CRGB::Black);
FastLED.show(); FastLED.show();
httpServer.send(200, "text/plain", "Off!"); httpServer.send(200, "text/plain", "Off!");
} }
void handleOn() {
isOn = true;
fill_solid( leds, NUM_LEDS, lastColor);
FastLED.show();
httpServer.send(200, "text/plain", "On!");
}
void handleToggle() {
if ( isOn ){
handleOff();
} else {
handleOn();
}
}
void handleBrightnessUp() { void handleBrightnessUp() {
// min/max don't work // min/max don't work
if (brightness > 255 - binterval) { if (brightness > 255 - binterval) {
@ -218,7 +247,6 @@ void handleRoot() {
<body>\ <body>\
<h1>Hello from ESP8266!</h1>\ <h1>Hello from ESP8266!</h1>\
<p>Uptime: %02d:%02d:%02d</p>\ <p>Uptime: %02d:%02d:%02d</p>\
<img src=\"/test.svg\" />\
</body>\ </body>\
</html>", </html>",
@ -325,9 +353,11 @@ void setup() {
httpServer.on ( "/", handleRoot ); httpServer.on ( "/", handleRoot );
httpServer.on ( "/clear", handleClearCredentials ); httpServer.on ( "/clear", handleClearCredentials );
httpServer.on ( "/credentials", handleSetCredentials ); httpServer.on ( "/credentials", handleSetCredentials );
httpServer.on ( "/white", handleWhite );
httpServer.on ( "/color", handleColor ); httpServer.on ( "/color", handleColor );
httpServer.on ( "/off", handleOff ); httpServer.on ( "/off", handleOff );
httpServer.on ( "/on", handleOn );
httpServer.on ( "/toggle", handleToggle );
httpServer.on ( "/white", handleWhite );
httpServer.on ( "/brightness", handleBrightness ); httpServer.on ( "/brightness", handleBrightness );
httpServer.on ( "/brightness/up", handleBrightnessUp ); httpServer.on ( "/brightness/up", handleBrightnessUp );
httpServer.on ( "/brightness/down", handleBrightnessDown ); httpServer.on ( "/brightness/down", handleBrightnessDown );
@ -337,8 +367,10 @@ void setup() {
// FastLED.setDither(0); // FastLED.setDither(0);
FastLED.setBrightness(255); FastLED.setBrightness(255);
FastLED.setMaxPowerInVoltsAndMilliamps(5, 20000); FastLED.setMaxPowerInVoltsAndMilliamps(5, 20000);
fill_solid(leds, NUM_LEDS, CRGB(255, 255, 255)); if (isOn){
fill_solid(leds, NUM_LEDS, lastColor);
FastLED.show(); FastLED.show();
}
} }
void loop() { void loop() {