click for Part 3
click for Part 4
Introduction
In this series I am going to make a network based temperature logger using a one wire temperature sensor (this is a digital sensor and readily available online) and a BeagleBone Black (BBB). At this early stage I plan to have fairly minimal functionality, with a server running on the BBB which periodically measures the temperature, and then access this over the internet using a client on another machine.The server will be written in C and make use of SQLite for the database. When it is set to run it will log the temperature at a regular interval, simultaneously recording the temperature and the time of measurement. The client will probably be written to run in a web browser using HTML with ReactJS. The client will be able to set some parameters in the server, such as turning on or off the data collection (of the temperature at a regular interval) and changing the interval between measurements. The client will also be able to obtain data from the server, plot a graph of it and download it to a file.
In this entry I will concentrate on getting a fairly complete working version of the server, and use a very crude client written in C to test its functionality.
Setting up the One Wire Sensor
To begin with it is necessary to connect and configure the one wire sensor on the BBB. This is easy to do if you have a new enough version of the Linux kernel, and I strongly recommend updating if you are having trouble. I am using Debian Stretch with kernel 4.14.67, for which everything worked very smoothly. Once you have a new enough system you can follow the simple instructions on this page https://github.com/beagleboard/linux/issues/142 and you should be in good shape.The probe has three wires, a positive, a negative, and a signal. The signal needs to be configured with a pull up resistor. The probe is nominally 5V but you need to use 3.3V on the BBB or risk frying it. Because of this lower voltage I used a 3.3k resistor instead of the recommended 4.7k. The probe's accuracy is not sensitive to this, so it doesn't matter much.
Each probe has its own serial number and this will effect the name of the directory where the driver file is located. In my case I have
/sys/bus/w1/devices/28-041750bdd9ff
where the 28- is the signature for the temperature probe and the 041750bdd9ff is particular to my individual probe. If you navigate into your version of this directory you may issue the command
cat w1_slave
In my case this produced
40 01 4b 46 7f ff 0c 10 bc : crc=bc YES
40 01 4b 46 7f ff 0c 10 bc t=20000
where the YES signifies a successful temperature read and the 20000 gives 20000/1000 = 20 degrees Celsius.
The Server
As a starting point for the server I used the simple bare bones version given here http://www.linuxhowtos.org/C_C++/socket.htm This server makes use of the Linux fork() function to spawn off child processes, so that it can handle multiple client requests at once. The functionality I have added requires a major expansion of this code.As it stands the function of my server depends upon the first character of the stream sent from the client when it initialises communication. When the server is started it does nothing until some instruction is provided from the client. Valid first characters are a, b, c, d & e: providing the following functions
- Starts the server measuring the temperature and logging it to the database with a period that has been determined elsewhere.
- Stops the server from measuring, provided it is currently measuring.
- Reads the data from the data base
- Changes the period for measurement. The initial period is set to 1 minute
- Shuts the server program down completely
create table tbl1(time varchar(23), temperature real).
Lets start the server on port 20001 by first compiling it on the BBB (you will need to change line 170 of server.c to match the directory belonging to your particular probe serial number) and then typing:
server 20001&
at the command prompt in the directory where the server program has been placed. We can then run the client program on another machine, or on the BBB itself. I ran it from a local Linux machine where the BBB can be accessed by the name beaglebone.local. So the client was run providing the following response:
client beaglebone.local 20001
Please enter the message: a
Temperature monitoring started
So upon running the client I was prompted for a message, which I entered as simply the single character a. The client then responds that: "Temperature monitoring started". Next we run the client again:
client beaglebone.local 20001
Please enter the message: d30
delay time set to 30
and this time we entered 'd30' which results in the server's response "delay time set to 30". After some time various entries will build up in the data base. These can be read by the client:
client beaglebone.local 20001
Please enter the message: c
8
2018-11-08T13:38, 18.8
2018-11-08T13:39, 18.8
2018-11-08T14:00, 18.9
2018-11-08T14:30, 19.3
2018-11-08T15:00, 19.5
2018-11-08T15:30, 19.8
2018-11-08T16:00, 20.0
2018-11-08T16:30, 20.1
database output done
In this case we responded with the character c and the server then responded with the database entries recorded so far. The first line from the server gives us the integer 8 which is the number of measurements returned. When the measurements were started the default value for the period of 1 minute was set, then some 3 plus minutes later this was changed to 30 minutes and the measuring continued. This was done in the afternoon when the temperature of the room I was in was increasing. Note that the server automatically synchronises the measurements to coincide with the turn of the hour. If an integer value which is not a factor of 60 (7 for example) is chosen for the period, then the first measurement of the next hour will be shifted forward to fall on the turn.
The Code
If you wish to obtain the code used in this project you can download if from
https://github.com/wilstep/IOT-temperature-logger-for-the-BeagleBone-Black/tree/1.0
Keep in mind that the server needs to be compiled on the BBB and that the client needs to be compiled where it will be used. This could be on the same BBB or another Linux computer. It might compile on OSX using gcc but I haven't tried it.
What's Next?
In the next instalment I will focus on improving the client to provide a GUI interface which plots a graph. The server will probably be tweaked a bit too; for now I would like to make it detect the temperature probe's serial number automatically, add an enumeration type for the response to the client's input and perhaps reference the time periods to the turn of the day rather than the turn of the hour.
Go to part 2
No comments:
Post a Comment