Added WaspMote --> Spacebrew Bridge class

This commit is contained in:
dviid 2013-12-01 23:55:00 +01:00
parent e02f8a3d7e
commit cd7fa5c908
10 changed files with 240 additions and 60 deletions

View File

@ -1,7 +1,5 @@
CRITICAL-INFRASTRUCTURE CRITICAL-INFRASTRUCTURE
======================= =======================
[Jamie Allen](http://heaviside.net) & [David Gauthier](http://gauthiier.info)
CRITICAL INFRASTRUCTURE / Tranmsmediale 2014
Jamie Allen & David Gauthier
CRITICAL INFRASTRUCTURE unearths the undergiring of media technics, as a media archeology of the present — understanding the post-digital as not just horizontal and temporal but material, stratal, and vertical — infra-digital and infra-technical. CRITICAL INFRASTRUCTURE unearths the undergiring of media technics, as a media archeology of the present — understanding the post-digital as not just horizontal and temporal but material, stratal, and vertical — infra-digital and infra-technical.

View File

@ -0,0 +1,125 @@
//
// Bridge.cpp
// waspmote_spacebrew_bridge
//
// Created by dviid on 12/1/13.
//
//
#include "Bridge.h"
#define wasp_signature "usbserial"
#define wasp_baud 115200
bool Bridge::setup_spacebrew() {
_sb_connection.connect(SPACEBREW_SERVER, "CRITTICAL INFRASTRUCTURE"); // blocking ??
sleep(5);
return _sb_connection.isConnected();
}
bool Bridge::setup_serial() {
//_serial.enumerateDevices();
vector <ofSerialDeviceInfo> list = _serial.getDeviceList();
for(int i = 0; i < list.size(); i++) {
if (list[i].getDeviceName().find(wasp_signature) != std::string::npos) {
_serial.setup(list[i].getDevicePath(), wasp_baud);
return true;
}
}
return false;
}
void Bridge::update(){
static vector<WaspFrame> r;
r.clear();
if(_serial.available() >= SERIAL_BUFFER_LEN) {
_serial.readBytes(_buffer, SERIAL_BUFFER_LEN);
_payload += string(_buffer, _buffer + SERIAL_BUFFER_LEN);
_wp.ASCII_process(_payload, r, false);
//LOOK up MAP !!!!!!!!!!!!!!
for(int i = 0; i < r.size(); i++) {
WaspFrame wf = r[i];
map<string, Publisher*>::iterator it = _map.find(wf.mote_id);
if(it == _map.end()) {
Publisher* p = new Publisher(wf.mote_id, &_sb_connection);
p->data(wf.adc_data, wf.battery_level, wf.frame_seq);
_map[wf.mote_id] = p;
} else {
Publisher* p = it->second;
p->data(wf.adc_data, wf.battery_level, wf.frame_seq);
}
}
}
}
Publisher::Publisher(string id, Spacebrew::Connection* sbc) : _id(id), _sb_connection(sbc), _current_seq(0), _is_publish_ready(false)
{
cout << "NEW PUBLISHER -- " << id << endl;
_ci_data_id = _id + "-DATA";
_batt_data_id = _id + "-BATT";
setup_spacebrew_publisher();
}
void Publisher::setup_spacebrew_publisher()
{
if(!_is_publish_ready && _sb_connection->isConnected()) {
// ADD PUBLISHER INFO -- 2 publishers per waspmote (DATA, BATT)
_sb_connection->addPublish(_batt_data_id, "range");
_sb_connection->addPublish(_ci_data_id, "range");
_is_publish_ready = true;
}
}
void Publisher::data(int ci_data, int batt_data, int seq)
{
// if(seq <= _current_seq) return;
// else _current_seq= seq;
_current_seq= seq;
cout << "Publisher: " << _id << endl;
cout << seq << " - " << ci_data << " - " << batt_data << endl;
if(_sb_connection->isConnected()) {
if (!_is_publish_ready) {
setup_spacebrew_publisher();
sleep(1);
}
_sb_connection->sendRange(_ci_data_id, ci_data);
_sb_connection->sendRange(_batt_data_id, batt_data);
}
if (_ci_data.size() >= SERIAL_BUFFER_LEN) _ci_data.pop_front();
if (_batt_data.size() >= SERIAL_BUFFER_LEN) _batt_data.pop_front();
_ci_data.push_back(ci_data);
_batt_data.push_back(batt_data);
}

View File

@ -0,0 +1,84 @@
//
// Bridge.h
// waspmote_spacebrew_bridge
//
// Created by dviid on 12/1/13.
//
//
#ifndef __waspmote_spacebrew_bridge__Bridge__
#define __waspmote_spacebrew_bridge__Bridge__
#include <iostream>
#include "ofxSpacebrew.h"
#include "WaspFrameParser.h"
static const int MAX_CACHED_DATA = 50;
static const int SERIAL_BUFFER_LEN = 136;
static const std::string SPACEBREW_SERVER = "sandbox.spacebrew.cc";
//static const std::string SPACEBREW_SERVER = "localhost";
class Publisher
{
public:
Publisher(string id, Spacebrew::Connection* sbc);
void setup_spacebrew_publisher();
void data(int ci_data, int batt_data, int seq);
string _id;
string _ci_data_id;
string _batt_data_id;
// cache
deque<int> _ci_data;
deque<int> _batt_data;
int _current_seq;
// spacebrew connection
Spacebrew::Connection* _sb_connection;
bool _is_publish_ready;
// - todo -
};
class Bridge
{
public:
Bridge(){;}
bool setup_spacebrew();
bool setup_serial();
void update(); // threadable
// wasp frame cb
void data(WaspFrame* wf);
Publisher* newPublisher(string id);
// serial interface
ofSerial _serial;
bool _serial_set;
unsigned char _buffer[SERIAL_BUFFER_LEN];
string _payload;
WaspFrameParser _wp;
// spacebrew interface
// map of spacebrew publishers
map<string, Publisher*> _map;
Spacebrew::Connection _sb_connection;
};
#endif /* defined(__waspmote_spacebrew_bridge__Bridge__) */

View File

@ -70,7 +70,7 @@ void WaspFrameParser::ASCII_token_frame(string &input, vector<string> &output)
} }
} }
void WaspFrameParser::ASCII_process(string &input, vector<WaspFrame> &output) void WaspFrameParser::ASCII_process(string &input, vector<WaspFrame> &output, bool emit)
{ {
vector<string> r; vector<string> r;
WaspFrameParser::ASCII_split_frame(input, r); WaspFrameParser::ASCII_split_frame(input, r);
@ -90,12 +90,16 @@ void WaspFrameParser::ASCII_process(string &input, vector<WaspFrame> &output)
wp->mote_id = t[2]; wp->mote_id = t[2];
wp->frame_seq = atoi(t[3].c_str()); wp->frame_seq = atoi(t[3].c_str());
wp->mac = t[4]; wp->mac = t[4];
wp->adc_data = atoi(WaspFrameParser::ASCII_parse("CI0:", t[5]).c_str()); wp->adc_data = atoi(WaspFrameParser::ASCII_parse(wp->mote_id + ":", t[5]).c_str());
wp->battery_level = atoi(WaspFrameParser::ASCII_parse("BAT:", t[6]).c_str()); wp->battery_level = atoi(WaspFrameParser::ASCII_parse("BAT:", t[6]).c_str());
//cout << (*wp) << endl; //cout << (*wp) << endl;
output.push_back(*wp); output.push_back(*wp);
if(emit) {
ofNotifyEvent(_frame_event, wp);
}
} }

