Jump to content
THIS IS THE TEST SITE OF EUROBRICKS!
THIS IS THE TEST SITE OF EUROBRICKS!

Featured Replies

On 12/27/2020 at 2:48 PM, Smotek7 said:

Hello,
I need advice.
I want to create a train switch driver for my son.
I want to control it using the Powered UP app.
That's why I was looking for a way to emulate HUB.
I want to use an RC servo for switching.
@toastie Up in the post you write that you have an emulator in nodeMcu ESP32.
I have this board. Can you please advise me how to edit the code so that I can upload the code to NodeMcu?
I have Arduino IDE installed and ESP32 boards installed.
My first step is to test NodeMcu communication with the Powered UP app.
Thank you for any advice.
Rasto

Would Legoino work for that?  https://github.com/corneliusmunz/legoino

 

2 minutes ago, dr_spock said:

Would Legoino work for that?

That is what I don't know. I am using Legoino to program the ESP32 as client and access hubs as servers. On "my" ESP32 side, there are buttons (start/stop/etc) and a display (very tiny OLED) to let me know what is going on.

As far as I understand, @Smotek7 wants to use the PU App as client to access the ESP32 as server (which then throws switches etc)? This may be incorrect though.

Best
Thorsten

Hi,

I'm very confused now.

12 hours ago, Toastie said:

I am using Legoino to program the ESP32 as client and access hubs as servers. On "my" ESP32 side, there are buttons (start/stop/etc) and a display (very tiny OLED) to let me know what is going on.

 

But then the ESP is the server, it makes the conncetions to the Hubs (clients) and sends commands to them, i.e. running a motor.

What @Smotek7 want's to do is other way round, the PoweredUp App is the server, it connects the ESP (as a client) and sends commands.

And that is just what is described in this thread, using the ESP like a Hub to execute commands from the App.

No?

And yes, legoino does, as far as I understood..

Hi @Lok24,
i need HUB emulation.
Today I managed to test legoino as a HUB emulation.
It's what I need, it's just for City HUB as I understand it.
I managed to control 1 piece of RC servo with LEGO app.
PORT A doesn't work, I wrote on github.

When I have something new, I will write.

Rasto

1 minute ago, Smotek7 said:

Hi @Lok24,
i need HUB emulation.
It's what I need

 

That's what I expected. Yes, it'not like a Move Hub, but City and technic should only differ in available ports.

But let's wait for Cornelius to explain on github.

7 hours ago, Lok24 said:

But then the ESP is the server, it makes the conncetions to the Hubs (clients) and sends commands to them, i.e. running a motor.

Well, I understood the BLE client server model this way: the hub is a server which, well, serves:pir_laugh2: its clients by providing services: This is from the LWP doc:

"The LEGO Bluetooth 3.X Hub Profile consists of a single Bluetooth LE GATT service. The service allows users to read info about the LEGO Hub (name, battery level, etc.) and to interact with any sensors and motors connected to it."

So clients need to discover what services are available and then sign up for or use these. As my ESP32 is doing exactly that (it asks the hub what type of motor is present and signs up for RSSI and bat level updates and then issues the appropriate motor commands - to be served ...).

Since messing around with CPM and UNIX/V systems I was always a little fuzzy about who is who regarding client/server models.

Oh well, this is not the topic here and you guys already found your way I guess.

Best
Thorsten 

PS.: Just saw the video @Smotek7 made: Very nice work indeed!!! So in this case the ESP32 is the server and the App is the client, I believe ... :pir-huzzah2:

Edited by Toastie
We call it master and servant ...

Sounds good as well....perhabs you're right :wink:

Master (central)  and slave(peripheral) is obviously what I described.

As from what I read now, the Hub seems to be client and server at the same time......

Edited by Lok24

Today I created a prototype HW for
LEGO servo HUB.
4 output ports for RC servo.
Just wait for the SW modification.
 

HUB2a.jpg

HUB1a.jpg

  • 7 months later...
On 12/30/2020 at 7:42 PM, Smotek7 said:

Today I created a prototype HW for
LEGO servo HUB.
4 output ports for RC servo.
Just wait for the SW modification.
 

HUB2a.jpg

HUB1a.jpg

@Smotek7

Very nice. Can you share the code to control RC servos using ESP32?

Is it possible to create multiple "fake hubs" using just one ESP32 to control more than 4 servos?

