Build a Java and Raspberry Pi weather station

22
BUILD A JAVA WEATHER STATION USE YOUR RASPBERRY PI2 AND DEVELOP A WEB CLIENT

Transcript of Build a Java and Raspberry Pi weather station

Page 1: Build a Java and Raspberry Pi weather station

BUILD A JAVA WEATHER STATION

USE YOUR RASPBERRY PI2 AND DEVELOP A WEB CLIENT

Page 2: Build a Java and Raspberry Pi weather station

MARCO PIRRUCCIOResearch and Development Director, Partner @ Kettydo+ srl

E-mail [email protected]

LinkedIn www.linkedin.com/in/marcopirruccio

Page 3: Build a Java and Raspberry Pi weather station

SPOT THE DIFFERENCES

Page 4: Build a Java and Raspberry Pi weather station

HARDWARE SHOPPING LIST• Raspberry Pi2

• DHT22 (temperature, humidity)

• BMP180 (temperature, pressure, altitude)

• ML8511 (UV intensity)

• TGS2600 (air contaminants)

• MCP3800 (ADC for ML8511 and TGS2600)

• A red and a green LED

• A tactile button switch

Page 5: Build a Java and Raspberry Pi weather station

SOFTWARE INGREDIENTSOn the Raspberry Pi

• Java 8

• Pi4J and WiringPi

• MQTT (Eclipse Paho Java client)

On the web client

• WebSockets (Eclipse Paho JavaScript client)

• JavaScript: jQuery, HighCharts

• HTML, CSS

Page 6: Build a Java and Raspberry Pi weather station

THE SKETCH

Fritzing

Page 7: Build a Java and Raspberry Pi weather station

ARCHITECTURE

Page 8: Build a Java and Raspberry Pi weather station

MQTT (MESSAGE QUEUING TELEMETRY TRANSPORT)• MQTT is a “machine-to-machine” (M2M) or “Internet of Things” (IoT) connectivity protocol.

• It was designed as an extremely lightweight publish/subscribe messaging transport.

• It was designed for constrained devices and low-bandwidth, high-latency or unreliable networks.

• MQTT was invented by Dr Andy Stanford-Clark of IBM and Arlen Nipper of Arcom (now Eurotech) in 1999.

• It is an application protocol over TCP/IP.

• Quality of service levels:

• 0: at most once delivery

• 1: at least once delivery

• 2: exactly once delivery (there is an increased overhead associated with this quality of service)

Page 9: Build a Java and Raspberry Pi weather station

WEBSOCKETS• The WebSockets specification was developed as part of the HTML5 initiative.

• The WebSockets standard defines a full-duplex single socket connection over which messages can be sent between client and server.

• WebSockets provide an enormous reduction in unnecessary network traffic and latency compared to the unscalable polling and long-polling solutions that were used to simulate a full-duplex connection by maintaining two connections.

Page 10: Build a Java and Raspberry Pi weather station

JAVA, PI4J, WIRINGPI• Raspbian includes the official Oracle Java 8 for ARM since the end of 2013. Older versions can install it using apt-get.

• The Pi4J project is intended to provide a friendly object-oriented I/O API and implementation libraries for Java Programmers to access the full I/O capabilities of the Raspberry Pi platform.

• It abstracts the low-level native integration and interrupt monitoring to enable Java programmers to focus on implementing their application business logic.

• Main features:

• Control, read and write GPIO pin states

• Listen for GPIO pin state changes (interrupt-based; not polling)

• I2C, SPI, RS232 communication

• WiringPi is a GPIO access library written in C for the BCM2835.

Page 11: Build a Java and Raspberry Pi weather station

WEATHER STATION MAIN CLASSWeatherStationReader reader = new WeatherStationReader();WeatherStationWriter writer = WeatherStationWriterFactory.getWriter(Writer.MQTT);

Runtime.getRuntime().addShutdownHook(new Thread() {public void run() {

reader.shutdown();writer.close();

}});

while (true) {WeatherStationData data = reader.read();writer.write(data);Thread.sleep(2000);

}

Page 12: Build a Java and Raspberry Pi weather station

WEATHER STATION READER: INITIALIZATIONGpioController controller = GpioFactory.getInstance();

MCP3008 adc = new MCP3008();ML8511 uv = new ML8511(adc, ML8511_ADC_CHANNEL);TGS2600 gas = new TGS2600(adc, TGS2600_ADC_CHANNEL);BMP180 bar = new BMP180();DHT22 t = new DHT22(DHT22_PIN);

