Measuring a Temperature with DS18B20

DS18B20 is a digital temperature sensor which measures temperatures from -55°C to +125°C. It uses 1-Wire communication protocol. We have only one data pin for sending and receiving data. For more information see DS18B20 datasheet.

In this post I will use Raspberry PI-III as a micro controller. For data pin I chose GPIO26. Before using this pin as a 1-Wire, we need to enable 1-Wire communication. You can do it raspi-config or adding the lines below end of the /boot/config.txt file and reboot your Raspberry.









After reboot the system. We should check whether or not device is connected properly. You can see below configuration looks good. 28-0000010edf01 is the my device.

Each DS18B20 contains a unique ROM code that is 64-bits long. The first 8 bits are a 1-Wire family
code (DS18B20 code is 28h). The next 48 bits are a unique serial number. The last 8 bits are a CRC of the first 56 bits.

pi@raspberrypi:~ $ ls -l /sys/bus/w1/devices/28-0000010edf01
lrwxrwxrwx 1 root root 0 Dec 25 08:02 /sys/bus/w1/devices/28-0000010edf01 -> ../../../devices/w1_bus_master1/
cat /sys/bus/w1/devices/28-0000010edf01/driver/28-0000010edf01/w1_slave

5c 01 4b 46 7f ff 04 10 a1 : crc=a1 YES
5c 01 4b 46 7f ff 04 10 a1 t=21750

As you see output above t is the temperature. Bu we need to some calculations conver to Celcius.

echo $(cat /sys/bus/w1/devices/28-0000010edf01/w1_slave | tail -n +2 | cut -f 2 -d '=') | awk '{x=$1}END{print(x/1000)}'
while true
                echo $(cat /sys/bus/w1/devices/28-0000010edf01/w1_slave | tail -n +2 | cut -f 2 -d '=') | awk '{x=$1}END{print(x/1000)}'
                sleep 2


Final Result.









Redirecting http to https

Do you have  a SSL certificated web site and your audience still connects you via  http because of their habit ? Easy way to handle this problem is redirecting http requests to https. By doing that any http requests are redirected to the https. To do that we will use Apache web server (httpd RHEL,CentOS).

Apache is the world’s most used web server software. It has many features loadbalancing, cgi support, headers and content rewriting, URL rewriting etc,. In this post we will use URL rewriting capability of Apache, which is supported by mod_rewrite.

mod_rewrite module provides flexible and powerful way to URLs using an unlimited number of rules. By default, mod_rewrite maps a URL to a filesystem path. However, it can also be used to redirect one URL to another URL, or to invoke an internal proxy fetch.

You can add the configuration below either .htacces or apache2.conf (httpd.conf on RHEL, CentOS).

#Redirect permanently any request, which comes from Port 80(http) to https.
<IfModule mod_rewrite.c>
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$$1 [R=301,L]

Do not forget to enable mod_rewrite module. You can check with apache2ctl or httpd utility.

On Debian, Ubuntu system.

root@debian:/etc/apache2# apache2ctl -M|grep rewrite

On RHEL, CentOS system.

[root@centos7 media]# httpd -M| grep rewrite

You can track the requests http to https with curl -v.