Arduino + XBee Wireless Sensor Network

Arduino + XBee Wireless Sensor Network

This project was built as a final project for my CPE400(Computer Networks) class

Remote Node:
Atmega328P
XBee Series 1 modules
TMP36 temperature sensors
misc resistors, caps, etc

Coordinator:
XBee Series 1 module
XBee USB adapter

PC Software:
Custom processing application
Data Logging

Schematic

Remote Nodes

Data Logging

YouTube Video

Arduino Code:

//Dino Tinitigan
//Sleeping Node
//Sleep code derived form sleep code from arduino website
//Sends temperature data to coordinator
//Goes back to sleep after sending data

#include <avr/sleep.h>


int wakePin = 2;  // pin used for waking up
int sleepStatus = 0;  // variable to store a request for sleep
int temp1;
int ID = 3;  //ID of node, change this!!!            
int ledPin = 3;
double t1;
int temp2;
double vref = 5.04;

void wakeUpNow()        // here the interrupt is handled after wakeup
{
  // execute code here after wake-up before returning to the loop() function
  // timers and code using timers (serial.print and more...) will not work here.
  // we don't really need to execute any special functions here, since we
  // just want the thing to wake up
}

void setup()
{
  pinMode(ledPin, OUTPUT);
  
  pinMode(wakePin, INPUT);

  Serial.begin(9600);

  attachInterrupt(0, wakeUpNow, LOW); // use interrupt 0 (pin 2) and run function
                                      // wakeUpNow when pin 2 gets LOW 
}

void sleepNow()         // here we put the arduino to sleep
{
    //Serial.println("Sleeping");
    digitalWrite(ledPin, LOW); 
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);   // sleep mode is set here

    sleep_enable();          // enables the sleep bit in the mcucr register
                             // so sleep is possible. just a safety pin 

    attachInterrupt(0,wakeUpNow, LOW); // use interrupt 0 (pin 2) and run function
                                       // wakeUpNow when pin 2 gets LOW 

    sleep_mode();            // here the device is actually put to sleep!!
                             // THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP

    sleep_disable();         // first thing after waking from sleep:
                             // disable sleep...
    detachInterrupt(0);      // disables interrupt 0 on pin 2 so the 
                             // wakeUpNow code will not be executed 
                             // during normal running time.

}

void loop()
{
  digitalWrite(ledPin, HIGH); //turn led on while awake
  // display information about the counter
  temp1 = getTemp();
  byte checksum = ID + temp1;
  
  Serial.write(255); //header
  Serial.write(ID); //ID BYTE
  Serial.write(temp1); //DATA BYTE/S
  Serial.write(checksum);
  
  //Serial.print("I am ");
  //Serial.println(ID);
  delay(3000);
  sleepNow();  
}

int getTemp()
{
  t1 = analogRead(0);
  t1 = t1*1000; //millivolts factor
  t1 = t1/1024; //10-bit range
  t1 = t1*vref; //vref
  temp2 = (t1-500)/10;
  return temp2;
}

Processing Code:

//Dino Tinitigan 
//CPE 400
//Coordinator Application

import processing.serial.*;

int incoming;
int nodeID;
int value;
int numMembers = 5;
int[] members = {2,3,4,5,6};
int[] values = new int[5];
int checksum;
int t = 5;
int ID;

Serial myPort;

//----File IO---------
//use hour(), minute(), seconds();
//ude date()
PrintWriter output;

void setup()
{
  myPort = new Serial(this, Serial.list()[0], 9600); 
  println(Serial.list());
  
  textSize(30);
  size(800, 600);
  background(80, 120, 255);
  
  //-------------------setup File IO-------------------
  output = createWriter("log.csv");
  
  output.print("Date and Time," + "millis since start,");
  for(int i = 0; i < numMembers; i++)
  {
    output.print("Node " + i + ",");
  }
  output.println();
  //-------------------setup File IO-------------------
}

void draw()
{
  if(myPort.available()>0) //data is on serial port
  {
    incoming = myPort.read();
    println("Data Received");
    while((incoming != 255) && myPort.available()>0) //look for header if their is data on serial port
    {
      incoming = myPort.read();
    }
    
    if(incoming == 255) //header found
    {
      //header received
      println("----------header received-----------");
      nodeID = myPort.read(); //read ID
      value = myPort.read();  //read data
      println("Node " + nodeID);
      println(value + " C");
      updateValue(nodeID, value);
    }
  }
  //-------------------------------------------------------------------------------------------------------
}

void updateValue(int ID, int val)
{
  switch(ID)
  {
    case 2:
    {
      int check = myPort.read();
      checksum = ID + val;
      if(checksum >=256)
      {
        checksum = checksum - 256;
      }
      if(checksum == check)
      {
        //draw box
        fill(0,255,0);
        rect(10,10,200, 100);
        fill(0);
        text("Node: " + ID, 30, 40);
        text(val + " C", 30, 100);
        output.print(month() + "/" + day() + "/" + year() + "/" + hour() + ":" + minute() + ":" + second() + "," + millis());
        output.println("," + val);
      }
      break;
    }
    case 3:
    {
      int check = myPort.read();
      checksum = ID + val;
      if(checksum >=256)
      {
        checksum = checksum - 256;
      }
      if(checksum == check)
      {
        fill(0,255,0);
        rect(10,200,200, 100);
        fill(0);
        text("Node: " + ID, 30, 225);
        text(val + " C", 30, 285);
        output.print(month() + "/" + day() + "/" + year() + "/" + hour() + ":" + minute() + ":" + second() + "," + millis());
        output.println(",," + val);
      }
      break;
    }
    case 4:
    {
      int check = myPort.read();
      checksum = ID + val;
      if(checksum >=256)
      {
        checksum = checksum - 256;
      }
      if(checksum == check)
      {
        //draw box
        fill(0,255,0);
        rect(200,10,200, 100);
        fill(0);
        text("Node: " + ID, 220, 40);
        text(val + " C", 220, 100);
        output.print(month() + "/" + day() + "/" + year() + "/" + hour() + ":" + minute() + ":" + second() + "," + millis());
        output.println(",,," + val);
      }
      break;
    }
    case 5:
    {
      int check = myPort.read();
      checksum = ID + val;
      if(checksum >=256)
      {
        checksum = checksum - 256;
      }
      if(checksum == check)
      {
        fill(0,255,0);
        rect(200,200,200, 100);
        fill(0);
        text("Node: " + ID, 220, 225);
        text(val + " C", 220, 285);
        output.print(month() + "/" + day() + "/" + year() + "/" + hour() + ":" + minute() + ":" + second() + "," + millis());
        output.println(",,,," + val);
      }
      break;
    }
    case 6:
    {
      int check = myPort.read();
      checksum = ID + val;
      if(checksum >=256)
      {
        checksum = checksum - 256;
      }
      if(checksum == check)
      {
        //draw box
        fill(0,255,0);
        rect(490,10,200, 100);
        fill(0);
        text("Node: " + ID, 510, 40);
        text(val + " C", 510, 100);
        output.print(month() + "/" + day() + "/" + year() + "/" + hour() + ":" + minute() + ":" + second() + "," + millis());
        output.println(",,,,," + val);
      }
      break;
    }
    default:
    {
    }
  }
  if(key=='x')
  {
    output.close();
    exit();
  }
}

void keyPressed()
{
  println("key pressed");
  if(key=='x')
  {
    exit();
  }
}

void stop()
{
  output.close();
  super.stop();
}