This is probably my latest version.
It's just a modified example, I don't have a description there.
I think only 4 outings can be made.
It's a limited Lego app.
Only add more pieces of ESP2, it can be distinguished in the app.

 

#include "Lpf2HubEmulation.h"
#include "PowerFunctions.h"
#include "LegoinoCommon.h"
#include <Servo.h>

// create a hub instance
Lpf2HubEmulation myEmulatedHub("CONTROL_PLUS_HUB", HubType::CONTROL_PLUS_HUB);
//Lpf2HubEmulation myEmulatedHub("TrainHub", HubType::CONTROL_PLUS_HUB);

// create a power functions instance (IR LED on Pin 12, IR Channel 0)
//PowerFunctions pf(12, 0);
static const int servoPin = 4;

Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;

int Hod;
void writeValueCallback(byte port, byte value)
{
  Serial.print("writeValueCallback: ");
  Serial.print("   PORT: ");
  Serial.print(port);
  Serial.print("   Hod: ");
  Serial.print(value);
  Serial.println("   ----------------------------");
  if (value < 101)
    {
      Hod = value * 0.9;
      Hod = Hod + 90; 
    }
  if (value > 150)
     {
      Hod = value - 256;
      Hod = Hod * 0.9;
      Hod = Hod + 90;
     }
  if (value == 127) 
    {  
      Hod = 90;
    }
  if (port == 0x00)
  {
    //pf.single_pwm(PowerFunctionsPort::RED, pf.speedToPwm(value)); 
     Serial.print("motor A  ");
     Serial.print("Value: ");
     Serial.print(value); 
     Serial.print("   Hod: ");
     Serial.println(Hod);
     servo1.write(Hod);
  }
  if (port == 0x01)
  {
   // pf.single_pwm(PowerFunctionsPort::BLUE, pf.speedToPwm(value));
     Serial.print("motor B  ");
     Serial.print("Value: ");
     Serial.print(value); 
     Serial.print("   Hod: ");
     Serial.println(Hod);
     servo2.write(Hod);
  }
  if (port == 0x02)
  {
   // pf.single_pwm(PowerFunctionsPort::RED, pf.speedToPwm(value)); 
     Serial.print("motor C  ");
     Serial.print("Value: ");
     Serial.print(value); 
     Serial.print("   Hod: ");
     Serial.println(Hod);
     servo3.write(Hod);   
  }
  if (port == 0x03)
  {
  //  pf.single_pwm(PowerFunctionsPort::RED, pf.speedToPwm(value)); 
     Serial.print("motor D  ");
     Serial.print("Value: ");
     Serial.print(value); 
     Serial.print("   Hod: ");
     Serial.println(Hod); 
     servo4.write(Hod);  
  }
  if (port == 0x32)
  {
    Serial.print("Hub LED command received with color: ");
    Serial.println(LegoinoCommon::ColorStringFromColor(value).c_str());
  }
}
void setup()
{
  Serial.begin(115200);
  // define the callback function if a write message event on the characteristic occurs
  myEmulatedHub.setWritePortCallback(&writeValueCallback); 
  myEmulatedHub.start();
  servo1.attach(13);
  servo2.attach(14);
  servo3.attach(4);
  servo4.attach(15);
}
// main loop
void loop()
{
  // if an app is connected, attach some devices on the ports to signalize 
  // the app that values could be received/written to that ports
  if (myEmulatedHub.isConnected && !myEmulatedHub.isPortInitialized)
  {
    delay(1000);
    myEmulatedHub.isPortInitialized = true;
    delay(1000);
    myEmulatedHub.attachDevice((byte)ControlPlusHubPort::A, DeviceType::TRAIN_MOTOR);
    delay(1000);
    myEmulatedHub.attachDevice((byte)ControlPlusHubPort::B, DeviceType::TRAIN_MOTOR);
    delay(1000);
    myEmulatedHub.attachDevice((byte)ControlPlusHubPort::C, DeviceType::TRAIN_MOTOR);
    delay(1000);
    myEmulatedHub.attachDevice((byte)ControlPlusHubPort::D, DeviceType::TRAIN_MOTOR);
    delay(1000);
    myEmulatedHub.attachDevice((byte)ControlPlusHubPort::LED, DeviceType::HUB_LED);
    delay(1000);
  }

// End of loop

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

Recently Browsing 0

  • No registered users viewing this page.
Sponsored Links