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

Wemo EMU - Multi device support #9208

Closed
wants to merge 1 commit into from

Conversation

Magic73
Copy link

@Magic73 Magic73 commented Sep 1, 2020

Description:

Now the Wemo emulation can be used for multi-device hardware like Sonoff CH4-PRO.
Alexa will recognize them as "switch", so you are free to say to Alexa "Alexa, turn on all the lights" without worrying about activating the switches too (as it happens if you are using HUE emulation)

Checklist:

  • [X ] The pull request is done against the latest dev branch
  • Only relevant files were touched
  • [X ] Only one feature/fix was added per PR.
  • The code change is tested and works on Tasmota core ESP8266 V.2.7.4.1
  • The code change is tested and works on core ESP32 V.1.12.2
  • I accept the CLA.

NOTE: The code change must pass CI tests. Your PR cannot be merged unless tests pass

Now event the Wemo emulation can be used for multi-device hardware like Sonoff CH4-PRO.
Alexa will recognize them as "switch", so you are free to say to Alexa "Alexa, turn on all the lights" without worrying about activating the switches too (as it happens if you are using HUE emulation)
Copy link
Collaborator

@s-hadinger s-hadinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job. I added some comments and questions.

LogUpnpWithClient(PSTR(D_WEMO_BASIC_EVENT));

char event[500];
strlcpy(event, _webServer->arg(0).c_str(), sizeof(event));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code was there already but I realize it could be cleaner. First take the length of the buffer and allocate on stack only what's needed. Anyways the UDP packets are capped to UDP_BUFFER_SIZE (120)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to apply any kind of optimization!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking of the following:

size_t event_len = _webServer->arg(0).length() + 1;
char event[event_len];
strlcpy(event, _webServer->arg(0).c_str(), event_len);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok!!

@Jason2866
Copy link
Collaborator

Tested with six relais. Works 😀 One thing that concerns me. Using WeMo emulation
uses a lot of RAM. Without WeMo i have 24kb free. Enabling and using WeMo with 6 relais
and now there are just only 16kb free. Build is identic just added (your) WeMo stuff.

@Magic73
Copy link
Author

Magic73 commented Sep 2, 2020

Tested with six relais. Works 😀 One thing that concerns me. Using WeMo emulation
uses a lot of RAM. Without WeMo i have 24kb free. Enabling and using WeMo with 6 relais
and now there are just only 16kb free. Build is identic just added (your) WeMo stuff.

This is my first time with Arduino platform. why in this scenario, is so important to have 16Kb or 24Kb of free memory?
About memory, are you talking about memory used to store the firmware, or memory used when the software runs?

WEMO protocol doesn't support multidevice (the developers were short-sighted in thinking about the protocol. even leaving everything like this would have taken very little.).
to make Alexa detect more than one relay, each relay must be managed with its own webserver.

@Magic73
Copy link
Author

Magic73 commented Sep 2, 2020

Anyway, while I'm writing

Tested with six relais. Works 😀 One thing that concerns me. Using WeMo emulation
uses a lot of RAM. Without WeMo i have 24kb free. Enabling and using WeMo with 6 relais
and now there are just only 16kb free. Build is identic just added (your) WeMo stuff.

This is my first time with Arduino platform. why in this scenario, is so important to have 16Kb or 24Kb of free memory?
About memory, are you talking about memory used to store the firmware, or memory used when the software runs?

WEMO protocol doesn't support multidevice (the developers were short-sighted in thinking about the protocol. even leaving everything like this would have taken very little.).
to make Alexa detect more than one relay, each relay must be managed with its own webserver.

however, while writing this answer, an idea occurred to me.
today I do some tests, but if it goes as it should .. maybe I can run everything on the default webserver. Maybe it works :)

@Magic73
Copy link
Author

Magic73 commented Sep 2, 2020

I review the code... how did I not notice? I think that could work with only one webserver :) fingers crossed

@s-hadinger
Copy link
Collaborator

That would be great!

@Magic73
Copy link
Author

Magic73 commented Sep 2, 2020

No, it doesn't work. but it's an Alexa issue.
Alexa ignore the url that I sent on the setup.

On discovering packet, I sent this setup.xml:

<root xmlns="urn:Belkin:device-1-0">
<device>
<deviceType>urn:Belkin:device:controllee:1</deviceType>
<friendlyName>Irrigazione Zona 1</friendlyName>
<manufacturer>Belkin International Inc.</manufacturer>
<modelName>Socket</modelName>
<modelNumber>3.1415</modelNumber>
<UDN>uuid:Socket-1_0-201612K00C4AA9A-1</UDN>
<serialNumber>201612K00C4AA9A-1</serialNumber>
<binaryState>0</binaryState>
<serviceList>
<service>
<serviceType>urn:Belkin:service:basicevent:1</serviceType>
<serviceId>urn:Belkin:serviceId:basicevent1</serviceId>
<controlURL>/1/upnp/control/basicevent1</controlURL>
<eventSubURL>/1/upnp/event/basicevent1</eventSubURL>
<SCPDURL>/1/eventservice.xml</SCPDURL>
</service>
<service>
<serviceType>urn:Belkin:service:metainfo:1</serviceType>
<serviceId>urn:Belkin:serviceId:metainfo1</serviceId>
<controlURL>/1/upnp/control/metainfo1</controlURL>
<eventSubURL>/1/upnp/event/metainfo1</eventSubURL>
<SCPDURL>/1/metainfoservice.xml</SCPDURL>
</service>
</serviceList>
</device>
</root>

but then Alexa, instead of use the url that I put in that XML (/1/upnp/control/basicevent1), use the default one:

09:51:08 HTP: Not found (/upnp/control/basicevent1)

but obviously it doesn't work, since in order to use one webserver, I have to listen on different url (one for eache device) :(

@Jason2866
Copy link
Collaborator

@Magic73 I am speaking of free RAM. Not flash. It does matter since my build is not the standard Tasmota build. I have reduced the included sensors / functions. Using the enhanced WeMo EMU
in a version with more features (for example tasmota-sensors) or enabling TLS would end in a situation with not enough free RAM for a working Tasmota.

@arendst arendst added the on hold by dev team Result - Feature request put on hold by member of development team label Sep 29, 2020
@arendst
Copy link
Owner

arendst commented Oct 3, 2020

If using less RAM it would be a nice addition.

If someone manages to use only one webserver to get this working I would probably merge.

@arendst arendst added the awaiting feedback Action - Waiting for response or more information label Oct 3, 2020
@Magic73
Copy link
Author

Magic73 commented Oct 3, 2020

Why don't merge it?

It will use more ram only if you have a device with more than 1 switch, and it's a good alternative to Philips Hue emu.
Really, I don't understand. It use more RAM if needed.

At the moment, you don't use WeMu on a device with multi switch, since only the first one is handled.
So what's the point?
Webservers are created only if you enable WeMu from Tasmota UI, and you will have more that one web server if your device have more than one switch.

@Magic73 I am speaking of free RAM. Not flash. It does matter since my build is not the standard Tasmota build. I have reduced the included sensors / functions. Using the enhanced WeMo EMU
in a version with more features (for example tasmota-sensors) or enabling TLS would end in a situation with not enough free RAM for a working Tasmota.

Again, if the device has just one switch.. nothing changed from how WeMu works now. And if your device has more than one switch, you don't use WeMu right now.

Sorry, but you are wrong. Anyway, no one can handle it with a single webserver, due to how Alexa works.

@Magic73 Magic73 closed this Oct 3, 2020
@Magic73 Magic73 reopened this Oct 3, 2020
@Magic73
Copy link
Author

Magic73 commented Oct 3, 2020

(sorry for closing/opening, I did a mistake while pressing the reply button)

@JensJott
Copy link

JensJott commented Nov 6, 2020

I'd love to have this feature, maybe enable it with a SetOption (optionally)?

@Magic73
Copy link
Author

Magic73 commented Nov 6, 2020

