Addition to emonTx

Hi.
Has anyone added to the emonTx K-type thermal sensor? I have a sketch for thermal sensor, but I can’t connect it to the original emonTX sketch, so it also moves to emoncms.org.
Thank You if someone helps me.

Here is the Tx sketch

/*
  EmonTx Pulse example


 
  -----------------------------------------
  Part of the openenergymonitor.org project
  Licence: GNU GPL V3
 
  Authors: Glyn Hudson, Trystan Lea
  Builds upon JeeLabs RF12 library and Arduino

  THIS SKETCH REQUIRES:

  Libraries in the standard arduino libraries folder:
 	- JeeLib		https://github.com/jcw/jeelib

  Other files in project directory (should appear in the arduino tabs above)
	- emontx_lib.ino
*/

/*Recommended node ID allocation
------------------------------------------------------------------------------------------------------------
-ID-	-Node Type- 
0	- Special allocation in JeeLib RFM12 driver - reserved for OOK use
1-4     - Control nodes 
5-10	- Energy monitoring nodes
11-14	--Un-assigned --
15-16	- Base Station & logging nodes
17-30	- Environmental sensing nodes (temperature humidity etc.)
31	- Special allocation in JeeLib RFM12 driver - Node31 can communicate with nodes on any network group
-------------------------------------------------------------------------------------------------------------
*/

#define RF69_COMPAT 1
#define FREQ RF12_433MHZ                                                // Frequency of RF12B module can be RF12_433MHZ, RF12_868MHZ or RF12_915MHZ. You should use the one matching the module you have.433MHZ, RF12_868MHZ or RF12_915MHZ. You should use the one matching the module you have.
const int nodeID = 9;                                                  // emonTx RFM12B node ID
const int networkGroup = 210;                                           // emonTx RFM12B wireless network group - needs to be same as emonBase and emonGLCD needs to be same as emonBase and emonGLCD

const int UNO = 1;                                                      // Set to 0 if your not using the UNO bootloader (i.e using Duemilanove) - All Atmega's shipped from OpenEnergyMonitor come with Arduino Uno bootloader
#include <avr/wdt.h>                                                    // the UNO bootloader 
#include <JeeLib.h>                                                     // Download JeeLib: http://github.com/jcw/jeelib
#include "EmonLib.h"
#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);                                          // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
DallasTemperature sensors(&oneWire);  

DeviceAddress address_T1 = { 0x28, 0x7E, 0x0C, 0x0F, 0x0A, 0x00, 0x00, 0x44 };      // Huussi Hormi
DeviceAddress address_T2 = { 0x10, 0x5A, 0x5A, 0x8D, 0x02, 0x08, 0x00, 0x4F };      // Huussi Ulko

ISR(WDT_vect) { Sleepy::watchdogEvent(); } 

typedef struct { 
          int power, pulse, T1, T2; 
}
       PayloadTX;
PayloadTX emontx;                        

const int LEDpin = 9;

// Pulse counting settings 
long pulseCount = 0;                                                    // Number of pulses, used to measure energy.
unsigned long pulseTime,lastTime;                                       // Used to measure power.
double power, elapsedWh;                                                // power and energy
int ppwh = 1;        

                          
void setup() 
{
  Serial.begin(115200);
  Serial.println("emonTX Pulse example");
  delay(100);
 
  sensors.begin(); 
 
  rf12_initialize(nodeID, FREQ, networkGroup);                          // initialize RF
  rf12_sleep(RF12_SLEEP);
 
  pinMode(LEDpin, OUTPUT);                                              // Setup indicator LED
  digitalWrite(LEDpin, HIGH); 
  attachInterrupt(1, onPulse, FALLING);      
 
if (UNO) wdt_enable(WDTO_8S);  
 }

void loop()  {
  sensors.requestTemperatures();                           // Send the command to get temperatures
  
  emontx.T1 = sensors.getTempC(address_T1) * 100 / 100,0;
  emontx.T2 = sensors.getTempC(address_T2) * 100 / 100,0;

  emontx.pulse = pulseCount; pulseCount=0;
  Serial.print(emontx.power);
  Serial.print("W ");
  Serial.println(emontx.pulse);  

  send_rf_data();  // *SEND RF DATA* - see emontx_lib

  Serial.print(emontx.power);
  Serial.print("W ");
  Serial.println(emontx.pulse);   
  emontx_sleep(10);                                                     // sleep or delay in seconds - see emontx_lib
  digitalWrite(LEDpin, HIGH); delay(2); digitalWrite(LEDpin, LOW);      // flash LED  

    }
