The Stalker sensor platform : Part 3 - Getting time stamped temperature data

Previously - The Stalker sensor platform : Part 2 - Reading a digital temperature sensor

The simple temperature tracking sketch is nice, but mostly useless without a time and date stamp.

 1.   Prepare the Stalker

The Seeeduino Stalker provides a on board Real Time Clock circuit. Insert a coin cell into the CR2032 Coin cell holder and the RTC circuit will start working. The coin cell will keep the RTC circuit running even if the main power gets off.

2.  Get sensor data through I2C bus

Now let's check how we can get a date and time using the I2C Bus.

#include <Wire.h>

OneWire ds(8); // pin 8

void setup() {
Wire.begin();
Serial.begin(9600);
}

void loop() {
Serial.println(getDate());
}

String bcdToStr(int val) {
// Convert binary coded decimal to
// normal decimal numbers
if (val/16 == 0)
// if vlaue is &lt; 10 add a "0" return ("0" + String(val%16)); else return ( String((val/16*10) + (val%16)) ); } String getDate(){ // start sending data, 0x indicator hex, // 68 address specified by manufacturer Wire.beginTransmission(0x68); // set pointer to register 0 Wire.write(0); // stop sending data Wire.endTransmission(); // request of 7 bytes from address 68 Wire.requestFrom(0x68, 7); String second = bcdToStr(Wire.read()); String minute = bcdToStr(Wire.read()); //24 hour time String hour = bcdToStr(Wire.read() &amp; 0b111111); //0-6 -&gt; sunday - Saturday
String weekDay = bcdToStr(Wire.read());
String monthDay = bcdToStr(Wire.read());
String month = bcdToStr(Wire.read());
String year = bcdToStr(Wire.read());
return(monthDay + "." + month + "." + year + "/" + hour + ":" + minute + ":" + second + "/");

}

The Arduino IDE comes already with a I2C library, called Wire. That means we don't need to download a library, but we still have to add it:  Sketch --> Import Library --> Wire.

RTC_serial_monitor_timenotset

Serial Monitor

The Serial Monitor should show now date and time, but as you can see the clock is not set.

To set the clock through serial communication makes  this project way more complex, optionally the clock data can be "patched" at the receiver (e.g. computer).

With this sketch you can set the clock once (we recommend to override this sketch asap because it will set your defined time/date data on every start-up of the Stalker).

3.  Get time stamped temperature data

We can now implement the time and date stamp to our temperature measurement sketch.

Main tab:

#include <OneWire.h>
#include <Wire.h>

OneWire ds(8); // pin 8

void setup() {
Wire.begin();
Serial.begin(9600);
}

void loop() {
Serial.print("/");
Serial.print(temp());
Serial.println("/" + getDate());
}

RTC tab:

String bcdToStr(int val) {
// Convert binary coded decimal to normal decimal numbers
if (val/16 == 0)
//if vlaue is just one digit add a "0" in front
return ("0" + String(val%16));
else
return ( String((val/16*10) + (val%16)) );
}
String getDate(){
// start sending data, 0x indicator hex,
// 68 address specified by manufacturer
Wire.beginTransmission(0x68);
// set pointer to register 0
Wire.write(0);
// stop sending data
Wire.endTransmission();
// request of 7 bytes from address 68
Wire.requestFrom(0x68, 7);

String second = bcdToStr(Wire.read());
String minute = bcdToStr(Wire.read());
//24 hour time
String hour = bcdToStr(Wire.read() &amp; 0b111111);
//0-6 -&gt; sunday - Saturday
String weekDay = bcdToStr(Wire.read());
String monthDay = bcdToStr(Wire.read());
String month = bcdToStr(Wire.read());
String year = bcdToStr(Wire.read());
return(monthDay + "." + month + "." + year + "/" + hour + ":" + minute + ":" + second + "/");

}

Temperature tab:

float temp() {
byte i;
// lower and upper temperature data byte
byte data[2];
// 64bit address, every sensor has a unique address
byte addr[8];
// temp float
float celsius;

ds.search(addr);
ds.reset();
ds.select(addr);
ds.write(0x44,1);
// A/D to scratchpad, takes up to 750ms
delay(1000);
ds.reset();
// select sensor
ds.select(addr);
// command to read Scratchpad
ds.write(0xBE);
// get lower temperature byte
data[0] = ds.read();
// get upper temperature byte
data[1] = ds.read();
// shift upperbyte 8 digits left
// and or it with lowerbyte
int raw = (data[1] &lt;&lt; 8 ) | data[0];
// positive temp value
if (raw &lt; 0x7FF)
//conversion sensor data to Celsius
celsius = (float)raw / 16.0;
// negative temp value
else
//conversion sensor data to Celsius
celsius = (((float)raw-0xFFFF)/16);
return celsius;
}

Sketch split in three tabs (left) and Serial Monitor (right)

For a better overview you can split the code into tab windows as you can see on the screenshot.

Next:

The Stalker sensor platform : Part 4 - Transmitting data using Xbees