Skip to content

Commit

Permalink
update relays
Browse files Browse the repository at this point in the history
  • Loading branch information
SK21 committed Dec 3, 2024
1 parent 96b4459 commit 354a0f0
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 72 deletions.
66 changes: 31 additions & 35 deletions Modules/ESP32 Rate/RC_ESP32/Begin.ino
Original file line number Diff line number Diff line change
Expand Up @@ -68,46 +68,43 @@ void DoSetup()
}

// ethernet
if (MDL.EthernetEnabled)
Serial.println("Starting Ethernet ...");
MDL.IP3 = MDL.ID + 50;
IPAddress LocalIP(MDL.IP0, MDL.IP1, MDL.IP2, MDL.IP3);
static uint8_t LocalMac[] = { 0x0A,0x0B,0x42,0x0C,0x0D,MDL.IP3 };

Ethernet.init(W5500_SS); // SS pin
Ethernet.begin(LocalMac, 0);
Ethernet.setLocalIP(LocalIP);
IPAddress Mask(255, 255, 255, 0);
Ethernet.setSubnetMask(Mask);
IPAddress Gateway(MDL.IP0, MDL.IP1, MDL.IP2, 1);
Ethernet.setGatewayIP(Gateway);

delay(1500);
ChipFound = (Ethernet.hardwareStatus() != EthernetNoHardware);
if (ChipFound)
{
Serial.println("Starting Ethernet ...");
MDL.IP3 = MDL.ID + 50;
IPAddress LocalIP(MDL.IP0, MDL.IP1, MDL.IP2, MDL.IP3);
static uint8_t LocalMac[] = { 0x0A,0x0B,0x42,0x0C,0x0D,MDL.IP3 };

Ethernet.init(W5500_SS); // SS pin
Ethernet.begin(LocalMac, 0);
Ethernet.setLocalIP(LocalIP);
IPAddress Mask(255, 255, 255, 0);
Ethernet.setSubnetMask(Mask);
IPAddress Gateway(MDL.IP0, MDL.IP1, MDL.IP2, 1);
Ethernet.setGatewayIP(Gateway);

delay(1500);
ChipFound = (Ethernet.hardwareStatus() != EthernetNoHardware);
if (ChipFound)
if (Ethernet.linkStatus() == LinkON)
{
if (Ethernet.linkStatus() == LinkON)
{
Serial.println("Ethernet connected.");
}
else
{
Serial.println("Ethernet not connected.");
}
Serial.print("IP Address: ");
Serial.println(Ethernet.localIP());
Serial.println("Ethernet connected.");
}
else
{
Serial.println("Ethernet hardware not found.");
Serial.println("Ethernet not connected.");
}
Serial.print("IP Address: ");
Serial.println(Ethernet.localIP());
}
else
{
Serial.println("Ethernet hardware not found.");
}

Ethernet_DestinationIP = IPAddress(MDL.IP0, MDL.IP1, MDL.IP2, 255); // update from saved data
Ethernet_DestinationIP = IPAddress(MDL.IP0, MDL.IP1, MDL.IP2, 255); // update from saved data

// UDP
UDP_Ethernet.begin(ListeningPort);
}
// UDP
UDP_Ethernet.begin(ListeningPort);

// sensors
for (int i = 0; i < MDL.SensorCount; i++)
Expand Down Expand Up @@ -429,7 +426,6 @@ void LoadDefaults()
MDL.WifiModeUseStation = false;
MDL.Is3Wire = true;
MDL.ADS1115Enabled = false;
MDL.EthernetEnabled = false;
MDL.InvertFlow = false;
MDL.InvertRelay = false;
}
Expand Down Expand Up @@ -469,7 +465,7 @@ bool ValidData()
}
if (!Result) break;
}
debug3 = Result;

if (Result)
{
for (int i = 0; i < MDL.SensorCount; i++)
Expand Down Expand Up @@ -512,7 +508,7 @@ bool ValidData()
if (!Result) break;
}
}
debug4 = Result;

if (Result && MDL.RelayControl == 1)
{
// check GPIOs for relays
Expand Down
8 changes: 4 additions & 4 deletions Modules/ESP32 Rate/RC_ESP32/RC_ESP32.ino
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
#include <EthernetUdp.h>

// rate control with ESP32 board: DOIT ESP32 DEVKIT V1
# define InoDescription "RC_ESP32 : 02-Dec-2024"
const uint16_t InoID = 2124; // change to send defaults to eeprom, ddmmy, no leading 0
# define InoDescription "RC_ESP32 : 03-Dec-2024"
const uint16_t InoID = 3124; // change to send defaults to eeprom, ddmmy, no leading 0
const uint8_t InoType = 4; // 0 - Teensy AutoSteer, 1 - Teensy Rate, 2 - Nano Rate, 3 - Nano SwitchBox, 4 - ESP Rate
const uint8_t Processor = 0; // 0 - ESP32-Wroom-32U

Expand Down Expand Up @@ -59,7 +59,6 @@ struct ModuleConfig
bool Is3Wire = true; // False - DRV8870 provides powered on/off with Output1/Output2, True - DRV8870 provides on/off with Output2 only, Output1 is off
uint8_t PressurePin = 15; // NC - no pressure pin
bool ADS1115Enabled = false;
bool EthernetEnabled = false;
};

