RaspberryPi MQTT high temp sensor code


#1

Hello,has anyone used a high temp thermocouple amp like the MAX 31855k with a RaspberryPi MQTT? I was looking for code examples to put one together for a Grill smoker temp probe.


#2

Fist, connect you with raspberry pi and get the sensor value https://learn.adafruit.com/max31855-thermocouple-python-library/overview. Once you have succesfully got the sensorr value, add a device to cayenne using cayenne MQTT python library https://github.com/myDevicesIoT/Cayenne-MQTT-Python. then combine got the codes and send the sensor value to cayenne,


#3

Ok, I’ve been unable to get the Adafruit code to work in the past but have been able to get this one to work https://github.com/Tuckie/max31855 do you think this one would work with Cayenne?


#4

Yes, that can work. just publish the sensor value from the above code to cayenne.


#5

Ok, so I would need to get the sensor value from the shell to Cayenne?


#6

yes using this https://github.com/myDevicesIoT/Cayenne-MQTT-Python/blob/master/examples/Example-01-SendData.py


#7

Ok,Thanks. I was thinking would it be best to use something like a Arduino Yun were you would’nt have to worry about powering it down since it would be unplugged and moved often. Ive been looking through code and found the Cayenne-Connections- Yun code.

// This example shows how to connect to Cayenne using an Arduino Yun and send/receive sample data.

//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTYun.h>

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "MQTT_USERNAME";
char password[] = "MQTT_PASSWORD";
char clientID[] = "CLIENT_ID";

unsigned long lastMillis = 0;

void setup() {
	Serial.begin(9600);
	Cayenne.begin(username, password, clientID);
}

void loop() {
	Cayenne.loop();

	//Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval.
	if (millis() - lastMillis > 10000) {
		lastMillis = millis();
		//Write data to Cayenne here. This example just sends the current uptime in milliseconds.
		Cayenne.virtualWrite(0, lastMillis);
		//Some examples of other functions you can use to send data.
		//Cayenne.celsiusWrite(1, 22.0);
		//Cayenne.luxWrite(2, 700);
		//Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
	}
}

//Default function for processing actuator commands from the Cayenne Dashboard.
//You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
CAYENNE_IN_DEFAULT()
{
	CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString());
	//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message")

I was wondering if there’s away to clean up and combine theses codes? I’m new to Arduino code and I’m not sure how to make this work.

/*************************************************** 
  This is an example for the Adafruit Thermocouple Sensor w/MAX31855K

  Designed specifically to work with the Adafruit Thermocouple Sensor
  ----> https://www.adafruit.com/products/269

  These displays use SPI to communicate, 3 pins are required to  
  interface
  Adafruit invests time and resources providing this open source code, 
  please support Adafruit and open-source hardware by purchasing 
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.  
  BSD license, all text above must be included in any redistribution
 ****************************************************/

#include <SPI.h>
#include "Adafruit_MAX31855.h"

// Default connection is using software SPI, but comment and uncomment one of
// the two examples below to switch between software SPI and hardware SPI:

// Example creating a thermocouple instance with software SPI on any three
// digital IO pins.
#define MAXDO   3
#define MAXCS   4
#define MAXCLK  5

// initialize the Thermocouple
Adafruit_MAX31855 thermocouple(MAXCLK, MAXCS, MAXDO);

// Example creating a thermocouple instance with hardware SPI
// on a given CS pin.
//#define MAXCS   10
//Adafruit_MAX31855 thermocouple(MAXCS);

void setup() {
  while (!Serial); // wait for Serial on Leonardo/Zero, etc
  
  Serial.begin(9600);
  
  Serial.println("MAX31855 test");
  // wait for MAX chip to stabilize
  delay(500);
}

void loop() {
  // basic readout test, just print the current temp
   Serial.print("Internal Temp = ");
   Serial.println(thermocouple.readInternal());

   double c = thermocouple.readCelsius();
   if (isnan(c)) {
     Serial.println("Something wrong with thermocouple!");
   } else {
     Serial.print("C = "); 
     Serial.println(c);
   }
   //Serial.print("F = ");
   //Serial.println(thermocouple.readFarenheit());
 
   delay(1000);
} 

Thanks.


#8

is this a separate topic? your issue with pi has been solved?


#9

Hello, sorry the last post should have been a separate topic, I’m still working with the RaspberryPi part.


#10

regarding the Yun topic you need to send double c = thermocouple.readCelsius(); to cayenne using:

if (millis() - lastMillis > 10000) {
	lastMillis = millis();
	Cayenne.virtualWrite(1, c, "analog_sensor", "null");
}

#11

Ok, so I would need to put the max31855 code and the Cayenne code together and flash it to the Yun correct?


#12

yes and send the sensor value using above code.


#13

Hello, I think I got it, something like this?

// This example shows how to connect to Cayenne using an Arduino Yun and send/receive sample data.

