Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch 2 - NMEA2000 Exhaust Gas Temperature Output on PGN130316 #692

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 153 additions & 0 deletions examples/dual_thermocouple_sensors/max6675_signalk_n2k.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// Example code to read MAX6675 thermocouple amplifiers
// and provide to signalk exhaust gas temperature and NMEA 2000

#include <Arduino.h>
#include "sensesp/signalk/signalk_output.h"
#include "sensesp/sensors/system_info.h"
#include "sensesp_app_builder.h"

// CAN bus (NMEA 2000) pins on SH-ESP32
#include <N2kMessages.h>
#include <NMEA2000_esp32.h>
#define CAN_RX_PIN GPIO_NUM_34
#define CAN_TX_PIN GPIO_NUM_32

// MAX6675
#include <max6675.h>
#define thermoDO GPIO_NUM_18
#define thermoCLK GPIO_NUM_25
#define thermo1CS GPIO_NUM_26
#define thermo2CS GPIO_NUM_27

// Serial console debugging disable
// #define SERIAL_DEBUG_DISABLED

using namespace sensesp;

tNMEA2000* nmea2000;

ReactESP app;

// MAX6675 setup
MAX6675 thermocouple0(thermoCLK, thermo1CS, thermoDO);
MAX6675 thermocouple1(thermoCLK, thermo2CS, thermoDO);

float temp0 = 0;
float temp1 = 0;

float temp0_callback() {
temp0 = thermocouple0.readCelsius() + 273.15;
return (temp0);
}

float temp1_callback() {
temp1 = thermocouple1.readCelsius() + 273.15;
return (temp1);
}

void setup() {
#ifndef SERIAL_DEBUG_DISABLED
SetupSerialDebug(115200);
#endif

SensESPAppBuilder builder;

sensesp_app = (&builder)
// Set a custom hostname for the app.
->set_hostname("egt-temp")
// Optionally, hard-code the WiFi and Signal K server
// settings. This is normally not needed.
->set_wifi("Off Hand 2.4G", "2222222222")
//->set_wifi("kitty3", "2222222222")
//->set_sk_server("192.168.8.10", 3443)
->get_app();

auto* engine_0_egt_temperature = new RepeatSensor<float>(1000, temp0_callback);
auto* engine_1_egt_temperature = new RepeatSensor<float>(1000, temp1_callback);

// define metadata for sensors
auto engine_egt_temperature_metadata =
new SKMetadata("K", // units
"Exhaust Gas Temperature", // display name
"Exhaust Gas Temperature", // description
"EGT", // short name
10. // timeout, in seconds
);

// connect the sensors to Signal K output paths
engine_0_egt_temperature->connect_to(
new SKOutput<float>("propulsion.0.exhaustTemperature",
engine_egt_temperature_metadata));

engine_1_egt_temperature->connect_to(
new SKOutput<float>("propulsion.1.exhaustTemperature",
engine_egt_temperature_metadata));

// initialize the NMEA 2000 subsystem
// instantiate the NMEA2000 object
nmea2000 = new tNMEA2000_esp32(CAN_TX_PIN, CAN_RX_PIN);

// Reserve enough buffer for sending all messages. This does not work on small
// memory devices like Uno or Mega
nmea2000->SetN2kCANSendFrameBufSize(250);
nmea2000->SetN2kCANReceiveFrameBufSize(250);

// Set Product information
nmea2000->SetProductInformation(
"7630101", // Manufacturer's Model serial code (max 32 chars)
103, // Manufacturer's product code
"SME EGT Interface", // Manufacturer's Model ID (max 33 chars)
"0.2 (2024-04-17)", // Manufacturer's Software version code (max 40
// chars)
"0.1 (2024-03-22)" // Manufacturer's Model version (max 24 chars)
);

// Set device information
nmea2000->SetDeviceInformation(
7630101.01, // Unique number. Use e.g. Serial number.
160, // Device function=Engine Gateway
50, // Device class=Propulsion
// https://manualzz.com/doc/12647142/nmea2000-class-and-function-codes
2012 // Manufacture code, free number taken from
// https://ttlappalainen.github.io/NMEA2000/md_7_glossary.html#secRefMfgCodes
);

nmea2000->SetMode(tNMEA2000::N2km_NodeOnly, 22);
// Disable all msg forwarding to USB (=Serial)
nmea2000->EnableForward(false);
nmea2000->Open();

// No need to parse the messages at every single loop iteration; 1 ms will do
app.onRepeat(1, []() { nmea2000->ParseMessages(); });

// Implement the N2K PGN sending.
engine_0_egt_temperature->connect_to(
new LambdaConsumer<float>([](float temperature) {
tN2kMsg N2kMsg;
SetN2kPGN130316(N2kMsg,
1, // SID
0, // TempInstance
N2kts_ExhaustGasTemperature, // TempSource
temperature // actual temperature
);
nmea2000->SendMsg(N2kMsg);
}));

engine_1_egt_temperature->connect_to(
new LambdaConsumer<float>([](float temperature) {
tN2kMsg N2kMsg;
SetN2kPGN130316(N2kMsg,
1, // SID
1, // TempInstance
N2kts_ExhaustGasTemperature, // TempSource
temperature // actual temperature
);
nmea2000->SendMsg(N2kMsg);
}));


sensesp_app->start();
}

// main program loop
void loop() { app.tick(); }
4 changes: 3 additions & 1 deletion examples/dual_thermocouple_sensors/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:wemos_d1_mini32]
[env:esp32_dev]
platform = espressif32
board = esp32dev
framework = arduino
lib_deps =
adafruit/MAX6675 library@^1.1.2
signalk/SensESP@^2.7.2
ttlappalainen/NMEA2000_esp32@^1.0.3
ttlappalainen/NMEA2000-library@^4.21.2
monitor_speed = 115200
board_build.partitions = min_spiffs.csv
Loading