commit 5258d34cf4803ef4c76d41dc9010756bd33eff44 Author: Alex Manning Date: Wed Jan 2 21:11:36 2019 +0000 Initialise git repository again having tidied up. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..402a59b --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +rwdata/* +!.gitinclude +config.json +sender/rf diff --git a/code_sniffing/extract.py b/code_sniffing/extract.py new file mode 100755 index 0000000..b082c3c --- /dev/null +++ b/code_sniffing/extract.py @@ -0,0 +1,133 @@ +#!/usr/bin/python3 +#This script parses and audacity 'sample data' file, and extracts a heating control code to send to the boiler. +#It is very poorly coded and badly commented. You only have to make it work twice though (for on and off). + +import numpy as np +import matplotlib.pyplot as plt + +#load the data +time, value = np.genfromtxt('./sample-data.txt', delimiter=None, dtype=None, unpack=True) + +#digitise the data +digital = [] +for x in value: + if x > 0: + digital.append(1) + else: + digital.append(0) + +if len(value) != len(digital): + raise(ValueError('panic')) + +lentime = time[-1] - time[0] +itemstime = len(time) +sfreq = lentime / itemstime + +#Convert the data to the lengths of time each peak lasts for. +changes = [] +changes.append(time[0]) +nexts = [] +nexts.append(digital[0]) + +last = digital[0] +lastt = time[0] + +#how long is each high or low sent for? +for xtime, x in zip(time, digital): + if x != last: + changes.append(lastt) + nexts.append(last) + changes.append(lastt) + nexts.append(x) + last = x + lastt = xtime + else: + last = x + lastt = xtime + + + +#print('states\n') +#print(nexts) +#print('state changes\n') +#print(changes) + +#Calculate intervals between the changes. +intervals = [y - x for x,y in zip(changes,changes[1:])] +#print('intervals\n') +#print(intervals) + +nnexts = [] +nints = [] +for n, i in zip(nexts,intervals): + if i != 0: + nnexts.append(n) + nints.append(i) + +ntimes = np.array(nints, dtype='float_') +nstates = np.array(nnexts, dtype='bool_') +ntimes = ntimes * 1000000 +print('BEFORE - This is the raw timings.\n') +print(ntimes) + +#This sets the binning time. I can't quite remember why 2000. +good_vals = np.arange(0,2000,20) + +mtimes = [] +for item in ntimes: + idx = (np.abs(good_vals-item)).argmin() + mtimes.append(good_vals[idx]) + +mtimes = np.array(mtimes, dtype='float_') + +togcd = np.array(mtimes, dtype='int_') +best = np.gcd.reduce(togcd) +waittime = best +print('AFTER\n - This is the binned times, along with thier gcd to be the interval.') +print(mtimes) +print(best) + +sends = mtimes / waittime + +print('numbers\n') +print(sends) + +#Convert to zeros and ones for the RF binary. +zeroone = [] +for t, am in zip(nnexts, sends): + lst = [int(t)] * int(am) + if not lst: + lst = [0] + zeroone.extend(lst) + +zeroones = list(zeroone) +print('\nTHIS IS THE OUTPUT STRING\n') +print(''.join(map(str, zeroones))) +print('\nTHIS IS THE DIVISOR\n') +print(best) + +check = np.cumsum(np.array(sends, dtype='int_') * best) + +check = check / 1000000 + + +#Do some plotting of the data, to see how the orginal wave compares to what we are outputing. Use this to tweak the binning interval. +xmin = time[0] +xmax = time[-1] +f, (ax1, ax2, ax3, ax4) = plt.subplots(4, 1, sharex=True) +ax1.plot(time, value) +ax2.plot(time, digital) +ax3.plot(changes, nexts) +ax4.plot(check, nnexts) + +ax1.set_xlim([xmin, xmax]) +ax2.set_xlim([xmin, xmax]) +ax3.set_xlim([xmin, xmax]) +ax4.set_xlim([xmin, xmax]) + +f2, (ax5) = plt.subplots(1, 1, sharex=True) +ax5.plot(changes, nexts) +ax5.plot(check, nnexts) + + +plt.show() diff --git a/code_sniffing/sample-data.txt b/code_sniffing/sample-data.txt new file mode 100644 index 0000000..015c405 --- /dev/null +++ b/code_sniffing/sample-data.txt @@ -0,0 +1,499 @@ +0.00000 -0.20273 +0.00002 -0.20410 +0.00005 -0.20273 +0.00007 -0.20285 +0.00009 -0.20285 +0.00011 -0.20303 +0.00014 -0.20337 +0.00016 -0.20358 +0.00018 -0.20355 +0.00020 -0.20325 +0.00023 -0.20316 +0.00025 -0.20309 +0.00027 -0.20303 +0.00029 -0.20285 +0.00032 -0.20288 +0.00034 -0.20291 +0.00036 -0.20279 +0.00039 -0.20279 +0.00041 -0.20236 +0.00043 -0.20221 +0.00045 -0.20239 +0.00048 -0.20233 +0.00050 -0.20239 +0.00052 -0.20203 +0.00054 -0.20172 +0.00057 -0.20187 +0.00059 -0.20169 +0.00061 -0.20148 +0.00063 -0.20145 +0.00066 -0.20163 +0.00068 -0.20181 +0.00070 -0.20154 +0.00073 -0.20154 +0.00075 -0.20151 +0.00077 -0.20123 +0.00079 -0.20114 +0.00082 -0.20026 +0.00084 -0.20071 +0.00086 -0.19995 +0.00088 -0.20096 +0.00091 -0.19928 +0.00093 -0.20084 +0.00095 -0.19873 +0.00098 -0.20129 +0.00100 -0.19843 +0.00102 -0.20145 +0.00104 -0.19702 +0.00107 -0.20496 +0.00109 -0.15506 +0.00111 0.15720 +0.00113 0.23276 +0.00116 0.21323 +0.00118 0.22415 +0.00120 0.21719 +0.00122 0.22296 +0.00125 0.21701 +0.00127 0.22153 +0.00129 0.21921 +0.00132 0.22000 +0.00134 0.21890 +0.00136 0.22040 +0.00138 0.21887 +0.00141 0.21970 +0.00143 0.22043 +0.00145 0.22052 +0.00147 0.21970 +0.00150 0.21936 +0.00152 0.22000 +0.00154 0.21970 +0.00156 0.21906 +0.00159 0.21985 +0.00161 0.21936 +0.00163 0.21829 +0.00166 0.21948 +0.00168 0.21915 +0.00170 0.21786 +0.00172 0.21832 +0.00175 0.21823 +0.00177 0.21793 +0.00179 0.21762 +0.00181 0.21921 +0.00184 0.21722 +0.00186 0.21960 +0.00188 0.21420 +0.00190 0.22269 +0.00193 0.20993 +0.00195 0.23126 +0.00197 -0.00754 +0.00200 -0.22122 +0.00202 -0.18750 +0.00204 -0.21750 +0.00206 -0.13434 +0.00209 0.17108 +0.00211 0.22913 +0.00213 0.21228 +0.00215 0.22263 +0.00218 0.21500 +0.00220 0.22046 +0.00222 0.21661 +0.00224 0.21927 +0.00227 0.21631 +0.00229 0.21841 +0.00231 0.21661 +0.00234 0.21692 +0.00236 0.21722 +0.00238 0.21808 +0.00240 0.21744 +0.00243 0.21793 +0.00245 0.21793 +0.00247 0.21753 +0.00249 0.21722 +0.00252 0.21725 +0.00254 0.21768 +0.00256 0.21667 +0.00259 0.21655 +0.00261 0.21664 +0.00263 0.21558 +0.00265 0.21497 +0.00268 0.21591 +0.00270 0.21631 +0.00272 0.21426 +0.00274 0.21640 +0.00277 0.21387 +0.00279 0.21738 +0.00281 0.21167 +0.00283 0.22156 +0.00286 0.20694 +0.00288 0.23029 +0.00290 -0.01245 +0.00293 -0.22162 +0.00295 -0.19266 +0.00297 -0.21280 +0.00299 -0.19733 +0.00302 -0.21048 +0.00304 -0.19769 +0.00306 -0.21432 +0.00308 -0.14401 +0.00311 0.16693 +0.00313 0.22473 +0.00315 0.21069 +0.00317 0.21933 +0.00320 0.21237 +0.00322 0.21640 +0.00324 0.21356 +0.00327 0.21609 +0.00329 0.21335 +0.00331 0.21564 +0.00333 0.21414 +0.00336 0.21426 +0.00338 0.21387 +0.00340 0.21533 +0.00342 0.21463 +0.00345 0.21454 +0.00347 0.21487 +0.00349 0.21503 +0.00351 0.21420 +0.00354 0.21378 +0.00356 0.21460 +0.00358 0.21457 +0.00361 0.21362 +0.00363 0.21368 +0.00365 0.21371 +0.00367 0.21298 +0.00370 0.21292 +0.00372 0.21432 +0.00374 0.21191 +0.00376 0.21371 +0.00379 0.21072 +0.00381 0.21652 +0.00383 0.20880 +0.00385 0.21921 +0.00388 0.20309 +0.00390 0.23291 +0.00392 0.01968 +0.00395 -0.21872 +0.00397 -0.19644 +0.00399 -0.21384 +0.00401 -0.20068 +0.00404 -0.21130 +0.00406 -0.20154 +0.00408 -0.21368 +0.00410 -0.15768 +0.00413 0.15482 +0.00415 0.22595 +0.00417 0.20728 +0.00420 0.21890 +0.00422 0.21103 +0.00424 0.21497 +0.00426 0.21100 +0.00429 0.21500 +0.00431 0.21082 +0.00433 0.21329 +0.00435 0.21231 +0.00438 0.21252 +0.00440 0.21188 +0.00442 0.21295 +0.00444 0.21298 +0.00447 0.21255 +0.00449 0.21231 +0.00451 0.21255 +0.00454 0.21201 +0.00456 0.21176 +0.00458 0.21280 +0.00460 0.21149 +0.00463 0.21094 +0.00465 0.21121 +0.00467 0.21231 +0.00469 0.21005 +0.00472 0.21191 +0.00474 0.21045 +0.00476 0.21130 +0.00478 0.20990 +0.00481 0.21133 +0.00483 0.21057 +0.00485 0.21228 +0.00488 0.19360 +0.00490 -0.11066 +0.00492 -0.22388 +0.00494 -0.20059 +0.00497 -0.21448 +0.00499 -0.20441 +0.00501 -0.21146 +0.00503 -0.20621 +0.00506 -0.20972 +0.00508 -0.20782 +0.00510 -0.20883 +0.00512 -0.18787 +0.00515 0.11282 +0.00517 0.23041 +0.00519 0.20178 +0.00522 0.21756 +0.00524 0.20590 +0.00526 0.21387 +0.00528 0.20908 +0.00531 0.21255 +0.00533 0.20938 +0.00535 0.21289 +0.00537 0.21002 +0.00540 0.21078 +0.00542 0.21136 +0.00544 0.21167 +0.00546 0.21094 +0.00549 0.21124 +0.00551 0.21158 +0.00553 0.21109 +0.00556 0.21066 +0.00558 0.21155 +0.00560 0.21063 +0.00562 0.20874 +0.00565 0.20959 +0.00567 0.20947 +0.00569 0.20996 +0.00571 0.20786 +0.00574 0.21164 +0.00576 0.20618 +0.00578 0.21268 +0.00580 0.20419 +0.00583 0.21713 +0.00585 0.19672 +0.00587 0.23206 +0.00590 0.05563 +0.00592 -0.21451 +0.00594 -0.20349 +0.00596 -0.21494 +0.00599 -0.20642 +0.00601 -0.21286 +0.00603 -0.20728 +0.00605 -0.21240 +0.00608 -0.20630 +0.00610 -0.21423 +0.00612 -0.20340 +0.00615 -0.21167 +0.00617 0.06110 +0.00619 0.23306 +0.00621 0.19800 +0.00624 0.21701 +0.00626 0.20459 +0.00628 0.21362 +0.00630 0.20673 +0.00633 0.21155 +0.00635 0.20874 +0.00637 0.20993 +0.00639 0.20822 +0.00642 0.21005 +0.00644 0.20938 +0.00646 0.20920 +0.00649 0.21021 +0.00651 0.20981 +0.00653 0.20883 +0.00655 0.20959 +0.00658 0.21011 +0.00660 0.20868 +0.00662 0.20840 +0.00664 0.20889 +0.00667 0.20868 +0.00669 0.20868 +0.00671 0.20801 +0.00673 0.20975 +0.00676 0.20618 +0.00678 0.21045 +0.00680 0.20468 +0.00683 0.21402 +0.00685 0.19778 +0.00687 0.22369 +0.00689 -0.02756 +0.00692 -0.22977 +0.00694 -0.20056 +0.00696 -0.21851 +0.00698 -0.20633 +0.00701 -0.21460 +0.00703 -0.20938 +0.00705 -0.21219 +0.00707 -0.21161 +0.00710 -0.20929 +0.00712 -0.21426 +0.00714 -0.20569 +0.00717 -0.20935 +0.00719 0.07086 +0.00721 0.23059 +0.00723 0.19617 +0.00726 0.21552 +0.00728 0.20349 +0.00730 0.21188 +0.00732 0.20557 +0.00735 0.21008 +0.00737 0.20639 +0.00739 0.20840 +0.00741 0.20709 +0.00744 0.20874 +0.00746 0.20782 +0.00748 0.20767 +0.00751 0.20868 +0.00753 0.20840 +0.00755 0.20746 +0.00757 0.20816 +0.00760 0.20856 +0.00762 0.20737 +0.00764 0.20764 +0.00766 0.20795 +0.00769 0.20743 +0.00771 0.20737 +0.00773 0.20724 +0.00776 0.20706 +0.00778 0.20642 +0.00780 0.20700 +0.00782 0.20749 +0.00785 0.20670 +0.00787 0.20636 +0.00789 0.20709 +0.00791 0.20651 +0.00794 0.20636 +0.00796 0.20743 +0.00798 0.20679 +0.00800 0.20575 +0.00803 0.20663 +0.00805 0.20682 +0.00807 0.20621 +0.00810 0.20602 +0.00812 0.20547 +0.00814 0.20590 +0.00816 0.20416 +0.00819 0.20615 +0.00821 0.20413 +0.00823 0.20648 +0.00825 0.20215 +0.00828 0.20874 +0.00830 0.20029 +0.00832 0.20956 +0.00834 0.19641 +0.00837 0.22076 +0.00839 0.12021 +0.00841 -0.18851 +0.00844 -0.21710 +0.00846 -0.21503 +0.00848 -0.21582 +0.00850 -0.21512 +0.00853 -0.21585 +0.00855 -0.21490 +0.00857 -0.21576 +0.00859 -0.21448 +0.00862 -0.21576 +0.00864 -0.21469 +0.00866 -0.21533 +0.00868 -0.21484 +0.00871 -0.21527 +0.00873 -0.21506 +0.00875 -0.21494 +0.00878 -0.21497 +0.00880 -0.21490 +0.00882 -0.21475 +0.00884 -0.21472 +0.00887 -0.21460 +0.00889 -0.21454 +0.00891 -0.21448 +0.00893 -0.21414 +0.00896 -0.21426 +0.00898 -0.21432 +0.00900 -0.21426 +0.00902 -0.21417 +0.00905 -0.21390 +0.00907 -0.21368 +0.00909 -0.21411 +0.00912 -0.21393 +0.00914 -0.21414 +0.00916 -0.21414 +0.00918 -0.21375 +0.00921 -0.21378 +0.00923 -0.21362 +0.00925 -0.21368 +0.00927 -0.21338 +0.00930 -0.21344 +0.00932 -0.21332 +0.00934 -0.21313 +0.00937 -0.21313 +0.00939 -0.21301 +0.00941 -0.21301 +0.00943 -0.21283 +0.00946 -0.21216 +0.00948 -0.21265 +0.00950 -0.21176 +0.00952 -0.21249 +0.00955 -0.21124 +0.00957 -0.21262 +0.00959 -0.21097 +0.00961 -0.21265 +0.00964 -0.21054 +0.00966 -0.21259 +0.00968 -0.20978 +0.00971 -0.21451 +0.00973 -0.17584 +0.00975 0.13437 +0.00977 0.22372 +0.00980 0.20099 +0.00982 0.21338 +0.00984 0.20523 +0.00986 0.21130 +0.00989 0.20560 +0.00991 0.21011 +0.00993 0.20740 +0.00995 0.20877 +0.00998 0.20779 +0.01000 0.20938 +0.01002 0.20746 +0.01005 0.20834 +0.01007 0.20908 +0.01009 0.20853 +0.01011 0.20801 +0.01014 0.20810 +0.01016 0.20847 +0.01018 0.20770 +0.01020 0.20728 +0.01023 0.20831 +0.01025 0.20767 +0.01027 0.20691 +0.01029 0.20798 +0.01032 0.20813 +0.01034 0.20715 +0.01036 0.20728 +0.01039 0.20813 +0.01041 0.20764 +0.01043 0.20715 +0.01045 0.20755 +0.01048 0.20728 +0.01050 0.20663 +0.01052 0.20673 +0.01054 0.20746 +0.01057 0.20685 +0.01059 0.20642 +0.01061 0.20731 +0.01063 0.20679 +0.01066 0.20511 +0.01068 0.20618 +0.01070 0.20560 +0.01073 0.20572 +0.01075 0.20444 +0.01077 0.20642 +0.01079 0.20380 +0.01082 0.20581 +0.01084 0.20407 +0.01086 0.20654 +0.01088 0.20288 +0.01091 0.20807 +0.01093 0.18222 +0.01095 -0.12857 +0.01098 -0.22797 +0.01100 -0.20792 +0.01102 -0.21964 +0.01104 -0.21133 +0.01107 -0.21735 +0.01109 -0.21262 +0.01111 -0.21640 +0.01113 -0.21332 +0.01116 -0.21539 +0.01118 -0.21368 +0.01120 -0.21478 +0.01122 -0.21390 +0.01125 -0.21460 +0.01127 -0.21469 +0.01129 -0.21429 diff --git a/config/config.json.example b/config/config.json.example new file mode 100644 index 0000000..bbabb93 --- /dev/null +++ b/config/config.json.example @@ -0,0 +1,24 @@ +{ + "auth": { + "user": "basic_auth_user", + "pass": "basic_auth_pass" + }, + "urls": + { + "override" : "https://url/of/overide/status", + "schedule" : "https://url/of/schedule/status" + }, + "control": { + "on": { + "code": "0000011111111111111111111111111111111111111100000000000011111111111111111111111111111111111111100000000000001111111111111111111111111111111111110000000000000001111111111111111111111111111111111111111111000000000000000001111111111111111111111111111111111100000000000000000011111111111111111111111111111111100000000000000000000000000000000000000000000111111111111111111111111111111110000000000000000000111111111111111111111111111111110000000000000000000111111111111111111111111111111110000000000000000000111111111111111111111111111111111111111111111111111111111000000000000000000000000000000000000000000000111111111111111111111111111111100000000000000000000011111111111111111111111111111100000000000000000000011111111111111111111111111111100000000000000000000011111111111111111111111111111100000000000000000000011111111111111111111111111111111111111111111111111111110000000000000000000001111111111111111111111111111110000000000000000000001111111111111111111111111111110000000000000000000000000000000000000000000000111111111111111111111111111111111111111111111111111111110000000000000000000000000000000000000000000000111111111111111111111111111111", + "multiplier": "20", + "inverse": "0" + }, + "off": { + "code": "0000011111111111111111111111111111111111111100000000000011111111111111111111111111111111111111100000000000001111111111111111111111111111111111110000000000000001111111111111111111111111111111111111111111000000000000000001111111111111111111111111111111111100000000000000000011111111111111111111111111111111100000000000000000000000000000000000000000000111111111111111111111111111111110000000000000000000111111111111111111111111111111110000000000000000000111111111111111111111111111111110000000000000000000111111111111111111111111111111111111111111111111111111111000000000000000000000000000000000000000000000111111111111111111111111111111100000000000000000000011111111111111111111111111111100000000000000000000011111111111111111111111111111100000000000000000000011111111111111111111111111111100000000000000000000011111111111111111111111111111111111111111111111111111110000000000000000000001111111111111111111111111111110000000000000000000001111111111111111111111111111110000000000000000000000000000000000000000000000111111111111111111111111111111111111111111111111111111110000000000000000000000000000000000000000000000111111111111111111111111111111", + "multiplier": "20", + "inverse": "0" + } + } +} + diff --git a/control/heating.py b/control/heating.py new file mode 100755 index 0000000..d6ec4b1 --- /dev/null +++ b/control/heating.py @@ -0,0 +1,56 @@ +#!/usr/bin/python3 +import os +import requests +import json +import subprocess +import time + +script_directory = os.path.dirname(os.path.realpath(__file__)) + +with open(script_directory + '/../config/config.json', 'r') as thefile: + config = json.load(thefile) + +auth = config['auth'] +urls = config['urls'] +control = config['control'] + +try: + ro = requests.get(urls['override'], auth=(auth['user'], auth['pass'])) +except: + print("Error getting override. Defaulting to off.") + override = False +else: + override = ro.json() + +try: + rs = requests.get(urls['override'], auth=(auth['user'], auth['pass'])) +except: + print("Error getting schedule from webservice. Defaulting to off.") + schedule = False +else: + schedule = rs.json() + +if schedule: + demand = schedule +else: + demand = False + +if override is not None: + demand = override + +runs = 0 +if demand: + while runs < 3: + subprocess.run([script_directory + '/../sender/rf', control['on']['code'], control['on']['multiplier'], control['on']['inverse']]) + with open(script_directory + '/../rwdata/state.txt', 'w+') as thefile: + thefile.write("1") + time.sleep(1) + runs = runs + 1 + +else: + while runs < 3: + subprocess.run([script_directory + '/../sender/rf', control['off']['code'], control['off']['multiplier'], control['off']['inverse']]) + with open(script_directory + '/../rwdata/state.txt', 'w+') as thefile: + thefile.write("0") + time.sleep(1) + runs = runs + 1 diff --git a/rwdata/.gitinclude b/rwdata/.gitinclude new file mode 100644 index 0000000..e69de29 diff --git a/sender/Makefile b/sender/Makefile new file mode 100644 index 0000000..38643f5 --- /dev/null +++ b/sender/Makefile @@ -0,0 +1,10 @@ +CC=gcc +CFLAGS=-Wall +all: rf + +rf: + $(CC) $(CFLAGS) $+ -o $@ $@.c -lwiringPi + +clean: + $(RM) rf + diff --git a/sender/rf.c b/sender/rf.c new file mode 100644 index 0000000..77a6d6d --- /dev/null +++ b/sender/rf.c @@ -0,0 +1,51 @@ +/* + * This code was taken from a forum post at "https://www.raspberrypi.org/forums/viewtopic.php?p=499538#p499538" + * Compile: gcc -Wall -o rf rf.c -lwiringPi + * Usage: sudo ./rf [binary] [delay in microseconds] [inverting bits] + * Example.. turning on Silvercrest outlet A + * sudo ./rf 11111111111111001001011001011011011011001011011011011011001001011001011011011011011011000000 500 1 + */ + +#include +#include +#include +#include + +#define PIN 29 + +void parse_bin(char *string, char *d, int invert, int repeat) { + int len = strlen(string), delay = atoi(d);; + int i; + while(repeat) { + for(i=0; i < len; i ++) { + digitalWrite( PIN, ((int)string[i] - 48) ^ invert ); + delayMicroseconds( delay ); + } + repeat --; + } +} + +void reset_pin(void) { + digitalWrite( PIN, LOW ); +} + +int main(int argc, char** argv) { + int invert = 0; + + if(argc < 3) { + printf("[bin] [delay] [invert]\n"); + return -1; + } + + if(argv[3][0] == 49) + invert = 1; + + wiringPiSetup (); + pinMode (PIN, OUTPUT); + + reset_pin(); + parse_bin(argv[1], argv[2], invert, 1); + reset_pin(); + +return 0; +}