ModuleConfig MDL;
Expand Down Expand Up @@ -317,7 +316,8 @@ void Blink()

//Serial.print(" Micros: ");
//Serial.print(MaxLoopTime);

debug1 = Sensor[0].FlowPin;
debug2 = Sensor[0].MeterCal;
//Serial.print(", ");
Serial.print(debug1);

Expand Down
1 change: 1 addition & 0 deletions Modules/ESP32 Rate/RC_ESP32/Rate.ino
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ void GetUPM()
{
if (PulseCount[i] && Sensor[i].MeterCal > 0)
{
debug3++;
LastPulse[i] = millis();

noInterrupts();
Expand Down
43 changes: 12 additions & 31 deletions Modules/ESP32 Rate/RC_ESP32/Relays.ino
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void CheckRelays()
if (Button[i + 8]) bitSet(NewHi, i);
}
}
}
}
else if ((millis() - Sensor[0].CommTime < 4000) || (millis() - Sensor[1].CommTime < 4000))
{
NewLo = RelayLo;
Expand Down Expand Up @@ -131,42 +131,23 @@ void CheckRelays()
uint8_t mcpOutA = 0; // Output for port A
uint8_t mcpOutB = 0; // Output for port B

if (MDL.Is3Wire)
{
// Only Out2 is used on the DRV8870. Each DRV8870 controls 1 valve.
// Calculate output for port A
mcpOutA = (bitRead(RelayLo, 0) ? 2 : 0) |
(bitRead(RelayLo, 1) ? 8 : 0) |
(bitRead(RelayLo, 2) ? 32 : 0) |
(bitRead(RelayLo, 3) ? 128 : 0);
// Calculate output for port A, DRV sections 9-16
mcpOutA = (bitRead(RelayLo, 4) ? 2 : 1) |
(bitRead(RelayLo, 5) ? 8 : 4) |
(bitRead(RelayLo, 6) ? 32 : 16) |
(bitRead(RelayLo, 7) ? 128 : 64);

// Calculate output for port B
mcpOutB = (bitRead(RelayLo, 4) ? 2 : 0) |
(bitRead(RelayLo, 5) ? 8 : 0) |
(bitRead(RelayLo, 6) ? 32 : 0) |
(bitRead(RelayLo, 7) ? 128 : 0);
}
else
{
// Calculate output for port A
mcpOutA = (bitRead(RelayLo, 0) ? 2 : 1) |
(bitRead(RelayLo, 1) ? 8 : 4) |
(bitRead(RelayLo, 2) ? 32 : 16) |
(bitRead(RelayLo, 3) ? 128 : 64);
// Calculate output for port B, DRV sections 1-8
mcpOutB = (bitRead(RelayLo, 0) ? 2 : 1) |
(bitRead(RelayLo, 1) ? 8 : 4) |
(bitRead(RelayLo, 2) ? 32 : 16) |
(bitRead(RelayLo, 3) ? 128 : 64);

// Calculate output for port B
mcpOutB = (bitRead(RelayLo, 4) ? 2 : 1) |
(bitRead(RelayLo, 5) ? 8 : 4) |
(bitRead(RelayLo, 6) ? 32 : 16) |
(bitRead(RelayLo, 7) ? 128 : 64);
}
if (MDL.InvertRelay)
{
mcpOutA = ~mcpOutA;
mcpOutB = ~mcpOutB;
}
debug1 = RelayLo;
debug2 = mcpOutA;

// Send both outputs in a single transmission
Wire.beginTransmission(MCP23017address);
Expand Down Expand Up @@ -222,7 +203,7 @@ void CheckRelays()
// Iterate through all 8 channels and write data using auto-increment
for (int i = 0; i < 8; i++)
{
if (NewLo & (1 << i))
if (NewLo & (1 << i))
{
// Turn on channel: ON = 0, OFF = 4096
Wire.write(0); // ON_L
Expand Down
2 changes: 0 additions & 2 deletions Modules/ESP32 Rate/RC_ESP32/UDPComm.ino
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,6 @@ void ParseData(byte Data[], uint16_t len)
// bit 3 - work pin is momentary
// bit 4 - Is3Wire valve
// bit 5 - ADS1115 enabled
// bit 6 - Ethernet enabled
//5 relay control type 0 - no relays, 1 - GPIOs, 2 - PCA9555 8 relays, 3 - PCA9555 16 relays, 4 - MCP23017
// , 5 - PCA9685, 6 - PCF8574
//6 wifi module serial port
Expand Down Expand Up @@ -410,7 +409,6 @@ void ParseData(byte Data[], uint16_t len)
MDL.WorkPinIsMomentary = ((tmp & 8) == 8);
MDL.Is3Wire = ((tmp & 16) == 16);
MDL.ADS1115Enabled = ((tmp & 32) == 32);
MDL.EthernetEnabled = ((tmp & 64) == 64);

MDL.RelayControl = Data[5];
Sensor[0].FlowPin = Data[7];
Expand Down

0 comments on commit 354a0f0

Please sign in to comment.