We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi all,
Working with students, i found a recurrent problem in the communication of sensors from Arduino to Processing and other languages. I wrote an easy to use library called SensorShieldLib.
SensorshieldLib allows you to easilly attach and communicate sensors value in JSON.
It focuses on a simple API yet powerful (setting sensors sensitivity, using functions as sensors, support for INPUT_PULLUP, support for different Stream like ethernet communication... ), having the Arduino updating its values on change (instead of continuously checking from Processing with Firmata), communicating with JSON available nearly all languages.
Here is an example:
Arduino Code:
#include <sensorShieldLib.h>
SensorShield board;
void setup()
{
board.init(); // initialises and starts Serial
board.addSensor( "btn1", 2 );
board.addSensor( "btn2", 8, INPUT_PULLUP );
board.addSensor( "pot1", A0 );
}
void loop()
{
board.update();
}
On board.update();, the lib checks sensors values and if any changed from previous loop, outputs JSON on Serial 9600 bds: {"btn1":1,"btn2":0,"pot1":768}
Then Processing code simply needs to parse JSON when arriving on Serial:
import processing.serial.*;
Serial myPort;
JSONObject json;
int potar = 0;
void setup()
{
size( 200, 200 );
printArray( Serial.list() );
String portName = "COM6";//adjust your port
myPort = new Serial( this, portName, 9600 );
}
void draw()
{
while ( myPort.available() > 0 ) {
String data = myPort.readStringUntil( '\n' );
if ( data != null ) {
println( data );
try {
json = JSONObject.parse( data );
println( json.getInt( "btn1" ) );
potar = json.getInt( "pot1" );
}
catch ( Exception e ) {
e.printStackTrace();
}
}
}
background( potar / 4 );
}
Check the lib and full API on github: https://github.com/MAKIO135/sensorshieldLib
Examples for use with Processing / Python / openFrameworks and NodeJS can be found in the interfaces rep
Hope it helps some of you.
Cheers,
Lionel Radisson / Makio135