196 lines
4.6 KiB
C++
196 lines
4.6 KiB
C++
// Basic Bluetooth sketch HC-06_01
|
|
// Connect the Hc-06 module and communicate using the serial monitor
|
|
//
|
|
// The HC-06 defaults to AT mode when first powered on.
|
|
// The default baud rate is 9600
|
|
// The Hc-06 requires all AT commands to be in uppercase. NL+CR should not be added to the command string
|
|
//
|
|
|
|
|
|
#include <SoftwareSerial.h>
|
|
|
|
#define INFO 1
|
|
|
|
//pins
|
|
const int tmp_sens = 7 // DS18x20 is on pin 7 (a 4.7K resistor is necessary)
|
|
|
|
// Connect the HC-06 RX to the Arduino TX on pin 5.
|
|
// If DC is 5v RX should be connected through a voltage divider.
|
|
// Connect the HC-06 TX to the Arduino RX on pin 4.
|
|
SoftwareSerial BTserial(5, 4); // RX | TX
|
|
|
|
OneWire ds(7);
|
|
// This is
|
|
byte addr[8];
|
|
|
|
#define INVALID_TEMP -1000.
|
|
|
|
struct TempSensor
|
|
{
|
|
// DS18x20 is on pin 7 (a 4.7K resistor is necessary)
|
|
OneWire ds(7);
|
|
byte addr[8];
|
|
byte data[12];
|
|
byte type_s;
|
|
|
|
bool start()
|
|
{
|
|
}
|
|
|
|
protected:
|
|
|
|
bool init_sensor()
|
|
{
|
|
if ( !ds.search(addr)) {
|
|
Serial.println("Failed to find address for DS18x20.");
|
|
ds.reset_search();
|
|
delay(250);
|
|
return false;
|
|
}
|
|
|
|
#if INFO
|
|
print_address( addr );
|
|
Serial.println();
|
|
#endif
|
|
|
|
if (OneWire::crc8(addr, 7) != addr[7]) {
|
|
print_address( addr );
|
|
Serial.print(" CRC=");
|
|
Serial.print(OneWire::crc8(addr, 7), HEX);
|
|
Serial.println(" is not valid!");
|
|
return false;
|
|
}
|
|
|
|
// the first ROM byte indicates which chip
|
|
switch (addr[0]) {
|
|
case 0x10:
|
|
Serial.println(" Chip = DS18S20"); // or old DS1820
|
|
type_s = 1;
|
|
break;
|
|
case 0x28:
|
|
Serial.println(" Chip = DS18B20");
|
|
type_s = 0;
|
|
break;
|
|
case 0x22:
|
|
Serial.println(" Chip = DS1822");
|
|
type_s = 0;
|
|
break;
|
|
default:
|
|
Serial.println("Device is not a DS18x20 family device.");
|
|
return;
|
|
}
|
|
}
|
|
|
|
void print_array( byte* arr, byte len )
|
|
{
|
|
byte i;
|
|
for( i = 0; i < len; i++) {
|
|
Serial.write(' ');
|
|
Serial.print(add[i], HEX);
|
|
}
|
|
}
|
|
|
|
void print_address()
|
|
{
|
|
Serial.print("ROM =");
|
|
print_array( addr, 8 );
|
|
}
|
|
|
|
void print_data()
|
|
{
|
|
Serial.print("Data =");
|
|
print_array( data, 9 );
|
|
}
|
|
|
|
};
|
|
|
|
void setup()
|
|
{
|
|
// We will use this for debug output.
|
|
Serial.begin(9600);
|
|
|
|
// HC-06 default serial speed is 9600
|
|
BTserial.begin(9600);
|
|
}
|
|
|
|
|
|
|
|
float read_temp(void)
|
|
{
|
|
byte i;
|
|
byte present = 0;
|
|
byte type_s;
|
|
byte data[12];
|
|
float celsius;
|
|
|
|
|
|
ds.reset();
|
|
ds.select(addr);
|
|
ds.write(0x44, 1); // start conversion, with parasite power on at the end
|
|
|
|
delay(1000); // maybe 750ms is enough, maybe not
|
|
// we might do a ds.depower() here, but the reset will take care of it.
|
|
|
|
present = ds.reset();
|
|
ds.select(addr);
|
|
ds.write(0xBE); // Read Scratchpad
|
|
|
|
for ( i = 0; i < 9; i++) { // we need 9 bytes
|
|
data[i] = ds.read();
|
|
}
|
|
|
|
if (OneWire::crc8(data, 8) != data[8]) {
|
|
print_data( data );
|
|
Serial.print(" CRC=");
|
|
Serial.print(OneWire::crc8(data, 7), HEX);
|
|
Serial.println(" is not valid!");
|
|
return;
|
|
}
|
|
|
|
// Convert the data to actual temperature
|
|
// because the result is a 16 bit signed integer, it should
|
|
// be stored to an "int16_t" type, which is always 16 bits
|
|
// even when compiled on a 32 bit processor.
|
|
int16_t raw = (data[1] << 8) | data[0];
|
|
if (type_s) {
|
|
raw = raw << 3; // 9 bit resolution default
|
|
if (data[7] == 0x10) {
|
|
// "count remain" gives full 12 bit resolution
|
|
raw = (raw & 0xFFF0) + 12 - data[6];
|
|
}
|
|
} else {
|
|
byte cfg = (data[4] & 0x60);
|
|
// at lower res, the low bits are undefined, so let's zero them
|
|
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
|
|
else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
|
|
else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
|
|
//// default is 12 bit resolution, 750 ms conversion time
|
|
}
|
|
celsius = (float)raw / 16.0;
|
|
|
|
#if INFO
|
|
Serial.print(" Temperature = ");
|
|
Serial.print(celsius);
|
|
Serial.print(" Celsius.");
|
|
#endif
|
|
|
|
return celsius;
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
|
|
// Keep reading from HC-06 and send to Arduino Serial Monitor
|
|
if (BTserial.available())
|
|
{
|
|
Serial.write(BTserial.read());
|
|
}
|
|
|
|
// Keep reading from Arduino Serial Monitor and send to HC-06
|
|
if (Serial.available())
|
|
{
|
|
BTserial.write(Serial.read());
|
|
}
|
|
|
|
}
|