Here is calibrated LN2 graph:
And code:
cpp
Copy code
#include <WiFiNINA.h>
#include <PubSubClient.h>
#include <Arduino.h>
#include <Arduino_MKRTHERM.h>
#include <math.h> // for pow function
// WiFi seadistused
const char* ssid = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"; // Sisesta oma WiFi SSID
const char* password = "xxxxxxxxxx"; // Sisesta oma WiFi parool
// MQTT serveri andmed
const char* mqtt_server = "xxxxxxxxx";
const char* mqtt_user = "xxxxxxxx";
const char* mqtt_password = "xxxxxxxxxxxx";
const char* mqtt_topic = "thermocouple1/temperature";
const char* clientId = "arduinotherm1";
// Termopaar
THERMClass myTherm; // Muutke 'THERM' näiteks 'myTherm'
// Non Linear Thermocouple Compensation Coefficients
// Used to calculate actual temp from voltage from the MAX31855
const float c0 = 0;
const float c1 = 27.0; // alandatud
const float c2 = -0.20; // hoia
const float c3 = 0.75; // alandatud
const float c4 = 0.40; // hoia
const float c5 = 0.12; // alandatud
const float c6 = 0.018; // hoia
const float c7 = 0.001; // hoia
const float d0 = 0;
const float d1 = 24.0; // alandatud
const float d2 = -0.70; // hoia
const float d3 = 0.045; // hoia
const float d4 = -0.002; // hoia
const float d5 = 0.00005; // hoia
const float d6 = -0.0000005; // hoia
const float d7 = 0; // hoia
const float MAXc = 41.276; // µV/°C
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);
void setup() {
Serial.begin(9600);
// Ühendu WiFi võrguga
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
// Oota, kuni WiFi on ühendatud
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nWiFi connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Seadista MQTT server
mqttClient.setServer(mqtt_server, 1883);
// Termopaar
if (myTherm.begin() == 0) {
Serial.println("Termopaar ei initsialiseeru!");
while (1); // Jääb lõksu
}
Serial.println("Termopaar initsialiseeritud.");
}
float TCComp(float external, float internal) {
float TCTemp;
float Vout = MAXc * (external - internal) / 1000; // calculate volts from external and internal temperatures in millivolts
// Apply non-linear temperature compensation
if (Vout < 0) {
TCTemp = internal + c0 + (c1 * Vout) + (c2 * pow(Vout, 2)) + (c3 * pow(Vout, 3)) + (c4 * pow(Vout, 4)) + (c5 * pow(Vout, 5)) + (c6 * pow(Vout, 6)) + (c7 * pow(Vout, 7)); // Below Zero
} else {
TCTemp = internal + d0 + (d1 * Vout) + (d2 * pow(Vout, 2)) + (d3 * pow(Vout, 3)) + (d4 * pow(Vout, 4)) + (d5 * pow(Vout, 5)) + (d6 * pow(Vout, 6)) + (d7 * pow(Vout, 7)); // Above Zero
}
return TCTemp;
}
void loop() {
// Kontrolli, kas MQTT on ühendatud, ja ühildu
if (!mqttClient.connected()) {
reconnect();
}
mqttClient.loop();
float internalTemp = myTherm.readReferenceTemperature(); // seadme ümbritseva temperatuuri lugemine
float externalTemp = myTherm.readTemperature(); // termopaari temperatuuri lugemine
// Rakenda kompensatsioon
float compensatedTemp = TCComp(externalTemp, internalTemp);
if (isnan(compensatedTemp)) {
Serial.println("Kompenseeritud temperatuuri lugemine ebaõnnestus!");
} else {
Serial.print("Kompenseeritud temperatuur: ");
Serial.print(compensatedTemp);
Serial.println(" °C");
// Teisenda temperatuur stringiks ja saada MQTT serverisse
char temperatureStr[6];
snprintf(temperatureStr, sizeof(temperatureStr), "%.2f", compensatedTemp);
mqttClient.publish(mqtt_topic, temperatureStr);
Serial.println("Kompenseeritud temperatuur saadetud MQTT serverisse");
}
delay(1000); // Oodake 1 sekund enne järgmise lugemise tegemist
}
void reconnect() {
// Oota, kuni oleme ühenduses
while (!mqttClient.connected()) {
Serial.print("Attempting MQTT connection...");
// Ühenda, kui oleme ühenduses
if (mqttClient.connect(clientId, mqtt_user, mqtt_password)) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(mqttClient.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}


