Thursday, November 18, 2010

More Code

So the code finally reached a tipping it's time to package the code up into a library. I'm calling it HomeNet because it describes its perfectly it's a new home net work and amazingly enough, there isn't anything going by the name yet.

Compared to the long mess of code posted before, Moving most of the code to a library really simplified things:

#include <ports.h>
#include <rf12.h>
#include <homenet.h>

HomeNet stack(0x04);//start stack with a node id
MilliTimer sendTimer;

void setup() {
  // put your setup code here, to run once:


void loop() {
  // put your main code here, to run repeatedly: 
  if (sendTimer.poll(2000)){
    PayloadBuffer buffer;
    stack.addUdpPacket(0, 255, 2, CMD_BYTE, buffer.payload());

and the processing sketch using the library
import processing.serial.*;
import xmlrpclib.*;
import org.apache.commons.codec.binary.Base64; //part of the xmlrpclib

XmlrpcClient homeNetXmlrpcClient = new XmlrpcClient("");
XmlrpcServer homeNetXmlrpcServer = new XmlrpcServer(8081);

final int  NODE_ID = 255;
final int PACKET_TIMEOUT = 150;
PacketStack stack = new PacketStack(NODE_ID);

Serial myPort;  // The serial port

XMLElement xmlConfig;

XMLElement commands[];
XMLElement devices[];

String findXMLid(XMLElement[] xml , int value, String rAttribute){
  String rValue = ""; 
  for(int a = 0; aif(unhex(commands[a].getAttribute("id")) == value){
      return commands[a].getAttribute(rAttribute);
  return rValue;

void setup() {
  size(600, 400);  // Stage size
  //noStroke();      // No border on the next thing drawn
  homeNetXmlrpcServer.add("testserver", this); //setup server
  println("HomeNet XML-RPC Server started at " + homeNetXmlrpcServer.ip()+":8081/RPC2"); // Note that /RPC2 is hard-coded to URI in the Apache Library.
  textFont(createFont("Consolas", 14));
  xmlConfig = new XMLElement(this, "config.txt");
  devices = xmlConfig.getChildren("devices/device");
  commands = xmlConfig.getChildren("commands/command");
  //load the above as hash table from my sevrer.
  // Print a list of the serial ports, for debugging purposes:
  String portName = Serial.list()[1];
  myPort = new Serial(this, portName, 115200);

boolean arduinoBoot = false;
int count = 0;
long sendTimer;
void draw() {
  if(arduinoBoot == false){
    sendTimer = millis();
    arduinoBoot = true;;
  if((millis() - sendTimer) > 2000){
    //DataPacket packet = build_udp_packet(NODE_ID,0,3,2,CMD_STRING,str_to_payload("Hello "+count));
    //packet.status = STATUS_READY;

  stack.tempSerialCheck(myPort); //this is temp until I add support for multiple serial ports, and pass a list of ports to the packetStack
void packetEvent(DataPacket packet){
  packet.status = STATUS_CLEAR;
  Hashtable send = new Hashtable();
  String packetBase64 = new String(Base64.encodeBase64(packet.payload));
  send.put("packet", packetBase64);
  println("Server Reply: "+homeNetXmlrpcClient.execute("HomeNet.addPacket", send));

void serialEvent(Serial port) {
Much easier to use now!

No comments:

Post a Comment