You can use it. If you only have one switch, it works exactly like the previous one. the code is the same. For each additional switch, a dedicated web server is created. I'm using it for my sprinkler system, and it works flawlessly.

@JensJott
Copy link

JensJott commented Nov 6, 2020

Hello,

I'm currently using the official build, so until now I can't use it, that's why I posted. :)

I'm not really skilled enough to fully understand your PR but did you try using subdomains instead of urls so you can use a single web server?

Thanks for reply.

@Magic73
Copy link
Author

Magic73 commented Nov 6, 2020

The reason why it doesn't work with one single webserver is because Alexa seems to ignore parts of the configuration:

#9208 (comment)

@JensJott
Copy link

JensJott commented Nov 7, 2020

Did you try sending full paths instead of relative ones? But I'm sure you did if it's possible here....

The reason I'm bothering you:
I'm using fauxmo on my pi to emulate WeMo devices and had some (more or less?) similar issues. Fauxmo runs a web server and forwards the "on" and "off" commands to the devices you specify with http and/or console calls. This works well to control my BT Thermostats using Alexa.
But I noticed I had to use the programs internal "fake state" instead of real answers from the devices. If I was not using the option Alexa executed the command without problems but kept waiting for a reply (which for some reason I don't understand would only be correct if only emulating ONE device) and then complained that the device could not be reached.

Maybe you can work out another solution with this, just ignore the replys and focus on executing the on and off commands.

The subdomain question was thought like this: One web server running "0.tasmota.device" and "1.tasmota.device" for the emulation. This should make you able to send relative paths from each subdomain and Alexa can use the "default" answer and still send it to "two" devices. You'd have to send two responses when Alexa is discovering devices, one for each device, from each subdomain, but you should be able to run this with one web server.

Again, I don't know if this is possible. If Alexa i.e. uses the IP for the call this won't help.

@Magic73
Copy link
Author

Magic73 commented Nov 7, 2020

Alexa send an UDP message to start discovering devices. WeMu reply to this UDP with that XML.
Then Alexa should query each service using the relative URL. I tryed to use different URLs schema, but neither worked. :(
Anyway, if you have to control a device with multi switch, I can't see any issue by have more than one webservice.
I'm controlling a device with 4 switches.. and I may ask Alexa turn off the lights without worring about Alexa turn off the switches too :) (if you don't use my WeMu modification, you have to use the Philips Emu.. then Alexa marks the switches as lights.)

@JensJott
Copy link

JensJott commented Nov 7, 2020

I've got your point and this is exactly why I'd like this feature in an official build.
Thanks for answering my rubbish ;)

@Magic73
Copy link
Author

Magic73 commented Nov 8, 2020

If you want to give it a try, here the binary:

tasmota.bin.gz
tasmota.bin

Notice:
sometime Alexa doesn't detect all the switches within a scan, so in case restart it again :)

@JensJott
Copy link

Thanks again! What version are these?

@Magic73
Copy link
Author

Magic73 commented Nov 10, 2020

Tasmota 8.4.0.3 (with the default component, already present in tasmota.bin)

@Wee20x
Copy link

Wee20x commented Nov 25, 2020

I'd love to have this feature too.

@arendst
Could you think about merging it into Tasmota via setOption ?
So users can decide whether to activate it or not.

@Magic73
Copy link
Author

Magic73 commented Nov 25, 2020

He could without any side effect. The multi-web server would be activated only when you use more than one switch.
With one switch, there's no difference between this and the original Wemo EMU.

@arendst
Copy link
Owner

arendst commented Nov 25, 2020

Working on merging now ...

arendst added a commit that referenced this pull request Nov 25, 2020
Add support for multiple WeMo devices by Magic73 (#9208)
@arendst arendst removed awaiting feedback Action - Waiting for response or more information on hold by dev team Result - Feature request put on hold by member of development team labels Nov 25, 2020
@arendst
Copy link
Owner

arendst commented Nov 25, 2020

Merged

@arendst arendst closed this Nov 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants