-=- Central RealJoia WiFiDCC -=-















































MotorShield Ponte H L298R




I M P O R T A N T E


isole o Vin do MotorShield




Volt Amper




Arduino UNO




Sketch RealJoia UNO DCC

/* 
               Central DCC BlueToWiFi
               Joaquim Herdy Menezes 
                   < Joahermen >
                      2020J16
                      
            Original DCC++ BASE STATION
               Autor Gregg E. Berman
              CopyRight (c) 2013-2016 
              
         *********************************
         *   Criador Genial do Arduino   *
         *        ===============        *
         *     <<  Massimo Banzi  >>     *
         *        ===============        *
         *********************************

      Modulos:
                Arduino Uno R3
                MotorShield L298 R3
                Regulador Fonte Para Protoboard 5v 3,3v
                WeMos 8266 Mini ou Bluetooth HC-06 ou HC-05
                Com WeMos 8266 utilizar Conversor de Nivel Logico Bidirecional I2c 5v - 3,3v
                Fonte Regulada de 16 Volts Corrente Continua
    Observacsoes:
    Em hipotese alguma conectar o modulo Bluetooth ou o Modulo WiFi nos pinos D0 e D1 do Arduino 
    enquanto ele estiver conectado via cabo USB para os testes
    isolar o pino Vin do MotorShield L298R da ligacsao do Vin do Arduino 
    Jumpar o pino D5 com o pino D13 e o pino D10 com o pino D12
*/

#include "DCCpp_Uno.h"
#include "PacketRegister.h"
#include "CurrentMonitor.h"
#include "Sensor.h"
#include "SerialCommand.h"
#include "Accessories.h"
#include "EEStore.h"
#include "Config.h"
#include "Comm.h"
#include <SoftwareSerial.h>

//                                      A T E N S A O: 
// O Bluetooth ou WiFi usando os pinos D0 e D1 do Arduino, 
// obrigatoriamente, ele nao pode estar conectado pelo Cabo via USB 

SoftwareSerial BlueToWiFi(0,1);

void showConfiguration();

#if COMM_TYPE == 1
  byte mac[] =  MAC_ADDRESS; 
  EthernetServer INTERFACE(ETHERNET_PORT); 
#endif

volatile RegisterList mainRegs(MAX_MAIN_REGISTERS); 
volatile RegisterList progRegs(2); 

CurrentMonitor mainMonitor(CURRENT_MONITOR_PIN_MAIN,"<p2>"); 
CurrentMonitor progMonitor(CURRENT_MONITOR_PIN_PROG,"<p3>"); 

void loop(){
  SerialCommand::process(); 
  if(CurrentMonitor::checkTime()){ 
    mainMonitor.check();
    progMonitor.check();
  }
  Sensor::check(); 
} 

void setup(){  
  Serial.begin(9600); 
  Serial.flush();
  BlueToWiFi.begin(9600); 
  BlueToWiFi.flush();
  #ifdef SDCARD_CS
  pinMode(SDCARD_CS,OUTPUT);
  digitalWrite(SDCARD_CS,HIGH); 
  #endif
  EEStore::init(); 
  pinMode(A3,INPUT); 
  digitalWrite(A3,HIGH);
  if(!digitalRead(A3))
    showConfiguration();
  Serial.print(" = Central DCC RealJoia BlueToWiFi =\n < Arduino "); 
  Serial.print(ARDUINO_TYPE);
  Serial.println(" >");
  Serial.print(" > ");
  Serial.print(MOTOR_SHIELD_NAME);
  Serial.print(": V-");
  Serial.print(VERSION);
  Serial.println(" < ");
  Serial.print(" | ");
  Serial.print(__DATE__);
  Serial.print(" ");
  Serial.print(__TIME__);
  Serial.println(" | ");
  #if COMM_TYPE == 1
    #ifdef IP_ADDRESS
      Ethernet.begin(mac,IP_ADDRESS); 
    #else
      Ethernet.begin(mac); 
    #endif
    INTERFACE.begin();
  #endif
  SerialCommand::init(&mainRegs, &progRegs, &mainMonitor); 
    Serial.print("<N ");
    Serial.print(COMM_TYPE);
  #if COMM_TYPE == 0
    Serial.println(" Serial>");
  #elif COMM_TYPE == 1
    Serial.print(" ");
    Serial.print(Ethernet.localIP());
    Serial.println(" >");
  #endif
  #define DCC_ZERO_BIT_TOTAL_DURATION_TIMER1 3199
  #define DCC_ZERO_BIT_PULSE_DURATION_TIMER1 1599
  #define DCC_ONE_BIT_TOTAL_DURATION_TIMER1 1855
  #define DCC_ONE_BIT_PULSE_DURATION_TIMER1 927
  pinMode(DIRECTION_MOTOR_CHANNEL_PIN_A,INPUT); 
  digitalWrite(DIRECTION_MOTOR_CHANNEL_PIN_A,LOW);
  pinMode(DCC_SIGNAL_PIN_MAIN, OUTPUT); 
  bitSet(TCCR1A,WGM10); 
  bitSet(TCCR1A,WGM11);
  bitSet(TCCR1B,WGM12);
  bitSet(TCCR1B,WGM13);
  bitSet(TCCR1A,COM1B1); 
  bitSet(TCCR1A,COM1B0);
  bitClear(TCCR1B,CS12); 
  bitClear(TCCR1B,CS11);
  bitSet(TCCR1B,CS10);
  OCR1A=DCC_ONE_BIT_TOTAL_DURATION_TIMER1;
  OCR1B=DCC_ONE_BIT_PULSE_DURATION_TIMER1;
  pinMode(SIGNAL_ENABLE_PIN_MAIN,OUTPUT); 
  mainRegs.loadPacket(1,RegisterList::idlePacket,2,0); 
  bitSet(TIMSK1,OCIE1B); 
#ifdef ARDUINO_AVR_UNO 
  #define DCC_ZERO_BIT_TOTAL_DURATION_TIMER0 49
  #define DCC_ZERO_BIT_PULSE_DURATION_TIMER0 24
  #define DCC_ONE_BIT_TOTAL_DURATION_TIMER0 28
  #define DCC_ONE_BIT_PULSE_DURATION_TIMER0 14
  pinMode(DIRECTION_MOTOR_CHANNEL_PIN_B,INPUT); 
  digitalWrite(DIRECTION_MOTOR_CHANNEL_PIN_B,LOW);
  pinMode(DCC_SIGNAL_PIN_PROG,OUTPUT); 
  bitSet(TCCR0A,WGM00); 
  bitSet(TCCR0A,WGM01);
  bitSet(TCCR0B,WGM02);
  bitSet(TCCR0A,COM0B1); 
  bitSet(TCCR0A,COM0B0);
  bitClear(TCCR0B,CS02); 
  bitSet(TCCR0B,CS01);
  bitSet(TCCR0B,CS00);
  OCR0A=DCC_ONE_BIT_TOTAL_DURATION_TIMER0;
  OCR0B=DCC_ONE_BIT_PULSE_DURATION_TIMER0;
  pinMode(SIGNAL_ENABLE_PIN_PROG,OUTPUT); 
  progRegs.loadPacket(1,RegisterList::idlePacket,2,0);   
  bitSet(TIMSK0,OCIE0B); 
#else 
  #define DCC_ZERO_BIT_TOTAL_DURATION_TIMER3 3199
  #define DCC_ZERO_BIT_PULSE_DURATION_TIMER3 1599
  #define DCC_ONE_BIT_TOTAL_DURATION_TIMER3 1855
  #define DCC_ONE_BIT_PULSE_DURATION_TIMER3 927
  pinMode(DIRECTION_MOTOR_CHANNEL_PIN_B,INPUT); 
  digitalWrite(DIRECTION_MOTOR_CHANNEL_PIN_B,LOW);
  pinMode(DCC_SIGNAL_PIN_PROG,OUTPUT); 
  bitSet(TCCR3A,WGM30); 
  bitSet(TCCR3A,WGM31);
  bitSet(TCCR3B,WGM32);
  bitSet(TCCR3B,WGM33);
  bitSet(TCCR3A,COM3B1); 
  bitSet(TCCR3A,COM3B0);
  bitClear(TCCR3B,CS32); 
  bitClear(TCCR3B,CS31);
  bitSet(TCCR3B,CS30);
  OCR3A=DCC_ONE_BIT_TOTAL_DURATION_TIMER3;
  OCR3B=DCC_ONE_BIT_PULSE_DURATION_TIMER3;
  pinMode(SIGNAL_ENABLE_PIN_PROG,OUTPUT); 
  progRegs.loadPacket(1,RegisterList::idlePacket,2,0); 
  bitSet(TIMSK3,OCIE3B); 
#endif
} 

#define DCC_SIGNAL(R,N) \
  if(R.currentBit==R.currentReg->activePacket->nBits){ \
      R.currentBit=0; \   
    if(R.nRepeat>0 && R.currentReg==R.reg){ \
      R.nRepeat--; \
    } else if(R.nextReg!=NULL){ \
      R.currentReg=R.nextReg; \
      R.nextReg=NULL; \
      R.tempPacket=R.currentReg->activePacket; \        
      R.currentReg->activePacket=R.currentReg->updatePacket;\
      R.currentReg->updatePacket=R.tempPacket;\
    } else{ \
      if(R.currentReg==R.maxLoadedReg) \
        R.currentReg=R.reg; \
        R.currentReg++; \
    } \
  } \
  if(R.currentReg->activePacket->buf[R.currentBit/8] & R.bitMask[R.currentBit%8]){ \
    OCR ## N ## A=DCC_ONE_BIT_TOTAL_DURATION_TIMER ## N; \
    OCR ## N ## B=DCC_ONE_BIT_PULSE_DURATION_TIMER ## N; \
  } else{ \
    OCR ## N ## A=DCC_ZERO_BIT_TOTAL_DURATION_TIMER ## N; \
    OCR ## N ## B=DCC_ZERO_BIT_PULSE_DURATION_TIMER ## N; \
  } \ 
    R.currentBit++;  
ISR(TIMER1_COMPB_vect){ 
  DCC_SIGNAL(mainRegs,1)
}
#ifdef ARDUINO_AVR_UNO 
ISR(TIMER0_COMPB_vect){ 
  DCC_SIGNAL(progRegs,0)
}
#else 
ISR(TIMER3_COMPB_vect){ 
  DCC_SIGNAL(progRegs,3)
}
#endif

void showConfiguration(){
  int mac_address[]=MAC_ADDRESS;

  Serial.print("\n*** DCC++ Configuracsao ***\n");
  Serial.print("\nVersao:      ");
  Serial.print(VERSION);
  Serial.print("\nCompilado:     ");
  Serial.print(__DATE__);
  Serial.print(" ");
  Serial.print(__TIME__);
  Serial.print("\nArduino:      ");
  Serial.print(ARDUINO_TYPE);
  Serial.print("\n\nMotorShield: ");
  Serial.print(MOTOR_SHIELD_NAME);
  Serial.print("\n\nDCC Sig Main: ");
  Serial.print(DCC_SIGNAL_PIN_MAIN);
  Serial.print("\n   Direction: ");
  Serial.print(DIRECTION_MOTOR_CHANNEL_PIN_A);
  Serial.print("\n   Enable: ");
  Serial.print(SIGNAL_ENABLE_PIN_MAIN);
  Serial.print("\n   Monitor: ");
  Serial.print(CURRENT_MONITOR_PIN_MAIN);
  Serial.print("\n\nDCC Sig Prog: ");
  Serial.print(DCC_SIGNAL_PIN_PROG);
  Serial.print("\n   Direction: ");
  Serial.print(DIRECTION_MOTOR_CHANNEL_PIN_B);
  Serial.print("\n   Enable: ");
  Serial.print(SIGNAL_ENABLE_PIN_PROG);
  Serial.print("\n   Monitor: ");
  Serial.print(CURRENT_MONITOR_PIN_PROG);
  Serial.print("\n\nNum Turnouts: ");
  Serial.print(EEStore::eeStore->data.nTurnouts);
  Serial.print("\n   Sensores: ");
  Serial.print(EEStore::eeStore->data.nSensors);
  Serial.print("\n   Outputs: ");
  Serial.print(EEStore::eeStore->data.nOutputs);
  Serial.print("\n\nInterface: ");
  #if COMM_TYPE == 0
    Serial.print("Serial");
  #elif COMM_TYPE == 1
    Serial.print(COMM_SHIELD_NAME);
    Serial.print("\nMAC Address:  ");
    for(int i=0;i<5;i++){
    Serial.print(mac_address[i],HEX);
    Serial.print(":");
    }
    Serial.print(mac_address[5],HEX);
    Serial.print("\nPorta:         ");
    Serial.print(ETHERNET_PORT);
    Serial.print("\nIP Address:   ");
  #ifdef IP_ADDRESS
    Ethernet.begin(mac,IP_ADDRESS); 
  #else
    Ethernet.begin(mac); 
  #endif     
    Serial.print(Ethernet.localIP());
  #ifdef IP_ADDRESS
    Serial.print(" (Static)");
  #else
    Serial.print(" (DHCP)");
  #endif
#endif
    Serial.print("\n\nPrograma Parado - Resete o Arduino");
while(true);
}

