Showing posts with label API. Show all posts
Showing posts with label API. Show all posts

7/27/2011

Temperature-Sensor Network with RTC and LCD Part V

Wow. More than a month since my last entry. Time is flying by. About 50 days since I started the project and my rechargeable batteries (I had to change the first three after a few days, because one cell was dead) still work in my outside sensor. My RTC is wrong by about 1.6 seconds per day and the deviation is very constant so this error can be erased by software. To compare the temperatures I used a Clock with inside/ outside temperatures I bought some time ago. The temperatures were accurate and so I finally assembled a device which I can buy for about 20 Euros in any electronics store. You might ask why so much effort for such little outcome? Wait for adding an ethernet shield, more Xbees, door bell/ mail alert, light and rain indicators and so on. And everything is broadcasted in the network!

LCD display with date, time, inside- and outside-temperature
For the last part there is unfortunately a lot of code to sift through. I made as many comments as possible - if there are any questions just put it in a comment on the page.

void loop() {

  DateTime now = RTC.now(); // new RTC object named "now"
 
  // get time and date from the RTC DS1307

  int hour = now.hour();
  int minute = now.minute();
  int second = now.second();

  // print time and date on the first line of the LCD
  // and make some adjustments regarding leading zeros
  // time display will change every second

    lcd.setCursor(0, 0);

  if (hour < 10) {
    lcd.print("0");
    lcd.print(hour,DEC);
  }
  else {
    lcd.print(hour,DEC);
  }
  lcd.print(":");
  if (minute < 10){
    lcd.print("0");
    lcd.print(now.minute(), DEC);
  }
  else {
    lcd.print(now.minute(), DEC);
  }
  lcd.print(":");
  if (second < 10){
    lcd.print("0");
    lcd.print(now.second(), DEC);
  }
  else{
    lcd.print(second, DEC);
  }

  int month = now.month();

  lcd.print("  ");
  lcd.print(now.day(),DEC);
  lcd.print(".");
  if (month < 10) {
    lcd.print("0");
    lcd.print(now.month(), DEC);
    lcd.print(".");
  }
  else {
    lcd.print(now.month(), DEC);
    lcd.print(".");
  }

  // wait one second to show off as a "real" clock ;)
  delay (1000);

  // here we are reading the serial port, that means everything that comes from the XBee Coordinator
  // the packet size (I/O Data Sample) is expected to be at least 23 bytes
  if (Serial.available() >= 23) {

    // look for the start byte, in this case I chose part of XBee end device address for easy parsing
    // this is a quick and dirty solution and you are welcome to make a better code for this
    // and of course you have to adjust the code for your XBee end device
    if (Serial.read() == 0x6F) {

      // blink debug LED to indicate when data is received
      digitalWrite(debugLED, HIGH);
      delay(50);
      digitalWrite(debugLED, LOW);

      // read the variables that we're not using out of the buffer
      for (int i = 0; i<9; i++) {
        byte discard = Serial.read();
      }
      // read the two bytes for the analog value
      int analogHigh = Serial.read();
      int analogLow = Serial.read();

      // combine high and low analog value, because it's in two bytes
      analogValue =  analogLow + (analogHigh * 256);

      // calculate temperature from TMP 36
      // 1.2V/ 1024 (10 bit ADC), 0°C is 500 mV, 10mV/ °C temperature coefficient
      float temperature = (((1.171875*analogValue)-500)/10);

      // we take 10 analog temperature samples and calculate the average

      temperature_sample=0; // reset temperature_sample value to zero

      for (int i=0; i<10; i++) {
        // calculate the temperature from the AD22100K
        // 5V/ 1024 (10 bit ADC), 1.375 V at =0°C, 22.5mV temperature coefficient per °C
        temperature_read = (analogRead(0)*0.0048828125 - 1.375) / 0.0225;
        temperature_sample = temperature_sample + temperature_read;
      }
      temperatureInside = (temperature_sample/10);

      // print inside and outside temperature in the second row of the LCD
      // display will only change when data is received

      lcd.clear();

      lcd.setCursor(0, 1);
      lcd.print("IN ");
      lcd.print(temperatureInside,1);
      lcd.print(" OUT ");
      lcd.print(temperature,1);

    }
  }
}


