ESP32-EVB CAN Bus freeze

Started by MeisterQ, October 27, 2022, 10:38:53 AM

Previous topic - Next topic

MeisterQ

Hello,

im new to this Matter, but are pretty experied with ESP-WROOM-32 Modules and Arduino Coding.

Got alot of Codes working.

Recently i was building a Gateway between UART and CAN für my PV System.

So i used 2 ESPs, one who reads UART, sending it to MQTT Broker, the other one, connected to an MCP2515 CAN modul sending the data from MQTT Broker to the Battery Inverter.

So this system contains 2 diffrent controller and alot of failure potential, so i decided to buy the ESP32-EVB Board, because here i can have a stable Ethernet Connection, i get CAN onboard, and i can use free Pins for UART communication.

But all trys to get the CAN working were a misstake.

ive uploadet my default Sketch for a working Networkconnection, MQTT, etc, everything which i need to control it from my Smarthome System. Everything was working fine.

But when i addet the CAN part, the board froze after the first CAN Transmission.

Ive tried the examples from ESP32CAN and CAN Library.


So i tried this:


// Init the can in setup
  // start the CAN bus at 500 kbps
  if (!CAN.begin(500E3)) {
    Serial.println("Starting CAN failed!");
    while (1);
  }

void can()
{
  unsigned long currentMillis1 = millis();
  if ((currentMillis1 - previousMillis1 >= interval1))
  {
    previousMillis1 = currentMillis1;

    sohLow =  soh & 0xff; // Nur die unteren 8 Bit
    sohHigh = soh >> 8; // um 8 Bit nach rechts schieben

    socLow =  soc & 0xff; // Nur die unteren 8 Bit
    socHigh = soc >> 8; // um 8 Bit nach rechts schieben

    chgVltLow =  chgVlt & 0xff; // Nur die unteren 8 Bit
    chgVltHigh = chgVlt >> 8; // um 8 Bit nach rechts schieben

    chgCurLow =  chgCur & 0xff; // Nur die unteren 8 Bit
    chgCurHigh = chgCur >> 8; // um 8 Bit nach rechts schieben

    disCurLow =  disCur & 0xff; // Nur die unteren 8 Bit
    disCurHigh = disCur >> 8; // um 8 Bit nach rechts schieben

    disVltLow =  disVlt & 0xff; // Nur die unteren 8 Bit
    disVltHigh = disVlt >> 8; // um 8 Bit nach rechts schieben

    data351[0] = chgVltLow;
    data351[1] = chgVltHigh;
    data351[2] = chgCurLow;
    data351[3] = chgCurHigh;
    data351[4] = disCurLow;
    data351[5] = disCurHigh;
    data351[6] = disVltLow;
    data351[7] = disVltHigh;

    data355[0] = socLow;
    data355[1] = socHigh;
    data355[2] = sohLow;
    data355[3] = sohHigh;




    Serial.print("Sending packet ... ");

    CAN.beginPacket(0x355);
    CAN.write(socLow);
    CAN.write(socHigh);
    CAN.write(sohLow);
    CAN.write(sohHigh);
    CAN.endPacket();
    client.publish(STATE_DEBUG_TOPIC, "Frame 355 sent");

    CAN.beginPacket(0x351);
    CAN.write(chgVltLow);
    CAN.write(chgVltHigh);
    CAN.write(chgCurLow);
    CAN.write(chgCurHigh);
    CAN.write(disCurLow);
    CAN.write(disCurHigh);
    CAN.write(disVltLow);
    CAN.write(disVltHigh);
    CAN.endPacket();
    client.publish(STATE_DEBUG_TOPIC, "Frame 351 sent");

    /*
        byte snd355 = CAN0.sendMsgBuf(0x355, 0, 8, data355);
        if (snd355 == CAN_OK) {
          Serial.println("Message 355 Sent Successfully!");
        } else {
          Serial.println("Error Sending Message 355");
          failCounter++;
          Serial.println(failCounter);
        }
        byte snd351 = CAN0.sendMsgBuf(0x351, 0, 8, data351);
        if (snd351 == CAN_OK) {
          Serial.println("Message 351 Sent Successfully!");
        } else {
          Serial.println("Error Sending Message 351");
        }

        //  byte snd35 = CAN0.sendMsgBuf(0x35A, 0, 8, data35);
        //  if (snd35 == CAN_OK) {
        //     Serial.println("Message 35A Sent Successfully!");
        //   } else {
        //     Serial.println("Error Sending Message 35A");
        //    }
    */
  }
}

