{"id":8,"date":"2007-08-06T23:53:41","date_gmt":"2007-08-07T04:53:41","guid":{"rendered":"http:\/\/www.tigoe.com\/pcomp\/code2\/?p=8"},"modified":"2008-01-21T21:31:06","modified_gmt":"2008-01-22T02:31:06","slug":"xbee-radio-received-signal-strength-graphing-program","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/Processing\/8\/","title":{"rendered":"XBee Radio Received Signal Strength Graphing Program"},"content":{"rendered":"<p>This Processing program takes a string of values in the serial port. It assumes the string is the API string from a <a href=\"http:\/\/maxstream.net\/\">Maxstream<\/a> XBee radio. It parses the string and graphs the signal strength. See the <a href=\"http:\/\/maxstream.net\/products\/xbee\/manual_xb_oem-rf-modules_802.15.4.pdf\">XBee product manual<\/a> for more info on the protocol.<\/p>\n<p>The XBee radio in this program is attached to the computer&#8217;s serial port using an FTDI USB-to-serial module like <a href=\"http:\/\/www.sparkfun.com\/commerce\/product_info.php?products_id=718\">this one from Spark Fun<\/a>.<\/p>\n<p><!-- technorati tags start --><\/p>\n<p style=\"text-align:right;font-size:10px;\">Technorati Tags: <a href=\"http:\/\/www.technorati.com\/tag\/networked objects\" rel=\"tag\">networked objects<\/a>, <a href=\"http:\/\/www.technorati.com\/tag\/networks\" rel=\"tag\">networks<\/a>, <a href=\"http:\/\/www.technorati.com\/tag\/XBee\" rel=\"tag\">XBee<\/a><\/p>\n<p><!-- technorati tags end --><br \/>\n<!--more--><\/p>\n<pre>\/*  Xbee Signal Strength Reader\n  Language: Processing\n  \n Reads a packet from an Xbee radio and parses it.  The packet\n should be 22 bytes long. It should be made up of the following:\n byte 1:     0x7E, the start byte value\n byte 2-3:   packet size, a 2-byte value  (not used here)\n byte 4:     API identifier value, a code that says what this response is (not used here)\n byte 5-6:   Sender's address\n byte 7:     RSSI, Received Signal Strength Indicator (not used here)\n byte 8:     Broadcast options (not used here)\n byte 9:     Number of samples to follow\n byte 10-11: Active channels indicator (not used here)\n byte 12-21: 5 10-bit values, each ADC samples from the sender \n \n Created 3 Mar. 2007\n by Tom Igoe\n *\/\nimport processing.serial.*;\n\nSerial xbee;                    \/\/ input serial port from the Xbee Radio\nint[] packet = new int[22];     \/\/ with 5 samples, the Xbee packet is 22 bytes long\nint byteCounter;                \/\/ keeps track of where you are in the packet\nint rssi = 0;                   \/\/ received signal strength\nint address = 0;                \/\/ the sending Xbee's address\n\nSerial myPort;                  \/\/ The serial port\n\nint fontSize = 18;              \/\/ size of the text on the screen\nint lastReading = 0;               \/\/ value of the previous incoming byte\n\nvoid setup () {\n  size(400, 300);        \/\/ window size\n\n  \/\/ create a font with the second font available to the system:\n  PFont myFont = createFont(PFont.list()[2], fontSize);\n  textFont(myFont);\n\n  \/\/ get a list of the serial ports:\n  println(Serial.list());   \n  \/\/ open the serial port attached to your Xbee radio:\n  xbee = new Serial(this, Serial.list()[0], 9600);\n}\n\nvoid draw() {\n  \/\/ if you have new data and it's valid (&gt;0), graph it:\n  if ((rssi &gt; 0 ) &#38;&#38; (rssi != lastReading)) {\n    \/\/ set the background:\n    background(0);\n    \/\/ set the bar height and width:\n    \/\/ rssi should range from 92 to 0:\n    int rectHeight = 92 - rssi;\n    int rectWidth = 50;\n    \/\/ draw the rect:\n    stroke(23, 127, 255);\n    fill (23, 127, 255);\n    rect(width\/2 - rectWidth, height-rectHeight, rectWidth, height);\n    \/\/ write the number:\n    text(\"Xbee Radio Signal Strength test\", 10, 20);\n    text(\"From: \" + hex(address), 10, 40);\n\n    text (\"RSSI: -\" + rssi + \" dB\", 10, 60);\n    \/\/ save the current byte for next read:\n    lastReading = rssi;\n  }\n}\n\nvoid serialEvent(Serial xbee) {\n  \/\/ read a byte from the port:\n  int thisByte = xbee.read();\n  \/\/ if the byte = 0x7E, the value of a start byte, you have a new packet:\n  if (thisByte == 0x7E) {   \/\/ start byte\n    \/\/ parse the previous packet if there's data:\n    if (packet[2] &gt; 0) {\n      parseData(packet);\n    }\n    \/\/ reset the byte counter:\n    byteCounter = 0;        \n  }\n  \/\/ put the current byte into the packet at the current position:\n  packet[byteCounter] = thisByte;\n  \/\/  increment the byte counter:\n  byteCounter++;\n}\n\n\/* \n Once you've got a packet, you need to extract the useful data. \n This method gets the address of the sender and RSSI.\n *\/\nvoid parseData(int[] thisPacket) {\n\n  \/\/ read the address. It's a two-byte value, so you\n  \/\/ add the two bytes as follows:\n  address = thisPacket[5] + thisPacket[4] * 256;\n  \/\/ get RSSI:\n  rssi = thisPacket[6];\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This Processing program takes a string of values in the serial port.  It assumes the string is the API string from a Maxstream XBee radio.<\/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-8","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\/8","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=8"}],"version-history":[{"count":0,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/8\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=8"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=8"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=8"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}