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

Max31856 Loom not working, raw example code does #32

Open
udellc opened this issue Sep 12, 2022 · 11 comments
Open

Max31856 Loom not working, raw example code does #32

udellc opened this issue Sep 12, 2022 · 11 comments
Assignees

Comments

@udellc
Copy link
Member

udellc commented Sep 12, 2022

Describe the bug
Max 31856 raw example (oneShot.ino) gives good data on serial monitor.
Loom version prints errors and bad reading.

This is the example code that works:

// Basic example using one-shot measurement.
// The call to readThermocoupleTemperature() is blocking for O(100ms)

#include <Adafruit_MAX31856.h>

// Use software SPI: CS, DI, DO, CLK
//Adafruit_MAX31856 maxthermo = Adafruit_MAX31856(10, 11, 12, 13);
// use hardware SPI, just pass in the CS pin
Adafruit_MAX31856 maxthermo = Adafruit_MAX31856(10);

void setup() {
  Serial.begin(115200);
  while (!Serial) delay(10);
  Serial.println("MAX31856 thermocouple test");

  maxthermo.begin();

  maxthermo.setThermocoupleType(MAX31856_TCTYPE_K);

  Serial.print("Thermocouple type: ");
  switch (maxthermo.getThermocoupleType() ) {
    case MAX31856_TCTYPE_B: Serial.println("B Type"); break;
    case MAX31856_TCTYPE_E: Serial.println("E Type"); break;
    case MAX31856_TCTYPE_J: Serial.println("J Type"); break;
    case MAX31856_TCTYPE_K: Serial.println("K Type"); break;
    case MAX31856_TCTYPE_N: Serial.println("N Type"); break;
    case MAX31856_TCTYPE_R: Serial.println("R Type"); break;
    case MAX31856_TCTYPE_S: Serial.println("S Type"); break;
    case MAX31856_TCTYPE_T: Serial.println("T Type"); break;
    case MAX31856_VMODE_G8: Serial.println("Voltage x8 Gain mode"); break;
    case MAX31856_VMODE_G32: Serial.println("Voltage x8 Gain mode"); break;
    default: Serial.println("Unknown"); break;
  }

}

void loop() {
  Serial.print("Cold Junction Temp: ");
  Serial.println(maxthermo.readCJTemperature());

  Serial.print("Thermocouple Temp: ");
  Serial.println(maxthermo.readThermocoupleTemperature());
  // Check and print any faults
  uint8_t fault = maxthermo.readFault();
  if (fault) {
    if (fault & MAX31856_FAULT_CJRANGE) Serial.println("Cold Junction Range Fault");
    if (fault & MAX31856_FAULT_TCRANGE) Serial.println("Thermocouple Range Fault");
    if (fault & MAX31856_FAULT_CJHIGH)  Serial.println("Cold Junction High Fault");
    if (fault & MAX31856_FAULT_CJLOW)   Serial.println("Cold Junction Low Fault");
    if (fault & MAX31856_FAULT_TCHIGH)  Serial.println("Thermocouple High Fault");
    if (fault & MAX31856_FAULT_TCLOW)   Serial.println("Thermocouple Low Fault");
    if (fault & MAX31856_FAULT_OVUV)    Serial.println("Over/Under Voltage Fault");
    if (fault & MAX31856_FAULT_OPEN)    Serial.println("Thermocouple Open Fault");
  }
  delay(1000);
}

This example in loom does not work:

/**
 * Temperature Readings using teh MAX31865 sensor
 * Pass in a variable number of arguments to the construct to designate the number of samples and which pin you're using
 * 
 * MANAGER MUST BE INCLUDED FIRST IN ALL CODE
 */


#include <Loom_Manager.h>

#include <Sensors/SPI/Loom_MAX318XX/Loom_MAX31856.h>


Manager manager("Device", 1);

// Reads the temperature
Loom_MAX31856 maxDude(manager);

void setup() {

  // Start the serial interface
  manager.beginSerial();

  // Initialize the manager
  manager.initialize();

  // Measure the data from the sensors
  manager.measure();

  // Package the data into JSON
  manager.package();

  // Print the JSON document to the Serial monitor
  manager.display_data();

}

void loop() {
  // put your main code here, to run repeatedly:

}

This prints the following:
[Manager] Initializing Modules...
[Manager] ** Setup Complete **
[MAX31856] Cold Junction Range Fault
[MAX31856] Thermocouple Range Fault
[MAX31856] Cold Junction High Fault
[MAX31856] Cold Junction Low Fault
[MAX31856] Thermocouple High Fault
[MAX31856] Thermocouple Low Fault
[MAX31856] Over/Under Voltage Fault
[MAX31856] Thermocouple Open Fault

[Manager] Data Json:
{
"type": "data",
"id": {
"name": "Device",
"instance": 1
},
"contents": [
{
"module": "Packet",
"data": {
"Number": 1
}
},
{
"module": "MAX31856",
"data": {
"Temperature": null
}
}
]
}

@udellc
Copy link
Member Author

udellc commented Sep 13, 2022

Document fix please!

@WL-Richards
Copy link
Member

changed name of variable from max to maxthermo?

