BUILD A JAVA
WEATHER STATION
USE YOUR RASPBERRY PI2 AND DEVELOP A WEB CLIENT
MARCO PIRRUCCIO
Research and Development Director, Partner @ Kettydo+ srl
E-mail marcopirruccio@gmail.com
LinkedIn www.linkedin.com/in/marcopirruccio
SPOT THE DIFFERENCES
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
SOFTWARE INGREDIENTS
On 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
THE SKETCH
Fritzing
ARCHITECTURE
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)
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.
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.
WEATHER STATION MAIN CLASS
WeatherStationReader 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);
}
WEATHER STATION READER: INITIALIZATION
GpioController 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");
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);
WEATHER STATION WRITER: INITIALIZATION
String 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");
WEATHER STATION WRITER: SENDING DATA
String 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);
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>
THE WEB CLIENT: WEBSOCKET
var 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);
}
});
THE WEB CLIENT: CHARTS
client.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);
};
THE WEB CLIENT: UI
VIEW A LIVE DEMO AT BOOTH MAK 72
REFERENCES
• http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html
• https://eclipse.org/paho/clients/java/
• https://www.websocket.org/
• https://eclipse.org/paho/clients/js/
• http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
• http://pi4j.com/
• http://wiringpi.com/
• http://www.highcharts.com/
THANKS
Q & A

More Related Content

PDF
RASPBERRY PI WITH JAVA 8 + Pi4J (Devoxx 2014)
PPTX
Raspberry pi weather storey
PPT
Smart Wireless Surveillance Monitoring using RASPBERRY PI
KEY
Raspberry pi on java 20121110
DOCX
A seminar report on Raspberry Pi
PPT
pressure sensors
PDF
Increased Developer Productivity for IoT with Java and Reactive Blocks (Oracl...
RASPBERRY PI WITH JAVA 8 + Pi4J (Devoxx 2014)
Raspberry pi weather storey
Smart Wireless Surveillance Monitoring using RASPBERRY PI
Raspberry pi on java 20121110
A seminar report on Raspberry Pi
pressure sensors
Increased Developer Productivity for IoT with Java and Reactive Blocks (Oracl...

Viewers also liked (7)

PPTX
Raspberry Pi with Java 8
PDF
MQTT with Java - a protocol for IoT and M2M communication
PDF
MQTT, Eclipse Paho and Java - Messaging for the Internet of Things
PPSX
Low Cost HD Surveillance Camera using Raspberry PI
PPTX
Con7968 let's get physical - io programming using pi4 j
PDF
Home Automation Using RPI
PPTX
Internet of things using Raspberry Pi
Raspberry Pi with Java 8
MQTT with Java - a protocol for IoT and M2M communication
MQTT, Eclipse Paho and Java - Messaging for the Internet of Things
Low Cost HD Surveillance Camera using Raspberry PI
Con7968 let's get physical - io programming using pi4 j
Home Automation Using RPI
Internet of things using Raspberry Pi
Ad

Similar to Build a Java and Raspberry Pi weather station (20)

PDF
Having fun with a solar panel, camera and Apache projects.pdf
PDF
IoT Implementation of Sensor Data Acquisition in Surveillance Applications - ...
PDF
IoT Implementation of Sensor Data Acquisition in Surveillance Applications - ...
PDF
SDARPiBot - VLES'16
PPT
Server/Client Remote platform logger.
DOCX
Report for weather pi
PDF
03 Make Things Talk
PDF
Quest for a low powered home hub 120522
PPTX
Node mcu x raspberrypi2 x mqtt
PDF
Arduino and the real time web
PPTX
Serial Data from Arduino to Raspberry Pi to MySQL using CoAP Protocol
PPTX
PPT on Weather Monitoring System-converted (1).pptx
PDF
Home Automation
PDF
Ardunio + MySQL = direct database connections.
PDF
Using Eclipse and Lua for the Internet of Things with Eclipse Koneki, Mihini ...
PDF
Making things that works with us
PDF
How to build own IoT Platform
PDF
Gaztea Tech Robotica 2016
PDF
Eclipse Kura Shoot a-pi
PDF
How the world gets its weather
Having fun with a solar panel, camera and Apache projects.pdf
IoT Implementation of Sensor Data Acquisition in Surveillance Applications - ...
IoT Implementation of Sensor Data Acquisition in Surveillance Applications - ...
SDARPiBot - VLES'16
Server/Client Remote platform logger.
Report for weather pi
03 Make Things Talk
Quest for a low powered home hub 120522
Node mcu x raspberrypi2 x mqtt
Arduino and the real time web
Serial Data from Arduino to Raspberry Pi to MySQL using CoAP Protocol
PPT on Weather Monitoring System-converted (1).pptx
Home Automation
Ardunio + MySQL = direct database connections.
Using Eclipse and Lua for the Internet of Things with Eclipse Koneki, Mihini ...
Making things that works with us
How to build own IoT Platform
Gaztea Tech Robotica 2016
Eclipse Kura Shoot a-pi
How the world gets its weather
Ad

Recently uploaded (20)

PPTX
Benefits of Physical activity for teenagers.pptx
PDF
Hybrid model detection and classification of lung cancer
PPT
What is a Computer? Input Devices /output devices
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PDF
August Patch Tuesday
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PPTX
Final SEM Unit 1 for mit wpu at pune .pptx
PDF
1 - Historical Antecedents, Social Consideration.pdf
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PDF
Zenith AI: Advanced Artificial Intelligence
PDF
Enhancing emotion recognition model for a student engagement use case through...
PDF
A comparative study of natural language inference in Swahili using monolingua...
PPTX
Modernising the Digital Integration Hub
PDF
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
PPTX
O2C Customer Invoices to Receipt V15A.pptx
PDF
Getting Started with Data Integration: FME Form 101
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PDF
STKI Israel Market Study 2025 version august
Benefits of Physical activity for teenagers.pptx
Hybrid model detection and classification of lung cancer
What is a Computer? Input Devices /output devices
NewMind AI Weekly Chronicles – August ’25 Week III
August Patch Tuesday
A contest of sentiment analysis: k-nearest neighbor versus neural network
Final SEM Unit 1 for mit wpu at pune .pptx
1 - Historical Antecedents, Social Consideration.pdf
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
Zenith AI: Advanced Artificial Intelligence
Enhancing emotion recognition model for a student engagement use case through...
A comparative study of natural language inference in Swahili using monolingua...
Modernising the Digital Integration Hub
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
O2C Customer Invoices to Receipt V15A.pptx
Getting Started with Data Integration: FME Form 101
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
STKI Israel Market Study 2025 version august

Build a Java and Raspberry Pi weather station

  • 1. BUILD A JAVA WEATHER STATION USE YOUR RASPBERRY PI2 AND DEVELOP A WEB CLIENT
  • 2. MARCO PIRRUCCIO Research and Development Director, Partner @ Kettydo+ srl E-mail marcopirruccio@gmail.com LinkedIn www.linkedin.com/in/marcopirruccio
  • 4. 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
  • 5. SOFTWARE INGREDIENTS On 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
  • 8. 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)
  • 9. 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.
  • 10. 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.
  • 11. WEATHER STATION MAIN CLASS WeatherStationReader 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); }
  • 12. WEATHER STATION READER: INITIALIZATION GpioController 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");
  • 13. 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);
  • 14. WEATHER STATION WRITER: INITIALIZATION String 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");
  • 15. WEATHER STATION WRITER: SENDING DATA String 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);
  • 16. 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>
  • 17. THE WEB CLIENT: WEBSOCKET var 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); } });
  • 18. THE WEB CLIENT: CHARTS client.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); };
  • 20. VIEW A LIVE DEMO AT BOOTH MAK 72
  • 21. REFERENCES • http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html • https://eclipse.org/paho/clients/java/ • https://www.websocket.org/ • https://eclipse.org/paho/clients/js/ • http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html • http://pi4j.com/ • http://wiringpi.com/ • http://www.highcharts.com/