Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
xaelsouth committed Apr 4, 2017
0 parents commit ebe0a8b
Show file tree
Hide file tree
Showing 29 changed files with 2,727 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
RM=rm
MKDIR=mkdir
CC=gcc
STRIP=strip

OUTDIR=build
OUTFILE="$(OUTDIR)/rtl_wmbus"
CFLAGS=-Iinclude -std=gnu99
CFLAGS_WARNINGS=-Wall -W -Waggregate-return -Wbad-function-cast -Wcast-align -Wcast-qual -Wchar-subscripts -Wcomment -Wfloat-equal -Winline -Wmain -Wmissing-noreturn -Wmissing-prototypes -Wparentheses -Wpointer-arith -Wredundant-decls -Wreturn-type -Wshadow -Wsign-compare -Wstrict-prototypes -Wswitch -Wunreachable-code -Wunused -Wuninitialized
LIB=-lm
SRC=rtl_wmbus.c

all: $(OUTDIR) release

$(OUTDIR):
$(MKDIR) -p "$(OUTDIR)"

release: $(OUTDIR)
$(CC) -msse4.2 -DNDEBUG -O3 $(CFLAGS) $(CFLAGS_WARNINGS) -o $(OUTFILE) $(SRC) $(LIB)

debug: $(OUTDIR)
$(CC) -msse4.2 -DDEBUG -O0 -g3 -ggdb -p -pg $(CFLAGS) $(CFLAGS_WARNINGS) -o $(OUTFILE) $(SRC) $(LIB)

# Will build on Raspberry Pi 1 only
pi1:
$(CC) -DNDEBUG -O3 -march=armv6 -mtune=arm1176jzf-s -mfloat-abi=hard -mfpu=vfp -ffast-math $(CFLAGS) $(CFLAGS_WARNINGS) -o $(OUTFILE) $(SRC) $(LIB)

rebuild: clean all

clean:
$(RM) -rf "$(OUTDIR)"
106 changes: 106 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@

rtl-wmbus - Software Decoder for Wireless-M-Bus with RTL-SDR
------------------------------------------------------------

rtl-wmbus is a software-defined radio receiver for Wireless-M-Bus. It is
written in plain C and uses RTL-SDR to interface with RTL2832-based hardware.

Wireless-M-Bus is the wireless version of M-Bus ("Meter-Bus", http://www.m-bus.com),
which is an European standard for remote reading of smart meters.

The primary purpose of rtl-wmbus is experimenting with digital signal
processing and software radio. rtl-wmbus can be used on resource constrained
devices such Raspberry Pi Zero or Raspberry PI B+ overclocked to 1GHz.
Any Android based tablet will do the same too.

rtl-wmbus provides:
* FSK demodulating
* clock recovery
* mode T1 and mode C1 packet decoding

rtl-wmbus requires:
* Linux or Android
* C99
* supported DVB-T receiver
* RTL-SDR library for Linux: http://sdr.osmocom.org/trac/wiki/rtl-sdr or
* (optional, only for android builds) RTL-SDR library port for Android: https://github.com/martinmarinov/rtl_tcp_andro-

For the latest version, see https://github.com/xaelsouth/rtl-wmbus


Installing
----------

The Osmocom RTL-SDR library must be installed before you can build
rtl-wmbus. See http://sdr.osmocom.org/trac/wiki/rtl-sdr for more
information. RTL-SDR library for Android would be installed via
Google Play.

To install rtl-wmbus, download, unpack the source code and go to the
top level directory. Then use one of these three options:

$ make debug # (no optimization at all, with debug options)

$ make release # (-O3 optimized version, without any debugging options)

$ make pi1 # (Raspberry Pi optimized version, without any debugging options,
will build on RasPi1) only

Before building Android version the SDK and NDK have to be installed.
See androidbuild.bat for how to build.

Usage
-----
Shown using "release"-version:

To save IQ-stream on disk and decode them off-line:

$ rtl_sdr samples.bin -f 868.9M -s 1600000

$ cat samples.bin | build/rtl_wmbus

To run continuously:

$ rtl_sdr -f 868.9M -s 1600000 - 2>/dev/null | build/rtl_wmbus

To count "good" (no 3 out of 6 errors, no checksum errors) packets:

$ cat samples.bin | build/rtl_wmbus 2>/dev/null | grep "[T,C]1;1;1" | wc -l

Carrier-frequency given at "-f" must be set properly. With my DVB-T-
Receiver I had to choose carrier 50kHz under the standard of 868.95MHz.
Sample rate at 1.6Ms/s is hardcoded and cannot be changed.

