1-Wire in OpenWrt
When you come to think of it a sensor is I think one of the commonest devices in our life. Sensors are everywhere, how terribly it might sound))) All the performance targets for one or another device have in their basis sensor measurement. If one sensor has changed its condition to another it means that that piece of iron has to do that operation...)). I think it is familiar to many of you.
There are a lot of sensor types: temperature sensor, humidity sensor, smoke sensor, gas sensor, liquid level sensor, shock sensor, vibration sensor, motion sensor, current sensor, voltage sensor, frequency sensor, power sensor, light sensor and so on. In spite of such type variety they have nearly identical structure. This is a sensitive sensor which gives us in output different voltage levels. It means that our target only to learn how to measure the voltage and then we can construct any sensor.
Therefore I suggest here to learn how to measure the voltage and at the same time to measure the temperature value on the basis of one-wire sensor DS2438.
On the stage of configuration OpenWrt the following modules are to be selected:
Kernel modules:
USB Support:
<M> kmod-usb-serial
<M> kmod-usb-serial-cp210x
Utilities:
Filesystem:
<M> owfs
<M> owshell
<M> digitemp
The operation with the 1-wire in OpenWrt is possible by using of 2 programs. Let’s consider the program digitemp.
Let’s install the packages:
root@OpenWrt:/# opkg install kmod-usb-serial-cp210x Installing kmod-usb-serial-cp210x (2.6.36.2-1) to root... Downloading ftp://ftp:ftp@192.168.0.9/kmod-usb-serial-cp210x_2.6.36.2-1_brcm47xx.ipk. Installing kmod-usb-serial (2.6.36.2-1) to root... Downloading ftp://ftp:ftp@192.168.0.9/kmod-usb-serial_2.6.36.2-1_brcm47xx.ipk. Configuring kmod-usb-serial. Configuring kmod-usb-serial-cp210x. root@OpenWrt:/# opkg install digitemp Installing digitemp (3.6.0-3) to root... Downloading ftp://ftp:ftp@192.168.0.9/digitemp_3.6.0-3_brcm47xx.ipk. Configuring digitemp. |
The network structure of 1-wire consists of an adaptor and sensors themselves. The adaptor modifies the protocol of USB in 1-wire. And the sensors can be connected one by one. The adaptor schema and the sensor schema you can find in the section “Download”.
Let’s connect the adaptor to the usb-port of the router and check the output dmesg:
root@OpenWrt:/# dmesg usb 1-1.4: new full speed USB device using ohci_hcd and address 4 cp210x 1-1.4:1.0: cp210x converter detected usb 1-1.4: reset full speed USB device using ohci_hcd and address 4 usb 1-1.4: cp210x converter now attached to ttyUSB0 |
The adaptor is determinated by the system as ttyUSB0. Now let’s connect a sensor to the adaptor. To use digitemp is comparatively easy. At first we should initialize in the following way:
root@OpenWrt:/# digitemp_DS9097 -i -s /dev/ttyUSB0 DigiTemp v3.5.0 Copyright 1996-2007 by Brian C. Lane GNU Public License v2.0 - http://www.digitemp.com Turning off all DS2409 Couplers . Searching the 1-Wire LAN 265576BA000000DF : DS2438 Temperature, A/D Battery Monitor ROM #0 : 265576BA000000DF Wrote .digitemprc |
As we can see the digitemp has started in this operation the procedure of devices’searching on the bus of 1-wire. As a result our sensor has been determinated — 265576BA000000DF : DS2438 Temperature, A/D Battery Monitor.
Now for reading of the temperature and voltage values from our sensor we should run the following operation:
root@OpenWrt:/# digitemp_DS9097 -a -A -l /home/1wire_log DigiTemp v3.5.0 Copyright 1996-2007 by Brian C. Lane GNU Public License v2.0 - http://www.digitemp.com |
Let’s open the file with the log and check what the temperature and the voltage are equal to in the adc-input.
root@OpenWrt:/# cat /home/1wire_log Jan 01 00:01:44 Sensor 0 VDD: 5.06 AD: 4.98 CAD: -1018 C: 24.59 |
As we can see the temperature is equal to 24.59C, and the voltage to 4.98В. Accuracy of measuring is high enough. Here the operation description of digitemp can be ended. It is worth to add that you can see the list of available options by writing of digitemp_DS9097 -h.
For considering the program owfs let’s install some additional packages:
root@OpenWrt:/# opkg install owshell owfs Installing owshell (2.8p4-1) to root... Downloading ftp://ftp:ftp@192.168.0.9/owshell_2.8p4-1_brcm47xx.ipk. Installing libow (2.8p4-1) to root... Downloading ftp://ftp:ftp@192.168.0.9/libow_2.8p4-1_brcm47xx.ipk. Installing libusb (0.1.12-2) to root... Downloading ftp://ftp:ftp@192.168.0.9/libusb_0.1.12-2_brcm47xx.ipk. Installing libpthread (0.9.31-64) to root... Downloading ftp://ftp:ftp@192.168.0.9/libpthread_0.9.31-64_brcm47xx.ipk. Installing librt (0.9.31-64) to root... Downloading ftp://ftp:ftp@192.168.0.9/librt_0.9.31-64_brcm47xx.ipk. Installing owfs (2.8p4-1) to root... Downloading ftp://ftp:ftp@192.168.0.9/owfs_2.8p4-1_brcm47xx.ipk. Installing libfuse (2.8.5-1) to root... Downloading ftp://ftp:ftp@192.168.0.9/libfuse_2.8.5-1_brcm47xx.ipk. Installing kmod-fuse (2.6.36.2-1) to root... Downloading ftp://ftp:ftp@192.168.0.9/kmod-fuse_2.6.36.2-1_brcm47xx.ipk. Installing fuse-utils (2.8.5-1) to root... Downloading ftp://ftp:ftp@192.168.0.9/fuse-utils_2.8.5-1_brcm47xx.ipk. Configuring librt. Configuring libpthread. Configuring libusb. Configuring libow. Configuring owshell. Configuring kmod-fuse. Configuring libfuse. Configuring fuse-utils. Configuring owfs. |
Owfs — is a 1-wire file system. It creates different logs and monitors everything! Owhttpd — is the same system but with using of web-server. Owfs is an ideal variant for building of one-wire networks. Everything is thought-out meticulously and there are a lot of capabilities.
The application Owhttpd in the current version of OpenWrt cannot be copied to the file system by installing of a package. Therefore you can copy the file owhttpd from there:
openwrt/build_dir/target-mipsel_uClibc-0.9.31/owfs-2.8p4/module/owhttpd/src/c/.libs
By starting of owfs we should determine a mount point and an adaptor type (I have this one: ds9097).
root@OpenWrt:/# owfs -d /dev/ttyUSB0 /home/1wire/ |
For debug mode we should start with the flag --debug. After starting of owfs a file system is created in the mounted directory:
root@OpenWrt:/# ls -l /home/1wire/ drwxr-xr-x 1 root root 8 Jan 1 00:02 drwxrwxrwx 1 root root 8 Jan 1 00:02 26.5576BA000000 drwxr-xr-x 1 root root 8 Jan 1 00:02 settings drwxrwxrwx 1 root root 8 Jan 1 00:02 simultaneous drwxr-xr-x 1 root root 8 Jan 1 00:02 statistics drwxr-xr-x 1 root root 30 Jan 1 00:02 structure drwxr-xr-x 1 root root 8 Jan 1 00:02 system drwxr-xr-x 1 root root 8 Jan 1 00:02 uncached |
To find out the temperature value let’s check the information content of the sensor directory:
root@OpenWrt:/# ls -l /home/1wire/26.5576BA000000 -r--r--r-- 1 root root 16 2011-02-16 20:52 address -r--r--r-- 1 root root 256 2011-02-16 20:52 alias drwxrwxrwx 1 root root 8 2011-02-16 20:56 B1-R1-A -rw-rw-rw- 1 root root 1 2011-02-16 20:56 CA -r--r--r-- 1 root root 2 2011-02-16 20:52 crc8 -rw-rw-rw- 1 root root 24 2011-02-16 20:52 date drwxrwxrwx 1 root root 8 2011-02-16 20:56 disconnect -rw-rw-rw- 1 root root 1 2011-02-16 20:56 EE drwxrwxrwx 1 root root 8 2011-02-16 20:56 endcharge -r--r--r-- 1 root root 2 2011-02-16 20:52 family drwxrwxrwx 1 root root 8 2011-02-16 20:56 HIH3600 drwxrwxrwx 1 root root 8 2011-02-16 20:56 HIH4000 drwxrwxrwx 1 root root 8 2011-02-16 20:56 HTM1735 -r--r--r-- 1 root root 12 2011-02-16 20:52 humidity -rw-rw-rw- 1 root root 1 2011-02-16 20:56 IAD -r--r--r-- 1 root root 12 2011-02-16 20:52 id -r--r--r-- 1 root root 16 2011-02-16 20:52 locator drwxrwxrwx 1 root root 8 2011-02-16 20:56 MultiSensor -rw-rw-rw- 1 root root 12 2011-02-16 20:56 offset drwxrwxrwx 1 root root 8 2011-02-16 20:56 pages -r--r--r-- 1 root root 16 2011-02-16 20:52 r_address -r--r--r-- 1 root root 12 2011-02-16 20:52 r_id -r--r--r-- 1 root root 16 2011-02-16 20:52 r_locator drwxrwxrwx 1 root root 8 2011-02-16 20:56 S3-R1-A -r--r--r-- 1 root root 12 2011-02-16 20:52 temperature -r--r--r-- 1 root root 32 2011-02-16 20:52 type -rw-rw-rw- 1 root root 12 2011-02-16 20:56 udate -r--r--r-- 1 root root 12 2011-02-16 20:56 VAD -r--r--r-- 1 root root 12 2011-02-16 20:56 VDD -r--r--r-- 1 root root 12 2011-02-16 20:56 vis |
If a humidity sensor was connected to the adc-input of the microcircuit ds2438, we could check also the humidity. But we check only the temperature value:
root@OpenWrt:/# cat ./temperature 22.3125 |
And now let’s start the web-server and check its options:
root@OpenWrt:/# owhttpd -d /dev/ttyUSB0 -p 8080 |
Later I upgrade the information and we can see how we can build diagrams using current logs.