GpioPinDigitalInput button = controller.provisionDigitalInputPin(BUTTON_GPIO, PinPullResistance.PULL_UP);button.addListener(new GpioPinListenerDigital() {

public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {System.exit(0);

}});

ActivityNotifier an = new ActivityNotifier(READ_ACTIVITY_NOTIFIER_GPIO, "readerLed");

Page 13: Build a Java and Raspberry Pi weather station

WEATHER STATION READER: READING SENSORS an.notifyActivity();

WeatherStationData res = new WeatherStationData();

DHT22Response th = t.read();res.setTemperature((float) th.getTemperature()); res.setHumidity((float) th.getHumidity());

gas.setReferenceHumidity(res.getHumidity()); gas.setReferenceTemperature(res.getTemperature());

TGS2600Response air = gas.read();res.setAirQuality(air.getResistance()); res.setEthanol((int) air.getC2h5ohPPM()); res.setButane((int) air.getC4h10PPM()); res.setHydrogen((int) air.getH2PPM());

float uvIntensity = uv.read(); res.setUvIntensity(uvIntensity);final float pressure = bar.readPressure(); res.setPressure(pressure / 100);

Page 14: Build a Java and Raspberry Pi weather station

WEATHER STATION WRITER: INITIALIZATIONString broker = "tcp://iot.eclipse.org:1883";

String clientId = WeatherStation.class.getName();

MemoryPersistence persistence = new MemoryPersistence();

MqttClient client = new MqttClient(broker, clientId, persistence);

MqttConnectOptions connOpts = new MqttConnectOptions();

connOpts.setCleanSession(true);

client.connect(connOpts);

ActivityNotifier an = new ActivityNotifier(WRITE_ACTIVITY_NOTIFIER_GPIO, "writerLed");

Page 15: Build a Java and Raspberry Pi weather station

WEATHER STATION WRITER: SENDING DATAString topic = "/WeatherStation";

int qos = 2;

String json = jsonSerializer.serialize(data);

an.notifyActivity();

MqttMessage message = new MqttMessage(json.getBytes());

message.setQos(qos);

client.publish(topic, message);

Page 16: Build a Java and Raspberry Pi weather station

THE WEB CLIENT: HTML MARKUP<div id="container-main"></div><div>

<div id="container-uv"></div><div id="container-aq"></div><div id="container-gas">

<table><thead>

<tr><td>PPM</td></tr></thead><tbody>

<tr><td>Ethanol</td><td id="ethanol">0</td></tr><tr><td>Butane</td><td id="butane">0</td></tr><tr><td>Hydrogen</td><td id="hydrogen">0</td></tr>

</tbody></table>

</div></div>

Page 17: Build a Java and Raspberry Pi weather station

THE WEB CLIENT: WEBSOCKETvar host = 'ws://iot.eclipse.org/ws';

var topic = '/WeatherStation';

var clientId = "WeatherStationClient";

var client = new Paho.MQTT.Client(host, clientId);

client.onConnectionLost = function(responseObject) { ... };

client.onMessageArrived = function(message) { ... };

client.connect({

onSuccess : function() {

client.subscribe(topic);

}

});

Page 18: Build a Java and Raspberry Pi weather station

THE WEB CLIENT: CHARTSclient.onMessageArrived = function(message) {

var data = JSON.parse(message.payloadString);var chart = $('#container-main').highcharts();chart.series[0].addPoint([data.timestamp, data.temperature]);chart.series[1].addPoint([data.timestamp, data.humidity]);chart.series[2].addPoint([data.timestamp, data.pressure]);chart.redraw();

chart = $('#container-uv').highcharts();var point = chart.series[0].points[0];point.update(parseFloat(data.uvIntensity.toFixed(2)));

chart = $('#container-aq').highcharts();point = chart.series[0].points[0];point.update(parseInt(data.airQuality.toFixed(0)));

$('#ethanol').html(data.ethanol);$('#butane').html(data.butane);$('#hydrogen').html(data.hydrogen);

};

Page 19: Build a Java and Raspberry Pi weather station

THE WEB CLIENT: UI

Page 20: Build a Java and Raspberry Pi weather station

VIEW A LIVE DEMO AT BOOTH MAK 72

Page 22: Build a Java and Raspberry Pi weather station

THANKS

Q & A