Jojo-A / MQTT-CAN-Gateway

A fully trasparent MQTT-CAN gateway
5 stars 1 forks source link

MCP_ANY was not delared in this scope #4

Closed Arof2 closed 1 year ago

Arof2 commented 1 year ago

Hi o/ I have a Wemos D1 and Im having a problem with the code compilation. It says that MCP_ANY wasnt declared in the code and that i should replace it with MCP_CAN. Im not very familliar with Arduino and havent found a solution online. Im guessing that i imported a libreary wrong or smth like that. Here is my current code(couldnt upload the Arduino file):ESP8266_MQTT_to_CAN_universal.txt The error is in line 172 in the while loop:

void Init_MCP()
{
  //cli();//stop interrupts
  Serial.println("CAN BUS Shield initialization... ");
  LED_ON;
  while (CAN_OK != CAN.begin(MCP_ANY, CAN_BAUDRATE, CAN_SHIELD_CRYSTAL))              // init can bus : baudrate = 500k
  {
    Serial.println("CAN BUS Shield init failed.");
    Serial.println(" Init CAN BUS Shield again...");

    delay(100);
    LED_OFF;
    delay(900);
    LED_ON;
  }
  LED_OFF;
  Serial.println("CAN BUS Shield init ok!");

  /*
     set mask, set filter
  */
  CAN.init_Mask(0, CAN_ID_STD, 0);  // accept ALL messages with standard identifier
  CAN.init_Mask(1, CAN_ID_EXT, 0);  // accept ALL messages with extended identifier

  CAN.setMode(MCP_NORMAL);                     // Set operation mode to normal so the MCP2515 sends acks to received data.
  //sei();//allow interrupts
}
Jojo-A commented 1 year ago

I think the problem is indeed that you are using the wrong library. In the past I used the "official" MCP CAN bus library from @sandeepmistry. But this library is not maintained very well and has major bugs and performance issues. So I switched over to this CAN library: https://github.com/timurrrr/arduino-CAN . You can download this lib and place it into your library folder where all the other libraries are placed. Instructions can also be found in the link above. Thats it :) .

Arof2 commented 1 year ago

if i remove the old CAN and replace it with the one from timurrrr then it says in line 81 'mcp2515_can' does not name a type . Also i added the #include <CAN.h>. What do i have to add that line 81 mcp2515_can CAN(CAN_CS_PIN); works again? Because previously it only worked through the old CAN and if i add it again it says conflicting declaration 'mcp2515_can CAN'.

Jojo-A commented 1 year ago

I just downloaded the sketch and compiled it successfully. I think you still have not installed the library from @timurrrr correctly. There must be a folder called "mcp_can" in your libraries directory. It should look similar to this: Bildschirmfoto vom 2023-03-30 21-27-34

I won't support the use of the library of @sandeepmistry because it is just bad maintained. Take a look at the recent changes, the long time ignored pull requests and the issues. I am planning to create a pull request for the Arduino IDE that they remove the lib of @sandeepmistry and replace it by the one of @timurrrr .

Jojo-A commented 1 year ago

Oh, I just see that you might have downloaded from the main branch, right? Please try the devel branch. Sorry, my documentation was wrong :/

Arof2 commented 1 year ago

Ok thx so the devel branch works with the mcp2515?

Jojo-A commented 1 year ago

Yes. I have just set up a CAN bus system on my desk with a NodeMCU and an MCP2515 module. I have sent MQTT messages to the gateway and also CAN messages from one terminal window to the CAN bus. Here is the result: Bildschirmfoto vom 2023-03-30 23-16-39

You can see at the bottom section the serial output from the Arduino IDE. You can see, that the MQTT message arrived and has been forwarded to the CAN bus (the terminal window with "can0 07C [5] 19 3A 29 01 05"). From the other terminal window I have sent to the CAN bus by "cansend can0 123#deadbeef01020304" which also arrives in the NodeMCU ("CAN Message arrived ID[0x123], DLC=8, Data[ 222 173 190 239 1 2 3 4 ] ") and then gets forwarded to MQTT ("Publishing on topic [mqtt2can/canbus/rx/0x123] 222,173,190,239,1,2,3,4").

Sorry again for the confusion regarding the branches. I am not that familiar with github XD .

Arof2 commented 1 year ago

np, I really appreciate the help. (: I tried the devel branch and when i compile it it says error message

and im still not sure if i understood the can libreary from timurrr correctly. Because on the github page the files dont look like the photo you send earlier. I also tried to install it with the command. timurrr branch' After i googled i found this library which looks very similar.

Jojo-A commented 1 year ago

Put the sandeepmistry library in place again. You can keep both libs there. The don't collide. Keep the "official" sandeepmistry library in place for downward compatibility reasons and also the one from timurrrr for future developments.

Jojo-A commented 1 year ago

No, wait, thats not the fault. You are already using the correct lib. The Problem is, that sandeepmistry does not support ESP8266 very well, especially regarding interrupts. You need to edit the lib files according to that here: https://github.com/sandeepmistry/arduino-CAN/issues/112

Arof2 commented 1 year ago

It compiles now Thanks for your help. <:

Jojo-A commented 1 year ago

Cool! Please let me know if I can help. I see that there needs to be done a lot more and better documentation. I have tried so many libraries now, that I confused myself...