2

I am a total newbie in this Raspberry Pi thing. I am also not familiar with python.
I have an academic project requires me to record temperature and humidity from a sensor and send the data through WiFi to a computer using some sort of REST method, the computer will then generate a temperature and humidity graph, updating periodically.
I now have the sensor set up, it is linked to a Grove Pi+ and then the Grove Pi+ is stacked on top of a Raspberry Pi. I manage to read the reading from the sensor and display it on the Raspberry Pi command window with a python script that I have written based on the Grove Pi+ library python scripts.

The main learning objective of this project is the WiFi and network part. But I am quite lost and confused. May I know how do I send this data to the PC via WiFi? I have a WiFi dongle connecting to the Raspberry Pi. I read some post about setting the RPi as a server, may I know how exactly to do that?

Thanks alot for helping.

  • You say you're not familiar with Python - is there a language you're more comfortable with, or existing infrastructure you need to interact with? – goobering Apr 22 '16 at 10:50
  • I am familiar with Java. But I'm willing to learn python as it is an academic project and I have a feeling my professor would prefer me to complete it with language other than Java. I'm doing this in a pair work, my teammate is in charge of generating and updating graph, he will be using Java. – Komgcn Apr 22 '16 at 11:15
2

I'd be inclined towards Flask for this sort of thing. I had very limited Python experience (coming from a C# background) before I started playing with it, and got on just fine.

Flask is a microframework for Python based on Werkzeug, Jinja 2 and good intentions.

I don't think I can provide a complete example of what you're trying to achieve in a reasonable amount of space. There's a decent basic tutorial showing how to serve sensor data from the Pi using the RPi.GPIO Python module at mattrichardson.com. There are further materials on creating a RESTful service using the Flask extension Flask-RESTful on their documentation pages

  • Thanks alot, I have solved the problem using the tutorial from both mattrichardson.com and Flask-RESTful documentation page. Thanks again for the help. – Komgcn Apr 22 '16 at 16:25
2

From the help of user goobering, based on mattrichardson.com and Flask RESTful documentation. I have solved the problem. Here is my solution for anyone who face the same problem. First write a python file in the raspberry pi (with GrovePi github repositories pre-installed for sensor reading).

from flask import Flask
from flask_restful import Resource, Api
import grovepi 

app = Flask(__name__)
api = Api(app)
sensor = 7

class TempHum(Resource):
    def get(self):
         [temp,hum] = grovepi.dht(sensor,0)
         return {'temperature' : temp,
                 'humidity' : hum }

api.add_resource(TempHum, '/')

if __name__ = "__main__":
    app.run(host='0.0.0.0', port=80, debug=True)

then run the python file, the raspberry pi will now act as a server. Type:

hostname -I

to get the ip address for the raspberry pi. Then whenever someone send a GET request to this ip address, it will return a temperature and humidity reading in JSON format.

{ "humidity" : 44, "temperature" : 25 }

Below is my java code for getting the readings in Eclipse every 5 seconds. A JSON parser is used to parsed the result return from the raspberry pi.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.json.*;

public class Monitor {

public static HttpURLConnection con;
public static URL url;
public static String url_string = "http://192.168.1.114";

public static void main(String[] args) {

    List<Double> temperature = new ArrayList<>();
    List<Double> humidity = new ArrayList<>();

    for (int i = 0; i < 10; i++) {
        try {
            url = new URL(url_string);
            con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");
            con.connect();
            InputStream is = con.getInputStream();
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            JSONObject obj = new JSONObject(sb.toString());
            JSONParser parser = new JSONParser();
            double[] readings = parser.parseReading(obj);
            temperature.add(readings[0]);
            humidity.add(readings[1]);
            br.close();
            Thread.sleep(5000);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(i+" read success.");
    }
    for(int i =0;i<10;i++){
        System.out.println("Temperature: "+temperature.get(i)+"C. Humidity: "+humidity.get(i)+"%.");
    }
}

}

1

Since you are to use REST protocol you will need to send the data to the server using HTTP. Python has a fairly extensive library for this called httplib.

In a nutshell you will need to connect to the web server and send the data using an HTTP POST request (or you could use GET but that sort of violates the REST design).

-1

U can use Bluetooth or if u want to send readings through internet then follow this website which may give you some idealink

  • Link only answers are discouraged across StackExchange. This question's quite broad, and not easy to answer in a short form, but can you give any detail at all on how Bluetooth could be used here? – goobering Apr 22 '16 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.