I am working on a project that uses MAX6675 module to read temperature using a K-type thermocouple. I am displaying that data reading on an OLED display and sending it with Bluetooth through an HC-05 module.
However, when I connect my phone and start receiving that data with Bluetooth which I display on a Bluetooth terminal. The serial monitor stops updating and displaying the data and my OLED display as well. And vice versa when I disconnect my HC-05 I am again able to receive the data with Serial monitor.
My question is: How can I use Bluetooth HC-05 and Arduino Serial Monitor at the same time ?
Please find my code below:
#include <Wire.h>
#include "max6675.h" //MAX6675-library by AdaFruit
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SoftwareSerial.h>
//Define Bluetooth pins
#define TxD 3 //Transmitter
#define RxD 2 //Receiver
SoftwareSerial mySerial(RxD, TxD);
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
int thermoDO = 8; // SO of MAX6675 module
int thermoCS = 9; // CS of MAX6675 module
int thermoCLK = 10; // SCK of MAX6675 module
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
float t, tF;
char *title="Temperature: ";
int type=1;// 1=C, 2=F, 3=K,
char receivedChar; //Stores Char type variables
int inputData; // Arbitrary variable, acts as buffer
void setup()
{
Serial.begin(9600); //Initialize board
Serial.println("MAX6675 Temperature sensing");
mySerial.begin(9600); //Initialize Bluetooth module HC-05
Serial.println("The device started, now you can pair it with bluetooth!");
// time for stabilization
delay(1000);
// initialize with the I2C address 0x3C for OLED Display
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay(); //Clears Dislay from previous data
display.setTextColor(WHITE);
}
void sendTemp()
{
mySerial.print(title);
if(type ==24)
{
mySerial.print(t);
mySerial.println("°C");
}
if(type ==25)
{
mySerial.print(tF);
mySerial.println("°F");
}
}
void getTemp()
{
t = thermocouple.readCelsius();// Celsius
tF = thermocouple.readFahrenheit();// Fahrenheit
}
void loop()
{
getTemp(); //Function to read temperature from Thermocouple attached to MAX6675
//current temperature readout
Serial.print("Deg C = ");
Serial.println(t);
Serial.print("\t Deg F = ");
Serial.println(tF);
Serial.println();
// Clear the buffer.
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 7);
display.print("Temperature: ");
display.setTextSize(2);
display.setCursor(0, 18);
display.print(t);
display.setTextSize(1);
display.cp437(true);
display.write(167);
display.setTextSize(2);
display.print("C");
//Fahrenheit
display.setTextSize(2);
display.setCursor(0, 45);
display.print(tF);
display.setTextSize(1);
display.cp437(true);
display.write(167);
display.setTextSize(2);
display.print("F");
display.display();
delay(1000);
if(mySerial.available()){
mySerial.write(mySerial.read()); //If bluetooth connected start data read
while(mySerial.available()){ //While data is being received print on smartphone app terminal
mySerial.print(title);
mySerial.print(thermocouple.readCelsius());
mySerial.println("°C");
delay(300);
}
delay(300); //Tic Toc transmission delay
}
}
If I understand your requirements, here is how I would do it. I get a new temperature reading every second (using non-blocking timing). When the new reading is acquired a flag is set to notify the rest of the code that new data is available. The true flag causes the functions to print data on serial monitor, the LCD and to the phone. Once the data is sent to serial monitor, the LCD and the phone, the flag is cleared to begin another cycle. Code is not verified (I don't have the libraries) nor tested, but hopefully will be of assistance.
#include <Wire.h>
#include "max6675.h" //MAX6675-library by AdaFruit
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SoftwareSerial.h>
//Define Bluetooth pins
#define TxD 3 //Transmitter
#define RxD 2 //Receiver
SoftwareSerial mySerial(RxD, TxD);
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
int thermoDO = 8; // SO of MAX6675 module
int thermoCS = 9; // CS of MAX6675 module
int thermoCLK = 10; // SCK of MAX6675 module
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
float t, tF;
char *title = "Temperature: ";
int type = 1; // 1=C, 2=F, 3=K,
char receivedChar; //Stores Char type variables
int inputData; // Arbitrary variable, acts as buffer
bool newTemp = false; // is new temperature data avaiable?
void setup()
{
Serial.begin(9600); //Initialize board
Serial.println("MAX6675 Temperature sensing");
mySerial.begin(9600); //Initialize Bluetooth module HC-05
Serial.println("The device started, now you can pair it with bluetooth!");
// time for stabilization
delay(1000);
// initialize with the I2C address 0x3C for OLED Display
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay(); //Clears Dislay from previous data
display.setTextColor(WHITE);
}
void sendTemp()
{
mySerial.print(title);
if (type == 24)
{
mySerial.print(t);
mySerial.println("°C");
}
if (type == 25)
{
mySerial.print(tF);
mySerial.println("°F");
}
}
void getTemp()
{
// get a new temperature reading every one second
static unsigned long timer = 0;
unsigned long interval = 1000;
if (millis() - timer >= interval)
{
timer = millis();
t = thermocouple.readCelsius();// Celsius
tF = thermocouple.readFahrenheit();// Fahrenheit
newTemp = true;
}
}
void loop()
{
getTemp(); //Function to read temperature from Thermocouple attached to MAX6675
if (newTemp)
{
printTempToSerialMonitor();
printTemperatureToLCD();
sendTemp();
newTemp = false;
}
}
void printTemperatureToLCD()
{
// print to display
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 7);
display.print("Temperature: ");
display.setTextSize(2);
display.setCursor(0, 18);
display.print(t);
display.setTextSize(1);
display.cp437(true);
display.write(167);
display.setTextSize(2);
display.print("C");
//Fahrenheit
display.setTextSize(2);
display.setCursor(0, 45);
display.print(tF);
display.setTextSize(1);
display.cp437(true);
display.write(167);
display.setTextSize(2);
display.print("F");
display.display();
}
void printTempToSerialMonitor()
{
// print to serial monitor
timer = millis();
Serial.print("Deg C = ");
Serial.println(t);
Serial.print("\t Deg F = ");
Serial.println(tF);
Serial.println();
}
it just might work. I only suggest you also get rid of the 300ms delays because I have no idea of what they are doing there - and it seems Google has no idea either..
If it does work, you might then look at deleting redundant code
Actually, yes. The functıon void sendTemp() ıs for thıs purpose.
I would lıke to send a HEX 1 ın the Bluetooth terminal to get temperature ın degree C. But I could not succeed.
I tried and it worked perfectly. However, I am willing to try out sending a request and according to it receive data. For instance in the Bluetooth terminal I send a HEX 1 and receive temperature in Degree C, and if it is a HEX 2 I receive temperature in Fahrenheit. The function void sendTemp() is for this purpose.
I did not because it did not work. Let me explain:
Above I declare int type = 1; // 1=C, 2=F, 3=K, .
In the Bluetooth terminal I define three "buttons" to have values 1, 2, and 3 respectively. Each corresponding to a unit of temperature (i.e, 1 for Celsius, 2 for Fahrenheit etc...).
When I press the button with value 1 HEX, that should send a request "Send temperature in Degree C"
when I send my request I do not receive a response, even this message is not received... mySerial.print("Received:");// write on BT app mySerial.println(inputData);
This is what I have tried doing but did not succeed:
void loop(){
getTemp();
if (Serial.available()) {
mySerial.write(Serial.read());
}
while (mySerial.available() ) {
receivedChar =(char)mySerial.read();
inputData += receivedChar;
if (receivedChar == ' ')
{
mySerial.print("Received:");// write on BT app
mySerial.println(inputData);// write on BT app
type = inputData;
sendTemp();
inputData = 0; // Clear recieved buffer
}
}
}