samples2.bin is a "life" example with two devices received.

On Android first the driver must be started with options given above.
IQ-data goes to a port which is would be already set by driver settings.
Use get_net to get IQ-data into rtl_wmbus.

License
-------

Copyright (c) 2017 <[email protected]>

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
16 changes: 16 additions & 0 deletions androidbuild.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@echo off

rem SETTINGS
set SDK_ROOT=c:\Android\sdk
set NDK_ROOT=c:\Android\android-ndk-r10e
set SYSROOT=%NDK_ROOT%\platforms\android-21\arch-arm
set CC=%NDK_ROOT%\toolchains\arm-linux-androideabi-4.9\prebuilt\windows-x86_64\bin\arm-linux-androideabi-gcc --sysroot=%SYSROOT%

rem BUILD
rd /S /Q Android
md Android
%CC% -o Android/rtl_wmbus rtl_wmbus.c -std=gnu99 -Iinclude -Wall -O3 -g0 -lm

rem INSTALL
%SDK_ROOT%\platform-tools\adb push Android\rtl_wmbus /mnt/sdcard/wmbus

77 changes: 77 additions & 0 deletions atan2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#ifndef ATAN2_H
#define ATAN2_H

#include <math.h>


static inline float atan2_libm(float complex y)
{
return carg(y) * (float)M_1_PI;
}


/** https://gist.github.com/volkansalma/2972237 */
static inline float atan2_approximation(float complex s)
{
static const float ONEQTR_PI = M_PI / 4.0;
static const float THRQTR_PI = 3.0 * M_PI / 4.0;

float y = cimagf(s), x = crealf(s);
float r, angle;
float abs_y = fabs(y) + 1e-10f; // kludge to prevent 0/0 condition

if (x < 0.0f)
{
r = (x + abs_y) / (abs_y - x);
angle = THRQTR_PI;
}
else
{
r = (x - abs_y) / (x + abs_y);
angle = ONEQTR_PI;
}

angle += (0.1963f*(float)M_1_PI * r * r - 0.9817f*(float)M_1_PI) * r;

if (y < 0.0f)
angle = -angle; // negate if in quad III or IV

return angle;
}


/** https://gist.github.com/volkansalma/2972237 */
static inline float atan2_approximation2(float complex s)
{
float y = cimagf(s), x = crealf(s);

if (x == 0.0f)
{
if (y > 0.0f) return 0.5f;
if (y == 0.0f) return 0.0f;
return -0.5f;
}

float atan;
const float z = y / x;

if (fabs(z) < 1.0f)
{
atan = z / (1.0f*(float)M_PI + 0.28086f*(float)M_PI*z*z);
if (x < 0.0f)
{
if (y < 0.0f) return atan - 1.0f;
return atan + 1.0f;
}
}
else
{
atan = 0.5f - z / (z*z + 0.28086f) * (float)M_1_PI;
if ( y < 0.0f ) return atan - 1.0f;
}

return atan;
}


#endif /* ATAN2_H */
29 changes: 29 additions & 0 deletions filter/bp_iir_cheb1_1600kHz_90kHz_98kHz_102kHz_110kHz.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
clear all;

samplerate = 1600e3;
nyqistrate = samplerate/2;

Ws1 = 90e3/nyqistrate;
Wp1 = 98e3/nyqistrate;
Wp2 = 102e3/nyqistrate;
Ws2 = 110e3/nyqistrate;
Rp = 1;
Rs = 40;

% seems to be the best
[n, Wc] = cheb1ord([Wp1, Wp2], [Ws1, Ws2], Rp, Rs);
[b, a] = cheby1(n, Rp, Wc);

% does not work at all
%[n, Wc] = cheb2ord([Wp1, Wp2], [Ws1, Ws2], Rp, Rs);
%[b, a] = cheby2(n, Rp, Wc);

% performs badly
%[n, Wc] = ellipord([Wp1, Wp2], [Ws1, Ws2], Rp, Rs);
%[b, a] = ellip(n, Rp, Rs, Wc);

% big filter order - unpracticable
%[n, Wc] = buttord([Wp1, Wp2], [Ws1, Ws2], Rp, Rs);
%[b, a] = butter(n, Wc);

print_iir_filter_coef(b ,a);
29 changes: 29 additions & 0 deletions filter/bp_iir_cheb1_400kHz_90kHz_98kHz_102kHz_110kHz.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
clear all;

samplerate = 400e3;
nyqistrate = samplerate/2;