There are still some things to consider and to change in the future. There is for example no adjustment for negative temperatures in the display, the parsing with the XBee address could be improved, the back light could be adjusted by software.

6/13/2011

Temperature-Sensor Network with RTC and LCD Part II

XBee on perfboard
Part II of the temperature-sensor network starts with the Base Station.
The XBee Series 2 sitting on the base station connected to the Arduino UNO is configured in Coordinator API mode. I use the following settings (all other settings are the default Coordinator API setttings of the firmware) which are programmed via the X-CTU software from digi:
  •  AT ID 2001 (PAN ID) (see #1)
  •  AT DH 0 (see #2)
  •  AT DL 0 (see #2)
  •  AT AP 1 (see #3)
  •  AT SP AF0 (see #4)
  1. The extended PAN ID for my network (AT ID = 2001) is 2001 (I hope it will be no "Space Odyssey" though)  - but you can take any 64-bit value up to 0xFFFFFFFFFFFFFFFF or even leave it to 0 where the Coordinator would select a random PAN ID. Be sure to set the same ID to the other devices which should join the network.
  2. The Destination High and Low Address is both set to 0 (AT DH = 0, AT DL =  0) which defines the coordinator.
  3. API mode (AT AP) is set to enable (=1) because we want to receive and parse packets instead of just routing the traffic via the serial port like you would do in transparent mode.
  4. Perfboard back side
  5. The last setting is for the sleep period according to longest sleep period on a/ the End Device(s) (AT SP = AF0). This is to take care that the Coordinator (or any Router which is configured to this length of sleep period) will buffer the messages for the End Device long enough to get them transferred when the End Device awakens (no snooze allowed!). The sleep period is set to 28 seconds on my End Device because the sleep period is multiplied by 10 the value is 2800ms in Decimal or AF0 in Hex.


Unfortunately and for the sake of formality I have to say that my wiring seen in the following picture could be of risk to damage your XBee! As the Arduino UNO runs on 5 volts and the level on the TX pin (digital pin 1) is about 5 volts when HIGH, the DIN pin of the XBee (pin 3) could be exposed to more power than allowed. So I give no guarantee that your XBee is as robust as mine - take care of it and use a level shifter in any case!
XBee with level shifter



Update: For the last days I tested a very simple level shifter, which works for me. I have very low traffic though and there may be problems with higher data transmit rates, but for the time being I just added two resistors (22kohm and 33kohm) as a simple voltage divider and now have maximum roughly 3 volts on DIN of the XBee. So that may be a quick workaround.



Part III will continue with the base station.

6/12/2011

Offroad: XBee Coordinator API + Arduino UNO + RTC +Temperature Sensor + LCD-Display AND Remote XBee (Sleep Mode with low power consumption) + Temperature Sensor - PART I

What a long headline you might think - but this actually describes very well my longer absence to the blog.

I'm sorry to be again off-road to the book, but before starting with the last Connect-Port experiment in Chapter 7 I still have some things on my to-do list that developed from the preceding chapters of the book.

With the knowledge of all the things learned until now I was eager to implement my first own little project to get a simple temperature sensor network (one outside/ one inside temperature) with a real time clock (RTC) and displaying everything on a LCD-display working.
Base Station (breadboard version)

It's consisiting of:


Base Station:
  • XBee in Coordinator API mode
  • connected to an Arduino UNO
  • Temperature sensor connected to Arduino (AD22100)
  • LCD-Display (16*2) connected to Arduino
  • Real-Time-Clock (RTC DS1307) connected to Arduino 
 Remote Sensor:
Remote Sensor
  • XBee End Device Sleep Mode
  • TMP36 temperature sensor connected
  • Voltage regulator MCP1700 (with very low quiescent current <2µA)
  • running on three AA rechargeable batteries



Part II will be on describing the Base Station.