Skip to content

Instantly share code, notes, and snippets.

@gautric
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gautric/e8eef9489d62288dedb9 to your computer and use it in GitHub Desktop.
Save gautric/e8eef9489d62288dedb9 to your computer and use it in GitHub Desktop.
package com.github.camellabs.component.pi4j;
import com.github.camellabs.component.pi4j.i2c.driver.LSM303Value;
import com.github.camellabs.component.pi4j.i2c.driver.MCP23017LCD;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A simple application to run RaspberryPi Camel
*/
public final class MQTT2LCD {
private static final Logger LOG = LoggerFactory.getLogger(MQTT2LCD.class);
private static final int STABLE_ZONE = 1000;
private static final int ERROR_ZONE = 900;
public static void main(String[] args) throws Exception {
LOG.info("main");
String host = "tcp://remotemqtt:1883"; // Change It
CamelContext context = new DefaultCamelContext();
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("mqtt://pi4j?subscribeTopicName=i2c/accel&host=" + host)
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
LSM303Value accel = exchange.getIn().getBody(LSM303Value.class);
if (STABLE_ZONE < accel.getZ()) {
exchange.getIn().setHeader(MCP23017LCD.LCD_COLOR, "GREEN");
} else if (ERROR_ZONE < accel.getZ() && accel.getZ() < STABLE_ZONE) {
exchange.getIn().setHeader(MCP23017LCD.LCD_COLOR, "YELLOW");
} else {
exchange.getIn().setHeader(MCP23017LCD.LCD_COLOR, "RED");
}
exchange.getIn().setHeader(MCP23017LCD.LCD_CURSOR, false); // all
// messages
// will
// clear
// cursor
exchange.getIn().setHeader(MCP23017LCD.LCD_BLINK_CURSOR, false);
exchange.getIn().setBody(accel.toString()); // Prepare
// for LCD
}
})
.to(Pi4jConstants.LOG_COMPONENT)
.to("pi4j-i2c://1/0x20?driver=mcp23017-lcd&color=BLUE&cursor=true&blinkCursor=true");
}
});
context.start();
Thread.sleep(60000);
context.stop();
System.exit(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment