ESP 32 Controlled LionChief

// for the ESP32 with LionChief
// by Joseph P Rampolla, December 2022

#include "BLEDevice.h"

// The remote service we wish to connect to.
static BLEUUID serviceUUID("e20a39f4-73f5-4bc4-a12f-17d1ad07a961");
// The characteristic of the remote service we are interested in.
static BLEUUID charUUID("08590f7e-db05-467e-8757-72f6faeb13d4");

static BLERemoteCharacteristic* pRemoteCharacteristic;

  int inPin1 = 2;  //insulated rail or IR sensor
  int inPin2 = 3;  //insulated rail or IR sensor        
  int outPin1 = 4; //relay
  void setup() {
  pinMode (inPin1, INPUT);
  pinMode (inPin2, INPUT);
  pinMode (outPin1, OUTPUT);

  digitalWrite (inPin1, LOW); 
  digitalWrite (inPin2, LOW);
  digitalWrite (outPin1, LOW); // for relay boards activated when pin is HIGH

  // start serial port at 1115200 bps:
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only

  BLEClient*  pClient = BLEDevice::createClient();

  if (pClient->connect(BLEAddress("44:A6:E5:3E:79:45"))) {
  } else Serial.println("Failed to connect");

  BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
  if (pRemoteService == nullptr) {
    Serial.println("Failed to get service");
  } else Serial.println("Got service");

  pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
  if (pRemoteCharacteristic == nullptr) {
    Serial.println("Failed to get characteristic");
  } else Serial.println("Got characteristic");

} // End of setup.

void loop() {
   // add 0x before values sniffed by Dagan Martinez, 
   // for his Python code.
  byte command[9][4] = {
    {0x00, 0x48, 0x01, 0x00 }, //   whistle on  
    {0x00, 0x48, 0x00, 0x00 }, //   whistle off
    {0x00, 0x46, 0x01, 0x00 }, // forward
    {0x00, 0x45, 0x08, 0x00 }, // speed 8 (experiment for various speed values)
    {0x00, 0x46, 0x02, 0x00 }, // reverse
    {0x00, 0x45, 0x05, 0x00 }, // speed 5
    {0x00, 0x47, 0x01, 0x00 }, //   bell on
    {0x00, 0x47, 0x00, 0x00 }, //   bell off
    {0x00, 0x45, 0x00, 0x00 } // speed 0 "stop"

  Serial.println("whistle On");
  pRemoteCharacteristic->writeValue(command[0], sizeof(command[0]));


  Serial.println("Whistle Off");
  pRemoteCharacteristic->writeValue(command[1], sizeof(command[1]));
  Serial.println("set to forward");
  pRemoteCharacteristic->writeValue(command[2], sizeof(command[2]));


  Serial.println("speed 8");  //speed dependent on track voltage and loco, I use 15VDC
  pRemoteCharacteristic->writeValue(command[3], sizeof(command[3]));

  while (digitalRead (inPin1 == LOW)) {

  Serial.println("set to reverse");
  pRemoteCharacteristic->writeValue(command[4], sizeof(command[4]));
  digitalWrite (outPin1, LOW); // for relay for turnout to be 'on'
  delay(300); //delay for momentary activation before back to "HIGH" for relay to be 'off'
  digitalWrite (outPin1, HIGH); // relay 'off'


  Serial.println("speed 5");
  pRemoteCharacteristic->writeValue(command[5], sizeof(command[5]));

  Serial.println("Bell On");
  pRemoteCharacteristic->writeValue(command[6], sizeof(command[6]));


  Serial.println("Bell Off");
  pRemoteCharacteristic->writeValue(command[7], sizeof(command[7]));

  while (digitalRead (inPin2 == LOW)) {
  Serial.println("set to forward");
  pRemoteCharacteristic->writeValue(command[2], sizeof(command[2]));

  Serial.println("speed 0, STOP");
  pRemoteCharacteristic->writeValue(command[8], sizeof(command[8]));


} // End of loop

      The site owner is not and cannot be held liable for fire, electrical shock, property damage, bodily injury, loss of life, or accident of any kind caused by electrical circuits or any ideas, projects, techniques, or information contained on this site. The visitor assumes all risk and responsibility for any loss, injury, accident or damage arising from the use of information, ideas, techniques, projects, concepts, components, products, and circuits on this site.

      Please refer to hobby reference materials for correct and safe use information regarding these and all electronic circuits.  These diagrams are intended to explain how things were accomplished in theory, but it is the responsibility of the individual to locate precise information regarding electrical circuits, materials, ratings of components, etc.  Do not attempt these hobby projects, or any electrical project, if you don't have the necessary skills and experience.

[back to main page]