View File

@ -13,6 +13,9 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "ofEvents.h"
using namespace std; using namespace std;
#define MIN_FRAME_ENTRIES 7 #define MIN_FRAME_ENTRIES 7
@ -38,13 +41,16 @@ class WaspFrameParser {
public: public:
WaspFrameParser(){;}
static void ASCII_split_frame(string &input, vector<string> &output); static void ASCII_split_frame(string &input, vector<string> &output);
static void ASCII_token_frame(string &input, vector<string> &output); static void ASCII_token_frame(string &input, vector<string> &output);
static void ASCII_process(string &input, vector<WaspFrame> &output);
static string ASCII_parse(string id, string input); static string ASCII_parse(string id, string input);
void ASCII_process(string &input, vector<WaspFrame> &output, bool emit);
ofEvent<WaspFrame*> _frame_event;
}; };
#endif /* defined(__waspmote_gateway_parser__WaspFrameParser__) */ #endif /* defined(__waspmote_gateway_parser__WaspFrameParser__) */

View File

@ -3,7 +3,7 @@
//======================================================================== //========================================================================
int main( ){ int main( ){
ofSetupOpenGL(1024,768,OF_WINDOW); // <-------- setup the GL context ofSetupOpenGL(200,200,OF_WINDOW); // <-------- setup the GL context
// this kicks off the running of my app // this kicks off the running of my app
// can be OF_WINDOW or OF_FULLSCREEN // can be OF_WINDOW or OF_FULLSCREEN

View File

@ -8,50 +8,17 @@
#define wasp_baud 115200 #define wasp_baud 115200
bool testApp::setup_serial() {
//_serial.enumerateDevices();
vector <ofSerialDeviceInfo> list = _serial.getDeviceList();
for(int i = 0; i < list.size(); i++) {
if (list[i].getDeviceName().find(wasp_signature) != std::string::npos) {
_serial.setup(list[i].getDevicePath(), wasp_baud);
return true;
}
}
return false;
}
string wp_ascii_split(string input)
{
}
//-------------------------------------------------------------- //--------------------------------------------------------------
void testApp::setup(){ void testApp::setup(){
_serial_set = setup_serial(); _bridge.setup_serial();
_bridge.setup_spacebrew();
} }
//-------------------------------------------------------------- //--------------------------------------------------------------
void testApp::update(){ void testApp::update(){
static vector<WaspFrame> r; _bridge.update();
r.clear();
if(_serial.available() >= BUFFER_LEN) {
_serial.readBytes(_buffer, BUFFER_LEN);
_payload += string(_buffer, _buffer + BUFFER_LEN);
WaspFrameParser::ASCII_process(_payload, r);
for(int i = 0; i < r.size(); i++) {
ofLog() << r[i] << endl;
}
}
} }
//-------------------------------------------------------------- //--------------------------------------------------------------

View File

@ -3,7 +3,8 @@
#include "ofMain.h" #include "ofMain.h"
#include "OfSerial.h" #include "OfSerial.h"
#define BUFFER_LEN 136 #include "Bridge.h"
class testApp : public ofBaseApp{ class testApp : public ofBaseApp{
@ -22,17 +23,5 @@ class testApp : public ofBaseApp{
void dragEvent(ofDragInfo dragInfo); void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg); void gotMessage(ofMessage msg);
bool setup_serial(); Bridge _bridge;
string wp_ascii_split(string input);
public:
ofSerial _serial;
bool _serial_set;
unsigned char _buffer[BUFFER_LEN];
string _payload;
}; };

View File

@ -16,6 +16,7 @@
B5F6FF34184A6F0E0063881D /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B5F6FF2F184A6F0E0063881D /* main.cpp */; }; B5F6FF34184A6F0E0063881D /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B5F6FF2F184A6F0E0063881D /* main.cpp */; };
B5F6FF35184A6F0E0063881D /* testApp.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B5F6FF30184A6F0E0063881D /* testApp.cpp */; }; B5F6FF35184A6F0E0063881D /* testApp.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B5F6FF30184A6F0E0063881D /* testApp.cpp */; };
B5F6FF36184A6F0E0063881D /* WaspFrameParser.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B5F6FF32184A6F0E0063881D /* WaspFrameParser.cpp */; }; B5F6FF36184A6F0E0063881D /* WaspFrameParser.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B5F6FF32184A6F0E0063881D /* WaspFrameParser.cpp */; };
B5F6FF39184B97040063881D /* Bridge.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B5F6FF37184B97040063881D /* Bridge.cpp */; };
BBAB23CB13894F3D00AA2426 /* GLUT.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = BBAB23BE13894E4700AA2426 /* GLUT.framework */; }; BBAB23CB13894F3D00AA2426 /* GLUT.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = BBAB23BE13894E4700AA2426 /* GLUT.framework */; };
C5688FC85C0AE0B5E4E5F9CE /* Server.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C53FF33B244987E09C8CC63F /* Server.cpp */; }; C5688FC85C0AE0B5E4E5F9CE /* Server.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C53FF33B244987E09C8CC63F /* Server.cpp */; };
C6E1C4962CC70798B430DCBC /* jsoncpp.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9413D837F3E273803A5F776E /* jsoncpp.cpp */; }; C6E1C4962CC70798B430DCBC /* jsoncpp.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9413D837F3E273803A5F776E /* jsoncpp.cpp */; };
@ -81,6 +82,8 @@
B5F6FF31184A6F0E0063881D /* testApp.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = testApp.h; sourceTree = "<group>"; }; B5F6FF31184A6F0E0063881D /* testApp.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = testApp.h; sourceTree = "<group>"; };
B5F6FF32184A6F0E0063881D /* WaspFrameParser.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WaspFrameParser.cpp; sourceTree = "<group>"; }; B5F6FF32184A6F0E0063881D /* WaspFrameParser.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WaspFrameParser.cpp; sourceTree = "<group>"; };
B5F6FF33184A6F0E0063881D /* WaspFrameParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WaspFrameParser.h; sourceTree = "<group>"; }; B5F6FF33184A6F0E0063881D /* WaspFrameParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WaspFrameParser.h; sourceTree = "<group>"; };
B5F6FF37184B97040063881D /* Bridge.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Bridge.cpp; sourceTree = "<group>"; };
B5F6FF38184B97040063881D /* Bridge.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Bridge.h; sourceTree = "<group>"; };
BBAB23BE13894E4700AA2426 /* GLUT.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = GLUT.framework; path = ../../../of_v0.8.0_osx_release/libs/glut/lib/osx/GLUT.framework; sourceTree = "<group>"; }; BBAB23BE13894E4700AA2426 /* GLUT.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = GLUT.framework; path = ../../../of_v0.8.0_osx_release/libs/glut/lib/osx/GLUT.framework; sourceTree = "<group>"; };
C49850835B8C1A87752B848B /* Client.cpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.cpp; fileEncoding = 30; name = Client.cpp; path = ../../../of_v0.8.0_osx_release/addons/ofxLibwebsockets/libs/ofxLibwebsockets/src/Client.cpp; sourceTree = SOURCE_ROOT; }; C49850835B8C1A87752B848B /* Client.cpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.cpp; fileEncoding = 30; name = Client.cpp; path = ../../../of_v0.8.0_osx_release/addons/ofxLibwebsockets/libs/ofxLibwebsockets/src/Client.cpp; sourceTree = SOURCE_ROOT; };
C53FF33B244987E09C8CC63F /* Server.cpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.cpp; fileEncoding = 30; name = Server.cpp; path = ../../../of_v0.8.0_osx_release/addons/ofxLibwebsockets/libs/ofxLibwebsockets/src/Server.cpp; sourceTree = SOURCE_ROOT; }; C53FF33B244987E09C8CC63F /* Server.cpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.cpp; fileEncoding = 30; name = Server.cpp; path = ../../../of_v0.8.0_osx_release/addons/ofxLibwebsockets/libs/ofxLibwebsockets/src/Server.cpp; sourceTree = SOURCE_ROOT; };
@ -274,6 +277,8 @@
B5F6FF31184A6F0E0063881D /* testApp.h */, B5F6FF31184A6F0E0063881D /* testApp.h */,
B5F6FF32184A6F0E0063881D /* WaspFrameParser.cpp */, B5F6FF32184A6F0E0063881D /* WaspFrameParser.cpp */,
B5F6FF33184A6F0E0063881D /* WaspFrameParser.h */, B5F6FF33184A6F0E0063881D /* WaspFrameParser.h */,
B5F6FF37184B97040063881D /* Bridge.cpp */,
B5F6FF38184B97040063881D /* Bridge.h */,
); );
path = src; path = src;
sourceTree = SOURCE_ROOT; sourceTree = SOURCE_ROOT;
@ -385,6 +390,7 @@
B5F6FF34184A6F0E0063881D /* main.cpp in Sources */, B5F6FF34184A6F0E0063881D /* main.cpp in Sources */,
B5F6FF35184A6F0E0063881D /* testApp.cpp in Sources */, B5F6FF35184A6F0E0063881D /* testApp.cpp in Sources */,
B5F6FF36184A6F0E0063881D /* WaspFrameParser.cpp in Sources */, B5F6FF36184A6F0E0063881D /* WaspFrameParser.cpp in Sources */,
B5F6FF39184B97040063881D /* Bridge.cpp in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };

View File

@ -11,6 +11,7 @@ SENSOR_CI3 <int>
SENSOR_CI4 <int> SENSOR_CI4 <int>
*/ */
#define CI_NODE_NAME "CI4"
#define SENSOR_PIN ANALOG1 // PIN for the ADC reading #define SENSOR_PIN ANALOG1 // PIN for the ADC reading
// declare ADC reading variable // declare ADC reading variable
@ -82,7 +83,7 @@ void loop() {
// B. BROADCAST PACKET // B. BROADCAST PACKET
//B.1 create ASCII frame //B.1 create ASCII frame
frame.createFrame(ASCII,"CRITICAL-0"); frame.createFrame(ASCII, CI_NODE_NAME);
//B.1.1 add CRITICAL INFRASTRUCTURE sensor descriptors //B.1.1 add CRITICAL INFRASTRUCTURE sensor descriptors
frame.addSensor(SENSOR_MAC, macLow); // MAC frame.addSensor(SENSOR_MAC, macLow); // MAC