Ws1 = 90e3/nyqistrate;
Wp1 = 98e3/nyqistrate;
Wp2 = 102e3/nyqistrate;
Ws2 = 110e3/nyqistrate;
Rp = 1;
Rs = 40;

% seems to be the best
[n, Wc] = cheb1ord([Wp1, Wp2], [Ws1, Ws2], Rp, Rs);
[b, a] = cheby1(n, Rp, Wc);

% does not work at all
%[n, Wc] = cheb2ord([Wp1, Wp2], [Ws1, Ws2], Rp, Rs);
%[b, a] = cheby2(n, Rp, Wc);

% performs badly
%[n, Wc] = ellipord([Wp1, Wp2], [Ws1, Ws2], Rp, Rs);
%[b, a] = ellip(n, Rp, Rs, Wc);

% big filter order - unpracticable
%[n, Wc] = buttord([Wp1, Wp2], [Ws1, Ws2], Rp, Rs);
%[b, a] = butter(n, Wc);

print_iir_filter_coef(b ,a);
29 changes: 29 additions & 0 deletions filter/bp_iir_cheb1_800kHz_90kHz_98Hz_102kHz_110kHz.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
clear all;

samplerate = 800e3;
nyqistrate = samplerate/2;

Ws1 = 90e3/nyqistrate;
Wp1 = 98e3/nyqistrate;
Wp2 = 102e3/nyqistrate;
Ws2 = 110e3/nyqistrate;
Rp = 1;
Rs = 40;

% seems to be the best
[n, Wc] = cheb1ord([Wp1, Wp2], [Ws1, Ws2], Rp, Rs);
[b, a] = cheby1(n, Rp, Wc);

% does not work at all
%[n, Wc] = cheb2ord([Wp1, Wp2], [Ws1, Ws2], Rp, Rs);
%[b, a] = cheby2(n, Rp, Wc);

% performs badly
%[n, Wc] = ellipord([Wp1, Wp2], [Ws1, Ws2], Rp, Rs);
%[b, a] = ellip(n, Rp, Rs, Wc);

% big filter order - unpracticable
%[n, Wc] = buttord([Wp1, Wp2], [Ws1, Ws2], Rp, Rs);
%[b, a] = butter(n, Wc);

print_iir_filter_coef(b ,a);
15 changes: 15 additions & 0 deletions filter/lp_fir_butter_1600kHz_160kHz_200kHz.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
clear all;

samplerate = 1600e3;
nyqistrate = samplerate/2;

Wp1 = 160e3/nyqistrate;
Ws1 = 200e3/nyqistrate;
Rp = 1;
Rs = 40;

[n, Wc] = buttord(Wp1, Ws1, Rp, Rs);
[b] = fir1(n, Wc);

print_fir_filter_coef(b);

15 changes: 15 additions & 0 deletions filter/lp_fir_butter_800kHz_100kHz_10kHz.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
clear all;

samplerate = 800e3;
nyqistrate = samplerate/2;

Wp1 = 100e3/nyqistrate;
Ws1 = 10e3/nyqistrate;
Rp = 1;
Rs = 40;

[n, Wc] = buttord(Wp1, Ws1, Rp, Rs);
[b] = fir1(n, Wc);

print_fir_filter_coef(b);

27 changes: 27 additions & 0 deletions filter/lp_ppf_butter_1600kHz_160kHz_200kHz.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
clear all;

samplerate = 1600e3;
nyqistrate = samplerate/2;

Wp1 = 160e3/nyqistrate;
Ws1 = 200e3/nyqistrate;
Rp = 1;
Rs = 40;

[n, Wc] = buttord(Wp1, Ws1, Rp, Rs);
[b] = fir1(n, Wc);

x = 1:22;
y = filter(b,1,x);
y = y(2:2:end);

phase_channels = 2;
b_poly = buffer(b, phase_channels);
y1 = filter(b_poly(2,:), 1, x(1:2:end));
y2 = filter(b_poly(1,:), 1, x(2:2:end));

y_poly = y1 + y2;

y_err = y - y_poly;

print_ppf_filter_coef(b, phase_channels);
14 changes: 14 additions & 0 deletions filter/print_fir_filter_coef.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function [] = print_fir_filter_coef(b);

[R, C] = size(b);

fprintf(stdout, "#define COEFFS %u\n", C);

fprintf(stdout, "static const float b[COEFFS] = {");
for c = 1:C,
fprintf(stdout, "%.10g, ", b(c));
end
fprintf(stdout, "};\n");

fprintf(stdout, "#undef COEFFS\n");

Loading

0 comments on commit ebe0a8b

Please sign in to comment.