Ive also tryed this from ESP32CAN.h

In Setup
  // start the CAN bus at 500 kbps
  CAN_cfg.speed = CAN_SPEED_125KBPS;
  CAN_cfg.tx_pin_id = GPIO_NUM_5;
  CAN_cfg.rx_pin_id = GPIO_NUM_35;
  CAN_cfg.rx_queue = xQueueCreate(rx_queue_size, sizeof(CAN_frame_t));
  // Init CAN Module
  ESP32Can.CANInit();


void can()
{
  unsigned long currentMillis1 = millis();
  if ((currentMillis1 - previousMillis1 >= interval1))
  {
    previousMillis1 = currentMillis1;

    sohLow =  soh & 0xff; // Nur die unteren 8 Bit
    sohHigh = soh >> 8; // um 8 Bit nach rechts schieben

    socLow =  soc & 0xff; // Nur die unteren 8 Bit
    socHigh = soc >> 8; // um 8 Bit nach rechts schieben

    chgVltLow =  chgVlt & 0xff; // Nur die unteren 8 Bit
    chgVltHigh = chgVlt >> 8; // um 8 Bit nach rechts schieben

    chgCurLow =  chgCur & 0xff; // Nur die unteren 8 Bit
    chgCurHigh = chgCur >> 8; // um 8 Bit nach rechts schieben

    disCurLow =  disCur & 0xff; // Nur die unteren 8 Bit
    disCurHigh = disCur >> 8; // um 8 Bit nach rechts schieben

    disVltLow =  disVlt & 0xff; // Nur die unteren 8 Bit
    disVltHigh = disVlt >> 8; // um 8 Bit nach rechts schieben

    data351[0] = chgVltLow;
    data351[1] = chgVltHigh;
    data351[2] = chgCurLow;
    data351[3] = chgCurHigh;
    data351[4] = disCurLow;
    data351[5] = disCurHigh;
    data351[6] = disVltLow;
    data351[7] = disVltHigh;

    data355[0] = socLow;
    data355[1] = socHigh;
    data355[2] = sohLow;
    data355[3] = sohHigh;


    Serial.println("Sending packages: ");
    CAN_frame_t tx_frame;
    tx_frame.FIR.B.FF = CAN_frame_std;
    tx_frame.MsgID = 0x351;
    tx_frame.FIR.B.DLC = 8;
    tx_frame.data.u8[0] = chgVltLow;
    tx_frame.data.u8[1] = chgVltHigh;
    tx_frame.data.u8[2] = chgCurLow;
    tx_frame.data.u8[3] = chgCurHigh;
    tx_frame.data.u8[4] = disCurLow;
    tx_frame.data.u8[5] = disCurHigh;
    tx_frame.data.u8[6] = disVltLow;
    tx_frame.data.u8[7] = disVltHigh;
    ESP32Can.CANWriteFrame(&tx_frame);
    client.publish(STATE_DEBUG_TOPIC, "Frame 351 sent");

    /*
        byte snd355 = CAN0.sendMsgBuf(0x355, 0, 8, data355);
        if (snd355 == CAN_OK) {
          Serial.println("Message 355 Sent Successfully!");
        } else {
          Serial.println("Error Sending Message 355");
          failCounter++;
          Serial.println(failCounter);
        }
        byte snd351 = CAN0.sendMsgBuf(0x351, 0, 8, data351);
        if (snd351 == CAN_OK) {
          Serial.println("Message 351 Sent Successfully!");
        } else {
          Serial.println("Error Sending Message 351");
        }

        //  byte snd35 = CAN0.sendMsgBuf(0x35A, 0, 8, data35);
        //  if (snd35 == CAN_OK) {
        //     Serial.println("Message 35A Sent Successfully!");
        //   } else {
        //     Serial.println("Error Sending Message 35A");
        //    }
    */
  }
}

It had 2 transmissions, and before the 3rd transmission was sent, it froze.

On Serial Monitor:
8:53:16.851 -> ETH Started
08:53:16.851 -> ........ETH MAC: E8:9F:6D:5D:30:93, IPv4: 192.168.10.51, FULL_DUPLEX, 100Mbps
08:53:20.847 -> ETH MAC: E8:9F:6D:5D:30:93, IPv4: 192.168.10.51, FULL_DUPLEX, 100Mbps
08:53:20.847 -> MQTT connecting ...connected
08:53:20.847 -> Sending packages:
08:53:21.820 -> Sending packages:
08:53:22.842 -> Sending packages:

And at my MQTT Broker, i just received 2 messages. Means the 3rd wasnt transmitted.

The device isnt reacting to my commands for switching the relays anymore, but its still reachable in network, pinganswers are ok.

I need help please.

LubOlimex

Technical support and documentation manager at Olimex

MeisterQ

Hello LubOlimex,

Thank you for your reply.

Ive tried the example "ESP32-EVB-CAN-Relay-Toggle"

After 2times pressing the button, the controller froze again.

10:43:50.222 -> iotsharing.com CAN demo
10:43:52.040 -> Message sent through CAN
10:43:52.040 -> Message ID = 1; DLC = 8
10:43:52.040 -> Sent data: ToggleR!
10:43:52.040 ->
10:43:52.040 ->
10:43:52.646 -> Message sent through CAN
10:43:52.646 -> Message ID = 1; DLC = 8
10:43:52.646 -> Sent data: ToggleR!
10:43:52.646 ->
10:43:52.646 ->

LubOlimex

1. What are the hardware revision of the ESP32-EVB boards that you have?
2. Did you load the whole archive from Sketch -> Include Library - > Add .ZIP Library?\
3. Did you follow all advice from the comment section at the beginning of "ESP32-EVB_CAN_RelayToggle.ino"? Like did you close the CAN_T1 jumpers on both boards?
Technical support and documentation manager at Olimex

MeisterQ

#4
1. On backsite of the board is printed Rev. 1
2. Yes i did.
3. Yes, CAN_T1 is closed with abit of solder.

Same behaivor.

I type in Terminal 1 or 2, its printing out
12:28:12.151 -> Message sent through CAN!
12:28:12.151 -> Message ID = 1; DLC = 8
12:28:12.151 -> Sent data: ToggleR1!
12:28:12.151 ->
12:28:12.151 ->
12:28:12.151 -> Message sent through CAN!
12:28:12.151 -> Message ID = 1; DLC = 8
12:28:12.151 -> Sent data: Unknown!
12:28:12.151 ->
12:28:12.151 ->

Then i type in 2
12:28:40.005 -> Message sent through CAN!
12:28:40.005 -> Message ID = 1; DLC = 8
12:28:40.005 -> Sent data: ToggleR1!
12:28:40.005 ->
12:28:40.005 ->

or i press the button
12:29:09.333 -> Message sent through CAN
12:29:09.333 -> Message ID = 1; DLC = 8
12:29:09.333 -> Sent data: Button!
12:29:09.333 ->
12:29:09.333 ->

But after 2 actions its freezing.

Board is powered by an USB Cable from PC USB3.0.

I figured out, sometimes if i upload the sketch, its working unlimited times, but mostly its crashing after 2 sendings.

Any idea?


I found out this
https://github.com/sandeepmistry/arduino-CAN/issues/10

Maybe the MCP2562 on the EVB Board is not being successfull initiated?

LubOlimex

We tested in the same terms. Same board revisions "I", same code. Just one board had jumper soldered together (mainly not to spoil a brand new board from the shop). Maybe try the same. You can see the test here:

https://youtu.be/GB_IONqecH4

Maybe it is something in the terminal or USB cable or driver. I don't know.
Technical support and documentation manager at Olimex

MeisterQ

#6
Thank you very much for your testing. Looks very good.

What can i say?

I did some testing in the daytime and sometimes its freezing after 2 sendings, and if i reupload the sketch 1 or 2 times (not only reset the controller), then i manage it to work good.

But if i upload the sketch again, its freezing.

What can i do? Ive spend 51€ from a german shop for this board just for CAN purposes, and its not working.

Have you at least seen my link from github?

There is something written of TX buffer and a while loop in Can.c


--------------


I think i found out the problem.

I got 3 CAN librarys installed. Maybe there was a crossover somehow?
Afer i deleted all librarys and only put in yours, it seems to work, even after lots of reuploads.

LubOlimex

Yes, that seems like a good reason to misbehave. The CAN library we use has a slight patch as mentioned in the GitHub. If the code uses unpatched library this is likely to happen. This thing about the patch is explained here in the README.md:


https://github.com/OLIMEX/ESP32-EVB/tree/master/SOFTWARE/Arduino%20IDE%20examples/CAN_library_and_examples
Technical support and documentation manager at Olimex

MeisterQ

Thank you very much for your help  :)