Skip to content

Commit

Permalink
update default pins
Browse files Browse the repository at this point in the history
  • Loading branch information
SK21 committed Dec 3, 2024
1 parent c1bd269 commit d245dd0
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 123 deletions.
130 changes: 70 additions & 60 deletions Modules/ESP32 Rate/RC_ESP32/Begin.ino
Original file line number Diff line number Diff line change
Expand Up @@ -38,70 +38,76 @@ void DoSetup()
Wire.setClock(400000); //Increase I2C data rate to 400kHz

// ADS1115
Serial.print("Starting ADS1115 at address ");
Serial.println(ADS1115_Address);
while (!ADSfound)
if (MDL.ADS1115Enabled)
{
Wire.beginTransmission(ADS1115_Address);
Wire.write(0b00000000); //Point to Conversion register
Wire.endTransmission();
Wire.requestFrom(ADS1115_Address, 2);
ADSfound = Wire.available();
Serial.print(".");
delay(500);
if (ErrorCount++ > 10) break;
}
Serial.println("");
if (ADSfound)
{
Serial.println("ADS1115 found.");
Serial.println("");
}
else
{
Serial.print("ADS1115 not found.");
Serial.println("ADS1115 disabled.");
Serial.print("Starting ADS1115 at address ");
Serial.println(ADS1115_Address);
while (!ADSfound)
{
Wire.beginTransmission(ADS1115_Address);
Wire.write(0b00000000); //Point to Conversion register
Wire.endTransmission();
Wire.requestFrom(ADS1115_Address, 2);
ADSfound = Wire.available();
Serial.print(".");
delay(500);
if (ErrorCount++ > 10) break;
}
Serial.println("");
if (ADSfound)
{
Serial.println("ADS1115 found.");
Serial.println("");
}
else
{
Serial.println("ADS1115 not found.");
Serial.println("ADS1115 disabled.");
Serial.println("");
}
}

// ethernet
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 (MDL.EthernetEnabled)
{
if (Ethernet.linkStatus() == LinkON)
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("Ethernet connected.");
if (Ethernet.linkStatus() == LinkON)
{
Serial.println("Ethernet connected.");
}
else
{
Serial.println("Ethernet not connected.");
}
Serial.print("IP Address: ");
Serial.println(Ethernet.localIP());
}
else
{
Serial.println("Ethernet not connected.");
Serial.println("Ethernet hardware not found.");
}
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 @@ -319,7 +325,7 @@ void DoSetup()
Serial.println("OTA started.");

// wifi client mode
if (MDL.WifiMode == 1)
if (MDL.WifiModeUseStation)
{
// connect to network
delay(1000);
Expand Down Expand Up @@ -388,13 +394,13 @@ void LoadDefaults()
Serial.println("Loading default settings.");

// RC17
MDL.WorkPin = 2;
MDL.PressurePin = 15;
MDL.WorkPin = 13;
MDL.PressurePin = 33;

// default flow pins
Sensor[0].FlowPin = 13;
Sensor[0].IN1 = 17;
Sensor[0].IN2 = 5;
Sensor[0].FlowPin = 32;
Sensor[0].IN1 = 4;
Sensor[0].IN2 = 16;

Sensor[1].FlowPin = NC;
Sensor[1].IN1 = NC;
Expand All @@ -420,8 +426,12 @@ void LoadDefaults()
}
MDL.SensorCount = 1;
MDL.RelayControl = 4;
MDL.WifiMode = 0;
MDL.WifiModeUseStation = false;
MDL.Is3Wire = true;
MDL.ADS1115Enabled = false;
MDL.EthernetEnabled = false;
MDL.InvertFlow = false;
MDL.InvertRelay = false;
}

bool ValidData()
Expand Down Expand Up @@ -459,7 +469,7 @@ bool ValidData()
}
if (!Result) break;
}

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