// The interrupt routine - runs each time a falling edge of a pulse is detected
  void onPulse()                  
{
  lastTime = pulseTime;        //used to measure time between pulses.
  pulseTime = micros();
  pulseCount++;                                                      //pulseCounter               
  emontx.power = int((3600000000.0 / (pulseTime - lastTime))/ppwh);  //Calculate powerprint_to_serial
   
  }

and here’s k-type sketcs

//
//    FILE: max31855_demo0.ino
//  AUTHOR: Rob Tillaart
// VERSION: 0.1.3
// PURPOSE: thermocouple lib demo application
//    DATE: 2014-01-01
//     URL:
//
// Released to the public domain
//

#include "MAX31855.h"

const int doPin = 8;
const int csPin = 11;
const int clPin = 13;

MAX31855 tc(clPin, csPin, doPin);

void setup() 
{
  Serial.begin(115200);
  Serial.print("Start max31855_demo0: ");
  Serial.println(MAX31855_VERSION);
  Serial.println();

  tc.begin();
}

void loop() {
  int status = tc.read();
  Serial.print("stat:\t\t");
  Serial.println(status);

  float internal = tc.getInternal();
  Serial.print("internal:\t");
  Serial.println(internal);

  float temp = tc.getTemperature();
  Serial.print("temperature:\t");
  Serial.println(temp);
  delay(100+00);
}

If I am interpreting the information that I’ve found correctly, it will not be possible to use that with the emonTx - at least easily - because some of the pins necessary are already being used by the RFM69CW radio and not easily available. You’d need to solder onto MOSI and SCK, possibly on the edge of the RFM itself, and find a DO pin to use as CS, to make the necessary connections to the temperature i.c.

You will then need to merge the two sketches.

Hey.Thanks for the tips .I have now joined the two sketches. But I do not get the thermocouple values, but the output is displayed on the serial monitor but does not pass on to emonsms.My knowledge and skills are not enough from now on.Can you still help me and look at this sketch if it’s wrong. sketch yesturns right.
Thank You
Here is combined version.

/* -----------------------------------------
  Part of the openenergymonitor.org project
  Licence: GNU GPL V3

  Authors: Glyn Hudson, Trystan Lea
  Builds upon JeeLabs RF12 library and Arduino

  THIS SKETCH REQUIRES:

  Libraries in the standard arduino libraries folder:
   - JeeLib    https://github.com/jcw/jeelib

 Other files in project directory (should appear in the arduino tabs above)
  - emontx_lib.ino


*/
//
//----------------------------------------------------------------------
//
//    FILE: max31855_demo0.ino
//  AUTHOR: Rob Tillaart
// VERSION: 0.1.3
// PURPOSE: thermocouple lib demo application
//    DATE: 2014-01-01
//     URL:
//
// Released to the public domain
//----------------------------------------------------------------------


// ================== Used Arduino 1.8.9   07.7.2019 ===========================

#define RF69_COMPAT 1
#define FREQ RF12_433MHZ                                                // Frequency of RF12B module can be RF12_433MHZ, RF12_868MHZ or RF12_915MHZ. You should use the one matching the module you have.433MHZ, RF12_868MHZ or RF12_915MHZ. You should use the one matching the module you have.
const int nodeID = 9;                                                  // emonTx RFM12B node ID
const int networkGroup = 210;                                           // emonTx RFM12B wireless network group - needs to be same as emonBase and emonGLCD needs to be same as emonBase and emonGLCD
const int UNO = 1;                                                      // Set to 0 if your not using the UNO bootloader (i.e using Duemilanove) - All Atmega's shipped from OpenEnergyMonitor come with Arduino Uno bootloader
#include <avr/wdt.h>                                                    // the UNO bootloader 
#include <JeeLib.h>   

#include "MAX31855.h"                //     Rob Tillaart
const int doPin = 7;                 //             "
const int csPin = 6;                 //             "
const int clPin = 5;                 //             "
MAX31855 tc(clPin, csPin, doPin);    //     Rob Tillaart

ISR(WDT_vect) { Sleepy::watchdogEvent(); }
  
typedef struct { int power, pulse, tc;} PayloadTX;
PayloadTX emontx;                                                     // neat way of packaging data for RF comms

