I am trying to create a data acquisition system record 8 temperatures and send them to a lora receiver. So far I have been able to log and output the data via serial and separately using the example sketch for LORA I can send packets.
The trouble I am having comes when I try and do both. When I add the functions to read the different temperatures the system no longer transmits lora packets.
I am new to programming past the basics so may be overlooking something obvious but it seems like the temperature reading function is disrupting the ability to send packets.
This is my main code and the 3 tabs I created to display, get the temperatures and transmit.
//***************************************************
//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
// ****************************************************/
//
//**************LORA
#include "LoRaWan_APP.h"
#include "Arduino.h"
//****************Lora/end
#include <Wire.h>
#include "HT_SSD1306Wire.h"
#include <SPI.h>
#include "Adafruit_MAX31855.h"
//***************************************LORA Defines************************
#define RF_FREQUENCY 915000000 // Hz
#define TX_OUTPUT_POWER 5 // dBm
#define LORA_BANDWIDTH 0 // [0: 125 kHz,
// 1: 250 kHz,
// 2: 500 kHz,
// 3: Reserved]
#define LORA_SPREADING_FACTOR 7 // [SF7..SF12]
#define LORA_CODINGRATE 1 // [1: 4/5,
// 2: 4/6,
// 3: 4/7,
// 4: 4/8]
#define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
#define LORA_SYMBOL_TIMEOUT 0 // Symbols
#define LORA_FIX_LENGTH_PAYLOAD_ON false
#define LORA_IQ_INVERSION_ON false
#define RX_TIMEOUT_VALUE 1000
#define BUFFER_SIZE 30 // Define the payload size here
char txpacket[BUFFER_SIZE];
char rxpacket[BUFFER_SIZE];
double txNumber;
bool lora_idle=true;
static RadioEvents_t RadioEvents;
void OnTxDone( void );
void OnTxTimeout( void );
//**********************************************Lora Defines/end
double Temp0 = 9999;
double Temp1 = 9999;
double Temp2 = 9999;
double Temp3 = 9999;
double Temp4 = 9999;
double Temp5 = 9999;
double Temp6 = 9999;
double Temp7 = 9999;
int kBoardDelay = 1000;
double myTemps[7];
SSD1306Wire factory_display(0x3c, 500000, SDA_OLED, SCL_OLED, GEOMETRY_128_64, RST_OLED);
int x;
int y;
int x1;
int y3;
// 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 19
#define MAXCS 20
#define MAXCLK 26
#define T0 7
#define T1 6
#define T2 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);
// Example creating a thermocouple instance with hardware SPI
// on SPI1 using specified CS pin.
//#define MAXCS 10
//Adafruit_MAX31855 thermocouple(MAXCS, SPI1);
void setup() {
Serial.begin(9600);
//*******************************LORA Setup
Mcu.begin();
txNumber=0;
RadioEvents.TxDone = OnTxDone;
RadioEvents.TxTimeout = OnTxTimeout;
Radio.Init( &RadioEvents );
Radio.SetChannel( RF_FREQUENCY );
Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
LORA_SPREADING_FACTOR, LORA_CODINGRATE,
LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
true, 0, 0, LORA_IQ_INVERSION_ON, 3000 );
//*******************************Lora Setup end
pinMode(T0, OUTPUT);
pinMode(T1, OUTPUT);
pinMode(T2, OUTPUT);
digitalWrite(T0, LOW);
digitalWrite(T1, LOW);
digitalWrite(T2, LOW);
while (!Serial) delay(1); // wait for Serial on Leonardo/Zero, etc
Serial.println("MAX31855 test");
// wait for MAX chip to stabilize
delay(500);
Serial.print("Initializing sensor...");
if (!thermocouple.begin()) {
Serial.println("ERROR.");
while (1) delay(10);
}
// OPTIONAL: Can configure fault checks as desired (default is ALL)
// Multiple checks can be logically OR'd together.
// thermocouple.setFaultChecks(MAX31855_FAULT_OPEN | MAX31855_FAULT_SHORT_VCC); // short to GND fault is ignored
Serial.println("DONE.");
VextON();
delay(100);
// Initialising the UI will init the display too.
factory_display.init();
factory_display.setFont(ArialMT_Plain_10);
}
void loop() {
getTemp0();
getTemp1();
getTemp2();
getTemp3();
getTemp4();
getTemp5();
getTemp6();
getTemp7();
myTemps[0] = Temp0;
myTemps[1] = Temp1;
myTemps[2] = Temp2;
myTemps[3] = Temp3;
myTemps[4] = Temp4;
myTemps[5] = Temp5;
myTemps[6] = Temp6;
myTemps[7] = Temp7;
// for (int i = 0; i < 8; i++) {
// Serial.print(" Temperature ");
// Serial.print(i);
// Serial.print(": ");
// Serial.print(myTemps[i]);
// Serial.println(" ");
// }
factory_display.clear();
displayTemp();
factory_display.display();
// Serial.print("Internal Temp = ");
// Serial.println(thermocouple.readInternal());
//***************************************LORA loop code
if(lora_idle == true)
{
delay(1000);
txNumber += 0.01;
sprintf(txpacket,"Hello world number %0.2f",txNumber); //start a package
Serial.printf("\r\nsending packet \"%s\" , length %d\r\n",txpacket, strlen(txpacket));
Radio.Send( (uint8_t *)txpacket, strlen(txpacket) ); //send the package out
lora_idle = false;
}
Radio.IrqProcess( );
//**************************************LORA loop code/end
}
void clrScreen() {
Serial.println(" ");
}
void VextON(void)
{
pinMode(Vext,OUTPUT);
digitalWrite(Vext, LOW);
}
void VextOFF(void) //Vext default OFF
{
pinMode(Vext,OUTPUT);
digitalWrite(Vext, HIGH);
}
void displayTemp() {
char str0[30];
char str1[30];
char str2[30];
char str3[30];
factory_display.setFont(ArialMT_Plain_10);
factory_display.setTextAlignment(TEXT_ALIGN_LEFT);
sprintf(str0,"T 0: %.2f T 4: %.2f", Temp0, Temp4);
factory_display.drawString(0, 0, str0);
factory_display.setFont(ArialMT_Plain_10);
factory_display.setTextAlignment(TEXT_ALIGN_LEFT);
sprintf(str1,"T 1: %.2f T 5: %.2f", Temp1, Temp5);
factory_display.drawString(0, 14, str1);
factory_display.setFont(ArialMT_Plain_10);
factory_display.setTextAlignment(TEXT_ALIGN_LEFT);
sprintf(str2,"T 2: %.2f T 6: %.2f", Temp2, Temp6);
factory_display.drawString(0, 28, str2);
factory_display.setFont(ArialMT_Plain_10);
factory_display.setTextAlignment(TEXT_ALIGN_LEFT);
sprintf(str3,"T 3: %.2f T 7: %.2f", Temp3, Temp7);
factory_display.drawString(0, 42, str3);
}
void drawTextAlignmentDemo() {
// Text alignment demo
char str[30];
int x = 0;
int y = 0;
factory_display.setFont(ArialMT_Plain_10);
// The coordinates define the left starting point of the text
factory_display.setTextAlignment(TEXT_ALIGN_LEFT);
factory_display.drawString(x, y, "Left aligned (0,0)");
}
void getTemp0() {
digitalWrite(T0, LOW);
digitalWrite(T1, LOW);
digitalWrite(T2, LOW);
// basic readout test, just print the current temp
delay(kBoardDelay);
double c = thermocouple.readCelsius();
if (isnan(c)) {
Serial.println("Thermocouple fault(s) detected!");
uint8_t e = thermocouple.readError();
if (e & MAX31855_FAULT_OPEN) Serial.println("FAULT: Thermocouple is open - no connections.");
if (e & MAX31855_FAULT_SHORT_GND) Serial.println("FAULT: Thermocouple is short-circuited to GND.");
if (e & MAX31855_FAULT_SHORT_VCC) Serial.println("FAULT: Thermocouple is short-circuited to VCC.");
} else {
Temp0 = c;
}
}
void getTemp1() {
digitalWrite(T0, HIGH);
digitalWrite(T1, LOW);
digitalWrite(T2, LOW);
// basic readout test, just print the current temp
delay(kBoardDelay);
double c = thermocouple.readCelsius();
if (isnan(c)) {
Serial.println("Thermocouple fault(s) detected!");
uint8_t e = thermocouple.readError();
if (e & MAX31855_FAULT_OPEN) Serial.println("FAULT: Thermocouple is open - no connections.");
if (e & MAX31855_FAULT_SHORT_GND) Serial.println("FAULT: Thermocouple is short-circuited to GND.");
if (e & MAX31855_FAULT_SHORT_VCC) Serial.println("FAULT: Thermocouple is short-circuited to VCC.");
} else {
Temp1 = c;
}
}
void getTemp2() {
digitalWrite(T0, LOW);
digitalWrite(T1, HIGH);
digitalWrite(T2, LOW);
// basic readout test, just print the current temp
delay(kBoardDelay);
double c = thermocouple.readCelsius();
if (isnan(c)) {
Serial.println("Thermocouple fault(s) detected!");
uint8_t e = thermocouple.readError();
if (e & MAX31855_FAULT_OPEN) Serial.println("FAULT: Thermocouple is open - no connections.");
if (e & MAX31855_FAULT_SHORT_GND) Serial.println("FAULT: Thermocouple is short-circuited to GND.");
if (e & MAX31855_FAULT_SHORT_VCC) Serial.println("FAULT: Thermocouple is short-circuited to VCC.");
} else {
Temp2 = c;
}
}
void getTemp3() {
digitalWrite(T0, HIGH);
digitalWrite(T1, HIGH);
digitalWrite(T2, LOW);
// basic readout test, just print the current temp
delay(kBoardDelay);
double c = thermocouple.readCelsius();
if (isnan(c)) {
Serial.println("Thermocouple fault(s) detected!");
uint8_t e = thermocouple.readError();
if (e & MAX31855_FAULT_OPEN) Serial.println("FAULT: Thermocouple is open - no connections.");
if (e & MAX31855_FAULT_SHORT_GND) Serial.println("FAULT: Thermocouple is short-circuited to GND.");
if (e & MAX31855_FAULT_SHORT_VCC) Serial.println("FAULT: Thermocouple is short-circuited to VCC.");
} else {
Temp3 = c;
}
}
void getTemp4() {
digitalWrite(T0, LOW);
digitalWrite(T1, LOW);
digitalWrite(T2, HIGH);
// basic readout test, just print the current temp
delay(kBoardDelay);
double c = thermocouple.readCelsius();
if (isnan(c)) {
Serial.println("Thermocouple fault(s) detected!");
uint8_t e = thermocouple.readError();
if (e & MAX31855_FAULT_OPEN) Serial.println("FAULT: Thermocouple is open - no connections.");
if (e & MAX31855_FAULT_SHORT_GND) Serial.println("FAULT: Thermocouple is short-circuited to GND.");
if (e & MAX31855_FAULT_SHORT_VCC) Serial.println("FAULT: Thermocouple is short-circuited to VCC.");
} else {
Temp4 = c;
}
}
void getTemp5() {
digitalWrite(T0, HIGH);
digitalWrite(T1, LOW);
digitalWrite(T2, HIGH);
// basic readout test, just print the current temp
delay(kBoardDelay);
double c = thermocouple.readCelsius();
if (isnan(c)) {
Serial.println("Thermocouple fault(s) detected!");
uint8_t e = thermocouple.readError();
if (e & MAX31855_FAULT_OPEN) Serial.println("FAULT: Thermocouple is open - no connections.");
if (e & MAX31855_FAULT_SHORT_GND) Serial.println("FAULT: Thermocouple is short-circuited to GND.");
if (e & MAX31855_FAULT_SHORT_VCC) Serial.println("FAULT: Thermocouple is short-circuited to VCC.");
} else {
Temp5 = c;
}
}
void getTemp6() {
digitalWrite(T0, LOW);
digitalWrite(T1, HIGH);
digitalWrite(T2, HIGH);
// basic readout test, just print the current temp
delay(kBoardDelay);
double c = thermocouple.readCelsius();
if (isnan(c)) {
Serial.println("Thermocouple fault(s) detected!");
uint8_t e = thermocouple.readError();
if (e & MAX31855_FAULT_OPEN) Serial.println("FAULT: Thermocouple is open - no connections.");
if (e & MAX31855_FAULT_SHORT_GND) Serial.println("FAULT: Thermocouple is short-circuited to GND.");
if (e & MAX31855_FAULT_SHORT_VCC) Serial.println("FAULT: Thermocouple is short-circuited to VCC.");
} else {
Temp6 = c;
}
}
void getTemp7() {
digitalWrite(T0, HIGH);
digitalWrite(T1, HIGH);
digitalWrite(T2, HIGH);
// basic readout test, just print the current temp
delay(kBoardDelay);
double c = thermocouple.readCelsius();
if (isnan(c)) {
Serial.println("Thermocouple fault(s) detected!");
uint8_t e = thermocouple.readError();
if (e & MAX31855_FAULT_OPEN) Serial.println("FAULT: Thermocouple is open - no connections.");
if (e & MAX31855_FAULT_SHORT_GND) Serial.println("FAULT: Thermocouple is short-circuited to GND.");
if (e & MAX31855_FAULT_SHORT_VCC) Serial.println("FAULT: Thermocouple is short-circuited to VCC.");
} else {
Temp7 = c;
}
}
void OnTxDone( void )
{
Serial.println("TX done......");
lora_idle = true;
}
void OnTxTimeout( void )
{
Radio.Sleep( );
Serial.println("TX Timeout......");
lora_idle = true;
}
Any help would be greatly appreciated.