Hello,
I am working on a project where I connect 7 MAX31855 temperature sensors to and Arduino mega 2560. Everything works fine when I test it without the Ethernet Shield W5100, and I can see valid temperature readings on the Serial Monitor
However, when I stack the Ethernet Shield W5100 on the Arduino mega 2560 and connect an Ethernet cable, the temperature readings from all sensors show 0°C.
Code 1: Reading Temperatures from MAX31855 Sensors
This code works fine when run on the Arduino mega 2560 without the Ethernet shield. I am able to see valid temperature readings on the Serial Monitor.
#include <SPI.h>
#include <Adafruit_MAX31855.h>
// Define CS pins for each thermocouple
#define TC1_CS 28
#define TC2_CS 26
#define TC3_CS 24
#define TC4_CS 22
#define TC5_CS 30 // New CS pin for HT. Rücklauf wasser
#define TC6_CS 32 // New CS pin for VT. Rücklauf wasser
#define TC7_CS 34
// Shared SPI Clock and MISO
#define MAX31855_CLK 52
#define MAX31855_MISO 50
// Create objects for each MAX31855 sensor
Adafruit_MAX31855 tc1(MAX31855_CLK, TC1_CS, MAX31855_MISO);
Adafruit_MAX31855 tc2(MAX31855_CLK, TC2_CS, MAX31855_MISO);
Adafruit_MAX31855 tc3(MAX31855_CLK, TC3_CS, MAX31855_MISO);
Adafruit_MAX31855 tc4(MAX31855_CLK, TC4_CS, MAX31855_MISO);
Adafruit_MAX31855 tc5(MAX31855_CLK, TC5_CS, MAX31855_MISO);
Adafruit_MAX31855 tc6(MAX31855_CLK, TC6_CS, MAX31855_MISO);
Adafruit_MAX31855 tc7(MAX31855_CLK, TC7_CS, MAX31855_MISO);
void setup() {
Serial.begin(9600); // Start serial communication
Serial.println("Initializing MAX31855 sensors...\n");
// Check if all sensors are properly connected
if (!tc1.begin()) Serial.println("Error: TC1 not detected!");
if (!tc2.begin()) Serial.println("Error: TC2 not detected!");
if (!tc3.begin()) Serial.println("Error: TC3 not detected!");
if (!tc4.begin()) Serial.println("Error: TC4 not detected!");
if (!tc5.begin()) Serial.println("Error: TC5 not detected!"); // Check for new sensor
if (!tc6.begin()) Serial.println("Error: TC6 not detected!"); // Check for new sensor
if (!tc7.begin()) Serial.println("Error: TC7 not detected!"); // Corrected error message
}
void loop() {
// Read temperature from each thermocouple
double temp1 = tc1.readCelsius();
double temp2 = tc2.readCelsius();
double temp3 = tc3.readCelsius();
double temp4 = tc4.readCelsius();
double temp5 = tc5.readCelsius(); // Read from new sensor
double temp6 = tc6.readCelsius(); // Read from new sensor
double temp7 = tc7.readCelsius(); // Read from the corrected sensor
// Print temperature readings with labels beside values
Serial.print("VT. Vorlauf (Heizgerät): ");
if (isnan(temp1)) {
Serial.println("Error");
} else {
Serial.print(temp1);
Serial.println(" °C");
}
Serial.print("VT. Rücklauf (Heizgerät): ");
if (isnan(temp2)) {
Serial.println("Error");
} else {
Serial.print(temp2);
Serial.println(" °C");
}
Serial.print("HT. Vorlauf (Heizgerät): ");
if (isnan(temp3)) {
Serial.println("Error");
} else {
Serial.print(temp3);
Serial.println(" °C");
}
Serial.print("HT. Rücklauf (Heizgerät): ");
if (isnan(temp4)) {
Serial.println("Error");
} else {
Serial.print(temp4);
Serial.println(" °C");
}
// Print readings for new thermocouples
Serial.print("HT. Rücklauf Wasser: ");
if (isnan(temp5)) {
Serial.println("Error");
} else {
Serial.print(temp5);
Serial.println(" °C");
}
Serial.print("VT. Rücklauf Wasser: ");
if (isnan(temp6)) {
Serial.println("Error");
} else {
Serial.print(temp6);
Serial.println(" °C");
}
Serial.print("VT. Vorlauf Wasser: ");
if (isnan(temp7)) { // Fixed missing closing parenthesis here
Serial.println("Error");
} else {
Serial.print(temp7);
Serial.println(" °C");
}
// Separator for readability
Serial.println("--------------------");
delay(10000); // Wait 10 seconds before the next reading
}
Code 2: Using Ethernet Shield W5100 and MQTT
This is the code where the Ethernet Shield W5100 is used along with MQTT. However, when I run this code, all the sensor readings return 0°C. I have tried connecting the Ethernet Shield with the sensors, but no valid temperature data is received.
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <Adafruit_MAX31855.h>
// Define CS pins for each thermocouple
#define TC1_CS 28
#define TC2_CS 26
#define TC3_CS 24
#define TC4_CS 22
#define TC5_CS 30 // New CS pin for HT. Rücklauf Wasser
#define TC6_CS 32 // New CS pin for VT. Rücklauf Wasser
#define TC7_CS 34 // VT. Vorlauf Wasser
// Shared SPI Clock and MISO
#define MAX31855_CLK 52
#define MAX31855_MISO 50
// Ethernet settings
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 91, 00);
const char* mqtt_server = "192.168.91.222"; // Replace with your MQTT server IP
const int mqtt_port = 1883;
const char* mqtt_topic = "temp";
// Ethernet and MQTT client objects
EthernetClient ethClient;
PubSubClient client(ethClient);
// Create sensor objects
Adafruit_MAX31855 tc1(MAX31855_CLK, TC1_CS, MAX31855_MISO);
Adafruit_MAX31855 tc2(MAX31855_CLK, TC2_CS, MAX31855_MISO);
Adafruit_MAX31855 tc3(MAX31855_CLK, TC3_CS, MAX31855_MISO);
Adafruit_MAX31855 tc4(MAX31855_CLK, TC4_CS, MAX31855_MISO);
Adafruit_MAX31855 tc5(MAX31855_CLK, TC5_CS, MAX31855_MISO);
Adafruit_MAX31855 tc6(MAX31855_CLK, TC6_CS, MAX31855_MISO);
Adafruit_MAX31855 tc7(MAX31855_CLK, TC7_CS, MAX31855_MISO);
void setup() {
Serial.begin(9600);
Serial.println("Initializing MAX31855 sensors...\n");
// Initialize Ethernet
Ethernet.begin(mac, ip);
delay(1000);
Serial.println("Ethernet initialized");
// Initialize MQTT
client.setServer(mqtt_server, mqtt_port);
while (!client.connected()) {
Serial.print("Connecting to MQTT broker...");
if (client.connect("ArduinoClient")) {
Serial.println("Connected");
} else {
Serial.print("Failed to connect, rc=");
Serial.print(client.state());
delay(2000);
}
}
// Check if all sensors are properly connected
checkSensor(tc1, TC1_CS, "VT. Vorlauf (Heizgerät)");
checkSensor(tc2, TC2_CS, "VT. Rücklauf (Heizgerät)");
checkSensor(tc3, TC3_CS, "HT. Vorlauf (Heizgerät)");
checkSensor(tc4, TC4_CS, "HT. Rücklauf (Heizgerät)");
checkSensor(tc5, TC5_CS, "HT. Rücklauf Wasser");
checkSensor(tc6, TC6_CS, "VT. Rücklauf Wasser");
checkSensor(tc7, TC7_CS, "VT. Vorlauf Wasser");
Serial.println("Initialization Complete!\n");
}
void loop() {
// Check MQTT connection
if (!client.connected()) {
while (!client.connected()) {
Serial.print("Connecting to MQTT broker...");
if (client.connect("ArduinoClient")) {
Serial.println("Connected");
} else {
Serial.print("Failed to connect, rc=");
Serial.print(client.state());
delay(2000);
}
}
}
// Read temperature from each thermocouple
double temp1 = tc1.readCelsius();
double temp2 = tc2.readCelsius();
double temp3 = tc3.readCelsius();
double temp4 = tc4.readCelsius();
double temp5 = tc5.readCelsius();
double temp6 = tc6.readCelsius();
double temp7 = tc7.readCelsius();
// Create MQTT message
String message = "";
message += "VT. Vorlauf (Heizgerät): " + checkForErrors(tc1, temp1) + " °C, ";
message += "VT. Rücklauf (Heizgerät): " + checkForErrors(tc2, temp2) + " °C, ";
message += "HT. Vorlauf (Heizgerät): " + checkForErrors(tc3, temp3) + " °C, ";
message += "HT. Rücklauf (Heizgerät): " + checkForErrors(tc4, temp4) + " °C, ";
message += "HT. Rücklauf Wasser: " + checkForErrors(tc5, temp5) + " °C, ";
message += "VT. Rücklauf Wasser: " + checkForErrors(tc6, temp6) + " °C, ";
message += "VT. Vorlauf Wasser: " + checkForErrors(tc7, temp7) + " °C";
// Publish the temperature readings to MQTT topic
client.publish(mqtt_topic, message.c_str());
Serial.println("Message sent: " + message);
// Wait before the next reading
delay(10000);
}
void checkSensor(Adafruit_MAX31855 &sensor, int csPin, String name) {
digitalWrite(csPin, LOW);
delay(10);
if (!sensor.begin()) {
Serial.println("Error: " + name + " not detected!");
could some one guide . thanks a lot .