Capture temperature data with the ESP8266 and the LM75 Sensor
Java Installation
The first step involves building the server. The "Lite" version of Raspbian is perfectly suited to act as the foundation for a Raspberry Pi based server.
There are no special requirements for the Java Development Kit (JDK, so you don't need to download the most recent one from the Oracle homepage. Install the existing JDK for Raspbian (Listing 2). Use MySQL for the database. The installation for this program is described in the Setting Up MySQL box.
Listing 2
Installing oracle-java7-jdk
$ sudo apt-get update $ sudo apt-get upgrade $ sudo apt-get install oracle-java7-jdk
Setting Up MySQL
MySQL is one of the most popular and widely used open source databases. It is suitable for small to mid-sized projects. Install it in Raspbian with the following commands:
$ sudo apt-get install mysql-server
During installation, the software will ask you for the root password for the database. Be sure to remember this. Apart from that, the installation will run without any further prompting from you.
To test if the installation has been successful, use the commands from Listing 3. You will then be connected to the database and can check if everything's in working order.
Listing 3
MySQL – Trial Run
$ mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 44 Server version: 5.5.40-0+wheezy1 (Debian) Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ 3 rows in set (0.01 sec)
You now need to set up a new database on the freshly installed MySQL server. This database should only contain one table. The Temperature table outlines the structure for this. The createDB.sql script (Listing 4) cane be downloaded from 7 and will set up a table for you once you call it via a terminal:
Table 2
Temperature
Name | Type |
---|---|
ts |
timestamp |
temp |
int |
Listing 4
createDB.sql
create database Temperature; use Temperature; create table Temperature (ts timestamp, temp int); GRANT ALL ON Temperature.* TO 'user'@'localhost' IDENTIFIED BY 'pass'; FLUSH PRIVILEGES;
$ mysql -u root -p < createDB.sql
After you have installed all of the necessary components on the Raspberry Pi, copy the REST server (Listing 5) to the Raspberry Pi and start it. To do this you will need the free community version from the IntelliJ-Idea-Project [5]. This is a Maven project (see the Maven box), which you can build with the usual Java methods.
Maven
The Maven framework helps you build and standardize Java projects. By following the prescribed conventions, you can easily create and administer software. The conventions primarily have to do with the structure of the project, meaning the project directories and names.
The central feature of Maven is the project object mode file pom.xml. This is where you will specify the dependencies for other components. These in turn have dependencies on other projects. Thus, a project tree will load when the program starts for the first time. Luckily, Maven stores all of these components in a local repository so that the build process runs quickly after the first run.
The word Maven is Yiddish for "accumulator of knowledge" which is quite fitting, as this is exactly what the software does by gathering all package dependencies.
Listing 5
TempRest.jar
01 import com.sun.jersey.api.container.httpserver.HttpServerFactory; 02 import com.sun.net.httpserver.HttpServer; 03 import javax.ws.rs.*; 04 import javax.ws.rs.core.MediaType; 05 import java.sql.Connection; 06 import java.sql.DriverManager; 07 import java.sql.Statement; 08 @Path( "setTemp" ) 09 public class SetTemp { 10 static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; 11 static final String DB_URL = "jdbc:mysql://localhost/Temperatur"; 12 static final String USER = "user"; 13 static final String PASS = "pass"; 14 Connection conn = null; 15 Statement stmt = null; 16 @GET @Produces( MediaType.TEXT_PLAIN ) 17 public String setTemp() { 18 System.out.println("normal"); 19 return "Saved"; 20 } 21 @GET @Produces( MediaType.TEXT_PLAIN ) 22 @Path( "temp/{temp}" ) 23 public String setTemp( @PathParam("temp") String temp) { 24 try { 25 Class.forName(JDBC_DRIVER); 26 conn = DriverManager.getConnection(DB_URL, USER, PASS); 27 stmt = conn.createStatement(); 28 String sql; 29 sql = "INSERT INTO Temperature values (now()," + temp + ")"; 30 stmt.executeUpdate(sql); 31 System.out.println(temp); 32 stmt.close(); 33 conn.close(); 34 } 35 catch (Exception e) { 36 e.printStackTrace(); 37 } 38 return "Saved: " + temp; 39 } 40 public static void main(String[] args) throws Exception { 41 HttpServer server = HttpServerFactory.create( "http://localhost:8080/rest" ); 42 server.start(); 43 System.out.println("To end please press [Enter] "); 44 System.in.read(); 45 server.stop( 0 ); 46 } 47 }
Copy the file TempRest.jar from the target/ folder to the Raspberry Pi.
From here you can use the following command to start the server:
$ java -jar TempRest.jar
As soon the server receives a request from the ESP8266, the data will be saved to the database. There is a useful extension for Google Chrome for debugging REST services called the Advanced REST Client AApplication (Figure 5).

Analysis
There are definitely many things that can be done with the measurement values once they're in the database. All of the established Office programs have a database module for easily pulling measurement values into a calculation table for instance. You can create a chart with the table with just a few mouse clicks as well if you wish.
Figure 6 shows an example of the data captured by our test system. The high values are due to the fact that the sensor was in a greenhouse.
« Previous 1 2 3 Next »
Buy this article as PDF
Pages: 7
(incl. VAT)