debug4 = Result;
if (Result && MDL.RelayControl == 1)
{
// check GPIOs for relays
Expand Down
2 changes: 1 addition & 1 deletion Modules/ESP32 Rate/RC_ESP32/GUI.ino
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void handleCredentials()

server.arg("prop1").toCharArray(MDL.SSID, sizeof(MDL.SSID) - 1);
server.arg("prop2").toCharArray(MDL.Password, sizeof(MDL.Password) - 1);
MDL.WifiMode = 1;
MDL.WifiModeUseStation = true;

server.send(200, "text/html", GetPage0());

Expand Down
2 changes: 1 addition & 1 deletion Modules/ESP32 Rate/RC_ESP32/Motor.ino
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void AdjustFlow()

void SetPWM(byte ID, double PWM)
{
if (MDL.FlowOnDirection == 0) PWM *= -1; // flow on low
if (MDL.InvertFlow) PWM *= -1; // flow on low

if (PWM > 0)
{
Expand Down
100 changes: 53 additions & 47 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 : 30-Nov-2024"
const uint16_t InoID = 30114; // change to send defaults to eeprom, ddmmy, no leading 0
# define InoDescription "RC_ESP32 : 02-Dec-2024"
const uint16_t InoID = 2124; // 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 All @@ -41,8 +41,8 @@ struct ModuleConfig
{
uint8_t ID = 0;
uint8_t SensorCount = 1; // up to 2 sensors, if 0 rate control will be disabled
uint8_t RelayOnSignal = 0; // value that turns on relays
uint8_t FlowOnDirection = 0; // sets on value for flow valve or sets motor direction
bool InvertRelay = false; // value that turns on relays
bool InvertFlow = false; // sets on value for flow valve or sets motor direction
uint8_t IP0 = 192;
uint8_t IP1 = 168;
uint8_t IP2 = 1;
Expand All @@ -51,13 +51,15 @@ struct ModuleConfig
uint8_t RelayControl = 4; // 0 - no relays, 1 - GPIOs, 2 - PCA9555 8 relays, 3 - PCA9555 16 relays, 4 - MCP23017, 5 - PCA9685, 6 - PCF8574
char APname[ModStringLengths] = "RateModule";
char APpassword[ModStringLengths] = "111222333";
uint8_t WifiMode = 0; // 0 AP mode, 1 Station + AP
bool WifiModeUseStation = false; // false - AP mode, true - AP + Station
char SSID[ModStringLengths] = "Tractor"; // name of network ESP32 connects to
char Password[ModStringLengths] = "111222333";
uint8_t WorkPin = 2;
bool WorkPinIsMomentary = false;
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 @@ -179,7 +181,7 @@ void WiFiStationDisconnected(WiFiEvent_t event, WiFiEventInfo_t info)
if (DisconnectCount > 10)
{
// use AP mode only
MDL.WifiMode = 0;
MDL.WifiModeUseStation = false;
SaveData();
ESP.restart();
}
Expand Down Expand Up @@ -219,7 +221,7 @@ void loop()
}
SendComm();
server.handleClient();
//Blink();
Blink();
}

byte ParseModID(byte ID)
Expand Down Expand Up @@ -295,43 +297,47 @@ void CheckPressure()
}
}

//bool State = false;
//uint32_t LastBlink;
//uint32_t LastLoop;
//byte ReadReset;
//uint32_t MaxLoopTime;
//double debug1;
//double debug2;
//double debug3;
//
//void Blink()
//{
// if (millis() - LastBlink > 1000)
// {
// LastBlink = millis();
// State = !State;
// //digitalWrite(LED_BUILTIN, State);
//
// //Serial.print(" Micros: ");
// //Serial.print(MaxLoopTime);
//
// //Serial.print(", ");
// Serial.print(debug1);
//
// Serial.print(", ");
// Serial.print(debug2);
//
// Serial.print(", ");
// Serial.print(debug3);
//
// Serial.println("");
//
// if (ReadReset++ > 5)
// {
// ReadReset = 0;
// MaxLoopTime = 0;
// }
// }
// if (micros() - LastLoop > MaxLoopTime) MaxLoopTime = micros() - LastLoop;
// LastLoop = micros();
//}
bool State = false;
uint32_t LastBlink;
uint32_t LastLoop;
byte ReadReset;
uint32_t MaxLoopTime;
double debug1;
double debug2;
double debug3;
double debug4;

void Blink()
{
if (millis() - LastBlink > 1000)
{
LastBlink = millis();
State = !State;
//digitalWrite(LED_BUILTIN, State);

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

//Serial.print(", ");
Serial.print(debug1);

Serial.print(", ");
Serial.print(debug2);

Serial.print(", ");
Serial.print(debug3);

Serial.print(", ");
Serial.print(debug4);

Serial.println("");

if (ReadReset++ > 5)
{
ReadReset = 0;
MaxLoopTime = 0;
}
}
if (micros() - LastLoop > MaxLoopTime) MaxLoopTime = micros() - LastLoop;
LastLoop = micros();
}
11 changes: 9 additions & 2 deletions Modules/ESP32 Rate/RC_ESP32/Relays.ino
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void CheckRelays()
{
if (MDL.RelayPins[i + j * 8] < NC) // check if relay is enabled
{
if (bitRead(Rlys, i)) digitalWrite(MDL.RelayPins[i + j * 8], MDL.RelayOnSignal); else digitalWrite(MDL.RelayPins[i + j * 8], !MDL.RelayOnSignal);
if (bitRead(Rlys, i)) digitalWrite(MDL.RelayPins[i + j * 8], MDL.InvertRelay); else digitalWrite(MDL.RelayPins[i + j * 8], !MDL.InvertRelay);
}
}
}
Expand Down Expand Up @@ -160,6 +160,13 @@ void CheckRelays()
(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 @@ -244,7 +251,7 @@ void CheckRelays()
{
for (int i = 0; i < 8; i++)
{
if (bitRead(NewLo, i)) PCF.write(i, MDL.RelayOnSignal); else PCF.write(i, !MDL.RelayOnSignal);
if (bitRead(NewLo, i)) PCF.write(i, MDL.InvertRelay); else PCF.write(i, !MDL.InvertRelay);
}
}
break;
Expand Down
Loading

0 comments on commit d245dd0

Please sign in to comment.