corneliusmunz / legoino

Arduino Library for controlling Powered UP and Boost controllers
MIT License
257 stars 34 forks source link

HubEmulation and PoweredUpRemote at a time #62

Open paki111 opened 2 years ago

paki111 commented 2 years ago

I combined HubEmulation and PoweredUpRemote functionality in one sketch. Now I can controll my esp32 with PoweredUp controller and at the same time in PoweredUp app I can connect emulated hub but controlls from the app doesn't work.

Every functionality separetely works fine, but in tandem only PoweredUpRemote works.

corneliusmunz commented 2 years ago

@paki111 First thanks for raising your first issue! I have never tried to connect a PoweredUpRemote to the ESP and in parallel use the HubEmulation feature and connect to the same ESP with the App. I think this will not work because here the ESP will be the Client (PoweredUpRemote) and the Server (HubEmulation) at the same time. But I am not sure. Can you send me the example of your code that I can have a look on it. Maybe I didn't get your idea right. What do you want to do with that setup?

APP <--> ESP (HubEmulation) PoweredUpRemote <--> ESP

corneliusmunz commented 2 years ago

I have searched the NimBLE-Arduino library about issues where the ESP works as Client and Server at the same time and I found the following issue: https://github.com/h2zero/NimBLE-Arduino/issues/298 Fortunately they got it up and running but it is a matter of when do you initialize what. I have to do a test setup to try it out. The solution of that issue could be found here: https://github.com/h2zero/NimBLE-Arduino/issues/298#issuecomment-934834448

corneliusmunz commented 2 years ago

@paki111 I have got it up and running. I have connected the APP first to the Emulated Hub on the ESP and then connected the PoweredUp remote to the ESP. After that procedure I was able to change the color of the Remote via the APP and the ESP as a proxy in between.

I have added a feature branch (https://github.com/corneliusmunz/legoino/tree/feature/client_server_at_the_same_time) where I have modified the HubEmulation.ino example. Due to the same Device definition I have had to rename the Device struct to get it up and running. Additionally I have stripped out some initialization commands in the Lpf2HubEmulation.cpp file but this is maybe not needed. The important thing is that you have to follow the initialization procedure

  1. Intit and connect the App to the "Server"
  2. Init and connect the Remote to the "Client"

Hope it will work on your side 😄

paki111 commented 2 years ago

@corneliusmunz Thank you very much for quick response and for resolution. My sketch (HubEmulation.ino) was quite similar, but I did not notice that connection should be client-server for the App and server-client for the Remote.

My idea was to have one sketch for two scenarios: lego RC car controlled sometimes by the App and sometimes by the Remote.

I will check it tonight on my setup.

paki111 commented 2 years ago

It works absolutely flawless! My idea was to have 2in1 functionality and now we have even 3in1 - we can control the same ESP by the Remote or by the App or by the Remote and App at the same time!

This opens up new possibilities... and this example definitely is worth a commit 🍺 Thank You very much.

paki111 commented 2 years ago

To use Remote without App connected I slightly modified line in HubEmulation void loop(): if (myEmulatedHub.isConnected && !myRemote.isConnecting() && !myRemote.isConnected() && !isInitializing) in to: if (!myRemote.isConnecting() && !myRemote.isConnected() && !isInitializing) and you even don't need to bother connection order

corneliusmunz commented 2 years ago

@paki111 Thanks for testing it on your side and giving feedback. I will check your changes and maybe add this as another example sketch to the library

paki111 commented 2 years ago

@corneliusmunz OK, I added example for ESP32 D1 mini with Wemos motor shield and tested control via the App and/or Remote.

yokohtan commented 2 years ago

Nice to meet you. I always enjoy using this wonderful library. This library doubles the fun of Lego. By the way, I'm trying to build an assist function for an excavator, and I'm trying to intervene with an ESP32 between the smartphone Lego app and the Technic hub x2. Is it possible to make two emulator hubs with one ESP32 and connect to two control hubs at the same time? Could you give me some advice.