@udellc
Copy link
Member Author

udellc commented Sep 14, 2022

That might do it as max looks to be reserved in the IDE

@udellc udellc reopened this Dec 3, 2024
@udellc
Copy link
Member Author

udellc commented Dec 3, 2024

Elliot found a way to use hardware spi with this chip. This uses less GPIO. can we implement it? But let's first check to ensure there are no strange things with the SD card. SPI functionality. There does seem to be an issue when reading the time period for sampling from the SD card using hardware spy, but it doesn't seem to be an issue. Any use software spy for the max chip.

@sarveshta sarveshta moved this to Backlog in Loom Kanban Jan 7, 2025
@sarveshta sarveshta moved this from Backlog to In progress in Loom Kanban Jan 14, 2025
@sarveshta sarveshta moved this from In progress to In review in Loom Kanban Jan 21, 2025
@sarveshta
Copy link
Collaborator

Elliot will test

@WL-Richards
Copy link
Member

This is on branch max31856-hardware-spi

@sarveshta
Copy link
Collaborator

sarveshta commented Jan 24, 2025

I'm assigning this issue to @ZimHockey13. Review it when you get a chance and contact Will/me if you have any questions. Thanks!

@ZimHockey13
Copy link
Collaborator

Testing the new fix for the max31856, we are still encountering the get config from SD card sleep interval error; details are shown below. The setup uses a Hypnos board, which uses SPI communication for the SD card reader, and a K-Type thermocouple, which uses the MAX31856 board and also relies on SPI communication.

sd card config file:
{ "SleepInterval": { "days" : 0, "hours" : 0, "minutes" : 0, "seconds" : 30 }, "timezone": "AKST" }

serial output when asked to serialize the config json:
{"":{"days":0,"hours":0,"minutes":0,"seconds":30},"timezone":"AKST"}[2025.02.04 16:56:45] [DEBUG] [:getConfigFromSD:582] Config successfully loaded from SD! [2025.02.04 16:56:45] [DEBUG] [:getConfigFromSD:586] Selected timezone: AKST, UTC offset: -9 [2025.02.04 16:56:45] [ERROR] [:getConfigFromSD:597] There was an error reading the sampling interval from SD, defaulting sampling interval to 20 minutes.

We see that the "timezone" header was successfully read and the correct timezone information was implemented. However, when the SPI thermocouple device is connected the header for "SleepInterval" is deleted or corrupted to "" which is why the sampling interval cannot be read from SD. After removing the SPI thermocouple sensor, the SD card is read correctly with no header dropping. This leads me to believe there is some deeper conflict with the SPI devices that we will have to investigate.

@sarveshta
Copy link
Collaborator

I had a similar issue when helping Quinn with his onboarding exam. He was trying to use a sensor (I don't recall which, but it was not SPI) and LoRa. The same issue happened. I will look into this as well with Quinn's setup.

@sarveshta
Copy link
Collaborator

moving free(fileRead); to the very end of the getConfigFromSD right before FUNCTION_END; makes it work!

TimeSpan Loom_Hypnos::getConfigFromSD(const char* fileName){
    FUNCTION_START;
    // Doc to store the JSON data from the SD card in
    StaticJsonDocument<OUTPUT_SIZE> doc;
    char output[OUTPUT_SIZE];
    char* fileRead = sdMan->readFile(fileName);
    DeserializationError deserialError = deserializeJson(doc, fileRead);

    // Create json object to easily pull data from
    JsonObject json = doc.as<JsonObject>();

    if(deserialError != DeserializationError::Ok){
        snprintf(output, OUTPUT_SIZE, "There was an error reading the config from SD: %s, defaulting sampling interval to 20 minutes.", deserialError.c_str());
        ERROR(output);
        return TimeSpan(0, 0, 20, 0);
    }
    else{
        LOG(F("Config successfully loaded from SD!"));
        if(!json["timezone"].isNull()){
            const char* timezoneStr = json["timezone"].as<const char*>();
            snprintf(output, OUTPUT_SIZE, "Selected timezone: %s, UTC offset: %i", timezoneStr, (int)timezoneMap[(const char*)timezoneStr]);
            LOG(output);
            timezone = timezoneMap[(const char*)timezoneStr];
        }

        // If the sleep interval key is not supplied we want to set some default
        if(!json["SleepInterval"].isNull()){
            // Return the interval as set in the json
            return TimeSpan(json["SleepInterval"]["days"].as<int>(), json["SleepInterval"]["hours"].as<int>(), json["SleepInterval"]["minutes"].as<int>(), json["SleepInterval"]["seconds"].as<int>());
        }
        else{
            snprintf(output, OUTPUT_SIZE, "There was an error reading the sampling interval from SD, defaulting sampling interval to 20 minutes.");
            ERROR(output);
            return TimeSpan(0, 0, 20, 0);
        }
    }
    free(fileRead);
    FUNCTION_END;
}

@sarveshta
Copy link
Collaborator

Assigned to @quinnedgar. I will shadow his testing.

@sarveshta sarveshta assigned quinnedgar and unassigned WL-Richards Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In review
Development

No branches or pull requests

5 participants