Aircoookie / Espalexa

Alexa voice control for ESP8266/ESP32 (including brightness and color!)
MIT License
541 stars 134 forks source link

WifiManager instead of hardcoding ssid+pwd #29

Open ChristianKnorr opened 5 years ago

ChristianKnorr commented 5 years ago

Hallo Aircoookie, I added the WifiManager because i love it 😁 BUT i don't know if it will work with ESP32 😢

Greetings (viele Grüße) Christian...

Voilà:

/*
   This is a basic example on how to use Espalexa with RGB color devices.
*/
#ifdef ARDUINO_ARCH_ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif

#include <DNSServer.h>            //Local DNS Server used for redirecting all requests to the configuration portal
#include <ESP8266WebServer.h>     //Local WebServer used to serve the configuration portal
#include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
#include <Espalexa.h>

//callback function prototype
void colorLightChanged(uint8_t brightness, uint32_t rgb);

Espalexa espalexa;

void setup()
{
  Serial.begin(115200);
  WiFiManager wifiManager;
  if (!wifiManager.autoConnect("Espalexa")) {
    Serial.println("failed to connect and hit timeout");
    //reset and try again, or maybe put it to deep sleep
    ESP.reset();
    delay(1000);
  } else {
    Serial.println("connected to WIFI..");
    espalexa_addDevices();
    espalexa.begin();
  }
  Serial.println("Now it's time to ask alexa to discover devices...");
}

void loop()
{
  espalexa.loop();
  delay(1);
}

void espalexa_addDevices() { // add Devices you want
  espalexa.addDevice("Light", colorLightChanged);
}

//the color device callback function has two parameters
void colorLightChanged(uint8_t brightness, uint32_t rgb) {
  //do what you need to do here, for example control RGB LED strip
  Serial.print("Brightness: ");
  Serial.print(brightness);
  Serial.print(", Red: ");
  Serial.print((rgb >> 16) & 0xFF); //get red component
  Serial.print(", Green: ");
  Serial.print((rgb >>  8) & 0xFF); //get green
  Serial.print(", Blue: ");
  Serial.println(rgb & 0xFF); //get blue
}
Aircoookie commented 5 years ago

Hi! Yeah, I agree with you that using WiFiManager is a far more practical solution than hardcoded values. Still, the example sketches should represent the minimal configuration necessary to run my library (so no external library dependencies), and it's up to the user to combine it with other libraries to make it more useful. For ESP32, there seems to be a fork of WiFiManager. Alternatively, c't Basecamp is a really good solution.

Viele Grüße :)