diff --git a/esp32/include/ControllerConfiguration.h b/esp32/include/ControllerConfiguration.h index 61ac30e..fb4d7ea 100644 --- a/esp32/include/ControllerConfiguration.h +++ b/esp32/include/ControllerConfiguration.h @@ -82,6 +82,9 @@ #endif #ifdef ANALOG_WATER #define SENSOR_TANK_ANALOG ANALOG_WATER /**< GPIO 34 - analog water sensor (GPIO_NUM_34) */ +/* Define CAN communication via I2C Connector */ +#define CAN_TX GPIO_NUM_17 /**< GPIO 17 - water sensor SDA */ +#define CAN_RX GPIO_NUM_16 /**< GPIO 16 - water sensor SCL */ #else #ifdef HWREVISION07 diff --git a/esp32/include/can.h b/esp32/include/can.h new file mode 100644 index 0000000..39a57e3 --- /dev/null +++ b/esp32/include/can.h @@ -0,0 +1,14 @@ +/** + * @file can.h + * + */ + +#include "ControllerConfiguration.h" + +#ifndef PLANTCTRL_CAN_H +#define PLANTCTRL_CAN_H + +void can_setup(void); +void can_loop(void); + +#endif \ No newline at end of file diff --git a/esp32/platformio.ini b/esp32/platformio.ini index 6bb8a06..66f3cdd 100644 --- a/esp32/platformio.ini +++ b/esp32/platformio.ini @@ -13,27 +13,20 @@ platform = espressif32@6.3.2 board = esp32doit-devkit-v1 framework = arduino build_flags = -DPIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY - -DPLANT0_SENSORTYPE=FREQUENCY_MOD_RESISTANCE_PROBE - -DPLANT1_SENSORTYPE=FREQUENCY_MOD_RESISTANCE_PROBE - -DPLANT2_SENSORTYPE=FREQUENCY_MOD_RESISTANCE_PROBE - -DPLANT3_SENSORTYPE=FREQUENCY_MOD_RESISTANCE_PROBE - -DPLANT4_SENSORTYPE=FREQUENCY_MOD_RESISTANCE_PROBE - -DPLANT5_SENSORTYPE=FREQUENCY_MOD_RESISTANCE_PROBE - -DPLANT6_SENSORTYPE=FREQUENCY_MOD_RESISTANCE_PROBE - -DTIMED_LIGHT_PIN=CUSTOM1_PIN5 - -DANALOG_WATER=GPIO_NUM_34 - ; Optional: Compatibilitymode for hardware revision 0.7 - ;-DHWREVISION07=1 - + -DPLANT0_SENSORTYPE=FREQUENCY_MOD_RESISTANCE_PROBE + -DPLANT1_SENSORTYPE=FREQUENCY_MOD_RESISTANCE_PROBE + -DPLANT2_SENSORTYPE=FREQUENCY_MOD_RESISTANCE_PROBE + -DPLANT3_SENSORTYPE=FREQUENCY_MOD_RESISTANCE_PROBE + -DPLANT4_SENSORTYPE=FREQUENCY_MOD_RESISTANCE_PROBE + -DPLANT5_SENSORTYPE=FREQUENCY_MOD_RESISTANCE_PROBE + -DPLANT6_SENSORTYPE=FREQUENCY_MOD_RESISTANCE_PROBE + -DTIMED_LIGHT_PIN=CUSTOM1_PIN5 + -DANALOG_WATER=GPIO_NUM_34 board_build.partitions = defaultWithSmallerSpiffs.csv - -;#https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/partition-tables.html - - -; the latest development brankitchen-lightch (convention V3.0.x) -lib_deps = bblanchon/ArduinoJson@^6.20.1 - paulstoffregen/OneWire@^2.3.6 - milesburton/DallasTemperature@^3.11.0 - pololu/VL53L0X@^1.3.1 - https://github.com/homieiot/homie-esp8266.git#develop - +lib_deps = + bblanchon/ArduinoJson@^6.20.1 + paulstoffregen/OneWire@^2.3.6 + milesburton/DallasTemperature@^3.11.0 + pololu/VL53L0X@^1.3.1 + https://github.com/homieiot/homie-esp8266.git#develop + handmade0octopus/ESP32-TWAI-CAN@^1.0.1 diff --git a/esp32/src/can.cpp b/esp32/src/can.cpp new file mode 100644 index 0000000..da13497 --- /dev/null +++ b/esp32/src/can.cpp @@ -0,0 +1,83 @@ +/** + * @file can.cpp + * + * Based on ESP32-TWAI-CAN example + */ + +#include +#include "can.h" + +#ifndef CAN_TX +#error CAN Interface not declared +#else + +CanFrame rxFrame; + +void sendObdFrame(uint8_t obdId) { + CanFrame obdFrame = { 0 }; + obdFrame.identifier = 0x7DF; // Default OBD2 address; + obdFrame.extd = 0; + obdFrame.data_length_code = 8; + obdFrame.data[0] = 2; + obdFrame.data[1] = 1; + obdFrame.data[2] = obdId; + obdFrame.data[3] = 0xAA; // Best to use 0xAA (0b10101010) instead of 0 + obdFrame.data[4] = 0xAA; // CAN works better this way as it needs + obdFrame.data[5] = 0xAA; // to avoid bit-stuffing + obdFrame.data[6] = 0xAA; + obdFrame.data[7] = 0xAA; + // Accepts both pointers and references + ESP32Can.writeFrame(obdFrame); // timeout defaults to 1 ms +} + +void can_setup() { + // Setup serial for debbuging. + Serial.begin(115200); + + // Set pins + ESP32Can.setPins(CAN_TX, CAN_RX); + + // You can set custom size for the queues - those are default + ESP32Can.setRxQueueSize(5); + ESP32Can.setTxQueueSize(5); + + // .setSpeed() and .begin() functions require to use TwaiSpeed enum, + // but you can easily convert it from numerical value using .convertSpeed() + ESP32Can.setSpeed(ESP32Can.convertSpeed(500)); + + // You can also just use .begin().. + if(ESP32Can.begin()) { + Serial.println("CAN bus started!"); + } else { + Serial.println("CAN bus failed!"); + } + + // or override everything in one command; + // It is also safe to use .begin() without .end() as it calls it internally + if(ESP32Can.begin(ESP32Can.convertSpeed(500), CAN_TX, CAN_RX, 10, 10)) { + Serial.println("CAN bus started!"); + } else { + Serial.println("CAN bus failed!"); + } +} + +void can_loop() { + static uint32_t lastStamp = 0; + uint32_t currentStamp = millis(); + + if(currentStamp - lastStamp > 1000) { // sends OBD2 request every second + lastStamp = currentStamp; + sendObdFrame(5); // For coolant temperature + } + + // You can set custom timeout, default is 1000 + if(ESP32Can.readFrame(rxFrame, 1000)) { + // Comment out if too many frames + Serial.printf("Received frame: %03X \r\n", rxFrame.identifier); + if(rxFrame.identifier == 0x7E8) { // Standard OBD2 frame responce ID + Serial.printf("Collant temp: %3d°C \r\n", rxFrame.data[3] - 40); // Convert to °C + } + } +} + +#endif \ No newline at end of file diff --git a/esp32/src/main.cpp b/esp32/src/main.cpp index 19dee86..ea1170a 100644 --- a/esp32/src/main.cpp +++ b/esp32/src/main.cpp @@ -38,6 +38,7 @@ #include "MQTTUtils.h" #include "esp_ota_ops.h" #include "Arduino.h" +#include "can.h" extern "C" bool verifyRollbackLater(){ return true; @@ -964,6 +965,7 @@ void safeSetup() Serial.println("Initial Setup. Start Accesspoint..."); vmDownloadMode = true; } + can_setup(); stayAlive.advertise("alive").setName("Alive").setDatatype(NUMBER_TYPE).settable(aliveHandler); setupFinishedTimestamp = millis(); } @@ -1078,6 +1080,8 @@ void loop() { pumpActiveLoop(); } + + can_loop(); } /***