BLE Shield Project: Thermometer

One of my goals to achieve with the BLE112 module is still to read the temperature without a microprocessor in between. But stacking together an Arduino based solution using the BLE Shield is that simple so I just did it and share the resulting project with you again.

Again I grabbed some parts out of my “Arduino Box”:

and stacked together a handy thermometer:

In this setup I ‘m using hardware serial to send data to the BLE Shield, so the switch needs to be set to the Tx0/Rx1 position. I programmed the ‘Eleven” with the following sketch. In this case the sketch will be in the loop until a character is written over the TX characteristic of the BLE Shield to control which value should be written to the RX Characteristic. If a ‘c’ character is send to the Shield from the iPhone, the temperature is read from the sensor and written to the RX characteristic. So a read of RX will result in the current temperature. If you send an ‘h’ character over TX to the Shield, the humidity is read and transferred to the RX characteristic and is available for reading using the iPhone. Of course the temperature and humidity readings are filled with spaces in order to fill the 16 byte BLE Shield buffer. (The LibHumidity library is used from Modern Device to compile the sketch using Arduino 0023.)

#include <;Wire.h>;
#include <;LibHumidity.h>;

LibHumidity sht21 = LibHumidity(0);

void setup() {
  Serial.begin(19200);
  pinMode(16, OUTPUT);
  digitalWrite(16, LOW);  //GND pin
  pinMode(17, OUTPUT);
  digitalWrite(17, HIGH); //VCC pin
}

void loop() {

  // Wait for a 'c' to write the temperature to the RX characteristic
  // and a 'h' to write the humidity to the RX characteristic.
  if (Serial.available()) {
    char c = Serial.read();
    if (c == 'c') {
      writeTemperature();
    }
    else if (c == 'h') {
      writeHumidity();
    }
  }
}

void writeTemperature() {
  float temp = sht21.GetTemperatureC();
  Serial.print(temp);
  Serial.print(" C");
  // Fill the BLE Shields 16 byte buffer with spaces!
  if (temp >;= 10.0) {
    Serial.print("         ");
  }
  else {
    Serial.print("          ");
  }
}

void writeHumidity() {
  float humidity = sht21.GetHumidity();
  Serial.print(humidity);
  Serial.print(" %");
  // Fill the BLE Shields 16 byte buffer with spaces!
  if (humidity >;= 10.0) {
    Serial.print("         ");
  }
  else {
    Serial.print("          ");
  }
}

You can use BLExplr v1.0.4 to test the thermometer. I added a feature to BLExplr where you can convert every hex value read from a characteristic directly into a human readable string. (Please stay tuned a little bit it will be available in the AppStore as soon as Apple reviewed v1.0.3 which is currently waiting for review. In the meantime you can use v1.0.2 to read the values, but you will see the hex values only). Just connect to the BLE Shield, send a ‘c’ using the “Write String” option of the TX characteristic, and then read the value of RX. You should read the current temperature. To read the humidity repeat the steps and write an ‘h’ over TX to the Shield and afterwards read RX again to see the humidity. The procedure is illustrated in the following screenshots:

Go to the TX characteristics:

Select Write String and type in ‘c’:

Press ‘Read” in order to see that the c was written:

And then switch to the RX characteristic and read the temperature value:

In order to read the humidity, go back to the TX characteristic and send an ‘h’ over the Shield to the Eleven.

Again you can check if the correct value is written to the shield’s GATT by pressing the ‘Read’ button:

And afterwards go back to the RX characteristic and read the value in order to see the humidity:

Currently it is 29.19 degrees celsius with a relative humidity of 55.78 % here in my home office… puuuuh….

Ok, lot of clicks to read the temperature and humidity, but I’m working on a small iOS sample application to make it simpler to read the values. Of course the app will be provided open source.

The above listed Arduino Sketch is available for download using this link: http://www.mkroll.mobi/BLE-Shield-Files/BLE_Temp_Humidity-1_0_0.zip and is listed in the BLE Shiels Files Text box on the blog’s main page as well.

12 Responses to BLE Shield Project: Thermometer

  1. Bob Kressin August 23, 2012 at 05:28 #

    Great project! Have you tried using BGScript to directly connect this particular temp/humidity sensor to the BLE112 using its I2C bus capabilities?

    • Michael Kroll August 23, 2012 at 06:13 #

      Hi,
      yes I tried, but not yet successful.
      But I’ll look into it again.
      Cheers,
      Michael.

      • Bob Kressin August 23, 2012 at 14:11 #

        Thanks for the reply. We have the BLE112′s ADCs working nicely with an analog ultrasonic sensor and the iPhone 4S. If I can figure out how to get an I2C device working, I’ll post the BGScript.

  2. Jose August 23, 2012 at 08:20 #

    Love it. Definitely something I want to try with the tōd too when that arrives ;) Just filled in my backer survey. Direct interface over I2C would be very useful for that.

    That said can’t wait for the shield either. Seems it’s going to be like the bus, you wait forever for one and the two come at once :)

  3. Azam Shahani September 1, 2012 at 21:45 #

    Hi Michael,
    Absolutely love your work! Please take my money and send a couple of BLE shields my way. What do I have to do to buy this shield from you?

    • Michael Kroll September 1, 2012 at 22:25 #

      Follow me on Twitter and wait for the Kickstarter Project announcement next week ;-)

  4. Muis March 7, 2013 at 12:05 #

    Why use an Arduino and external temp sensor, when you can just read out the BLE112′s internal temp sensor using: hardware_adc_read(14,3,0)?

    • Michael Kroll March 7, 2013 at 12:15 #

      If it is accurate enough, you can use the internal one. With e.g. Sensirion Sensors you can read temperature and humidity over I2C and I think they are indeed more accurate.

  5. J Krejcik March 31, 2013 at 19:10 #

    Hello,

    i would like to ask if is possible to connect module to LilyPad Arduino USB – ATmega32U4 Board.

    Thank you
    J Krejcik

    • Michael Kroll April 1, 2013 at 16:23 #

      Hello,
      which module would you like to connect to a Lilypad?
      The BLEtherm has an Arduino already.

      Cheers,
      Michael.

Trackbacks/Pingbacks

  1. Bluetooth-enabled personal thermometer with Arduino « freetronicsblog - August 28, 2012

    [...] more information and code, check out MIchael’s page here. And we’re on twitter and Google+, so follow us for news and [...]

Leave a Reply


*