After you have successfully installed CouchDB as decribed here, you can simply start it like this:
~$ couchdb start
This short example is build upon the offical tutorial of the core API. Lets create a new database called temperatures.
~$ curl -X PUT http://127.0.0.1:5984/temperatures
Inserting new documents into the temperatures database is as simple as creating a database:
~$ curl -X PUT http://127.0.0.1:5984/temperatures/e7e56c6c-822f-442d-a707-973e20fc8d87 -d '{"celsius": "23.73", "timestamp": 1461326744.1631064}'
I used the Python3 uuid module to create the UUID e7e56c6c-822f-442d-a707-973e20fc8d87 which is used as the document ID. I also created a view called "by-timestamp" inside the design document with the help of Futon and wrote the following map function:
function(doc) {
var celsius, timestamp;
if (doc.celsius && doc.timestamp) {
celsius = doc.celsius;
timestamp = doc.timestamp;
emit(timestamp, {temperature: celsius});
}
}
Since the view can be parameterized with sort and filter options, its possible to make a GET request that yields the newest document:
http://127.0.0.1:5984/temperatures/_design/foo/_view/by-timestamp?descending=true&limit=1
Furthermore, I created a show called "celsius" in the design document, which adds some HTML around the document content. To insert a show into the design document, the key "shows" must be added with the following value:
{
"celsius" : "function (doc, req) {
return '<p>Temperature: ' + doc.celsius + '°C</p>';
}"
}
The following URL can be used to query the celsius show with a document ID:
http://127.0.0.1:5984/temperaturues/_design/foo/_show/celsius/1644712d-e5ab-48c3-aaa7-ac6eb75b750b
That's it! Any easy way to persist your data, but the concepts behind views and shows are a bit tricky.
Sunday, April 24, 2016
Thursday, April 7, 2016
Handling null in Scala
When you are using Java APIs, you might find yourself in the situation that you have to deal with null. In plain Scala, usually you wouldn't use null, but use an Option instead.
The apply method of the Option singleton object has a nice way to convert null to None (see Option.scala):
Hence, you can do Option(null) and you'll get None.
A simple example that handles null in a safe way:
val maybeNull = someJavaMethodThatMightReturnNull(42)
Option(maybeNull).foreach(println)
On the other hand, if you do Some(null) you'll get back Some(null).
The apply method of the Option singleton object has a nice way to convert null to None (see Option.scala):
def apply[A](x: A): Option[A] = if (x == null) None else Some(x)
Hence, you can do Option(null) and you'll get None.
A simple example that handles null in a safe way:
val maybeNull = someJavaMethodThatMightReturnNull(42)
Option(maybeNull).foreach(println)
On the other hand, if you do Some(null) you'll get back Some(null).
Wednesday, March 30, 2016
Measuring water temperature in food-safe applications with Raspberry PI + PT100 sensor + Tinkerforge PTC Bricklet
I was looking for a temperature sensor (thermometer) that can be used with a Raspberry PI for measuring water temperature in food-safe (lebensmittelecht) applications.
The sensor should satisfy the following requirements:
The problem is, that a PT100 sensor cannot just be connected with a Raspberry PI, because you need an analog-digital converter. Lucky as I am, the company Tinkerforge offers some very nice modules to improve this situation. To connect a PT100 temperature sensor, you can use the Tinkerforge PTC Bricklet which gives you the current temperatur in Celsius as an integer number. Its getting even better: Tinkerforge offers an easy to use API for the "most popular" programming languages - including Python.
To connect the Tinkerforge PTC Bricklet with the Raspberry PI, you also need the Tinkerforge Master Brick, which is capable of connecting up to four Bricklets and has a mini USB connector to connect it with a computer.
The first thing to do, is to install brickd. When brickd is up and running, I recommend to change the following two lines in /etc/brickd.conf:
ipcon.authenticate('topsecret')
Replace 'topsecret' with your authentication password. Before you can make use of the Tinkerforge API, you have to install the bindings for the language that you want to use. In our case, the easiest way to do that, is to download the ZIP file with the Python bindings and copy the directory source/tinkerforge to your prefered location on your harddisc. To be able to import the bindings from a Python script, just copy the Python script to the same location where you copied the tinkerforge directory. After this is all done, you can run the script and it will output all connected devices and their corresponding identifiers.
The sensor should satisfy the following requirements:
- food-safe
- waterproof
- heat resistant until at least 100°C
The problem is, that a PT100 sensor cannot just be connected with a Raspberry PI, because you need an analog-digital converter. Lucky as I am, the company Tinkerforge offers some very nice modules to improve this situation. To connect a PT100 temperature sensor, you can use the Tinkerforge PTC Bricklet which gives you the current temperatur in Celsius as an integer number. Its getting even better: Tinkerforge offers an easy to use API for the "most popular" programming languages - including Python.
To connect the Tinkerforge PTC Bricklet with the Raspberry PI, you also need the Tinkerforge Master Brick, which is capable of connecting up to four Bricklets and has a mini USB connector to connect it with a computer.
The first thing to do, is to install brickd. When brickd is up and running, I recommend to change the following two lines in /etc/brickd.conf:
- listen.address = 127.0.0.1
- authentication.secret = topsecret
ipcon.authenticate('topsecret')
Replace 'topsecret' with your authentication password. Before you can make use of the Tinkerforge API, you have to install the bindings for the language that you want to use. In our case, the easiest way to do that, is to download the ZIP file with the Python bindings and copy the directory source/tinkerforge to your prefered location on your harddisc. To be able to import the bindings from a Python script, just copy the Python script to the same location where you copied the tinkerforge directory. After this is all done, you can run the script and it will output all connected devices and their corresponding identifiers.
Labels:
food-safe,
lebensmittelecht,
Master Brick,
PT100,
PTC Bricklet,
Python,
Raspberry PI,
temperature sensor,
thermometer,
Tinkerforge
Subscribe to:
Posts (Atom)