// Joahermen - 22 de Outubro de 2020 - Joaquim Herdy Menezes
//

WeMos 8266 Mini




Sketch RealJoia WeMos DCC

/*
                 WiFiDCC RealJoia HTTP 
                    Autor Joahermen
                 Joaquim Herdy Menezes
                  Modulo WeMos ESP8266
          Placa: "LOLIN(WEMOS) D1 R2 & mini"460
                         20J17
*/

#include <WiFiClient.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <SoftwareSerial.h>

SoftwareSerial SerialWiFi(3,1); 

#define HostiP "192.168.4.1" 

#ifndef APSSID
#define APSSID "wifijoia"
#define APPSK  "WiFiJoia"
#endif

const char *ssid = APSSID;
const char *password = APPSK;

IPAddress staticIP(192,168,4,134); 
IPAddress dns(192,168,4,1); 
IPAddress gateway(192,168,4,1); 
IPAddress subnet(255,255,255,0); 

ESP8266WebServer Server(51623); 
WiFiClient Client; 

String Ordem;
String Ativa = "";
int Conta = 0;
String NossoIP = "192.168.4.1:51623";

const String postForms = "<html> <body> <RealJoia WiFiDCC> </body> </html>";

void setup(void) {
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, 0); 
  Serial.begin(9600);
  SerialWiFi.begin(9600);
  WiFi.disconnect(); 
  WiFi.mode(WIFI_STA); 
  WiFi.config(staticIP, dns, gateway, subnet);
  WiFi.softAP(ssid, password);
  Server.on("/", handleClient);
  Server.on("/ordem/", handleOrdem);
  Server.begin();
  delay(3000); 
  digitalWrite(LED_BUILTIN, 1); 
  delay(100);
  }

void handleOrdem() {
  Server.send(204, "");
  if (Server.args() > 0 ) {
  for ( uint8_t Conta = 0; Conta < Server.args(); Conta++ ) {
  Ordem = Server.argName(Conta); 
  if (Server.argName(Conta) == "Ordem") {
  Ativa = Server.arg(Conta);
  SerialWiFi.println(Server.arg(Conta)); 
  SerialWiFi.flush();
  delay(10);
   }
  }
 }
}

void handleClient() {
if (Server.args() > 0 ){
  for ( uint8_t Conta = 0; Conta < Server.args(); Conta++ ) {
  Ordem = Server.argName(Conta); 
if (Server.argName(Conta) == "plain") {
  Ativa = Server.arg(Conta);
  SerialWiFi.println(Server.arg(Conta)); 
  SerialWiFi.flush();
  delay(10);
  return;
   }
  }
} else {
  Server.send(200, "text/html", postForms); 
 }
}

void loop() {Server.handleClient(); delay(10);}

// Joahermen - 22 de Outubro de 2020 - Joaquim Herdy Menezes
//

Conexoes Central DCC WiFi RealJoia




Conexoes Central DCC com Volt Amper




Codigo da Central DCC UNO RealJoia

Codigo do WeMos Mini 8266 RealJoia