Code for Weather Station prototype


            

Update March 7th, 2023: This is the first article published on this website. Exactly twelve years ago !

I took some steps in learning the capabilities of Arduino Platforms. Below is the code of my Bluetooth-enabled temperature reader with OS X integration.

#define TMP102_I2C_ADDRESS 72

byte firstbyte, secondbyte; //bytes
int val; //integer for temperature reading

#define btRxPin 4 //definire pin rx bluetooth
#define btTxPin 5 //definire pin tx bluetooth

NewSoftSerial BlueTooth(btRxPin,btTxPin); //serial object

void setup()
{
  Serial.begin(115200);     //serial baud
  BlueTooth.begin(115200);  //Bluetooth baud
  Wire.begin();

  pinMode(btRxPin, INPUT);  //digital input
  pinMode(btTxPin, OUTPUT); //digital output

  /* announce Bluetooth connection success */
  BlueTooth.println("Bluetooth succesfully connected...");
  delay(1000);
}

void getTemp102() {
    Wire.beginTransmission(TMP102_I2C_ADDRESS);
    Wire.send(0x00);
    Wire.endTransmission();
    Wire.requestFrom(TMP102_I2C_ADDRESS, 2);
    Wire.endTransmission();

    firstbyte = Wire.receive(); /* full */
    secondbyte = Wire.receive(); /* decimal */

    float convertedTemp;
    float correctedTemp;

    val = ((firstbyte) << 4);  //concatenate values-step 1
    val |= (secondbyte >> 4);  //concatenate values-step 2

    Serial.println((float)val * 0.0625); //check temp reading at terminal

    convertedTemp = float(val) * 0.0625; //convert to float
    correctedTemp = convertedTemp - 5; //correct overreading
  /* check first byte at serial */
    Serial.print("first byte: ");
    Serial.print("t");
    Serial.println(firstbyte,BIN);
   /* check second byte at serial */
    Serial.print("second byte: ");
    Serial.print("t");
    Serial.println(secondbyte,BIN);
  /* check the string at serial */
    Serial.print("concatenated byte is ");
    Serial.print("t");
    Serial.println(val,BIN);
  /* check converted temperature */
    Serial.print("Converted temp is ");
    Serial.print("t");
    Serial.println(convertedTemp);
  /* check corrected temperature */
    Serial.print("Corrected temp is ");
    Serial.print("t");
    Serial.println(correctedTemp);

    BlueTooth.println(correctedTemp); //send to Bluetooth
}

/* execute the loop */
void loop() {
  getTemp102();
  delay(5000);
}

/* end */

See also this post on creating additional brushes for SyntaxHighlighter.

Previous and next posts

Globals and Singletons

My preferred approach to creation and maintaing global variables/ constants is by singletons and singleton instance[s]. Not only is an elegant method that adds compliance to a very important Cocoa design pattern, but also provides you a lot of flexibility.

Comments are closed.