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: stack.init(); } void loop() { // put your main code here, to run repeatedly: stack.receive(); if (sendTimer.poll(2000)){ PayloadBuffer buffer; buffer.print(123,DEC); stack.addUdpPacket(0, 255, 2, CMD_BYTE, buffer.payload()); } stack.process(); }
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("http://matthew.sgcs.usf.edu/mcdportal/public/xmlrpc.php?api_key=dfgdfgdgdgd"); 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; aMuch easier to use now!if(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)); //fixedFont); 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: //println(Serial.list()); String portName = Serial.list()[1]; myPort = new Serial(this, portName, 115200); } boolean arduinoBoot = false; int count = 0; long sendTimer; void draw() { if(arduinoBoot == false){ delay(2500); 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; sendTimer=millis(); count++; } stack.process(); 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("received",packet.received); send.put("packet", packetBase64); println("Server Reply: "+homeNetXmlrpcClient.execute("HomeNet.addPacket", send)); } void serialEvent(Serial port) { stack.buildSerialPacket(port); }
No comments:
Post a Comment