//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTYun.h>
#include <SPI.h>
#include "Adafruit_MAX31855.h"

// Default connection is using software SPI, but comment and uncomment one of
// the two examples below to switch between software SPI and hardware SPI:

// Example creating a thermocouple instance with software SPI on any three
// digital IO pins.
#define MAXDO   3
#define MAXCS   4
#define MAXCLK  5

// initialize the Thermocouple
Adafruit_MAX31855 thermocouple(MAXCLK, MAXCS, MAXDO);

// Example creating a thermocouple instance with hardware SPI
// on a given CS pin.
//#define MAXCS   10
//Adafruit_MAX31855 thermocouple(MAXCS);

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "MQTT_USERNAME";
char password[] = "MQTT_PASSWORD";
char clientID[] = "CLIENT_ID";

unsigned long lastMillis = 0;

void setup() {
  while (!Serial); // wait for Serial on Leonardo/Zero, etc
  
	Serial.begin(9600);

 Serial.println("MAX31855 test");
  // wait for MAX chip to stabilize
  delay(500);
	Cayenne.begin(username, password, clientID);
}

void loop() {
	Cayenne.loop();
 // basic readout test, just print the current temp
   Serial.print("Internal Temp = ");
   Serial.println(thermocouple.readInternal());

   double c = thermocouple.readCelsius();
   if (isnan(c)) {
     Serial.println("Something wrong with thermocouple!");
   } else {
     //Serial.print("C = "); 
     //Serial.println(c);
   }
     Serial.print("F = ");
     Serial.println(thermocouple.readFarenheit());
 
   delay(1000);

  //Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval.
	if (millis() - lastMillis > 10000) {
		lastMillis = millis();
		//Write data to Cayenne here. This example just sends the current uptime in milliseconds.
		Cayenne.virtualWrite(1, c, "analog_sensor", "null");
}
		//Some examples of other functions you can use to send data.
		//Cayenne.celsiusWrite(1, 22.0);
		//Cayenne.luxWrite(2, 700);
		//Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
	}


//Default function for processing actuator commands from the Cayenne Dashboard.
//You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
CAYENNE_IN_DEFAULT()
{
	CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString());
	//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
}

Thanks,Adam.


#14

yes that should work. try removing delay(1000); from main loop.


#15

Ok thanks, would I need to change double c = thermocouple.readCelsius(); to make this read and send the data in Fahrenheit?


#16

double F = thermocouple.readFarenheit();
Cayenne.virtualWrite(2, F, "temp", "f");

and also you need to change:

 Cayenne.virtualWrite(1, c, "analog_sensor", "null");

to

Cayenne.virtualWrite(1, c, "temp", "c");

`


#17

ok, something like this?

// This example shows how to connect to Cayenne using an Arduino Yun and send/receive sample data.

//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTYun.h>
#include <SPI.h>
#include "Adafruit_MAX31855.h"

// Default connection is using software SPI, but comment and uncomment one of
// the two examples below to switch between software SPI and hardware SPI:

// Example creating a thermocouple instance with software SPI on any three
// digital IO pins.
#define MAXDO   3
#define MAXCS   10
#define MAXCLK  5

// initialize the Thermocouple
Adafruit_MAX31855 thermocouple(MAXCLK, MAXCS, MAXDO);

// Example creating a thermocouple instance with hardware SPI
// on a given CS pin.
//#define MAXCS   10
//Adafruit_MAX31855 thermocouple(MAXCS);

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "*************************";
char password[] = "****************************";
char clientID[] = "********************************";

unsigned long lastMillis = 0;

void setup() {
  Serial.begin(9600);
  Cayenne.begin(username, password, clientID);
  //while (!Serial); // wait for Serial on Leonardo/Zero, etc
  
	//Serial.println("MAX31855 test");
  // wait for MAX chip to stabilize
  delay(500);
	
}

void loop() {
	Cayenne.loop();
 // basic readout test, just print the current temp
   //Serial.print("Internal Temp = ");
   //Serial.println(thermocouple.readInternal())

   double F = thermocouple.readFarenheit();
  

  //Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval.
	if (millis() - lastMillis > 10000) {
		lastMillis = millis();
		//Write data to Cayenne here. This example just sends the current uptime in milliseconds.
		Cayenne.virtualWrite(2, F, "temp", "f");


}
		//Some examples of other functions you can use to send data.
		//Cayenne.celsiusWrite(1, 22.0);
		//Cayenne.luxWrite(2, 700);
		//Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
	}


//Default function for processing actuator commands from the Cayenne Dashboard.
//You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
CAYENNE_IN_DEFAULT()
{
	CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString());
	//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
}

#18

yes that will work.


#19

Ok, thanks. On the RaspberryPi side the code would have to be combined with the Max31855 code and saved on the Pi in a file?


#20

Yes you have to save it with .py extension and run it.