Call REST API using JAVA on Raspberry Pi

REST API from Raspberry Pi using JAVA

This video is around Raspberry Pi to showcase the usage of Raspberry Pi in IOT projects and that’s using JAVA. This is going to be a series of video including Sensor interfacing using pi4J, REST API usage with JAVA, MQTT usage with JAVA and finally adding all to the same project and running over Raspberry Pi. In the last video, we saw how to interface DS18B20 1 wire temperature sensor with Raspberry Pi by using JAVA. If you have not checked it out you can do that and for that, I have provided the link down below in the description. This video is the 2nd in the series where we will see how to publish or send sensor data using REST API to cloud. And in this, we are using ThingSpeak as cloud service to publish data. HTTP calls for REST API are done using Unirest lightweight HTTP client library. In the next video, we will do the same by using MQTT.




import com.pi4j.component.temperature.TemperatureSensor;
package in.weargenius.restapi;

import com.mashape.unirest.http.*;
import com.mashape.unirest.http.async.Callback;
import com.mashape.unirest.http.exceptions.UnirestException;

public class RESTCall {
	public void sendDataOverRest(int temperature){"")
		  .header("accept", "application/json")
		  .field("api_key", "0C8704XEZ68UFGB3")
		  .asJsonAsync(new Callback() {

		    public void failed(UnirestException e) {
		        System.out.println("The request has failed");

		    public void completed(HttpResponse response) {
		         int code = response.getStatus();
		       //  Map<String, String> headers = response.getHeaders();
		         JsonNode body = response.getBody();
		         InputStream rawBody = response.getRawBody();

			        try {
					} catch (IOException e) {
						// TODO Auto-generated catch block

		    public void cancelled() {
		        System.out.println("The request has been cancelled");




Links :
Github Repo:
Download Unirest Library:
Unirest Website:
Unirest Jar Download (With Dependencies):
First Part (DS18B20 Interfacing with Raspberry Pi using JAVA):

Java Application on Pi Playlist:

Related posts

Leave a Comment