{"id":148,"date":"2008-02-15T15:12:10","date_gmt":"2008-02-15T20:12:10","guid":{"rendered":"http:\/\/www.tigoe.net\/pcomp\/code\/category\/code\/processing\/148"},"modified":"2008-02-15T16:11:39","modified_gmt":"2008-02-15T21:11:39","slug":"xbee-library-graphing-application","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/Processing\/148\/","title":{"rendered":"XBee Library graphing application"},"content":{"rendered":"<p>Here&#8217;s a simple program that uses Rob Faludi and Dan Shiffman&#8217;s <a href=\"http:\/\/www.faludi.com\/code\/xbee-api-library-for-processing\/\">XBee library for processing<\/a> to read three analog sensors from a remote <a href=\"http:\/\/www.digi.com\/products\/wireless\/point-multipoint\/xbee-series1-module.jsp\">XBee<\/a> radio and graph it. For this application, you need two XBee series 1 radios. One is attached to the serial port of the computer, and the other is remote, broadcasting three analog values.<\/p>\n<p><!--more--><\/p>\n<p>The settings for the radios are as follows:<\/p>\n<p>Base station radio:<\/p>\n<ul>\n<li>Personal Area Network (PAN) ID: AAAA<\/li>\n<li>Source address: ATMY 0<\/li>\n<li>Baud rate 115200 bits per second: ATBD 7<\/li>\n<\/ul>\n<p>Remote radio:<\/p>\n<ul>\n<li>Personal Area Network (PAN) ID: AAAA<\/li>\n<li>Source address: ATMY 1<\/li>\n<li>Destination address: ATDL 0<\/li>\n<li>Analog inputs activated:<\/li>\n<li style=\"list-style-type: none; list-style-position: initial; list-style-image: initial;\">\n<ul>\n<li>ATD0 2<\/li>\n<li>ATD1 2<\/li>\n<li>ATD2 2<\/li>\n<\/ul>\n<\/li>\n<li>Sample rate 80 milliseconds: ATIR 50<\/li>\n<li>1 sample per transmission: ATIT 1<\/li>\n<li>Baud rate 115200 bits per second: ATBD 7<\/li>\n<li style=\"list-style: none\"><\/li>\n<\/ul>\n<p>And here&#8217;s the code:<\/p>\n<pre>\n\/*\n  XBee sensor data graphing sketch\n \n This sketch takes data in from an XBee radio and graphs the analog \n input values.  It's for XBee series 1 radios\n \n Based on code from  Rob Faludi and Daniel Shiffman\n http:\/\/www.faludi.com\n http:\/\/www.shiffman.net \n \n created 2 Feb 2008\n by Tom Igoe  \n *\/\n\n\/\/import the xbee and serial libraries:\nimport xbee.*;\nimport processing.serial.*;\n\n\n\/\/ Your Serial Port\nSerial port;\n\/\/ Your XBee Reader object\nXBeeReader xbee;\n\n\/\/ set up a font for displaying text:\nPFont myFont;\nint fontSize = 12;\n\n\/\/ set up Xbee parameters:\nint rssi = 0;                       \/\/ received signal strength\nint address = 0;                    \/\/ sender's address\nint[] analog = new int[6];          \/\/ values from the analog I\/O pins\nint[] previousValue = new int[6];   \/\/ previous values from the pins\n\nint numSensors = 3;                 \/\/ number of sensors you actually plan to graph\n\nint xpos = 0;                       \/\/ graph x position\nint oldYPos = 0;                    \/\/ graph y position\nint yPos = 0;                       \/\/ previous y position\n\nint hitThreshold = 250;             \/\/ peak value to check hits against\nint hits = 0;                       \/\/ hit count\nint lastHitValue = 0;               \/\/ value of last hit\n\nboolean newData = false;            \/\/whether or not you got a new reading \n\nvoid setup() {\n  size(400, 300);                \/\/ window size\n  frameRate(60);                 \/\/ frame rate\n  smooth();                      \/\/ clean the jagged edges\n\n  \/\/ create a font with the third font available to the system:\n  myFont = createFont(PFont.list()[2], fontSize);\n  textFont(myFont);\n\n  \/\/ you might need a list of the serial ports to find yours:\n  println(\"Available serial ports:\");\n  println(Serial.list());\n  \/\/ open the first serial port.  Change this to match \n  \/\/ your serial port number in the list:\n  port = new Serial(this, Serial.list()[0], 115200);\n  \/\/ initialize the xbee object:\n  xbee = new XBeeReader(this,port);\n  xbee.startXBee();\n\n  \/\/ black screen:\n  background(0);\n}\n\npublic void draw() {\n  \/\/ only draw when you have a new reading:\n  if (newData == true) {\n    \/\/ iterate over the number of sensors:\n    for (int thisSensor = 0; thisSensor &lt; numSensors; thisSensor++) {\n      \/\/ if you have new data and it's valid (&gt;0), graph it:\n      if (analog[thisSensor] &gt; -1) {\n        \/\/ map the sensor values to the screen size for graphing:\n        yPos = int(map(analog[thisSensor], 0, 1023, 0, height));\n        oldYPos = int(map(previousValue[thisSensor], 0, 1023, 0, height));\n\n        \/\/ if we get a big change, increment the hit counter:\n        if (abs(yPos - oldYPos) &gt;= hitThreshold) {\n          hits++;\n          \/\/ note the magnitude of the hit:\n          lastHitValue = abs(yPos - oldYPos);\n        }\n      }\n      \/\/ draw the graph axis: \n      stroke(255);\n      int xAxis = height\/2;\n      line(0, xAxis, width, xAxis);\n\n      \/\/ use a different the graphing color for each axis:\n      switch (thisSensor) {\n      case 0:\n        stroke(255,0,0);\n        break;\n      case 1:\n        stroke(0,255,0);\n        break;\n      case 2: \n        stroke(0,0,255);\n        break;\n      }\n      \/\/ draw the graph line from last value to current:\n      line(xpos, oldYPos, xpos+1,yPos);\n      newData = false;\n\n      \/\/ write the text at the top\n      noStroke();\n      fill(0);\n      rect(0, 0, 300, 100);\n      fill(255);\n      text(\"From: \" + hex(address), 10, 20);\n      text (\"RSSI: \" + rssi + \" dBm\", 10, 40);\n      text(\"X: \" + analog[0] + \"  Y: \" + analog[1] + \"  Z: \" + analog[2], 10, 60);\n      text(\"Last hit value: \" + lastHitValue,  10, 80);\n    }\n    \/\/ if you're at the right of the screen, \n    \/\/ clear and go back to the left:\n    if (xpos &gt;= width) {\n      xpos = 0;\n      background(0);\n    } \n    else {\n      xpos++;\n    }\n  }\n}\n\n\n\n\/*  \n  This function works just like a \"serialEvent()\" and is \n  called for you when data is available to be read from your XBee radio.\n*\/\n  \npublic void xBeeEvent(XBeeReader xbee) {\n  \/\/ Grab a frame of data\n  XBeeDataFrame data = xbee.getXBeeReading();\n\n  \/\/ This version of the library only works with IOPackets\n  \/\/ For ZNet radios, you would say XBeeDataFrame.ZNET_IOPACKET\n  if (data.getApiID() == XBeeDataFrame.SERIES1_IOPACKET) {\n\n    \/\/ Get the transmitter address\n    address = data.getAddress16();\n\n    \/\/ Get the RSSI reading in dBM \n    rssi = data.getRSSI();\n\n    \/\/ save previous state of analog values:\n    arraycopy(analog, previousValue);\n    \n    \/\/ get the current values\n   \/\/  (-1 indicates channel is not configured):\n    analog = data.getAnalog(); \n    \n      \/\/ trip the new data flag so the draw() loop will graph:\n      newData = true;    \n  } \n  else {\n    \/\/ it's not I\/O data:\n    println(\"Not I\/O data: \" + data.getApiID());\n  }\n}\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a simple program that uses Rob Faludi and Dan Shiffman&#8217;s XBee library for processing to read three analog sensors from a remote XBee radio and graph it. For this application, you need two XBee series 1 radios. One is attached to the serial port of the computer, and the other is remote, broadcasting three &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.tigoe.com\/pcomp\/code\/Processing\/148\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;XBee Library graphing application&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,21],"tags":[],"class_list":["post-148","post","type-post","status-publish","format-standard","hentry","category-Processing","category-XBee"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/comments?post=148"}],"version-history":[{"count":0,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/148\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}