Esp32 section control

Could someone modify this code so that the ESP32 can control an 8-channel relay board?

Thanks

Try this:

#include <EEPROM.h>

#define EEP_Ident 0x5400

// Program counter reset
void(* resetFunc) (void) = 0;

// Variables for config - 0 is false
struct Config {
uint8_t raiseTime = 2;
uint8_t lowerTime = 4;
uint8_t enableToolLift = 0;
uint8_t isRelayActiveHigh = 0; // if zero, active low (default)

uint8_t user1 = 0; // user defined values set in machine tab
uint8_t user2 = 0;
uint8_t user3 = 0;
uint8_t user4 = 0;

};

Config aogConfig;

// 24 possible pins assigned to these functions
uint8_t pin = { 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
uint8_t relayState[24] = {0}; // Adjusted to 24 to accommodate the new pin setup

const uint8_t LOOP_TIME = 200; // 5hz
uint32_t lastTime = LOOP_TIME;
uint32_t currentTime = LOOP_TIME;
uint8_t watchdogTimer = 0; // make sure we are talking to AOG
uint8_t serialResetTimer = 0; // if serial buffer is getting full, empty it
bool isRaise = false;
bool isLower = false;

// Communication with AgOpenGPS
int16_t temp, EEread = 0;
bool isPGNFound = false, isHeaderFound = false;
uint8_t pgn = 0, dataLength = 0, idx = 0;
uint8_t AOG = {0x80, 0x81, 0x7f, 0xED, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0xCC };

float gpsSpeed;
uint8_t hydLift = 0, tramline = 0;

void setup() {
Serial.begin(38400);

EEPROM.get(0, EEread); // read identifier

if (EEread != EEP_Ident) { // check on first start and write EEPROM
    EEPROM.put(0, EEP_Ident);
    EEPROM.put(6, aogConfig);
    EEPROM.put(20, pin);
} else {
    EEPROM.get(6, aogConfig);
    EEPROM.get(20, pin);
}

// Set the pins to be outputs (adjust pin numbers for ESP32)
for (uint8_t i = 0; i < 24; i++) {
    if (pin[i] != 0) {
        pinMode(pin[i], OUTPUT);
    }
}

}

void loop() {
currentTime = millis();

if (currentTime - lastTime >= LOOP_TIME) {
    lastTime = currentTime;

    // Clean out serial buffer to prevent buffer overflow
    if (serialResetTimer++ > 20) {
        while (Serial.available() > 0) Serial.read();
        serialResetTimer = 0;
    }

    // Update watchdog timer
    if (watchdogTimer++ > 250) watchdogTimer = 12;

    // Handle hydraulic lift and other functions...

    // Call SetRelays() to update relays
    SetRelays();

    // Prepare and send AOG message
    AOG[5] = pin[0];
    AOG[6] = pin[1];
    AOG[7] = (uint8_t)tramline;

    // Add checksum
    int16_t CK_A = 0;
    for (uint8_t i = 2; i < sizeof(AOG) - 1; i++) {
        CK_A = (CK_A + AOG[i]);
    }
    AOG[sizeof(AOG) - 1] = CK_A;

    Serial.write(AOG, sizeof(AOG));
    Serial.flush();
}

// Serial Receive and handling...

}

void SetRelays(void) {
// Load the current pgn relay state - Sections
for (uint8_t i = 0; i < 8; i++) {
relayState[i] = bitRead(relayLo, i);
}

for (uint8_t i = 0; i < 8; i++) {
    relayState[i + 8] = bitRead(relayHi, i);
}

// Hydraulics
relayState[16] = isLower;
relayState[17] = isRaise;

// Tram
relayState[18] = bitRead(tramline, 0); // right
relayState[19] = bitRead(tramline, 1); // left

// GeoStop
relayState[20] = (geoStop == 0) ? 0 : 1;

for (uint8_t i = 0; i < 24; i++) {
    if (pin[i] != 0) {
        digitalWrite(pin[i], relayState[i]);
    }
}

}

Unfortunately, the code isn’t working.

It was converted bij ChatGpt.

So its maybe usefull for you to see how you can get started.

Someone who knows how to do it should rewrite the code.

Who was your servant last year?:joy:

1 Like

Wiznet_Machine2.zip (6,4 КБ)
for example

Then i guess you have to wait until someone who knows how to code makes it for his own and is willing to share.

1 Like

Okay!

I compile Machine_USB_V5.ino for ESP Dev Module without any problems!

Hi Krisz, use chatGPT codeing.

Learn how to communicate with you may code and alter the code quicker than someone who is actually to code in c++

Then just test code with each change to make sure you are going in the right direction.

1 Like

Hi,
there is a complete code for section control for ESP32 or Arduino:

you could use it without section switches, just disable them in line 97 and set all not present switches to pin 255 = unused

for USB use, set data transfer via USB = 0 in line 23

later you can access a settings page via WiFi at 192.168.1.1. If it is connected to a router it has DHCP and the address will be x.x.x.71 or connect it to windows access point so it has the IP 192.168.137.71

If you change any settings in the code, they will be applied only when version number is changed, or if EEPROM_clear = true; in line 135

Greetings from Germany
Matthias

And which GPIOs need to be used to make it work?
Thanks

look at AOG_SectionControl_ESP32/AOG Section control ESP32 pinout example (Lolin D32).pdf at master · mtz8302/AOG_SectionControl_ESP32 · GitHub

You can define it in line 94:
Relay_PIN = { 15, 2, 0, 4, 16, 17, 18, 19, 21, 255, 255, 255, 255, 255, 255, 255 }; (for above example)

I’m trying to make it work, but it doesn’t. I’ve tried all kinds of code in every possible way.

You should post more details from your setup / tests (photos / screenshots) :

which ESP32 do you use ?
ESP32 library installed ?
which board selected in arduino IDE ?
can you compile/ download the section contol code to ESP32 ? (which SC code)
your wiring ?

1 Like

https://drive.google.com/drive/folders/1Xa1T6lodRSiidMeaFf9aScccqTKc7oos
Here are some pictures

Ok, should work

Connect ESP32 via USB
Select ESP32 Dev Module in Arduino IDE
Open your section control code

if you want wifi functionality use
mtz8302 section control code

or just USB
Machine_USB_v5.ino

Verify (compile) it,
and afterwards dowload it

if there are any error messages, make a screenshot


ok, im done with this part, but whats next?

That means the Esp32 is programmed correctly. Now for hardware and testing:)