xoseperez / espurna

Home automation firmware for ESP8266-based devices
http://tinkerman.cat
GNU General Public License v3.0
2.99k stars 636 forks source link

AI Light #113

Closed xoseperez closed 6 years ago

xoseperez commented 7 years ago

Originally reported by: Michael (Bitbucket: nnelgleahcim, GitHub: Unknown)


Hi Xose,

I was able to compile and upload espurna code to the AI Light using the recommended settings in the documentation of 1M (128K SPIFFS), but when I tried to "Select Tools > ESP8266 Sketch Data Upload" I received the following error:

warning: espcomm_sync failed
error: espcomm_open failed
error: espcomm_upload_mem failed
SPIFFS Upload failed!

As a work around I tried changing the Flash Size to 1M (256K SPIFFS) and everything seemed to uploaded perfectly. The documentation may need to be adjusted?

Edit: Maybe that wasn't such a good work around after all. I'm able to connect to AI LIGHT_06XXXE, but when I try visiting "http://espurna.local" I receive a page not found error.

Thank you again for all your hard work!!!

xoseperez commented 7 years ago

Removing milestone: 1.8.0 (automated comment)

xoseperez commented 7 years ago

Version 1.8.0 borrows code from Sacha's project as well as some other contributions to different light devices supported.

xoseperez commented 7 years ago

Original comment by Sacha Telgenhof (Bitbucket: stelgenhof, GitHub: stelgenhof):


@xoseperez Sure! Please be my guest to use the library or parts of it (it's open source MIT license). After all you did most of the ground work.

xoseperez commented 7 years ago

Original comment by Michael (Bitbucket: nnelgleahcim, GitHub: Unknown):


@xoseperez Thank you very much!

@stelgenhof The code looks great! Thank you very much for taking the time to share it. I look forward to trying it out very soon!

xoseperez commented 7 years ago

@nnelgleahcim Nice write-up. Thank you!

@stelgenhof, I will test your library for sure. I'd like to use it in ESPurna too if you don't mind (once you feel it's ready for production).

xoseperez commented 7 years ago

Original comment by Sacha Telgenhof (Bitbucket: stelgenhof, GitHub: stelgenhof):


Hi Michael,

Finally got it done. Have a look at my github repos: https://github.com/stelgenhof/AiLight. I created a library/class that encapsulates the MY9291 LED driver from Xose. It contains a function named 'setColorTemperature'. Have a look at the code to see how it is done.

Basically color temperature is pretty straightforward. It will set a white color based on the given temperature (e.g 2700K) and converts that to the equivalent RGB values. The conversion is based on the algorithm of Tanner Helland (See the code for the sources). Whether this is perfect/accurate I don't know: it depends partly on the quality of the LED's. However I get a nice 'warm' color if I set the slider in Home Assistant all the way to the right :)

Please have a look and let me know what you think. Happy to hear recommendations and improvements (Don't use in production yet, haha).

xoseperez commented 7 years ago

Original comment by Michael (Bitbucket: nnelgleahcim, GitHub: Unknown):


Hi Sacha, sure thing. No worries, bugs are inevitable. :-)

xoseperez commented 7 years ago

Original comment by Sacha Telgenhof (Bitbucket: stelgenhof, GitHub: stelgenhof):


Thanks Michael for the nice write up. I am refactoring some of my code, so will make it available later. Lots of bugs in it, haha

xoseperez commented 7 years ago

Original comment by Michael (Bitbucket: nnelgleahcim, GitHub: Unknown):


FYI, I included a How To: writeup of the Ai Light in the Home Assistant Forums.

xoseperez commented 7 years ago

Original comment by Michael (Bitbucket: nnelgleahcim, GitHub: Unknown):


Hi Sacha,

Very nice work! I would love to see more of how you did it!

Agreed, I'm a big fan of Xose and his work as well!

xoseperez commented 7 years ago

Original comment by Sacha Telgenhof (Bitbucket: stelgenhof, GitHub: stelgenhof):


I haven't had any issues so far running both the RGB and the White LED's. Then again, I haven't done a long run yet...

BTW, Big thanks for all your work and articles Xose! They are a valuable resource and inspiration :)

xoseperez commented 7 years ago

No worries. The dev branch of ESPurna should work well with Home Assistant now, maybe you can give it a try. Not as complete as your solution thou. One thing I have read is that is generally not adviced to drive the white LEDs and the RGB LEDs at the same time, have you had any trouble with that?

xoseperez commented 7 years ago

Original comment by Sacha Telgenhof (Bitbucket: stelgenhof, GitHub: stelgenhof):


Michael, I managed to get this AI light working with Home Assistant. However not using Espurna this time (sorry). In Home Assistant I am using the MQTT JSON Light component which uses JSON for its payload. It works very well and I managed to code all pieces like brightness, rgb colors, white value level and color temperature.

(Updated with screenshot)

Screen Shot 2017-03-30 at 15.01.35.png

xoseperez commented 7 years ago

Original comment by Michael (Bitbucket: nnelgleahcim, GitHub: Unknown):


Excellent. Thank you so much Xose!!!

xoseperez commented 7 years ago

Absolutely. It makes sense. I would even go further to allow HEX RGB (#FF00000), 3 decimal comma separated values (255,0,0) or just one value for dimmers (128).

xoseperez commented 7 years ago

Original comment by Michael (Bitbucket: nnelgleahcim, GitHub: Unknown):


Thank you very much Xose! I had a chance to use the AI Light a bit and was disappointed to find that espurna didn't work with Home Assistant very well. After reviewing the espurna debug output I learned that the Home Assistant MQTT Light component publishes MQTT RGB color commands in the following format: {"color": "238,0,32"}, while espurna seems to recognize MQTT RGB color commands in the following format: {"color": "#256bf2"}. I spent a lot of time searching the Home Assistant forums looking for a way to modify the MQTT RGB publishing format and wasn't able to find anything. Perhaps I missed something and someone far more clever than I knows of a anther way, but I simply couldn't find a way to format the Home Assistant MQTT publishing of RGB colors. With that said, I modified the espurna function color_rgb2array in light.ino to differentiate between color payloads that began with a #. I created a quick hack to read the format based on whether the color payload started with a # or not. I'm sure more intelligent format detection could be implemented, such as looking for XX, XX, XX type formatting, but this was a quick proof of concept that showed that it works. I would like to make a feature request to include this capability in espurna, but I leave it to you as to whether it makes sense. In the meantime, I've implemented the following with great sucess. I was able to communicate with espurna using the Home Assistant MQTT Light component, using the color picker, as well as the espurna web interface. Additionaly the mosquitto_pub and curl commands work too, provided that a leading # is used in front of the RGB color. Please see below for my hack:

#!arduino

void color_rgb2array(const char * rgb, unsigned int * array) {

    char * p = (char *) rgb;
    // if color begins with a # then assume HEX RGB
    if (p[0] == '#') {                            
      ++p;
      unsigned long value = strtol(p, NULL, 16);
      array[0] = (value >> 16) & 0xFF;
      array[1] = (value >> 8) & 0xFF;
      array[2] = (value) & 0xFF;
    // otherwise assume DEC RGB separated by commas (credit to Samuel Mertenat)
    } else {
      String payload;
      for (uint8_t i = 0; i < 10; i++) {
        payload.concat((char)p[i]);
      }
      // get the position of the first and second commas and convert RGB string to integers
      uint8_t firstIndex = payload.indexOf(',');
      uint8_t lastIndex = payload.lastIndexOf(',');
      uint8_t rgb_red = payload.substring(0, firstIndex).toInt();
      uint8_t rgb_green = payload.substring(firstIndex + 1, lastIndex).toInt();
      uint8_t rgb_blue = payload.substring(lastIndex + 1).toInt();
      array[0] = rgb_red;
      array[1] = rgb_green;
      array[2] = rgb_blue;  
    }
}  

I used the following within my Home Assistant configuration.yaml file and I'm really pleased with how nicely everything works now:

light:
  platform: mqtt
  name: 'AI Light'
  state_topic: '/test/switch/AI LIGHT_06068E/relay/0'
  command_topic: '/test/switch/AI LIGHT_06068E/relay/0'
  payload_on: 1
  payload_off: 0
  rgb_state_topic: '/test/switch/AI LIGHT_06068E/color'
  rgb_command_topic: '/test/switch/AI LIGHT_06068E/color'
  rgb: true
  optimistic: false 
xoseperez commented 7 years ago

Just released version 1.7.1 which fixes the bug in the registerAPI method. The space in the "AI LIGHT" hostname was a minor bug fixed in 1.7.0. You can also change the hostname from the web interface to fix the issue.

xoseperez commented 7 years ago

Original comment by Michael (Bitbucket: nnelgleahcim, GitHub: Unknown):


After downloading version 1.7.0 and making the below changes I was able to get the AI Light working as expected.

  1. In web.inf change: _server->on(url, methods, _bindAPI(_apis.size() - 1)); to read: _server->on(buffer, methods, _bindAPI(_apis.size() - 1));
  2. In arduino.h, include the following line: #define AI_LIGHT 1

From there I was able to issue to following API commands to turn ON my AI Light, change the color to GREEN, and turn it OFF:

curl "http://YOUR_IP_ADDRESS/api/relay/0?apikey=YOUR_API_KEY&value=1"
curl "http://YOUR_IP_ADDRESS/api/color/?apikey=YOUR_API_KEY&value=00FF00"
curl "http://YOUR_IP_ADDRESS/api/relay/0?apikey=YOUR_API_KEY&value=0"

I was struggling with MQTT until I realized, after seeing the debug output, that the MQTT message requires a space in the words "AI LIGHT" and not an underscore. After changing my MQTT message format to include a space I was able to turn ON my AI Light, change the color to RED and then turn it OFF with the following MQTT commands (Don't forget to change 06068E with your own CHIP ID, also the username and password are optional if you don't use MQTT security):

mosquitto_pub -u username -P password -t "/test/switch/AI LIGHT_06068E/relay/0" -m 1
mosquitto_pub -u username -P password -t "/test/switch/AI LIGHT_06068E/color" -m "#FF0000"
mosquitto_pub -u username -P password -t "/test/switch/AI LIGHT_06068E/relay/0" -m 0
xoseperez commented 7 years ago

Original comment by Markus Ulsass (Bitbucket: db9fun111, GitHub: Unknown):


@xoseperez Yes I'm monitoring the MQTT messages and they look good. UDP debugging is enabled, but there are no messages which look suspcious. The lamp is just not reacting as expected. At first glance it looks like after changing the color the relay does not work any more. But this is not always the case. In light.ino the color is saved to EEPROM for failsafe operation. You might take a look there.

xoseperez commented 7 years ago

There is a bug in HTTP requests. It's fixed in dev branch and will release it tonight if I can. Also, the bulb is not reporting the status (ON/OFF) correctly. I'm looking to fix it right now. If you are using Arduino IDE you have to uncomment the device you are targeting in the arduino.h file, in this case the AI_LIGHT like @db9fun111 says. @db9fun111: are you monitoring the MQTT messages to see what's going on? Also: you can enable UDP debugging to receive debug messages from the light bulb in your machine. It may help finding out what going on.

xoseperez commented 7 years ago

Original comment by Markus Ulsass (Bitbucket: db9fun111, GitHub: Unknown):


Thanks for updating the code. I had to change

"#define AI_LIGHT" in arduino.h to "#define AI_LIGHT 1"

else it would throw an error (in hardware.ino) while compiling with the Arduino IDE.

Alexa Integration is working, you can control the bulb via the webpage and I can also send MQTT commands. curl (HTTP REST) isn't working for me. Funny thing: If you send mqtt commands it overturns Alexa commands, e.g. setting a color with MQTT would overturn Alexa commands. Might have to do something how MQTT is handled?

@nnelgleahcim Did you test it without "-u username -P password" (because you are using those variables against your MQTT-broker) and adjusted the "AI_LIGHT_06068E" to your specific lamp?

EDIT: After some more tests, it seems MQTT is broken somehow. After some time it wouldn't accept new commands.

xoseperez commented 7 years ago

Original comment by Michael (Bitbucket: nnelgleahcim, GitHub: Unknown):


I upgraded to version 1.7.0. Thank you very much for that latest version! I'm still having trouble with command line updates. The web interface works fine, but now the following no longer seems to work:

curl "http://AI_LIGHT_IP/api/relay/0?apikey=YOUR_HTTP_API_KEY&value=1"
curl "http://AI_LIGHT_IP/api/relay/0?apikey=YOUR_HTTP_API_KEY&value=0"

I also tried the following with no success, even though it says CONNECTED on the web status page:

mosquitto_pub -d -u username -P password -t /test/switch/AI_LIGHT_06068E/relay/0 -m 1
mosquitto_pub -d -u username -P password -t /test/switch/AI_LIGHT_06068E/color -m "#FFFF00"

For the MQTT root topic should I be using: "/test/switch/{identifier}/" (disregard the quotes) when using the above mosquitto_pub command lines?

Thank you so much for any pointers you may be able to provide.

xoseperez commented 7 years ago

Indeed there was a bug in the code. I deleted a critical line while refactoring of the light.ino file. It has been restored now on version 1.7.0 just released.

xoseperez commented 7 years ago

Original comment by Markus Ulsass (Bitbucket: db9fun111, GitHub: Unknown):


It isn't working for me either with 1.69. No Web and MQTT-control and also no Alexa integration. But I get the "normal" MQTT-traffic when publishing commands and receiving status messages while being subscribed to /test/switch/#

Any idea what has changed from 1.67/ 1.68 to 1.69 which could be the cause for not responding?

xoseperez commented 7 years ago

Is your AiLight connected to the MQTT broker? If so you should see several messages from it (hostname, version, ip, ...). Also, the bulb has to be both ON and a color set (different from #000000).

mosquitto_pub -d -u username -P password -t /test/switch/AI_LIGHT_06068E/relay/0 -m 1
mosquitto_pub -d -u username -P password -t /test/switch/AI_LIGHT_06068E/color -m "#FFFF00"
xoseperez commented 7 years ago

Original comment by Michael (Bitbucket: nnelgleahcim, GitHub: Unknown):


Thank you Xose. Sadly for me mosquitto_pub does not seem to work with 1.6.9. I am connected, I issue the below MQTT command, no errors are shown, but unfortunately I see no light.

mosquitto_pub -d -u username -P password -t /test/switch/AI_LIGHT_06068E/color -m "#FFFF00"
xoseperez commented 7 years ago

You have currently two ways to change the light color: web interface of MQTT. From the web interface it's a matter of choosing the color from the color wheel. Via MQTT you have to do something like this:

mosquitto_pub -t /test/switch/AI_LIGHT_16FA80/color -m "#FFFF00"

In version 1.7.0 you will be able to change it also via a REST request.

xoseperez commented 7 years ago

Original comment by Michael (Bitbucket: nnelgleahcim, GitHub: Unknown):


Per Emmanuel Tatto's feedback it appears that 1.6.9 does not properly support AI LIGHT via the Web or MQTT. I've found that I'm able to connect to an MQTT broker with 1.6.9, but not able to turn the AI LIGHT on/off or change colors.

The Web works with 1.6.8 for AI LIGHT, but I'm not able to connect to an MQTT broker. I've been able to turn the AI LIGHT on and off using the following commands, but that's about it:

curl "http://AI_LIGHT_IP/api/relay/0?apikey=YOUR_HTTP_API_KEY&value=1"
curl "http://AI_LIGHT_IP/api/relay/0?apikey=YOUR_HTTP_API_KEY&value=0"

I may be incorrect, but I don't see api support in the code to change the light color in this version. At this point I'm done for now and am hoping for some code fixes before doing anything else with the AI LIGHT.

xoseperez commented 7 years ago

Original comment by Michael (Bitbucket: nnelgleahcim, GitHub: Unknown):


Also, the 1.6.8 web interface also seems to work well with the AI Light. The only changes I needed to make were in relay.ino: function relaySetup(), I modified as follows in order to get it to compile:

    #ifdef SONOFF_DUAL

        // Two dummy relays for the dual
        _relays.push_back((relay_t) {0, 0});
        _relays.push_back((relay_t) {0, 0});

    #endif
    #ifdef AI_LIGHT

I'll start testing MQTT shortly to see if I'm able to get that working too.

xoseperez commented 7 years ago

Original comment by Michael (Bitbucket: nnelgleahcim, GitHub: Unknown):


I was able to get the web interface of 1.6.7 to work properly and change colors on my AI Light, once I modified some errors compiling in the Arduino environment. I'll do a bit more testing and share any learnings.

xoseperez commented 7 years ago

Original comment by Michael (Bitbucket: nnelgleahcim, GitHub: Unknown):


Thank you very much for that Emmanuel! I'm going to give 1.6.7 a try now and will report back.

xoseperez commented 7 years ago

Original comment by Emmanuel Tatto (Bitbucket: etatto, GitHub: etatto):


I think 1.6.9 have an issue with AI Light : No command (Web or MQTT) is working. I downgraded to 1.6.7 (First version with AI Light support IIRC) : Everything is working

xoseperez commented 7 years ago

Original comment by Michael (Bitbucket: nnelgleahcim, GitHub: Unknown):


Hi Pal,

Sorry to hear you're having trouble too. If I find any solutions I'll certainly let you know.

Regards, Michael

xoseperez commented 7 years ago

Original comment by Pal (Bitbucket: kalavai, GitHub: Unknown):


Michael,

I'm having the same issue with my AI LIGHT, if you find any answer please let me know also.

Thanks, Pal.

xoseperez commented 7 years ago

Original comment by Michael (Bitbucket: nnelgleahcim, GitHub: Unknown):


I have the espurna firmware installed on the AI Light, but sadly am stuck on how to use it. I tried using the web interface, but no matter what I do the bulb will not respond by lighting up. I'm thinking that I need to turn the light on somehow, but haven't been able to do so. I've toggled the light on and off multiple times, but it will not turn on or change color, even though I click "update" the web interface.

Secondarily, I tried communicating with the light via MQTT, but have not made much progress there either. Thus far this is what I have tried: mosquitto_pub -d -u myuser -P mypassword -t "/test/switch/AI LIGHT_06068E" -m on

Client mosqpub/4639-raspberryp sending CONNECT
Client mosqpub/4639-raspberryp received CONNACK
Client mosqpub/4639-raspberryp sending PUBLISH (d0, q0, r0, m1, '/test/switch/AI LIGHT_06068E', ... (2 bytes))
Client mosqpub/4639-raspberryp sending DISCONNECT

Still no illumination. Have I formatted my MQTT request properly? Is there something else I need to do to get the light to turn on, beyond using the web interface?

Thank you for any help that you may be able to provide. Once I get everything working I plan to do a full writeup on the Home Assistant web site.

xoseperez commented 7 years ago

Original comment by Michael (Bitbucket: nnelgleahcim, GitHub: Unknown):


My apologies, I did not disconnect the power from the AI LIGHT after flashing the espurna code. The first time I immediately tried to "Select Tools > ESP8266 Sketch Data Upload" at which point I received the SPIFFS Upload failed! message.

By power cycling the device, after the initial flashing of the espurna code, I was able to "Select Tools > ESP8266 Sketch Data Upload" with SPIFFS set to 128K and all worked as expected!

Thank you very much for mentioning that the device may not have been in flash mode!!!

xoseperez commented 7 years ago

The AiLight has 1MByte flash memory, enough for the firmware (~400-ish kilobytes) and the filesystem. The reason for the 128K SPIFFS size is to allow enough room for OTA updates, and believe me, you will need OTA with this device. It should work with 128K (all the ESPurna configuration are set to 128K).

The first error I guess it was probably due to not being in flash mode, or maybe a loose connection. The second one (after you flashed the filesystem using 256K size) is almost granted due to the firmware being unable to locate the filesystem since it was looking for it on the last 128K. You have to flash both the firmware and filesystem using the same configuration.