Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom bridge name #90

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions ESP8266HueEmulator/ESP8266HueEmulator.ino
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ RgbColor black = RgbColor(0);
#define pixelPin 2 // Strip is attached to GPIO2 on ESP-01
NeoPixelBus<NeoGrbFeature, NeoEsp8266Uart800KbpsMethod> strip(MAX_LIGHT_HANDLERS * NUM_PIXELS_PER_LIGHT, pixelPin);
NeoPixelAnimator animator(MAX_LIGHT_HANDLERS * NUM_PIXELS_PER_LIGHT, NEO_MILLISECONDS); // NeoPixel animation management object
LightServiceClass LightService;

HsbColor getHsb(int hue, int sat, int bri) {
float H, S, B;
Expand Down
59 changes: 51 additions & 8 deletions ESP8266HueEmulator/LightService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ String bridgeIDString;
String ipString;
String netmaskString;
String gatewayString;
const char * friendlyName;
// The username of the client (currently we authorize all clients simulating a pressed button on the bridge)
String client;

Expand Down Expand Up @@ -188,11 +189,15 @@ class WcFnRequestHandler : public RequestHandler {
char _wildcard;
};

LightServiceClass LightService;

LightHandler *lightHandlers[MAX_LIGHT_HANDLERS] = {}; // interfaces exposed to the outside world

LightServiceClass::LightServiceClass() { }
LightServiceClass::LightServiceClass() {
friendlyName = "hue emulator";
}

LightServiceClass::LightServiceClass(const char* friendlyNameArg) {
friendlyName = friendlyNameArg;
}

bool LightServiceClass::setLightHandler(int index, LightHandler *handler) {
if (index >= currentNumLights || index < 0) return false;
Expand Down Expand Up @@ -265,7 +270,7 @@ static const char* _ssdp_xml_template = "<?xml version=\"1.0\" ?>"
"<URLBase>http://{ip}:80/</URLBase>"
"<device>"
"<deviceType>urn:schemas-upnp-org:device:Basic:1</deviceType>"
"<friendlyName>Philips hue ( {ip} )</friendlyName>"
"<friendlyName>Philips hue ({ip})</friendlyName>"
"<manufacturer>Royal Philips Electronics</manufacturer>"
"<manufacturerURL>http://www.philips.com</manufacturerURL>"
"<modelDescription>Philips hue Personal Wireless Lighting</modelDescription>"
Expand All @@ -275,7 +280,22 @@ static const char* _ssdp_xml_template = "<?xml version=\"1.0\" ?>"
"<serialNumber>{mac}</serialNumber>"
"<UDN>uuid:2f402f80-da50-11e1-9b23-{mac}</UDN>"
"<presentationURL>index.html</presentationURL>"
//"<iconList><icon><mimetype>image/png</mimetype><height>48</height><width>48</width><depth>24</depth><url>hue_logo_0.png</url></icon><icon><mimetype>image/png</mimetype><height>120</height><width>120</width><depth>24</depth><url>hue_logo_3.png</url></icon></iconList>"
"<iconList>"
" <icon>"
" <mimetype>image/png</mimetype>"
" <height>48</height>"
" <width>48</width>"
" <depth>24</depth>"
" <url>hue_logo_0.png</url>"
" </icon>"
" <icon>"
" <mimetype>image/png</mimetype>"
" <height>120</height>"
" <width>120</width>"
" <depth>24</depth>"
" <url>hue_logo_3.png</url>"
" </icon>"
"</iconList>"
"</device>"
"</root>";

Expand Down Expand Up @@ -374,6 +394,30 @@ void on(HandlerFunction fn, const String &wcUri, HTTPMethod method, char wildcar
HTTP->addHandler(new WcFnRequestHandler(fn, wcUri, method, wildcard));
}

void indexPageFn() {
String response = "<html><body>"
"<h2>Philips HUE - {name} ( {ip} )</h2>"
"<p>Available lights:</p>"
"<ul>{lights}</ul>"
"</body></html>";

String lights = "";

for (int i = 0; i < LightService.getLightsAvailable(); i++) {
if (!lightHandlers[i]) {
continue;
}

lights += "<li>" + lightHandlers[i]->getFriendlyName(i) + "</li>";
}

response.replace("{ip}", ipString);
response.replace("{lights}", lights);
response.replace("{name}", friendlyName);

HTTP->send(200, "text/html", response);
}

void descriptionFn() {
String response = _ssdp_xml_template;

Expand Down Expand Up @@ -757,6 +801,7 @@ void LightServiceClass::begin(ESP8266WebServer *svr) {
Serial.print(":");
Serial.println(80);

HTTP->on("/index.html", HTTP_GET, indexPageFn);
HTTP->on("/description.xml", HTTP_GET, descriptionFn);
on(configFn, "/api/*/config", HTTP_ANY);
on(configFn, "/api/config", HTTP_GET);
Expand Down Expand Up @@ -1060,10 +1105,8 @@ void addSingleLightJson(aJsonObject* light, int numberOfTheLight, LightHandler *
aJson.addStringToObject(light, "swversion", "0.1"); // type of lamp (all "Extended colour light" for now)
aJson.addStringToObject(light, "type", "Extended color light"); // type of lamp (all "Extended colour light" for now)
aJson.addStringToObject(light, "uniqueid", ((String) (numberOfTheLight + 1)).c_str());

}


void addLightJson(aJsonObject* root, int numberOfTheLight, LightHandler *lightHandler) {
if (!lightHandler)
return;
Expand Down Expand Up @@ -1126,7 +1169,7 @@ static String format2Digits(int num) {

void addConfigJson(aJsonObject *root)
{
aJson.addStringToObject(root, "name", "hue emulator");
aJson.addStringToObject(root, "name", friendlyName);
aJson.addStringToObject(root, "swversion", "81012917");
aJson.addStringToObject(root, "bridgeid", bridgeIDString.c_str());
aJson.addBooleanToObject(root, "portalservices", false);
Expand Down
3 changes: 1 addition & 2 deletions ESP8266HueEmulator/LightService.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ class LightHandler {

class ESP8266WebServer;
class LightServiceClass {
const char* _friendlyName;

public:
LightServiceClass();
LightServiceClass(const char* friendlyName);
LightHandler *getLightHandler(int numberOfTheLight);
bool setLightsAvailable(int numLights);
int getLightsAvailable();
Expand Down