const int LEDpin = 9;  

// Pulse counting settings 
long pulseCount = 1000;                                                    // Number of pulses, used to measure energy.
unsigned long pulseTime,lastTime;                                       // Used to measure power.
double power, elapsedWh;                                                // power and energy
int ppwh = 1;                                                           // 1000 pulses/kwh = 1 pulse per wh - Number of pulses per wh - found or set on the meter.

void setup() 
{
  Serial.begin(9600);
  Serial.println("emonTX Pulse");
  delay(100);
  rf12_initialize(nodeID, FREQ, networkGroup);                          // initialize RF
  rf12_sleep(RF12_SLEEP);
 
  pinMode(LEDpin, OUTPUT);                                              // Setup indicator LED
  digitalWrite(LEDpin, HIGH);
  attachInterrupt(1, onPulse, FALLING);                                 // KWH interrupt attached to IRQ 1  = pin3 - hardwired to emonTx pulse jackplug. For connections see: http://openenergymonitor.org/emon/node/208
    if (UNO) wdt_enable(WDTO_8S); 
  
  Serial.print("Start max31855: ");     //   Rob Tillaart
  Serial.println(MAX31855_VERSION);     //          "
  Serial.println();                     //          "
  
   tc.begin();                          //    Rob Tillaart
 }
void loop() 
{ 
  emontx.pulse = pulseCount; pulseCount=0; 
  send_rf_data(); // *SEND RF DATA* - see emontx_lib
 
  Serial.print(emontx.power);
  Serial.print("W ");
  Serial.println(emontx.pulse);
  
  emontx_sleep(10);                                                     // sleep or delay in seconds - see emontx_lib
  digitalWrite(LEDpin, HIGH); delay(2); digitalWrite(LEDpin, LOW);
 
 
   int status = tc.read();                //  Rob Tillaart
   Serial.print("stat:\t\t");             //       "
   Serial.println(status);                //  Rob Tillaart
  
  float temp = tc.getTemperature();       //  Rob Tillaart
  Serial.print("temperature:\t");         //        "
  Serial.println(temp);                   //        "
  delay(1000);                            //  Rob Tillaart
 

}  
  // The interrupt routine - runs each time a falling edge of a pulse is detected

 void onPulse()                  
{
  lastTime = pulseTime;        //used to measure time between pulses.
  pulseTime = micros();
  pulseCount++;                                                      //pulseCounter               
  emontx.power = int((3600000000.0 / (pulseTime - lastTime))/ppwh);  //Calculate power
}

That is correct - the sketch does not send the temperature value to emoncms!

Normally, we send the temperature × 10 as an integer, then divide by 10 in emonHub to give a temperature with one decimal.

You have added it here:

typedef struct { int power, pulse, tc;} PayloadTX;

and then you must copy the value of the temp that you have into that tc. After
float temp = tc.getTemperature();
add
emontx.tc = temp * 10;

There was no link between tc (the temperature class by Rob Tillaart) and emontx.tc - the integer tc in the PayloadTX structure. It is only when you copy the value of one into the other that they then have the same value, there is no other link between them. This explains why you did not see the temperature in emoncms. (And, you were trying to copy the class into an integer - it won’t go.)

If you want to learn how C++ works, these are what I recommend:

Reference Textbooks
My bible for C is of course “Kernigan & Ritchie” http://www.amazon.co.uk/C-Programming-Language-2nd/dp/0131103628/ref=sr_1_3?s=books&ie=UTF8&qid=1292502807&sr=1-3. This is the standard text book. The normal place I point people at who want to move up to C++ is C++ In Action: Industrial Strength Programming Techniques - Free Computer, Programming, Mathematics, Technical Books, Lecture Notes and Tutorials and that assumes you know C. Because the Arduino environment normally uses a very small subset of the language, neither are the best place for a beginner to start, nor are many of the other on-line tutorials. I’d still suggest you have both of those, and maybe Bruce Eckel’s “Thinking in C++” available for reference.

However, there is this: http://www.me.umn.edu/courses/me2011/arduino/arduinoGuide.pdf which does look to be a good starting place for a beginner. It does not go as far as classes and methods that are used here, though. For that, you need the “Cpp in Action…”.

Thank you Robert! now all ok. Thank you very much for your help and clarification.
I need to go through the read links that you put in to help.:blush: