├── bin ├── .gitignore └── in │ └── weargenius │ ├── main │ └── MainClass.class │ └── hardware │ └── WaterTemperatureSensor.class ├── lib ├── pi4j-core.jar ├── pi4j-device.jar ├── httpcore-4.4.4.jar ├── httpmime-4.5.2.jar ├── json-20160212.jar ├── httpclient-4.5.2.jar ├── commons-codec-1.9.jar ├── commons-logging-1.2.jar ├── httpcore-nio-4.4.4.jar ├── pi4j-gpio-extension.jar ├── unirest-java-1.4.9.jar ├── httpasyncclient-4.1.1.jar ├── org.eclipse.paho.client.mqttv3-1.0.2.jar └── org.eclipse.paho.client.mqttv3-1.1.1.jar ├── Circuit Diagram.png ├── src └── in │ └── weargenius │ ├── main │ └── MainClass.java │ ├── hardware │ └── WaterTemperatureSensor.java │ ├── restapi │ └── RESTCall.java │ └── mqtt │ └── SendDataUsingMQTT.java ├── .project ├── README.md └── .classpath /bin/.gitignore: -------------------------------------------------------------------------------- 1 | /in/ 2 | -------------------------------------------------------------------------------- /lib/pi4j-core.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oksbwn/IOT_Raspberry_Pi/HEAD/lib/pi4j-core.jar -------------------------------------------------------------------------------- /Circuit Diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oksbwn/IOT_Raspberry_Pi/HEAD/Circuit Diagram.png -------------------------------------------------------------------------------- /lib/pi4j-device.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oksbwn/IOT_Raspberry_Pi/HEAD/lib/pi4j-device.jar -------------------------------------------------------------------------------- /lib/httpcore-4.4.4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oksbwn/IOT_Raspberry_Pi/HEAD/lib/httpcore-4.4.4.jar -------------------------------------------------------------------------------- /lib/httpmime-4.5.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oksbwn/IOT_Raspberry_Pi/HEAD/lib/httpmime-4.5.2.jar -------------------------------------------------------------------------------- /lib/json-20160212.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oksbwn/IOT_Raspberry_Pi/HEAD/lib/json-20160212.jar -------------------------------------------------------------------------------- /lib/httpclient-4.5.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oksbwn/IOT_Raspberry_Pi/HEAD/lib/httpclient-4.5.2.jar -------------------------------------------------------------------------------- /lib/commons-codec-1.9.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oksbwn/IOT_Raspberry_Pi/HEAD/lib/commons-codec-1.9.jar -------------------------------------------------------------------------------- /lib/commons-logging-1.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oksbwn/IOT_Raspberry_Pi/HEAD/lib/commons-logging-1.2.jar -------------------------------------------------------------------------------- /lib/httpcore-nio-4.4.4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oksbwn/IOT_Raspberry_Pi/HEAD/lib/httpcore-nio-4.4.4.jar -------------------------------------------------------------------------------- /lib/pi4j-gpio-extension.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oksbwn/IOT_Raspberry_Pi/HEAD/lib/pi4j-gpio-extension.jar -------------------------------------------------------------------------------- /lib/unirest-java-1.4.9.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oksbwn/IOT_Raspberry_Pi/HEAD/lib/unirest-java-1.4.9.jar -------------------------------------------------------------------------------- /lib/httpasyncclient-4.1.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oksbwn/IOT_Raspberry_Pi/HEAD/lib/httpasyncclient-4.1.1.jar -------------------------------------------------------------------------------- /src/in/weargenius/main/MainClass.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oksbwn/IOT_Raspberry_Pi/HEAD/src/in/weargenius/main/MainClass.java -------------------------------------------------------------------------------- /bin/in/weargenius/main/MainClass.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oksbwn/IOT_Raspberry_Pi/HEAD/bin/in/weargenius/main/MainClass.class -------------------------------------------------------------------------------- /lib/org.eclipse.paho.client.mqttv3-1.0.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oksbwn/IOT_Raspberry_Pi/HEAD/lib/org.eclipse.paho.client.mqttv3-1.0.2.jar -------------------------------------------------------------------------------- /lib/org.eclipse.paho.client.mqttv3-1.1.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oksbwn/IOT_Raspberry_Pi/HEAD/lib/org.eclipse.paho.client.mqttv3-1.1.1.jar -------------------------------------------------------------------------------- /src/in/weargenius/hardware/WaterTemperatureSensor.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oksbwn/IOT_Raspberry_Pi/HEAD/src/in/weargenius/hardware/WaterTemperatureSensor.java -------------------------------------------------------------------------------- /bin/in/weargenius/hardware/WaterTemperatureSensor.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oksbwn/IOT_Raspberry_Pi/HEAD/bin/in/weargenius/hardware/WaterTemperatureSensor.class -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | IOTAPP_Pi 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # IOT_Raspberry_Pi 2 | A project to demonstrate IOT on Raspberry Pi by using JAVA. 3 | 4 | ## Dependencies: 5 | * [Pi4J](http://bit.ly/2j32blF) 6 | * [Unirest](http://bit.ly/2rFQ2t3) 7 | * [Eclipse PAHO](http://bit.ly/2srfxPE) 8 | 9 | ## Videos on this project: 10 | * Part I: [DS18B20 Interfacing with Pi using Pi4J and JAVA](http://bit.ly/2rPEHq8) 11 | * Part II: [Upload data to Thingspeak using REST API ](http://bit.ly/2GbTPGx) 12 | * Part III:[Publish data using MQTT with JAVA](http://bit.ly/2tCqI4W) 13 | * Part IV: [Completing the Project](http://bit.ly/2TznkVW) 14 | -------------------------------------------------------------------------------- /src/in/weargenius/restapi/RESTCall.java: -------------------------------------------------------------------------------- 1 | package in.weargenius.restapi; 2 | import java.io.InputStream; 3 | import com.mashape.unirest.http.*; 4 | import com.mashape.unirest.http.async.Callback; 5 | import com.mashape.unirest.http.exceptions.UnirestException; 6 | 7 | 8 | public class RESTCall implements Callback{ 9 | 10 | public void sendDataOverRest(double temp) { 11 | 12 | Unirest.post("https://api.thingspeak.com/update.json") 13 | .header("accept", "application/json") 14 | .field("api_key", "0C8704XEZ68UFGB3") 15 | .field("field1",temp) 16 | .asJsonAsync(this); 17 | } 18 | 19 | @Override 20 | public void cancelled() { 21 | // TODO Auto-generated method stub 22 | System.out.println("The request has been cancelled"); 23 | } 24 | 25 | @Override 26 | public void completed(HttpResponse response) { 27 | // TODO Auto-generated method stub 28 | int code = response.getStatus(); 29 | // Map headers = response.getHeaders(); 30 | JsonNode body =response.getBody(); 31 | InputStream rawBody = response.getRawBody(); 32 | 33 | System.out.println(code); 34 | System.out.println(body); 35 | System.out.println(rawBody); 36 | } 37 | 38 | @Override 39 | public void failed(UnirestException arg0) { 40 | // TODO Auto-generated method stub 41 | System.out.println("The request has failed"); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /src/in/weargenius/mqtt/SendDataUsingMQTT.java: -------------------------------------------------------------------------------- 1 | package in.weargenius.mqtt; 2 | 3 | 4 | import java.sql.Timestamp; 5 | 6 | import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; 7 | import org.eclipse.paho.client.mqttv3.MqttCallback; 8 | import org.eclipse.paho.client.mqttv3.MqttClient; 9 | import org.eclipse.paho.client.mqttv3.MqttConnectOptions; 10 | import org.eclipse.paho.client.mqttv3.MqttException; 11 | import org.eclipse.paho.client.mqttv3.MqttMessage; 12 | import org.eclipse.paho.client.mqttv3.MqttPersistenceException; 13 | import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence; 14 | 15 | 16 | public class SendDataUsingMQTT{ 17 | private MqttClient client; 18 | private MqttConnectOptions conOpt; 19 | 20 | 21 | String brokerUrl="tcp://mqtt.thingspeak.com:1883"; 22 | String clientId="ExamplePublish"; 23 | String channel="channels/281567/publish/LVSBKQWJI3JL293Y"; 24 | int qos=0; 25 | public SendDataUsingMQTT() throws MqttException { 26 | String tmpDir = System.getProperty("java.io.tmpdir"); 27 | MqttDefaultFilePersistence dataStore = new MqttDefaultFilePersistence(tmpDir); 28 | 29 | try { 30 | // Construct the connection options object that contains connection parameters 31 | // such as cleanSession and LWT 32 | conOpt = new MqttConnectOptions(); 33 | conOpt.setCleanSession(true); 34 | //conOpt. 35 | // Construct an MQTT blocking mode client 36 | client = new MqttClient(brokerUrl,MqttClient.generateClientId(), dataStore); 37 | 38 | // Set this wrapper as the callback handler 39 | 40 | client.setCallback(new MqttCallback() { 41 | 42 | @Override 43 | public void messageArrived(String arg0, MqttMessage arg1) throws Exception { 44 | // TODO Auto-generated method stub 45 | String time = new Timestamp(System.currentTimeMillis()).toString(); 46 | System.out.println("Time:\t" +time + 47 | " Topic:\t" + arg0 + 48 | " Message:\t" + new String(arg1.getPayload()) + 49 | " QoS:\t" + arg1.getQos()); 50 | } 51 | 52 | @Override 53 | public void deliveryComplete(IMqttDeliveryToken arg0) { 54 | // TODO Auto-generated method stub 55 | 56 | try { 57 | System.out.println(arg0.getMessageId()+" "+arg0.getMessage()); 58 | } catch (MqttException e) { 59 | // TODO Auto-generated catch block 60 | e.printStackTrace(); 61 | } 62 | } 63 | 64 | @Override 65 | public void connectionLost(Throwable arg0) { 66 | // TODO Auto-generated method stub 67 | 68 | System.out.println("Connection to " + brokerUrl + " lost!"); 69 | 70 | System.out.println("Reconnecting.."); 71 | try { 72 | client.connect(conOpt); 73 | System.out.println("Connected"); 74 | } catch (MqttException e) { 75 | // TODO Auto-generated catch block 76 | e.printStackTrace(); 77 | } 78 | // System.exit(1); 79 | //client. 80 | } 81 | }); 82 | if(client.isConnected()){ 83 | 84 | System.out.println("was already Connected"); 85 | client.disconnect(); 86 | } 87 | client.connect(conOpt); 88 | System.out.println("Connected"); 89 | 90 | 91 | } catch (MqttException e) { 92 | e.printStackTrace(); 93 | System.out.println("Unable to set up client: "+e.toString()); 94 | System.exit(1); 95 | } 96 | 97 | 98 | 99 | } 100 | public void publish(String data) throws MqttPersistenceException, MqttException{ 101 | String time = new Timestamp(System.currentTimeMillis()).toString(); 102 | System.out.println("Publishing at: "+time+ " to topic \""+channel+"\" qos "+qos); 103 | 104 | // Create and configure a message 105 | MqttMessage message = new MqttMessage(data.getBytes()); 106 | message.setQos(qos); 107 | message.setRetained(false); 108 | 109 | client.publish(channel, message); 110 | 111 | // Disconnect the client 112 | 113 | } 114 | public void close() throws MqttException{ 115 | client.disconnect(); 116 | System.out.println("Disconnected"); 117 | } 118 | } 119 | --------------------------------------------------------------------------------